From 8f464d95fd859a04119bde9374bdaad798552bc4 Mon Sep 17 00:00:00 2001 From: aminecmi Date: Fri, 19 Jan 2024 21:32:15 +0100 Subject: [PATCH] fix: moving listeners. --- .../android/adapters/ItemCardAdapter.kt | 89 +++++++++---------- .../android/adapters/ItemListAdapter.kt | 31 +++---- .../android/adapters/SourcesListAdapter.kt | 62 ++++++------- 3 files changed, 85 insertions(+), 97 deletions(-) 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 b4a84cf..23424cb 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 @@ -34,6 +34,7 @@ class ItemCardAdapter( override var items: ArrayList, override val updateItems: (ArrayList) -> Unit, ) : ItemsAdapter() { + private lateinit var binding: CardItemBinding private val c: Context = app.baseContext private val imageMaxHeight: Int = c.resources.getDimension(R.dimen.card_image_max_height).toInt() @@ -46,10 +47,48 @@ class ItemCardAdapter( parent: ViewGroup, viewType: Int, ): ViewHolder { - val binding = CardItemBinding.inflate(LayoutInflater.from(parent.context), parent, false) + binding = CardItemBinding.inflate(LayoutInflater.from(parent.context), parent, false) return ViewHolder(binding) } + private fun handleClickListeners(position: Int) { + binding.favButton.setOnClickListener { + val item = items[position] + if (item.starred) { + CoroutineScope(Dispatchers.IO).launch { + repository.unstarr(item) + } + binding.favButton.isSelected = false + } else { + CoroutineScope(Dispatchers.IO).launch { + repository.starr(item) + } + binding.favButton.isSelected = true + } + } + + binding.shareBtn.setOnClickListener { + val item = items[position] + c.shareLink(item.getLinkDecoded(), item.title.getHtmlDecoded()) + } + + binding.browserBtn.setOnClickListener { + c.openInBrowserAsNewTask(items[position]) + } + } + + private fun handleLinkOpening(position: Int) { + binding.root.setOnClickListener { + repository.setReaderItems(items) + c.openItemUrl( + position, + items[position].getLinkDecoded(), + appSettingsService.isArticleViewerEnabled(), + app, + ) + } + } + override fun onBindViewHolder( holder: ViewHolder, position: Int, @@ -57,6 +96,9 @@ class ItemCardAdapter( with(holder) { val itm = items[position] + handleClickListeners(position) + handleLinkOpening(position) + binding.favButton.isSelected = itm.starred if (appSettingsService.getPublicAccess()) { binding.favButton.visibility = View.GONE @@ -96,48 +138,5 @@ class ItemCardAdapter( return items.size } - inner class ViewHolder(val binding: CardItemBinding) : RecyclerView.ViewHolder(binding.root) { - init { - handleClickListeners() - handleLinkOpening() - } - - private fun handleClickListeners() { - binding.favButton.setOnClickListener { - val item = items[bindingAdapterPosition] - if (item.starred) { - CoroutineScope(Dispatchers.IO).launch { - repository.unstarr(item) - } - binding.favButton.isSelected = false - } else { - CoroutineScope(Dispatchers.IO).launch { - repository.starr(item) - } - binding.favButton.isSelected = true - } - } - - binding.shareBtn.setOnClickListener { - val item = items[bindingAdapterPosition] - c.shareLink(item.getLinkDecoded(), item.title.getHtmlDecoded()) - } - - binding.browserBtn.setOnClickListener { - c.openInBrowserAsNewTask(items[bindingAdapterPosition]) - } - } - - private fun handleLinkOpening() { - binding.root.setOnClickListener { - repository.setReaderItems(items) - c.openItemUrl( - bindingAdapterPosition, - items[bindingAdapterPosition].getLinkDecoded(), - appSettingsService.isArticleViewerEnabled(), - app, - ) - } - } - } + inner class ViewHolder(val binding: CardItemBinding) : RecyclerView.ViewHolder(binding.root) } 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 bdd9d6f..9fad1a9 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 @@ -25,6 +25,7 @@ class ItemListAdapter( override var items: ArrayList, override val updateItems: (ArrayList) -> Unit, ) : ItemsAdapter() { + private lateinit var binding: ListItemBinding private val c: Context = app.baseContext override val di: DI by closestDI(app) @@ -35,7 +36,7 @@ class ItemListAdapter( parent: ViewGroup, viewType: Int, ): ViewHolder { - val binding = ListItemBinding.inflate(LayoutInflater.from(parent.context), parent, false) + binding = ListItemBinding.inflate(LayoutInflater.from(parent.context), parent, false) return ViewHolder(binding) } @@ -46,6 +47,16 @@ class ItemListAdapter( with(holder) { val itm = items[position] + binding.root.setOnClickListener { + repository.setReaderItems(items) + c.openItemUrl( + bindingAdapterPosition, + items[bindingAdapterPosition].getLinkDecoded(), + appSettingsService.isArticleViewerEnabled(), + app, + ) + } + binding.title.text = itm.title.getHtmlDecoded() binding.title.setOnTouchListener(LinkOnTouchListener()) @@ -68,21 +79,5 @@ class ItemListAdapter( override fun getItemCount(): Int = items.size - inner class ViewHolder(val binding: ListItemBinding) : RecyclerView.ViewHolder(binding.root) { - init { - handleLinkOpening() - } - - private fun handleLinkOpening() { - binding.root.setOnClickListener { - repository.setReaderItems(items) - c.openItemUrl( - bindingAdapterPosition, - items[bindingAdapterPosition].getLinkDecoded(), - appSettingsService.isArticleViewerEnabled(), - app, - ) - } - } - } + inner class ViewHolder(val binding: ListItemBinding) : RecyclerView.ViewHolder(binding.root) } 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 e6422ee..069e227 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 @@ -50,6 +50,33 @@ class SourcesListAdapter( ) { val itm = items[position] + val deleteBtn: Button = holder.mView.findViewById(R.id.deleteBtn) + + deleteBtn.setOnClickListener { + val (id, title) = items[position] + CoroutineScope(Dispatchers.IO).launch { + val successfullyDeletedSource = repository.deleteSource(id, title) + if (successfullyDeletedSource) { + items.removeAt(position) + notifyItemRemoved(position) + notifyItemRangeChanged(position, itemCount) + } else { + Toast.makeText( + app, + R.string.can_delete_source, + Toast.LENGTH_SHORT, + ).show() + } + } + } + + holder.mView.setOnClickListener { + val source = items[position] + + repository.setSelectedSource(source) + app.startActivity(Intent(app, UpsertSourceActivity::class.java)) + } + if (itm.getIcon(repository.baseUrl).isEmpty()) { binding.itemImage.setBackgroundAndText(itm.title.getHtmlDecoded()) } else { @@ -72,38 +99,5 @@ class SourcesListAdapter( override fun getItemCount(): Int = items.size - inner class ViewHolder(private val mView: ConstraintLayout) : RecyclerView.ViewHolder(mView) { - init { - handleClickListeners() - } - - private fun handleClickListeners() { - val deleteBtn: Button = mView.findViewById(R.id.deleteBtn) - - deleteBtn.setOnClickListener { - val (id, title) = items[bindingAdapterPosition] - CoroutineScope(Dispatchers.IO).launch { - val successfullyDeletedSource = repository.deleteSource(id, title) - if (successfullyDeletedSource) { - items.removeAt(bindingAdapterPosition) - notifyItemRemoved(bindingAdapterPosition) - notifyItemRangeChanged(bindingAdapterPosition, itemCount) - } else { - Toast.makeText( - app, - R.string.can_delete_source, - Toast.LENGTH_SHORT, - ).show() - } - } - } - - mView.setOnClickListener { - val source = items[bindingAdapterPosition] - - repository.setSelectedSource(source) - app.startActivity(Intent(app, UpsertSourceActivity::class.java)) - } - } - } + inner class ViewHolder(val mView: ConstraintLayout) : RecyclerView.ViewHolder(mView) }