Skip to content

Commit

Permalink
Fix fetching tags from Linkding
Browse files Browse the repository at this point in the history
  • Loading branch information
fibelatti committed Oct 23, 2024
1 parent 140ccd0 commit aae9c45
Showing 1 changed file with 20 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ 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.onFailure
import com.fibelatti.pinboard.core.android.ConnectivityInfoProvider
import com.fibelatti.pinboard.core.functional.resultFrom
import com.fibelatti.pinboard.core.network.resultFromNetwork
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.emitAll
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach

internal class TagsDataSourceLinkdingApi @Inject constructor(
Expand All @@ -25,7 +28,7 @@ internal class TagsDataSourceLinkdingApi @Inject constructor(
override fun getAllTags(): Flow<Result<List<Tag>>> = flow {
emit(getLocalTags())
if (connectivityInfoProvider.isConnected()) {
emit(getRemoteTags())
emitAll(getRemoteTags().map(::Success))
}
}.onEach { result -> result.getOrNull()?.let { localTags = it } }

Expand All @@ -38,14 +41,24 @@ internal class TagsDataSourceLinkdingApi @Inject constructor(
.sortedBy { it.name }
}

private suspend fun getRemoteTags(): Result<List<Tag>> = resultFromNetwork { linkdingApi.getTags() }
.mapCatching { paginatedResponse ->
val localTags = getLocalTags().getOrNull()?.associateBy { it.name }.orEmpty()
private fun getRemoteTags(): Flow<List<Tag>> = flow {
val localTags = getLocalTags().getOrNull()?.associateBy { it.name }.orEmpty()
val remoteTags = mutableSetOf<Tag>()
var currentPage: PaginatedResponseRemote<TagRemote>

paginatedResponse.results.map {
Tag(name = it.name, posts = localTags[it.name]?.posts ?: 0)
}
resultFromNetwork {
do {
currentPage = linkdingApi.getTags(offset = remoteTags.size, limit = 1_000)
remoteTags += currentPage.results.map { tag ->
Tag(name = tag.name, posts = localTags[tag.name]?.posts ?: 0)
}

emit(remoteTags.toList())
} while (currentPage.next != null)
}.onFailure {
emit(localTags.values.toList())
}
}

override suspend fun renameTag(oldName: String, newName: String): Result<List<Tag>> {
// Linkding doesn't support renaming tags
Expand Down

0 comments on commit aae9c45

Please sign in to comment.