Allow updating bottom sheet chooser items
This commit is contained in:
parent
d496a328ed
commit
3a5b0ad832
3 changed files with 36 additions and 17 deletions
|
@ -1,9 +1,10 @@
|
|||
package com.simplemobiletools.commons.adapters
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.app.Activity
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.recyclerview.widget.DiffUtil
|
||||
import androidx.recyclerview.widget.ListAdapter
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import com.simplemobiletools.commons.R
|
||||
import com.simplemobiletools.commons.extensions.applyColorFilter
|
||||
|
@ -13,16 +14,7 @@ import com.simplemobiletools.commons.models.SimpleListItem
|
|||
import kotlinx.android.synthetic.main.item_simple_list.view.*
|
||||
|
||||
open class SimpleListItemAdapter(val activity: Activity, val onItemClicked: (SimpleListItem) -> Unit) :
|
||||
RecyclerView.Adapter<SimpleListItemAdapter.SimpleItemViewHolder>() {
|
||||
|
||||
private val listItems = arrayListOf<SimpleListItem>()
|
||||
|
||||
@SuppressLint("NotifyDataSetChanged")
|
||||
fun updateData(items: Array<SimpleListItem>) {
|
||||
listItems.clear()
|
||||
listItems.addAll(items)
|
||||
notifyDataSetChanged()
|
||||
}
|
||||
ListAdapter<SimpleListItem, SimpleListItemAdapter.SimpleItemViewHolder>(SimpleListItemDiffCallback()) {
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SimpleItemViewHolder {
|
||||
val view = activity.layoutInflater.inflate(R.layout.item_simple_list, parent, false)
|
||||
|
@ -30,12 +22,10 @@ open class SimpleListItemAdapter(val activity: Activity, val onItemClicked: (Sim
|
|||
}
|
||||
|
||||
override fun onBindViewHolder(holder: SimpleItemViewHolder, position: Int) {
|
||||
val route = listItems[position]
|
||||
val route = getItem(position)
|
||||
holder.bindView(route)
|
||||
}
|
||||
|
||||
override fun getItemCount() = listItems.size
|
||||
|
||||
open inner class SimpleItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
|
||||
|
||||
fun bindView(item: SimpleListItem) {
|
||||
|
@ -54,3 +44,15 @@ open class SimpleListItemAdapter(val activity: Activity, val onItemClicked: (Sim
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class SimpleListItemDiffCallback : DiffUtil.ItemCallback<SimpleListItem>() {
|
||||
|
||||
override fun areItemsTheSame(oldItem: SimpleListItem, newItem: SimpleListItem): Boolean {
|
||||
return SimpleListItem.areItemsTheSame(oldItem, newItem)
|
||||
}
|
||||
|
||||
override fun areContentsTheSame(oldItem: SimpleListItem, newItem: SimpleListItem): Boolean {
|
||||
return SimpleListItem.areContentsTheSame(oldItem, newItem)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -50,8 +50,8 @@ class SimpleBottomSheetChooserDialog : BottomSheetDialogFragment() {
|
|||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
private fun setupRecyclerView() {
|
||||
val data = arguments?.getParcelableArray(DATA) as Array<SimpleListItem>
|
||||
getAudioRouteAdapter().updateData(data)
|
||||
val listItems = arguments?.getParcelableArray(DATA) as Array<SimpleListItem>
|
||||
getAudioRouteAdapter().submitList(listItems.toList())
|
||||
}
|
||||
|
||||
private fun getAudioRouteAdapter(): SimpleListItemAdapter {
|
||||
|
@ -66,6 +66,12 @@ class SimpleBottomSheetChooserDialog : BottomSheetDialogFragment() {
|
|||
return adapter
|
||||
}
|
||||
|
||||
fun updateChooserItems(newItems: Array<SimpleListItem>) {
|
||||
if (isAdded) {
|
||||
getAudioRouteAdapter().submitList(newItems.toList())
|
||||
}
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
super.onDestroy()
|
||||
onItemClick = null
|
||||
|
|
|
@ -4,4 +4,15 @@ import android.os.Parcelable
|
|||
import kotlinx.android.parcel.Parcelize
|
||||
|
||||
@Parcelize
|
||||
data class SimpleListItem(val id: Int, val imageRes: Int?, val textRes: Int) : Parcelable
|
||||
data class SimpleListItem(val id: Int, val imageRes: Int?, val textRes: Int) : Parcelable {
|
||||
|
||||
companion object {
|
||||
fun areItemsTheSame(old: SimpleListItem, new: SimpleListItem): Boolean {
|
||||
return old.id == new.id
|
||||
}
|
||||
|
||||
fun areContentsTheSame(old: SimpleListItem, new: SimpleListItem): Boolean {
|
||||
return old.imageRes == new.imageRes && old.textRes == new.textRes
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue