Glimpse: MediaViewerAdapter: Move to ListAdapter
Change-Id: Ib21fd493b4f08fd969d3fafcac6a845dc56f8b5e
diff --git a/app/src/main/java/org/lineageos/glimpse/ViewActivity.kt b/app/src/main/java/org/lineageos/glimpse/ViewActivity.kt
index e8182ab..37c6992 100644
--- a/app/src/main/java/org/lineageos/glimpse/ViewActivity.kt
+++ b/app/src/main/java/org/lineageos/glimpse/ViewActivity.kt
@@ -454,12 +454,12 @@
}
private fun initData(data: List<Media>) {
- mediaViewerAdapter.data = data.toTypedArray()
+ mediaViewerAdapter.submitList(data)
// If we already have a position, keep that, else get one from
// the passed media, else go to the first one
val mediaPosition = model.mediaPosition ?: media?.let { media ->
- mediaViewerAdapter.data.indexOfFirst {
+ data.indexOfFirst {
it.uri == media.uri
}.takeUnless {
it == -1
diff --git a/app/src/main/java/org/lineageos/glimpse/recyclerview/MediaViewerAdapter.kt b/app/src/main/java/org/lineageos/glimpse/recyclerview/MediaViewerAdapter.kt
index 746c43d..45109a2 100644
--- a/app/src/main/java/org/lineageos/glimpse/recyclerview/MediaViewerAdapter.kt
+++ b/app/src/main/java/org/lineageos/glimpse/recyclerview/MediaViewerAdapter.kt
@@ -15,6 +15,8 @@
import androidx.media3.exoplayer.ExoPlayer
import androidx.media3.ui.PlayerControlView
import androidx.media3.ui.PlayerView
+import androidx.recyclerview.widget.DiffUtil
+import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import coil.load
import org.lineageos.glimpse.R
@@ -30,29 +32,14 @@
private val exoPlayer: Lazy<ExoPlayer>,
private val mediaViewerViewModel: MediaViewerViewModel,
private val mediaViewerUIViewModel: MediaViewerUIViewModel,
-) : RecyclerView.Adapter<MediaViewerAdapter.MediaViewHolder>() {
- var data: Array<Media> = arrayOf()
- set(value) {
- if (value.contentEquals(field)) {
- return
- }
-
- field = value
-
- field.let {
- @Suppress("NotifyDataSetChanged") notifyDataSetChanged()
- }
- }
-
- override fun getItemCount() = data.size
-
+) : ListAdapter<Media, MediaViewerAdapter.MediaViewHolder>(DATA_TYPE_COMPARATOR) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = MediaViewHolder(
LayoutInflater.from(parent.context).inflate(R.layout.media_view, parent, false),
exoPlayer, mediaViewerViewModel, mediaViewerUIViewModel
)
override fun onBindViewHolder(holder: MediaViewHolder, position: Int) {
- holder.bind(data[position], position)
+ holder.bind(getItem(position), position)
}
@androidx.media3.common.util.UnstableApi
@@ -67,7 +54,7 @@
holder.onViewDetachedFromWindow()
}
- fun getItemAtPosition(position: Int) = data[position]
+ fun getItemAtPosition(currentItem: Int): Media = getItem(currentItem)
class MediaViewHolder(
private val view: View,
@@ -163,4 +150,23 @@
mediaViewerUIViewModel.fullscreenModeLiveData.removeObserver(fullscreenModeObserver)
}
}
+
+ companion object {
+ val DATA_TYPE_COMPARATOR = object : DiffUtil.ItemCallback<Media>() {
+ override fun areItemsTheSame(oldItem: Media, newItem: Media) = when {
+ oldItem is MediaStoreMedia && newItem is MediaStoreMedia ->
+ oldItem.id == newItem.id
+
+ else -> oldItem.uri == oldItem.uri
+ }
+
+ override fun areContentsTheSame(oldItem: Media, newItem: Media) = when {
+ oldItem is MediaStoreMedia && newItem is MediaStoreMedia ->
+ oldItem.id == newItem.id &&
+ oldItem.dateModified == newItem.dateModified
+
+ else -> oldItem.uri == oldItem.uri
+ }
+ }
+ }
}