diff --git a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/model/SelfossModel.kt b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/model/SelfossModel.kt index be6eb13..71e5d65 100644 --- a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/model/SelfossModel.kt +++ b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/model/SelfossModel.kt @@ -1,6 +1,5 @@ package bou.amine.apps.readerforselfossv2.model -import bou.amine.apps.readerforselfossv2.dao.SOURCE import bou.amine.apps.readerforselfossv2.utils.DateUtils import bou.amine.apps.readerforselfossv2.utils.getHtmlDecoded import kotlinx.serialization.KSerializer @@ -70,26 +69,6 @@ class SelfossModel { var unread: Int? var error: String? var icon: String? - - fun checkSameSource(source: Source): Boolean { - return this.id != source.id - } - - fun update(source: Source) { - if (source is SourceDetail) { - this.update(source) - } else if (source is SourceStats) { - this.update(source) - } - } - fun update(source: SourceStats) - - fun update(source: SourceDetail) - - fun toEntity(): SOURCE - - operator fun component1(): Int { return id } - operator fun component2(): String { return title } } @Serializable @@ -99,36 +78,7 @@ class SelfossModel { override var unread: Int?, override var error: String? = null, override var icon: String? = null - ) : Source { - override fun update(source: SourceStats) { - if (checkSameSource(source)) { - throw Exception() - } - this.title = source.title - this.unread = source.unread - } - - override fun update(source: SourceDetail) { - if (checkSameSource(source)) { - throw Exception() - } - this.title = source.title - this.error = source.error - this.icon = source.icon - } - - override fun toEntity(): SOURCE { - return SOURCE( - this.id.toString(), - this.title.getHtmlDecoded(), - null, - null, - this.error, - this.icon, - null - ) - } - } + ) : Source @Serializable data class SourceDetail( @@ -141,39 +91,7 @@ class SelfossModel { override var error: String?, override var icon: String?, var params: SourceParams? - ) : Source { - override fun update(source: SourceStats) { - if (checkSameSource(source)) { - throw Exception() - } - this.title = source.title - this.unread = source.unread - } - - override fun update(source: SourceDetail) { - if (checkSameSource(source)) { - throw Exception() - } - this.title = source.title - this.tags = source.tags - this.spout = source.spout - this.error = source.error - this.icon = source.icon - this.params = source.params - } - - override fun toEntity(): SOURCE { - return SOURCE( - this.id.toString(), - this.title.getHtmlDecoded(), - this.tags?.joinToString(","), - this.spout, - this.error, - this.icon.orEmpty(), - this.params?.url - ) - } - } + ) : Source @Serializable data class SourceParams( 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 b1ad6e3..f59a367 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 @@ -180,27 +180,6 @@ class Repository( } } - private fun updateSources(newSources: List): List { - val isDatabaseEnabled = - appSettingsService.isItemCachingEnabled() || !appSettingsService.isUpdateSourcesEnabled() - return if (isDatabaseEnabled) { - var sources = getDBSources().map { it.toView() } - for (source in sources) { - val newSource = newSources.find { it.id == source.id } - if (newSource != null) { - source.update(newSource) - } - } - - sources = (sources + newSources).distinctBy { it.id } - - resetDBSourcesWithData(sources) - sources - } else { - newSources - } - } - suspend fun getSourcesDetailsOrStats(): ArrayList { var sources = ArrayList() val isDatabaseEnabled = @@ -211,7 +190,7 @@ class Repository( val apiSources = api.sourcesStats() if (apiSources.success && apiSources.data != null) { fetchedSources = true - sources = updateSources(apiSources.data) as ArrayList + sources = apiSources.data as ArrayList } } else { sources = getSourcesDetails() as ArrayList @@ -232,7 +211,10 @@ class Repository( val apiSources = api.sourcesDetailed() if (apiSources.success && apiSources.data != null) { fetchedSources = true - sources = updateSources(apiSources.data) as ArrayList + sources = apiSources.data + if (isDatabaseEnabled) { + resetDBSourcesWithData(sources) + } } } else if (isDatabaseEnabled) { sources = getDBSources().map { it.toView() } as ArrayList @@ -523,7 +505,7 @@ class Repository( } } - private fun resetDBSourcesWithData(sources: List) { + private fun resetDBSourcesWithData(sources: List) { db.sourcesQueries.deleteAllSources() db.sourcesQueries.transaction { diff --git a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/utils/EntityUtils.kt b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/utils/EntityUtils.kt index 05e0b60..7c1c1af 100644 --- a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/utils/EntityUtils.kt +++ b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/utils/EntityUtils.kt @@ -12,7 +12,7 @@ fun TAG.toView(): SelfossModel.Tag = this.unread.toInt() ) -fun SOURCE.toView(): SelfossModel.Source = +fun SOURCE.toView(): SelfossModel.SourceDetail = SelfossModel.SourceDetail( this.id.toInt(), this.title, @@ -24,6 +24,17 @@ fun SOURCE.toView(): SelfossModel.Source = if (this.url != null) SelfossModel.SourceParams(this.url) else null ) +fun SelfossModel.SourceDetail.toEntity(): SOURCE = + SOURCE( + this.id.toString(), + this.title.getHtmlDecoded(), + this.tags?.joinToString(",").orEmpty(), + this.spout.orEmpty(), + this.error.orEmpty(), + this.icon.orEmpty(), + this.params?.url + ) + fun SelfossModel.Tag.toEntity(): TAG = TAG( this.tag, diff --git a/shared/src/commonMain/sqldelight/bou/amine/apps/readerforselfossv2/dao/Sources.sq b/shared/src/commonMain/sqldelight/bou/amine/apps/readerforselfossv2/dao/Sources.sq index b5332d8..9dd5082 100644 --- a/shared/src/commonMain/sqldelight/bou/amine/apps/readerforselfossv2/dao/Sources.sq +++ b/shared/src/commonMain/sqldelight/bou/amine/apps/readerforselfossv2/dao/Sources.sq @@ -1,10 +1,10 @@ CREATE TABLE SOURCE ( `id` TEXT NOT NULL, `title` TEXT NOT NULL, - `tags` TEXT, - `spout` TEXT, - `error` TEXT, - `icon` TEXT, + `tags` TEXT NOT NULL, + `spout` TEXT NOT NULL, + `error` TEXT NOT NULL, + `icon` TEXT NOT NULL, `url` TEXT, PRIMARY KEY(`id`) );