Make bottom sheet chooser static

No need for recyclerview as the options are static most of the time
This commit is contained in:
Naveen 2022-11-11 21:19:46 +05:30
parent 945b374187
commit 4842d3dcb3
3 changed files with 34 additions and 65 deletions

View file

@ -27,36 +27,38 @@ open class SimpleListItemAdapter(val activity: Activity, val onItemClicked: (Sim
open inner class SimpleItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { open inner class SimpleItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bindView(item: SimpleListItem) { fun bindView(item: SimpleListItem) {
itemView.apply { setupSimpleListItem(itemView, item, onItemClicked)
val color = if (item.selected) { }
context.getProperPrimaryColor() }
} else {
context.getProperTextColor()
}
bottom_sheet_item_title.setText(item.textRes) private class SimpleListItemDiffCallback : DiffUtil.ItemCallback<SimpleListItem>() {
bottom_sheet_item_title.setTextColor(color) override fun areItemsTheSame(oldItem: SimpleListItem, newItem: SimpleListItem): Boolean {
bottom_sheet_item_icon.setImageResourceOrBeGone(item.imageRes) return SimpleListItem.areItemsTheSame(oldItem, newItem)
bottom_sheet_item_icon.applyColorFilter(color) }
bottom_sheet_selected_icon.beVisibleIf(item.selected)
bottom_sheet_selected_icon.applyColorFilter(color)
setOnClickListener { override fun areContentsTheSame(oldItem: SimpleListItem, newItem: SimpleListItem): Boolean {
onItemClicked(item) return SimpleListItem.areContentsTheSame(oldItem, newItem)
}
}
} }
} }
} }
private class SimpleListItemDiffCallback : DiffUtil.ItemCallback<SimpleListItem>() { 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 { bottom_sheet_item_title.setText(item.textRes)
return SimpleListItem.areItemsTheSame(oldItem, newItem) 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)
}
} }

View file

@ -4,42 +4,21 @@ import android.os.Bundle
import android.view.ViewGroup import android.view.ViewGroup
import androidx.fragment.app.FragmentManager import androidx.fragment.app.FragmentManager
import com.simplemobiletools.commons.R 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.fragments.BaseBottomSheetDialogFragment
import com.simplemobiletools.commons.models.SimpleListItem 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 var onItemClick: ((SimpleListItem) -> Unit)? = null
override fun setupContentView(parent: ViewGroup) { override fun setupContentView(parent: ViewGroup) {
val child = layoutInflater.inflate(R.layout.layout_simple_recycler_view, parent, false) val listItems = arguments?.getParcelableArray(ITEMS) as Array<SimpleListItem>
parent.addView(child) listItems.forEach { item ->
setupRecyclerView() val view = layoutInflater.inflate(R.layout.item_simple_list, parent, false)
} setupSimpleListItem(view, item) {
private fun setupRecyclerView() {
@Suppress("UNCHECKED_CAST")
val listItems = arguments?.getParcelableArray(DATA) as Array<SimpleListItem>
getRecyclerViewAdapter().submitList(listItems.toList())
}
private fun getRecyclerViewAdapter(): SimpleListItemAdapter {
var adapter = recycler_view.adapter as? SimpleListItemAdapter
if (adapter == null) {
adapter = SimpleListItemAdapter(requireActivity()) {
onItemClick?.invoke(it) onItemClick?.invoke(it)
dismissAllowingStateLoss()
} }
recycler_view.adapter = adapter
}
return adapter
}
fun updateChooserItems(newItems: Array<SimpleListItem>) {
if (isAdded) {
getRecyclerViewAdapter().submitList(newItems.toList())
} }
} }
@ -50,7 +29,7 @@ class BottomSheetChooserDialog : BaseBottomSheetDialogFragment() {
companion object { companion object {
private const val TAG = "BottomSheetChooserDialog" private const val TAG = "BottomSheetChooserDialog"
private const val DATA = "data" private const val ITEMS = "data"
fun createChooser( fun createChooser(
fragmentManager: FragmentManager, fragmentManager: FragmentManager,
@ -62,7 +41,7 @@ class BottomSheetChooserDialog : BaseBottomSheetDialogFragment() {
if (title != null) { if (title != null) {
putInt(BOTTOM_SHEET_TITLE, title) putInt(BOTTOM_SHEET_TITLE, title)
} }
putParcelableArray(DATA, items) putParcelableArray(ITEMS, items)
} }
return BottomSheetChooserDialog().apply { return BottomSheetChooserDialog().apply {
arguments = extras arguments = extras

View file

@ -1,12 +0,0 @@
<androidx.recyclerview.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_margin"
android:layout_marginBottom="@dimen/big_margin"
android:overScrollMode="never"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:itemCount="3"
tools:listitem="@layout/item_simple_list" />