This commit is contained in:
davidoskky 2023-03-12 21:06:17 +01:00
parent 4966fb704e
commit 2ae32794be
4 changed files with 24 additions and 113 deletions

View File

@ -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(

View File

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

View File

@ -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,

View File

@ -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`)
);