Support both sources endpoints in the repository

This commit is contained in:
davidoskky 2023-02-07 20:44:17 +01:00
parent a286d0c5cf
commit 772b5535b9

View File

@ -44,7 +44,8 @@ class Repository(
private val _badgeStarred = MutableStateFlow(0) private val _badgeStarred = MutableStateFlow(0)
val badgeStarred = _badgeStarred.asStateFlow() val badgeStarred = _badgeStarred.asStateFlow()
private var fetchedSources = false private var sources = ArrayList<SelfossModel.Source>()
private var fetchedTags = false private var fetchedTags = false
private var _readerItems = ArrayList<SelfossModel.Item>() private var _readerItems = ArrayList<SelfossModel.Item>()
@ -180,23 +181,49 @@ class Repository(
} }
} }
suspend fun getSources(): ArrayList<SelfossModel.Source> { private fun updateSources(newSources: ArrayList<SelfossModel.Source>) {
val isDatabaseEnabled = val isDatabaseEnabled =
appSettingsService.isItemCachingEnabled() || !appSettingsService.isUpdateSourcesEnabled() appSettingsService.isItemCachingEnabled() || !appSettingsService.isUpdateSourcesEnabled()
return if (isNetworkAvailable() && !fetchedSources) { val newIds = newSources.map {it.id}
val apiSources = api.sources() val filteredSources = sources.filterNot { it.id in newIds }
if (apiSources.success && apiSources.data != null && isDatabaseEnabled) { for (source in filteredSources) {
resetDBSourcesWithData(apiSources.data) val newSource = newSources.find { it.id == source.id }
if (!appSettingsService.isUpdateSourcesEnabled()) { source.update(newSource!!)
fetchedSources = true }
sources = (filteredSources + newSources).distinctBy { it.id } as ArrayList<SelfossModel.Source>
if (isDatabaseEnabled) {
resetDBSourcesWithData(sources)
} }
} }
apiSources.data ?: ArrayList()
suspend fun getSourcesStats(): ArrayList<SelfossModel.Source> {
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) { } else if (isDatabaseEnabled) {
ArrayList(getDBSources().map { it.toView() }) updateSources(getDBSources().map { it.toView() } as ArrayList)
} else {
ArrayList()
} }
return sources
}
suspend fun getSourcesDetails(): ArrayList<SelfossModel.Source> {
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 { suspend fun markAsRead(item: SelfossModel.Item): Boolean {
@ -373,6 +400,7 @@ class Repository(
items = ArrayList(items.filter { it.sourcetitle != title }) items = ArrayList(items.filter { it.sourcetitle != title })
setReaderItems(items) setReaderItems(items)
db.itemsQueries.deleteItemsWhereSource(title) db.itemsQueries.deleteItemsWhereSource(title)
sources.removeAll { it.id == id }
} }
return success return success