From a286d0c5cf47c953c1c78cd29b3514ca53658969 Mon Sep 17 00:00:00 2001 From: davidoskky Date: Tue, 7 Feb 2023 20:41:38 +0100 Subject: [PATCH] Add a data model to contain information from both sources stats and details --- .../readerforselfossv2/model/SelfossModel.kt | 73 ++++++++++++++++++- .../readerforselfossv2/utils/EntityUtils.kt | 6 +- .../amine/apps/readerforselfossv2/dao/3.sqm | 1 + .../apps/readerforselfossv2/dao/Sources.sq | 9 ++- 4 files changed, 82 insertions(+), 7 deletions(-) create mode 100644 shared/src/commonMain/sqldelight/bou/amine/apps/readerforselfossv2/dao/3.sqm 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 8fdf5e8..673b316 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 @@ -63,8 +63,79 @@ class SelfossModel { fun isPublicModeEnabled() = publicMode ?: false } - @Serializable data class Source( + val id: Int, + val title: String, + var unread: Int?, + var tags: List?, + 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 title: String, @Serializable(with = TagsListSerializer::class) 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 875da49..8b932ae 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 @@ -16,7 +16,8 @@ fun SOURCE.toView(): SelfossModel.Source = SelfossModel.Source( this.id.toInt(), this.title, - this.tags.split(","), + this.unread?.toInt(), + this.tags?.split(","), this.spout, this.error, this.icon, @@ -27,7 +28,8 @@ fun SelfossModel.Source.toEntity(): SOURCE = SOURCE( this.id.toString(), this.title.getHtmlDecoded(), - this.tags.joinToString(","), + this.unread?.toLong(), + this.tags?.joinToString(","), this.spout, this.error, this.icon.orEmpty(), diff --git a/shared/src/commonMain/sqldelight/bou/amine/apps/readerforselfossv2/dao/3.sqm b/shared/src/commonMain/sqldelight/bou/amine/apps/readerforselfossv2/dao/3.sqm new file mode 100644 index 0000000..fc42709 --- /dev/null +++ b/shared/src/commonMain/sqldelight/bou/amine/apps/readerforselfossv2/dao/3.sqm @@ -0,0 +1 @@ +ALTER TABLE SOURCE ADD COLUMN `unread` INTEGER; \ No newline at end of file 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 9dd5082..249a86d 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,11 @@ CREATE TABLE SOURCE ( `id` TEXT NOT NULL, `title` TEXT NOT NULL, - `tags` TEXT NOT NULL, - `spout` TEXT NOT NULL, - `error` TEXT NOT NULL, - `icon` TEXT NOT NULL, + `unread` INTEGER, + `tags` TEXT, + `spout` TEXT, + `error` TEXT, + `icon` TEXT, `url` TEXT, PRIMARY KEY(`id`) );