From 73e6742ceef8913c29edcd0e66fa6923032ff542 Mon Sep 17 00:00:00 2001 From: davide Date: Mon, 25 Jul 2022 14:08:57 +0200 Subject: [PATCH] Register edits locally through the repository --- .../repository/RepositoryImpl.kt | 65 ++++++++++++++----- 1 file changed, 49 insertions(+), 16 deletions(-) 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 fb04f4d..874a289 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 @@ -140,15 +140,8 @@ class RepositoryImpl(private val api: SelfossApi, private val apiDetails: ApiDet } override suspend fun markAsRead(id: Int): Boolean { - // TODO: Check success, store in DB - api.markAsRead(id.toString()) - badgeUnread -= 1 - return true - } - - override suspend fun unmarkAsRead(id: Int): Boolean { - // TODO: Check success, store in DB - val success = api.unmarkAsRead(id.toString())?.isSuccess == true + // TODO: Check internet connection + val success = api.markAsRead(id.toString())?.isSuccess == true if (success) { markAsReadLocally(id) @@ -156,18 +149,34 @@ class RepositoryImpl(private val api: SelfossApi, private val apiDetails: ApiDet return success } + override suspend fun unmarkAsRead(id: Int): Boolean { + // TODO: Check internet connection + val success = api.unmarkAsRead(id.toString())?.isSuccess == true + + if (success) { + unmarkAsReadLocally(id) + } + return success + } + override suspend fun starr(id: Int): Boolean { // TODO: Check success, store in DB - api.starr(id.toString()) - badgeStarred += 1 - return true + val success = api.starr(id.toString())?.isSuccess == true + + if (success) { + starrLocally(id) + } + return success } override suspend fun unstarr(id: Int): Boolean { - // TODO: Check success, store in DB - api.unstarr(id.toString()) - badgeStarred -= 1 - return true + // TODO: Check internet connection + val success = api.unstarr(id.toString())?.isSuccess == true + + if (success) { + unstarrLocally(id) + } + return success } override suspend fun markAllAsRead(ids: List): Boolean { @@ -191,6 +200,30 @@ class RepositoryImpl(private val api: SelfossApi, private val apiDetails: ApiDet } } + private fun unmarkAsReadLocally(id: Int) { + // TODO: Mark also in the database + if (!items.first {it.id == id}.unread) { + items.first {it.id == id}.unread = true + badgeUnread += 1 + } + } + + private fun starrLocally(id: Int) { + // TODO: Mark also in the database + if (!items.first {it.id == id}.starred) { + items.first {it.id == id}.starred = true + badgeStarred += 1 + } + } + + private fun unstarrLocally(id: Int) { + // TODO: Mark also in the database + if (items.first {it.id == id}.starred) { + items.first {it.id == id}.starred = false + badgeStarred -= 1 + } + } + override suspend fun createSource( title: String, url: String,