-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Now, tags are retrieved from the server
- Loading branch information
1 parent
94ad5b4
commit 4492ae8
Showing
18 changed files
with
274 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
data/src/main/java/com/desarrollodroide/data/local/room/dao/TagDao.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.desarrollodroide.data.local.room.dao | ||
|
||
import androidx.room.Dao | ||
import androidx.room.Delete | ||
import androidx.room.Insert | ||
import androidx.room.OnConflictStrategy | ||
import androidx.room.Query | ||
import com.desarrollodroide.data.local.room.entity.TagEntity | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
@Dao | ||
interface TagDao { | ||
@Query("SELECT * FROM tags") | ||
fun getAllTags(): Flow<List<TagEntity>> | ||
|
||
@Insert(onConflict = OnConflictStrategy.REPLACE) | ||
suspend fun insertTag(tag: TagEntity) | ||
|
||
@Insert(onConflict = OnConflictStrategy.REPLACE) | ||
suspend fun insertAllTags(tags: List<TagEntity>) | ||
|
||
@Delete | ||
suspend fun deleteTag(tag: TagEntity) | ||
|
||
@Query("DELETE FROM tags") | ||
suspend fun deleteAllTags() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
data/src/main/java/com/desarrollodroide/data/local/room/entity/TagEntity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.desarrollodroide.data.local.room.entity | ||
|
||
import androidx.room.ColumnInfo | ||
import androidx.room.Entity | ||
import androidx.room.PrimaryKey | ||
|
||
@Entity(tableName = "tags") | ||
data class TagEntity( | ||
@PrimaryKey(autoGenerate = true) val id: Int, | ||
val name: String, | ||
@ColumnInfo(name = "n_bookmarks") val nBookmarks: Int, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
data/src/main/java/com/desarrollodroide/data/repository/TagsRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.desarrollodroide.data.repository | ||
|
||
import kotlinx.coroutines.flow.Flow | ||
import com.desarrollodroide.common.result.Result | ||
import com.desarrollodroide.model.Tag | ||
|
||
interface TagsRepository { | ||
fun getTags( | ||
token: String, | ||
serverUrl: String | ||
): Flow<Result<List<Tag>?>> | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
data/src/main/java/com/desarrollodroide/data/repository/TagsRepositoryImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.desarrollodroide.data.repository | ||
|
||
import com.desarrollodroide.common.result.ErrorHandler | ||
import com.desarrollodroide.data.extensions.removeTrailingSlash | ||
import com.desarrollodroide.data.local.room.dao.TagDao | ||
import com.desarrollodroide.data.mapper.* | ||
import com.desarrollodroide.model.Tag | ||
import com.desarrollodroide.network.model.TagsDTO | ||
import com.desarrollodroide.network.retrofit.NetworkBoundResource | ||
import com.desarrollodroide.network.retrofit.RetrofitNetwork | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.flowOn | ||
import kotlinx.coroutines.flow.map | ||
|
||
class TagsRepositoryImpl( | ||
private val apiService: RetrofitNetwork, | ||
private val tagsDao: TagDao, | ||
private val errorHandler: ErrorHandler | ||
) : TagsRepository { | ||
|
||
override fun getTags( | ||
token: String, | ||
serverUrl: String | ||
) = object : | ||
NetworkBoundResource<TagsDTO, List<Tag>>(errorHandler = errorHandler) { | ||
|
||
override suspend fun saveRemoteData(response: TagsDTO) { | ||
response.message?.map { it.toEntityModel() }?.let { tagsList -> | ||
tagsDao.deleteAllTags() | ||
tagsDao.insertAllTags(tagsList) | ||
} | ||
} | ||
|
||
override fun fetchFromLocal(): Flow<List<Tag>> = tagsDao.getAllTags().map { | ||
it.map { it.toDomainModel() } | ||
} | ||
|
||
override suspend fun fetchFromRemote() = apiService.getTags( | ||
authorization = "Bearer $token", | ||
url = "${serverUrl.removeTrailingSlash()}/api/v1/tags" | ||
) | ||
|
||
override fun shouldFetch(data: List<Tag>?) = true | ||
|
||
}.asFlow().flowOn(Dispatchers.IO) | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
domain/src/main/java/com/desarrollodroide/domain/usecase/GetTagsUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.desarrollodroide.domain.usecase | ||
|
||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.flowOn | ||
import com.desarrollodroide.common.result.Result | ||
import com.desarrollodroide.data.repository.TagsRepository | ||
import com.desarrollodroide.model.Tag | ||
|
||
class GetTagsUseCase( | ||
private val tagsRepository: TagsRepository | ||
) { | ||
operator fun invoke( | ||
serverUrl: String, | ||
token: String, | ||
): Flow<Result<List<Tag>?>> { | ||
return tagsRepository.getTags( | ||
token = token, | ||
serverUrl = serverUrl | ||
).flowOn(Dispatchers.IO) | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
network/src/main/java/com/desarrollodroide/network/model/TagsDTO.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.desarrollodroide.network.model | ||
|
||
class TagsDTO ( | ||
val ok: Boolean?, | ||
val message: List<TagDTO>? | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.