From 772b5535b913b2f8a3666fc62077434433b815ec Mon Sep 17 00:00:00 2001 From: davidoskky Date: Tue, 7 Feb 2023 20:44:17 +0100 Subject: [PATCH] Support both sources endpoints in the repository --- .../repository/RepositoryImpl.kt | 58 ++++++++++++++----- 1 file changed, 43 insertions(+), 15 deletions(-) diff --git a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/RepositoryImpl.kt b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/RepositoryImpl.kt index 1bcf35e..078d842 100644 --- a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/RepositoryImpl.kt +++ b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/RepositoryImpl.kt @@ -44,7 +44,8 @@ class Repository( private val _badgeStarred = MutableStateFlow(0) val badgeStarred = _badgeStarred.asStateFlow() - private var fetchedSources = false + private var sources = ArrayList() + private var fetchedTags = false private var _readerItems = ArrayList() @@ -180,23 +181,49 @@ class Repository( } } - suspend fun getSources(): ArrayList { + private fun updateSources(newSources: ArrayList) { val isDatabaseEnabled = appSettingsService.isItemCachingEnabled() || !appSettingsService.isUpdateSourcesEnabled() - return if (isNetworkAvailable() && !fetchedSources) { - val apiSources = api.sources() - if (apiSources.success && apiSources.data != null && isDatabaseEnabled) { - resetDBSourcesWithData(apiSources.data) - if (!appSettingsService.isUpdateSourcesEnabled()) { - fetchedSources = true - } - } - apiSources.data ?: ArrayList() - } else if (isDatabaseEnabled) { - ArrayList(getDBSources().map { it.toView() }) - } else { - ArrayList() + val newIds = newSources.map {it.id} + val filteredSources = sources.filterNot { it.id in newIds } + for (source in filteredSources) { + val newSource = newSources.find { it.id == source.id } + source.update(newSource!!) } + sources = (filteredSources + newSources).distinctBy { it.id } as ArrayList + + if (isDatabaseEnabled) { + resetDBSourcesWithData(sources) + } + } + + suspend fun getSourcesStats(): ArrayList { + val isDatabaseEnabled = + appSettingsService.isItemCachingEnabled() || !appSettingsService.isUpdateSourcesEnabled() + if (isNetworkAvailable() && sources.none { it.unread != null }) { + val apiSources = api.sourcesStats() + if (apiSources.success && apiSources.data != null) { + updateSources(apiSources.data.map { SelfossModel.Source(it) } as ArrayList) + } + } else if (isDatabaseEnabled) { + updateSources(getDBSources().map { it.toView() } as ArrayList) + } + + return sources + } + + suspend fun getSourcesDetails(): ArrayList { + val isDatabaseEnabled = + appSettingsService.isItemCachingEnabled() || !appSettingsService.isUpdateSourcesEnabled() + if (isNetworkAvailable() && sources.none { it.spout != null }) { + val apiSources = api.sourcesDetailed() + if (apiSources.success && apiSources.data != null) { + updateSources(apiSources.data.map { SelfossModel.Source(it) } as ArrayList) + } + } else if (isDatabaseEnabled) { + updateSources(getDBSources().map { it.toView() } as ArrayList) + } + return sources } suspend fun markAsRead(item: SelfossModel.Item): Boolean { @@ -373,6 +400,7 @@ class Repository( items = ArrayList(items.filter { it.sourcetitle != title }) setReaderItems(items) db.itemsQueries.deleteItemsWhereSource(title) + sources.removeAll { it.id == id } } return success