This commit is contained in:
davidoskky 2023-03-12 21:06:17 +01:00
parent 4966fb704e
commit 23c44487ca
4 changed files with 25 additions and 108 deletions

View File

@ -1,6 +1,5 @@
package bou.amine.apps.readerforselfossv2.model 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.DateUtils
import bou.amine.apps.readerforselfossv2.utils.getHtmlDecoded import bou.amine.apps.readerforselfossv2.utils.getHtmlDecoded
import kotlinx.serialization.KSerializer import kotlinx.serialization.KSerializer
@ -70,26 +69,6 @@ class SelfossModel {
var unread: Int? var unread: Int?
var error: String? var error: String?
var icon: 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 @Serializable
@ -99,36 +78,7 @@ class SelfossModel {
override var unread: Int?, override var unread: Int?,
override var error: String? = null, override var error: String? = null,
override var icon: String? = null override var icon: String? = null
) : Source { ) : 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
)
}
}
@Serializable @Serializable
data class SourceDetail( data class SourceDetail(
@ -141,39 +91,7 @@ class SelfossModel {
override var error: String?, override var error: String?,
override var icon: String?, override var icon: String?,
var params: SourceParams? var params: SourceParams?
) : Source { ) : 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
)
}
}
@Serializable @Serializable
data class SourceParams( data class SourceParams(

View File

@ -180,24 +180,11 @@ class Repository(
} }
} }
private fun updateSources(newSources: List<SelfossModel.Source>): List<SelfossModel.Source> { private fun updateSources(newSources: List<SelfossModel.SourceDetail>) {
val isDatabaseEnabled = val isDatabaseEnabled =
appSettingsService.isItemCachingEnabled() || !appSettingsService.isUpdateSourcesEnabled() appSettingsService.isItemCachingEnabled() || !appSettingsService.isUpdateSourcesEnabled()
return if (isDatabaseEnabled) { if (isDatabaseEnabled) {
var sources = getDBSources().map { it.toView() } resetDBSourcesWithData(newSources)
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
} }
} }
@ -211,7 +198,7 @@ class Repository(
val apiSources = api.sourcesStats() val apiSources = api.sourcesStats()
if (apiSources.success && apiSources.data != null) { if (apiSources.success && apiSources.data != null) {
fetchedSources = true fetchedSources = true
sources = updateSources(apiSources.data) as ArrayList<SelfossModel.Source> sources = apiSources.data as ArrayList<SelfossModel.Source>
} }
} else { } else {
sources = getSourcesDetails() as ArrayList<SelfossModel.Source> sources = getSourcesDetails() as ArrayList<SelfossModel.Source>
@ -232,7 +219,8 @@ class Repository(
val apiSources = api.sourcesDetailed() val apiSources = api.sourcesDetailed()
if (apiSources.success && apiSources.data != null) { if (apiSources.success && apiSources.data != null) {
fetchedSources = true fetchedSources = true
sources = updateSources(apiSources.data) as ArrayList<SelfossModel.SourceDetail> sources = apiSources.data
updateSources(sources)
} }
} else if (isDatabaseEnabled) { } else if (isDatabaseEnabled) {
sources = getDBSources().map { it.toView() } as ArrayList<SelfossModel.SourceDetail> sources = getDBSources().map { it.toView() } as ArrayList<SelfossModel.SourceDetail>
@ -523,7 +511,7 @@ class Repository(
} }
} }
private fun resetDBSourcesWithData(sources: List<SelfossModel.Source>) { private fun resetDBSourcesWithData(sources: List<SelfossModel.SourceDetail>) {
db.sourcesQueries.deleteAllSources() db.sourcesQueries.deleteAllSources()
db.sourcesQueries.transaction { db.sourcesQueries.transaction {

View File

@ -12,7 +12,7 @@ fun TAG.toView(): SelfossModel.Tag =
this.unread.toInt() this.unread.toInt()
) )
fun SOURCE.toView(): SelfossModel.Source = fun SOURCE.toView(): SelfossModel.SourceDetail =
SelfossModel.SourceDetail( SelfossModel.SourceDetail(
this.id.toInt(), this.id.toInt(),
this.title, this.title,
@ -24,6 +24,17 @@ fun SOURCE.toView(): SelfossModel.Source =
if (this.url != null) SelfossModel.SourceParams(this.url) else null 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 = fun SelfossModel.Tag.toEntity(): TAG =
TAG( TAG(
this.tag, this.tag,

View File

@ -1,10 +1,10 @@
CREATE TABLE SOURCE ( CREATE TABLE SOURCE (
`id` TEXT NOT NULL, `id` TEXT NOT NULL,
`title` TEXT NOT NULL, `title` TEXT NOT NULL,
`tags` TEXT, `tags` TEXT NOT NULL,
`spout` TEXT, `spout` TEXT NOT NULL,
`error` TEXT, `error` TEXT NOT NULL,
`icon` TEXT, `icon` TEXT NOT NULL,
`url` TEXT, `url` TEXT,
PRIMARY KEY(`id`) PRIMARY KEY(`id`)
); );