From 12e174dacd439bb8b446fdb6d73cc0ee12fd292d Mon Sep 17 00:00:00 2001 From: davide Date: Sat, 23 Jul 2022 01:44:47 +0200 Subject: [PATCH] Remove SelfossApi from SourcesActivity All network calls to access sources go through the repository --- .../android/SourcesActivity.kt | 18 ++++---------- .../android/adapters/SourcesListAdapter.kt | 24 +++++++++++-------- .../repository/Repository.kt | 4 ++-- .../repository/RepositoryImpl.kt | 16 +++++++++---- 4 files changed, 33 insertions(+), 29 deletions(-) diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/SourcesActivity.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/SourcesActivity.kt index 22869df..fe94cf3 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/SourcesActivity.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/SourcesActivity.kt @@ -11,9 +11,8 @@ import bou.amine.apps.readerforselfossv2.android.databinding.ActivitySourcesBind import bou.amine.apps.readerforselfossv2.android.themes.AppColors import bou.amine.apps.readerforselfossv2.android.themes.Toppings 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 com.ftinc.scoop.Scoop import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers @@ -28,7 +27,7 @@ class SourcesActivity : AppCompatActivity(), DIAware { private lateinit var binding: ActivitySourcesBinding override val di by closestDI() - private val apiDetailsService : ApiDetailsService by instance() + private val repository : Repository by instance() override fun onCreate(savedInstanceState: Bundle?) { appColors = AppColors(this@SourcesActivity) @@ -60,13 +59,6 @@ class SourcesActivity : AppCompatActivity(), DIAware { super.onResume() val mLayoutManager = LinearLayoutManager(this) - val api = SelfossApiImpl( -// this, -// this@SourcesActivity, -// settings.getBoolean("isSelfSignedCert", false), -// prefs.getString("api_timeout", "-1")!!.toLong() - apiDetailsService - ) var items: ArrayList binding.recyclerView.setHasFixedSize(true) @@ -74,11 +66,11 @@ class SourcesActivity : AppCompatActivity(), DIAware { if (this@SourcesActivity.isNetworkAvailable(binding.recyclerView)) { CoroutineScope(Dispatchers.Main).launch { - val response = api.sources() + val response = repository.getSources() if (response != null) { items = response - val mAdapter = SourcesListAdapter(this@SourcesActivity, items, api, - apiDetailsService + val mAdapter = SourcesListAdapter( + this@SourcesActivity, items ) binding.recyclerView.adapter = mAdapter mAdapter.notifyDataSetChanged() diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/SourcesListAdapter.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/SourcesListAdapter.kt index 6357863..30c9260 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/SourcesListAdapter.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/SourcesListAdapter.kt @@ -16,26 +16,30 @@ import bou.amine.apps.readerforselfossv2.android.utils.Config import bou.amine.apps.readerforselfossv2.android.utils.glide.circularBitmapDrawable import bou.amine.apps.readerforselfossv2.android.utils.network.isNetworkAvailable import bou.amine.apps.readerforselfossv2.android.utils.toTextDrawableString -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.amulyakhare.textdrawable.TextDrawable import com.amulyakhare.textdrawable.util.ColorGenerator import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch +import org.kodein.di.DI +import org.kodein.di.DIAware +import org.kodein.di.android.closestDI +import org.kodein.di.instance class SourcesListAdapter( private val app: Activity, - private val items: ArrayList, - private val api: SelfossApiImpl, - private val apiDetailsService: ApiDetailsService -) : RecyclerView.Adapter() { + private val items: ArrayList +) : RecyclerView.Adapter(), DIAware { private val c: Context = app.baseContext private val generator: ColorGenerator = ColorGenerator.MATERIAL private lateinit var config: Config private lateinit var binding: SourceListItemBinding + override val di: DI by closestDI(app) + private val repository : Repository by instance() + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { binding = SourceListItemBinding.inflate(LayoutInflater.from(parent.context), parent, false) return ViewHolder(binding.root) @@ -45,7 +49,7 @@ class SourcesListAdapter( val itm = items[position] config = Config() - if (itm.getIcon(apiDetailsService.getBaseUrl()).isEmpty()) { + if (itm.getIcon(repository.baseUrl).isEmpty()) { val color = generator.getColor(itm.getTitleDecoded()) val drawable = @@ -55,7 +59,7 @@ class SourcesListAdapter( .build(itm.getTitleDecoded().toTextDrawableString(c), color) binding.itemImage.setImageDrawable(drawable) } else { - c.circularBitmapDrawable(config, itm.getIcon(apiDetailsService.getBaseUrl()), binding.itemImage) + c.circularBitmapDrawable(config, itm.getIcon(repository.baseUrl), binding.itemImage) } binding.sourceTitle.text = itm.getTitleDecoded() @@ -77,8 +81,8 @@ class SourcesListAdapter( if (c.isNetworkAvailable(null)) { val (id) = items[adapterPosition] CoroutineScope(Dispatchers.IO).launch { - val action = api.deleteSource(id) - if (action != null && action.isSuccess) { + val successfullyDeletedSource = repository.deleteSource(id) + if (successfullyDeletedSource) { items.removeAt(adapterPosition) notifyItemRemoved(adapterPosition) notifyItemRangeChanged(adapterPosition, itemCount) 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 505e226..dc5fe14 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 @@ -16,7 +16,7 @@ interface Repository { fun stats(): SelfossModel.Stats fun getTags(): List suspend fun getSpouts(): Map? - fun getSources(): List + suspend fun getSources(): ArrayList? suspend fun markAsRead(id: String): Boolean suspend fun unmarkAsRead(id: String): Boolean suspend fun starr(id: String): Boolean @@ -27,7 +27,7 @@ interface Repository { spout: String, tags: String, filter: String): Boolean - fun deleteSource(id: Int): Boolean + suspend fun deleteSource(id: Int): Boolean fun updateRemote(): Boolean suspend fun login(): Boolean fun refreshLoginInformation() 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 8456c74..6f055eb 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 @@ -42,8 +42,9 @@ class RepositoryImpl(private val api: SelfossApi, private val apiDetails: ApiDet return api.spouts() } - override fun getSources(): List { - TODO("Not yet implemented") + override suspend fun getSources(): ArrayList? { + // TODO: Check success + return api.sources() } override suspend fun markAsRead(id: String): Boolean { @@ -98,8 +99,15 @@ class RepositoryImpl(private val api: SelfossApi, private val apiDetails: ApiDet return result } - override fun deleteSource(id: Int): Boolean { - TODO("Not yet implemented") + override suspend fun deleteSource(id: Int): Boolean { + // TODO: Check connectivity, store in DB + var success = false + val response = api.deleteSource(id) + if (response != null) { + success = response.isSuccess + } + + return success } override fun updateRemote(): Boolean {