From b14a6427da4a06fe37756150a9b9861c91df0131 Mon Sep 17 00:00:00 2001 From: davide Date: Sat, 23 Jul 2022 17:36:34 +0200 Subject: [PATCH] Inject the Repository in the Reader Activity Removed ApiDetailsService and SelfossApi from the activity --- .../android/ReaderActivity.kt | 27 ++++++------------- .../android/adapters/ItemCardAdapter.kt | 4 +-- .../repository/Repository.kt | 4 +-- .../repository/RepositoryImpl.kt | 8 +++--- 4 files changed, 16 insertions(+), 27 deletions(-) diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/ReaderActivity.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/ReaderActivity.kt index 2da8e05..01a7aa3 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/ReaderActivity.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/ReaderActivity.kt @@ -20,9 +20,8 @@ import bou.amine.apps.readerforselfossv2.android.persistence.migrations.MIGRATIO import bou.amine.apps.readerforselfossv2.android.themes.AppColors import bou.amine.apps.readerforselfossv2.android.themes.Toppings import bou.amine.apps.readerforselfossv2.android.utils.toggleStar -import bou.amine.apps.readerforselfossv2.rest.SelfossApiImpl +import bou.amine.apps.readerforselfossv2.repository.Repository import bou.amine.apps.readerforselfossv2.rest.SelfossModel -import bou.amine.apps.readerforselfossv2.service.ApiDetailsService import com.ftinc.scoop.Scoop import com.russhwolf.settings.Settings import kotlinx.coroutines.CoroutineScope @@ -39,8 +38,6 @@ class ReaderActivity : AppCompatActivity(), DIAware { private lateinit var userIdentifier: String private lateinit var appColors: AppColors - private lateinit var api: SelfossApiImpl - private lateinit var toolbarMenu: Menu private lateinit var db: AppDatabase @@ -51,7 +48,7 @@ class ReaderActivity : AppCompatActivity(), DIAware { private val ALIGN_LEFT = 2 override val di by closestDI() - private val apiDetailsService : ApiDetailsService by instance() + private val repository : Repository by instance() private fun showMenuItem(willAddToFavorite: Boolean) { if (willAddToFavorite) { @@ -96,14 +93,6 @@ class ReaderActivity : AppCompatActivity(), DIAware { markOnScroll = settings.getBoolean("mark_on_scroll", false) activeAlignment = settings.getInt("text_align", JUSTIFY) - api = SelfossApiImpl( -// this, -// this@ReaderActivity, -// settings.getBoolean("isSelfSignedCert", false), -// prefs.getString("api_timeout", "-1")!!.toLong() - apiDetailsService - ) - if (allItems.isEmpty()) { finish() } @@ -125,8 +114,8 @@ class ReaderActivity : AppCompatActivity(), DIAware { private fun readItem(item: SelfossModel.Item) { if (markOnScroll) { CoroutineScope(Dispatchers.IO).launch { - api.markAsRead(item.id.toString()) - // TODO: update item in DB + repository.markAsRead(item.id.toString()) + // TODO: Handle failure } } } @@ -223,14 +212,14 @@ class ReaderActivity : AppCompatActivity(), DIAware { R.id.star -> { if (allItems[binding.pager.currentItem].starred) { CoroutineScope(Dispatchers.IO).launch { - api.unstarr(allItems[binding.pager.currentItem].id.toString()) - // TODO: update in DB + repository.unstarr(allItems[binding.pager.currentItem].id) + // TODO: Handle failure } afterUnsave() } else { CoroutineScope(Dispatchers.IO).launch { - api.starr(allItems[binding.pager.currentItem].id.toString()) - // TODO: update in DB + repository.starr(allItems[binding.pager.currentItem].id) + // TODO: Handle failure } afterSave() } diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/ItemCardAdapter.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/ItemCardAdapter.kt index 2428217..949acfc 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/ItemCardAdapter.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/ItemCardAdapter.kt @@ -119,14 +119,14 @@ class ItemCardAdapter( if (c.isNetworkAvailable()) { if (item.starred) { CoroutineScope(Dispatchers.IO).launch { - repository.unstarr(item.id.toString()) + repository.unstarr(item.id) // TODO: Handle failure } item.starred = false binding.favButton.isSelected = false } else { CoroutineScope(Dispatchers.IO).launch { - repository.starr(item.id.toString()) + repository.starr(item.id) // TODO: Handle failure } item.starred = true 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 dc5fe14..f43c451 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,8 +19,8 @@ interface Repository { suspend fun getSources(): ArrayList? suspend fun markAsRead(id: String): Boolean suspend fun unmarkAsRead(id: String): Boolean - suspend fun starr(id: String): Boolean - suspend fun unstarr(id: String): Boolean + suspend fun starr(id: Int): Boolean + suspend fun unstarr(id: Int): Boolean fun markAllAsRead(ids: List): Boolean suspend fun createSource(title: String, url: 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 6f055eb..fa6ef71 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 @@ -58,15 +58,15 @@ class RepositoryImpl(private val api: SelfossApi, private val apiDetails: ApiDet api.unmarkAsRead(id) return true } - override suspend fun starr(id: String): Boolean { + override suspend fun starr(id: Int): Boolean { // TODO: Check success, store in DB - api.starr(id) + api.starr(id.toString()) return true } - override suspend fun unstarr(id: String): Boolean { + override suspend fun unstarr(id: Int): Boolean { // TODO: Check success, store in DB - api.unstarr(id) + api.unstarr(id.toString()) return true }