-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add fallback strategy to fetching tags
Pinboard has been acting up, and from time to time the tags endpoint will not respond with the expected model. When that happens, fallback to the tags inferred from all bookmarks. Also, finally split the original `TagsDataSource` into `TagsDataSourcePinboardApi` and `TagsDataSourceNoApi` (same as the `PostsRepository`)
- Loading branch information
Showing
5 changed files
with
90 additions
and
38 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
45 changes: 45 additions & 0 deletions
45
app/src/main/kotlin/com/fibelatti/pinboard/features/tags/data/TagsDataSourceNoApi.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,45 @@ | ||
package com.fibelatti.pinboard.features.tags.data | ||
|
||
import com.fibelatti.core.functional.Failure | ||
import com.fibelatti.core.functional.Result | ||
import com.fibelatti.core.functional.Success | ||
import com.fibelatti.core.functional.getOrNull | ||
import com.fibelatti.core.functional.mapCatching | ||
import com.fibelatti.core.functional.onFailureReturn | ||
import com.fibelatti.core.functional.onSuccess | ||
import com.fibelatti.pinboard.core.functional.resultFrom | ||
import com.fibelatti.pinboard.features.posts.data.PostsDao | ||
import com.fibelatti.pinboard.features.tags.domain.TagsRepository | ||
import com.fibelatti.pinboard.features.tags.domain.model.Tag | ||
import javax.inject.Inject | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.flow | ||
import kotlinx.coroutines.flow.onEach | ||
|
||
internal class TagsDataSourceNoApi @Inject constructor( | ||
private val postsDao: PostsDao, | ||
) : TagsRepository { | ||
|
||
private var localTags: List<Tag>? = null | ||
|
||
override fun getAllTags(): Flow<Result<List<Tag>>> = flow { | ||
localTags?.let { value -> emit(Success(value)) } | ||
emit(getLocalTags()) | ||
}.onEach { result -> | ||
result.onSuccess { value -> localTags = value } | ||
} | ||
|
||
private suspend fun getLocalTags(): Result<List<Tag>> = resultFrom { postsDao.getAllPostTags() } | ||
.mapCatching { concatenatedTags -> | ||
concatenatedTags | ||
.flatMap { it.split(" ") } | ||
.groupBy { it } | ||
.map { (tag, postList) -> Tag(tag, postList.size) } | ||
.sortedBy { it.name } | ||
} | ||
.onFailureReturn(localTags.orEmpty()) | ||
|
||
override suspend fun renameTag(oldName: String, newName: String): Result<List<Tag>> { | ||
return Failure(UnsupportedOperationException()) | ||
} | ||
} |
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