From 4842d3dcb3ff251ec722ab2a4a77b43c08bce875 Mon Sep 17 00:00:00 2001 From: Naveen Date: Fri, 11 Nov 2022 21:19:46 +0530 Subject: [PATCH] Make bottom sheet chooser static No need for recyclerview as the options are static most of the time --- .../commons/adapters/SimpleListItemAdapter.kt | 50 ++++++++++--------- .../dialogs/BottomSheetChooserDialog.kt | 37 +++----------- .../layout/layout_simple_recycler_view.xml | 12 ----- 3 files changed, 34 insertions(+), 65 deletions(-) delete mode 100644 commons/src/main/res/layout/layout_simple_recycler_view.xml diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/adapters/SimpleListItemAdapter.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/adapters/SimpleListItemAdapter.kt index 1507cbb92..e6003a5df 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/adapters/SimpleListItemAdapter.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/adapters/SimpleListItemAdapter.kt @@ -27,36 +27,38 @@ open class SimpleListItemAdapter(val activity: Activity, val onItemClicked: (Sim open inner class SimpleItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { fun bindView(item: SimpleListItem) { - itemView.apply { - val color = if (item.selected) { - context.getProperPrimaryColor() - } else { - context.getProperTextColor() - } + setupSimpleListItem(itemView, item, onItemClicked) + } + } - bottom_sheet_item_title.setText(item.textRes) - bottom_sheet_item_title.setTextColor(color) - bottom_sheet_item_icon.setImageResourceOrBeGone(item.imageRes) - bottom_sheet_item_icon.applyColorFilter(color) - bottom_sheet_selected_icon.beVisibleIf(item.selected) - bottom_sheet_selected_icon.applyColorFilter(color) + private class SimpleListItemDiffCallback : DiffUtil.ItemCallback() { + override fun areItemsTheSame(oldItem: SimpleListItem, newItem: SimpleListItem): Boolean { + return SimpleListItem.areItemsTheSame(oldItem, newItem) + } - setOnClickListener { - onItemClicked(item) - } - } + override fun areContentsTheSame(oldItem: SimpleListItem, newItem: SimpleListItem): Boolean { + return SimpleListItem.areContentsTheSame(oldItem, newItem) } } } -private class SimpleListItemDiffCallback : DiffUtil.ItemCallback() { +fun setupSimpleListItem(view: View, item: SimpleListItem, onItemClicked: (SimpleListItem) -> Unit) { + view.apply { + val color = if (item.selected) { + context.getProperPrimaryColor() + } else { + context.getProperTextColor() + } - override fun areItemsTheSame(oldItem: SimpleListItem, newItem: SimpleListItem): Boolean { - return SimpleListItem.areItemsTheSame(oldItem, newItem) + bottom_sheet_item_title.setText(item.textRes) + bottom_sheet_item_title.setTextColor(color) + bottom_sheet_item_icon.setImageResourceOrBeGone(item.imageRes) + bottom_sheet_item_icon.applyColorFilter(color) + bottom_sheet_selected_icon.beVisibleIf(item.selected) + bottom_sheet_selected_icon.applyColorFilter(color) + + setOnClickListener { + onItemClicked(item) + } } - - override fun areContentsTheSame(oldItem: SimpleListItem, newItem: SimpleListItem): Boolean { - return SimpleListItem.areContentsTheSame(oldItem, newItem) - } - } diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/dialogs/BottomSheetChooserDialog.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/dialogs/BottomSheetChooserDialog.kt index 5dbcbf598..382fda856 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/dialogs/BottomSheetChooserDialog.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/dialogs/BottomSheetChooserDialog.kt @@ -4,42 +4,21 @@ import android.os.Bundle import android.view.ViewGroup import androidx.fragment.app.FragmentManager import com.simplemobiletools.commons.R -import com.simplemobiletools.commons.adapters.SimpleListItemAdapter +import com.simplemobiletools.commons.adapters.setupSimpleListItem import com.simplemobiletools.commons.fragments.BaseBottomSheetDialogFragment import com.simplemobiletools.commons.models.SimpleListItem -import kotlinx.android.synthetic.main.layout_simple_recycler_view.* -class BottomSheetChooserDialog : BaseBottomSheetDialogFragment() { +open class BottomSheetChooserDialog : BaseBottomSheetDialogFragment() { var onItemClick: ((SimpleListItem) -> Unit)? = null override fun setupContentView(parent: ViewGroup) { - val child = layoutInflater.inflate(R.layout.layout_simple_recycler_view, parent, false) - parent.addView(child) - setupRecyclerView() - } - - private fun setupRecyclerView() { - @Suppress("UNCHECKED_CAST") - val listItems = arguments?.getParcelableArray(DATA) as Array - getRecyclerViewAdapter().submitList(listItems.toList()) - } - - private fun getRecyclerViewAdapter(): SimpleListItemAdapter { - var adapter = recycler_view.adapter as? SimpleListItemAdapter - if (adapter == null) { - adapter = SimpleListItemAdapter(requireActivity()) { + val listItems = arguments?.getParcelableArray(ITEMS) as Array + listItems.forEach { item -> + val view = layoutInflater.inflate(R.layout.item_simple_list, parent, false) + setupSimpleListItem(view, item) { onItemClick?.invoke(it) - dismissAllowingStateLoss() } - recycler_view.adapter = adapter - } - return adapter - } - - fun updateChooserItems(newItems: Array) { - if (isAdded) { - getRecyclerViewAdapter().submitList(newItems.toList()) } } @@ -50,7 +29,7 @@ class BottomSheetChooserDialog : BaseBottomSheetDialogFragment() { companion object { private const val TAG = "BottomSheetChooserDialog" - private const val DATA = "data" + private const val ITEMS = "data" fun createChooser( fragmentManager: FragmentManager, @@ -62,7 +41,7 @@ class BottomSheetChooserDialog : BaseBottomSheetDialogFragment() { if (title != null) { putInt(BOTTOM_SHEET_TITLE, title) } - putParcelableArray(DATA, items) + putParcelableArray(ITEMS, items) } return BottomSheetChooserDialog().apply { arguments = extras diff --git a/commons/src/main/res/layout/layout_simple_recycler_view.xml b/commons/src/main/res/layout/layout_simple_recycler_view.xml deleted file mode 100644 index 4dc87308b..000000000 --- a/commons/src/main/res/layout/layout_simple_recycler_view.xml +++ /dev/null @@ -1,12 +0,0 @@ -