From 11eac7b1418b72d00b24aed949017a3c94282e61 Mon Sep 17 00:00:00 2001 From: davide Date: Sun, 24 Jul 2022 14:14:04 +0200 Subject: [PATCH] Get badges through the repository --- .../android/HomeActivity.kt | 12 ++++++----- .../repository/Repository.kt | 5 ++++- .../repository/RepositoryImpl.kt | 21 +++++++++++++++++-- 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/HomeActivity.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/HomeActivity.kt index 84e1360..d0963ae 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/HomeActivity.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/HomeActivity.kt @@ -1044,8 +1044,10 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar private fun reloadBadges() { if (displayUnreadCount || displayAllCount) { CoroutineScope(Dispatchers.Main).launch { - service.reloadBadges(applicationContext.isNetworkAvailable()) - reloadBadgeContent() + if (applicationContext.isNetworkAvailable()) { + repository.reloadBadges() + reloadBadgeContent() + } } } } @@ -1053,15 +1055,15 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar private fun reloadBadgeContent() { if (displayUnreadCount) { tabNewBadge - .setText(searchService.badgeUnread.toString()) + .setText(repository.badgeUnread.toString()) .maybeShow() } if (displayAllCount) { tabArchiveBadge - .setText(searchService.badgeAll.toString()) + .setText(repository.badgeAll.toString()) .maybeShow() tabStarredBadge - .setText(searchService.badgeStarred.toString()) + .setText(repository.badgeStarred.toString()) .maybeShow() } } diff --git a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/Repository.kt b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/Repository.kt index ad780c8..a1e6ec2 100644 --- a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/Repository.kt +++ b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/Repository.kt @@ -12,10 +12,13 @@ interface Repository { // API var apiMajorVersion: Int + var badgeUnread: Int + var badgeAll: Int + var badgeStarred: Int suspend fun getNewerItems(): ArrayList suspend fun getOlderItems(): ArrayList - fun stats(): SelfossModel.Stats + suspend fun reloadBadges(): Boolean fun getTags(): List suspend fun getSpouts(): Map? suspend fun getSources(): ArrayList? 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 e5f38f1..3714a56 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 @@ -35,11 +35,15 @@ class RepositoryImpl(private val api: SelfossApi, private val apiDetails: ApiDet private var search: String? = null override var apiMajorVersion = 0 + override var badgeUnread = 0 + override var badgeAll = 0 + override var badgeStarred = 0 init { // TODO: Dispatchers.IO not available in KMM, an alternative solution should be found CoroutineScope(Dispatchers.Main).launch { updateApiVersion() + reloadBadges() } } @@ -104,8 +108,17 @@ class RepositoryImpl(private val api: SelfossApi, private val apiDetails: ApiDet return tmpItems } - override fun stats(): SelfossModel.Stats { - TODO("Not yet implemented") + override suspend fun reloadBadges(): Boolean { + // TODO: Check connectivity, calculate from DB + var success = false + val response = api.stats() + if (response != null) { + badgeUnread = response.unread + badgeAll = response.total + badgeStarred = response.starred + success = true + } + return success } override fun getTags(): List { @@ -125,23 +138,27 @@ 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 api.unmarkAsRead(id.toString()) + badgeUnread += 1 return true } override suspend fun starr(id: Int): Boolean { // TODO: Check success, store in DB api.starr(id.toString()) + badgeStarred += 1 return true } override suspend fun unstarr(id: Int): Boolean { // TODO: Check success, store in DB api.unstarr(id.toString()) + badgeStarred -= 1 return true }