From 924f4da1ec71cba27d2e247e4e767d248ce10fc7 Mon Sep 17 00:00:00 2001 From: davide Date: Sun, 24 Jul 2022 14:32:40 +0200 Subject: [PATCH] Remove Selfoss Service from Home Activity Initial implementation of the missing functions in the repository --- .../android/HomeActivity.kt | 9 +---- .../repository/Repository.kt | 4 +- .../repository/RepositoryImpl.kt | 39 +++++++++++++++---- 3 files changed, 36 insertions(+), 16 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 d0963ae..89f0558 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 @@ -34,7 +34,6 @@ import bou.amine.apps.readerforselfossv2.android.persistence.AndroidDeviceDataba import bou.amine.apps.readerforselfossv2.android.persistence.AndroidDeviceDatabaseService import bou.amine.apps.readerforselfossv2.android.persistence.database.AppDatabase import bou.amine.apps.readerforselfossv2.android.persistence.entities.ActionEntity -import bou.amine.apps.readerforselfossv2.android.persistence.entities.AndroidItemEntity import bou.amine.apps.readerforselfossv2.android.persistence.migrations.MIGRATION_1_2 import bou.amine.apps.readerforselfossv2.android.persistence.migrations.MIGRATION_2_3 import bou.amine.apps.readerforselfossv2.android.persistence.migrations.MIGRATION_3_4 @@ -49,13 +48,11 @@ import bou.amine.apps.readerforselfossv2.android.utils.network.isNetworkAvailabl import bou.amine.apps.readerforselfossv2.android.utils.persistence.toEntity import bou.amine.apps.readerforselfossv2.android.utils.persistence.toView import bou.amine.apps.readerforselfossv2.repository.Repository - -import bou.amine.apps.readerforselfossv2.utils.DateUtils import bou.amine.apps.readerforselfossv2.rest.SelfossApiImpl import bou.amine.apps.readerforselfossv2.rest.SelfossModel import bou.amine.apps.readerforselfossv2.service.ApiDetailsService import bou.amine.apps.readerforselfossv2.service.SearchService -import bou.amine.apps.readerforselfossv2.service.SelfossService +import bou.amine.apps.readerforselfossv2.utils.DateUtils import bou.amine.apps.readerforselfossv2.utils.longHash import com.ashokvarma.bottomnavigation.BottomNavigationBar import com.ashokvarma.bottomnavigation.BottomNavigationItem @@ -92,7 +89,6 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar private lateinit var dataBase: AndroidDeviceDatabase private lateinit var dbService: AndroidDeviceDatabaseService private lateinit var searchService: SearchService - private lateinit var service: SelfossService private val MENU_PREFERENCES = 12302 private val DRAWER_ID_TAGS = 100101L private val DRAWER_ID_HIDDEN_TAGS = 101100L @@ -204,7 +200,6 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar dataBase = AndroidDeviceDatabase(applicationContext) searchService = SearchService(DateUtils(repository.apiMajorVersion)) dbService = AndroidDeviceDatabaseService(dataBase, searchService) - service = SelfossService(api, dbService, searchService) handleBottomBar() handleDrawer() @@ -1154,7 +1149,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar if (this@HomeActivity.isNetworkAvailable(null, offlineShortcut)) { CoroutineScope(Dispatchers.Main).launch { - val success = service.readAll(items.map { it.id.toString() }, applicationContext.isNetworkAvailable()) + val success = repository.markAllAsRead(items.map { it.id }) if (success) { Toast.makeText( this@HomeActivity, 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 a1e6ec2..40d22e2 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 @@ -19,14 +19,14 @@ interface Repository { suspend fun getNewerItems(): ArrayList suspend fun getOlderItems(): ArrayList suspend fun reloadBadges(): Boolean - fun getTags(): List + suspend fun getTags(): List? suspend fun getSpouts(): Map? suspend fun getSources(): ArrayList? suspend fun markAsRead(id: Int): Boolean suspend fun unmarkAsRead(id: Int): Boolean suspend fun starr(id: Int): Boolean suspend fun unstarr(id: Int): Boolean - fun markAllAsRead(ids: List): Boolean + suspend fun markAllAsRead(ids: List): Boolean suspend fun createSource(title: String, url: String, spout: String, 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 3714a56..fb04f4d 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 @@ -36,8 +36,11 @@ class RepositoryImpl(private val api: SelfossApi, private val apiDetails: ApiDet override var apiMajorVersion = 0 override var badgeUnread = 0 + set(value) {field = if (value < 0) { 0 } else { value } } override var badgeAll = 0 + set(value) {field = if (value < 0) { 0 } else { value } } override var badgeStarred = 0 + set(value) {field = if (value < 0) { 0 } else { value } } init { // TODO: Dispatchers.IO not available in KMM, an alternative solution should be found @@ -121,8 +124,9 @@ class RepositoryImpl(private val api: SelfossApi, private val apiDetails: ApiDet return success } - override fun getTags(): List { - TODO("Not yet implemented") + override suspend fun getTags(): List? { + // TODO: Check success, store in DB + return api.tags() } override suspend fun getSpouts(): Map? { @@ -144,9 +148,13 @@ class RepositoryImpl(private val api: SelfossApi, private val apiDetails: ApiDet override suspend fun unmarkAsRead(id: Int): Boolean { // TODO: Check success, store in DB - api.unmarkAsRead(id.toString()) - badgeUnread += 1 - return true } + val success = api.unmarkAsRead(id.toString())?.isSuccess == true + + if (success) { + markAsReadLocally(id) + } + return success + } override suspend fun starr(id: Int): Boolean { // TODO: Check success, store in DB @@ -162,8 +170,25 @@ class RepositoryImpl(private val api: SelfossApi, private val apiDetails: ApiDet return true } - override fun markAllAsRead(ids: List): Boolean { - TODO("Not yet implemented") + override suspend fun markAllAsRead(ids: List): Boolean { + // TODO: Check Internet connectivity, store in DB + + val success = api.markAllAsRead(ids.map { it.toString() })?.isSuccess == true + + if (success) { + for (id in ids) { + markAsReadLocally(id) + } + } + return success + } + + private fun markAsReadLocally(id: Int) { + // TODO: Mark also in the database + if (items.first {it.id == id}.unread) { + items.first {it.id == id}.unread = false + badgeUnread -= 1 + } } override suspend fun createSource(