Add a data model to contain information from both sources stats and details

This commit is contained in:
davidoskky 2023-02-07 20:41:38 +01:00
parent 63b69b2822
commit a286d0c5cf
4 changed files with 82 additions and 7 deletions

View File

@ -63,8 +63,79 @@ class SelfossModel {
fun isPublicModeEnabled() = publicMode ?: false fun isPublicModeEnabled() = publicMode ?: false
} }
@Serializable
data class Source( data class Source(
val id: Int,
val title: String,
var unread: Int?,
var tags: List<String>?,
var spout: String?,
var error: String?,
var icon: String?,
var params: SourceParams?
) {
constructor(sourceDetail: SourceDetail) : this(
id = sourceDetail.id,
title = sourceDetail.title,
unread = null,
tags = sourceDetail.tags,
spout = sourceDetail.spout,
error = sourceDetail.error,
icon = sourceDetail.icon,
params = sourceDetail.params
)
constructor(sourceStat: SourceStats) : this(
id = sourceStat.id,
title = sourceStat.title,
unread = sourceStat.unread,
tags = null,
spout = null,
error = null,
icon = null,
params = null
)
fun update(sourceStat: SourceStats) {
if (this.id != sourceStat.id || this.title != sourceStat.title) {
throw Exception()
}
this.unread = sourceStat.unread
}
fun update(sourceDetail: SourceDetail) {
if (this.id != sourceDetail.id || this.title != sourceDetail.title) {
throw Exception()
}
this.tags = sourceDetail.tags
this.spout = sourceDetail.spout
this.error = sourceDetail.error
this.icon = sourceDetail.icon
this.params = sourceDetail.params
}
fun update(source: Source) {
if (this.id != source.id || this.title != source.title) {
throw Exception()
}
this.unread = source.unread
this.tags = source.tags
this.spout = source.spout
this.error = source.error
this.icon = source.icon
this.params = source.params
}
}
@Serializable
data class SourceStats(
val id: Int,
val title: String,
val unread: Int
)
@Serializable
data class SourceDetail(
val id: Int, val id: Int,
val title: String, val title: String,
@Serializable(with = TagsListSerializer::class) @Serializable(with = TagsListSerializer::class)

View File

@ -16,7 +16,8 @@ fun SOURCE.toView(): SelfossModel.Source =
SelfossModel.Source( SelfossModel.Source(
this.id.toInt(), this.id.toInt(),
this.title, this.title,
this.tags.split(","), this.unread?.toInt(),
this.tags?.split(","),
this.spout, this.spout,
this.error, this.error,
this.icon, this.icon,
@ -27,7 +28,8 @@ fun SelfossModel.Source.toEntity(): SOURCE =
SOURCE( SOURCE(
this.id.toString(), this.id.toString(),
this.title.getHtmlDecoded(), this.title.getHtmlDecoded(),
this.tags.joinToString(","), this.unread?.toLong(),
this.tags?.joinToString(","),
this.spout, this.spout,
this.error, this.error,
this.icon.orEmpty(), this.icon.orEmpty(),

View File

@ -0,0 +1 @@
ALTER TABLE SOURCE ADD COLUMN `unread` INTEGER;

View File

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