From 9373024147cb2c5c6cbaab5c53c4e6aeb46ed4d6 Mon Sep 17 00:00:00 2001 From: davide Date: Wed, 20 Jul 2022 14:41:27 +0200 Subject: [PATCH] Strip Selfoss API from Items Adapters --- .../android/HomeActivity.kt | 2 -- .../android/adapters/ItemCardAdapter.kt | 17 +++++++----- .../android/adapters/ItemListAdapter.kt | 9 +++++-- .../android/adapters/ItemsAdapter.kt | 11 ++++---- .../repository/Repository.kt | 8 +++--- .../repository/RepositoryImpl.kt | 27 ++++++++++++------- 6 files changed, 45 insertions(+), 29 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 b72f211..427ee28 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 @@ -1012,7 +1012,6 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar ItemCardAdapter( this, items, - api, apiDetailsService, db, customTabActivityHelper, @@ -1031,7 +1030,6 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar ItemListAdapter( this, items, - api, apiDetailsService, db, customTabActivityHelper, 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 8fad61e..c1ca4b9 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 @@ -17,7 +17,7 @@ import bou.amine.apps.readerforselfossv2.android.utils.customtabs.CustomTabActiv import bou.amine.apps.readerforselfossv2.android.utils.glide.bitmapCenterCrop import bou.amine.apps.readerforselfossv2.android.utils.glide.circularBitmapDrawable import bou.amine.apps.readerforselfossv2.android.utils.network.isNetworkAvailable -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 bou.amine.apps.readerforselfossv2.service.SearchService @@ -28,11 +28,13 @@ import com.bumptech.glide.Glide import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch +import org.kodein.di.DI +import org.kodein.di.android.closestDI +import org.kodein.di.instance class ItemCardAdapter( override val app: Activity, override var items: ArrayList, - override val api: SelfossApiImpl, override val apiDetailsService: ApiDetailsService, override val db: AppDatabase, private val helper: CustomTabActivityHelper, @@ -50,6 +52,9 @@ class ItemCardAdapter( private val imageMaxHeight: Int = c.resources.getDimension(R.dimen.card_image_max_height).toInt() + override val di: DI by closestDI(app) + override val repository : Repository by instance() + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { val binding = CardItemBinding.inflate(LayoutInflater.from(parent.context), parent, false) return ViewHolder(binding) @@ -114,15 +119,15 @@ class ItemCardAdapter( if (c.isNetworkAvailable()) { if (item.starred) { CoroutineScope(Dispatchers.IO).launch { - api.unstarr(item.id.toString()) - // TODO: save to db + repository.unstarr(item.id.toString()) + // TODO: Handle failure } item.starred = false binding.favButton.isSelected = false } else { CoroutineScope(Dispatchers.IO).launch { - api.starr(item.id.toString()) - // TODO: save to db + repository.starr(item.id.toString()) + // TODO: Handle failure } item.starred = true binding.favButton.isSelected = true diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/ItemListAdapter.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/ItemListAdapter.kt index f440180..cacbe45 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/ItemListAdapter.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/ItemListAdapter.kt @@ -13,18 +13,20 @@ import bou.amine.apps.readerforselfossv2.android.utils.* import bou.amine.apps.readerforselfossv2.android.utils.customtabs.CustomTabActivityHelper import bou.amine.apps.readerforselfossv2.android.utils.glide.bitmapCenterCrop import bou.amine.apps.readerforselfossv2.android.utils.glide.circularBitmapDrawable -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 bou.amine.apps.readerforselfossv2.service.SearchService import bou.amine.apps.readerforselfossv2.utils.DateUtils import com.amulyakhare.textdrawable.TextDrawable import com.amulyakhare.textdrawable.util.ColorGenerator +import org.kodein.di.DI +import org.kodein.di.android.closestDI +import org.kodein.di.instance class ItemListAdapter( override val app: Activity, override var items: ArrayList, - override val api: SelfossApiImpl, override val apiDetailsService: ApiDetailsService, override val db: AppDatabase, private val helper: CustomTabActivityHelper, @@ -39,6 +41,9 @@ class ItemListAdapter( private val generator: ColorGenerator = ColorGenerator.MATERIAL private val c: Context = app.baseContext + override val di: DI by closestDI(app) + override val repository : Repository by instance() + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { val binding = ListItemBinding.inflate(LayoutInflater.from(parent.context), parent, false) return ViewHolder(binding) diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/ItemsAdapter.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/ItemsAdapter.kt index c7f8da0..168b944 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/ItemsAdapter.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/ItemsAdapter.kt @@ -8,7 +8,7 @@ import bou.amine.apps.readerforselfossv2.android.R import bou.amine.apps.readerforselfossv2.android.persistence.database.AppDatabase import bou.amine.apps.readerforselfossv2.android.themes.AppColors import bou.amine.apps.readerforselfossv2.android.utils.Config -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 bou.amine.apps.readerforselfossv2.service.SearchService @@ -16,11 +16,12 @@ import com.google.android.material.snackbar.Snackbar import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch +import org.kodein.di.DIAware -abstract class ItemsAdapter : RecyclerView.Adapter() { +abstract class ItemsAdapter : RecyclerView.Adapter(), DIAware { abstract var items: ArrayList - abstract val api: SelfossApiImpl abstract val apiDetailsService: ApiDetailsService + abstract val repository: Repository abstract val db: AppDatabase abstract val userIdentifier: String abstract val app: Activity @@ -82,7 +83,7 @@ abstract class ItemsAdapter : RecyclerView.Adapte private fun readItemAtIndex(position: Int, showSnackbar: Boolean = true) { val i = items[position] CoroutineScope(Dispatchers.IO).launch { - api.markAsRead(i.id.toString()) + repository.markAsRead(i.id.toString()) // TODO: update db } @@ -101,7 +102,7 @@ abstract class ItemsAdapter : RecyclerView.Adapte private fun unreadItemAtIndex(position: Int, showSnackbar: Boolean = true) { CoroutineScope(Dispatchers.IO).launch { - api.unmarkAsRead(items[position].id.toString()) + repository.unmarkAsRead(items[position].id.toString()) // Todo: SharedItems.unreadItem(app, api, db, items[position]) // TODO: update db 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 d9ad93b..0e23bcd 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 @@ -14,10 +14,10 @@ interface Repository { fun getTags(): List fun getSpouts(): List fun getSources(): List - fun markAsRead(id: String): Boolean - fun unmarkAsRead(id: String): Boolean - fun starr(id: String): Boolean - fun unstarr(id: String): Boolean + suspend fun markAsRead(id: String): Boolean + suspend fun unmarkAsRead(id: String): Boolean + suspend fun starr(id: String): Boolean + suspend fun unstarr(id: String): Boolean fun markAllAsRead(ids: List): Boolean 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 3432dde..62e49dd 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 @@ -31,20 +31,27 @@ class RepositoryImpl(private val api: SelfossApi) : Repository { TODO("Not yet implemented") } - override fun markAsRead(id: String): Boolean { - TODO("Not yet implemented") + override suspend fun markAsRead(id: String): Boolean { + // TODO: Check success, store in DB + api.markAsRead(id) + return true } - override fun unmarkAsRead(id: String): Boolean { - TODO("Not yet implemented") + override suspend fun unmarkAsRead(id: String): Boolean { + // TODO: Check success, store in DB + api.unmarkAsRead(id) + return true } + + override suspend fun starr(id: String): Boolean { + // TODO: Check success, store in DB + api.starr(id) + return true } - override fun starr(id: String): Boolean { - TODO("Not yet implemented") - } - - override fun unstarr(id: String): Boolean { - TODO("Not yet implemented") + override suspend fun unstarr(id: String): Boolean { + // TODO: Check success, store in DB + api.unstarr(id) + return true } override fun markAllAsRead(ids: List): Boolean {