Glimpse: Use stable IDs in our adapters

Our album IDs and photo IDs are unique and stable.

Change-Id: Ic37051ea71b3f773fc3dc241d1b513d636b8688c
This commit is contained in:
Luca Stefani 2023-08-03 19:57:55 +02:00 committed by LuK1337
parent 238e5754a1
commit 8720344ae4
2 changed files with 19 additions and 0 deletions

View File

@ -23,6 +23,10 @@ class AlbumThumbnailAdapter(
) : RecyclerView.Adapter<AlbumThumbnailAdapter.AlbumViewHolder>() {
private var albums: Array<Album>? = null
init {
setHasStableIds(true)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AlbumViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
@ -33,6 +37,8 @@ class AlbumThumbnailAdapter(
override fun getItemCount() = albums?.size ?: 0
override fun getItemId(position: Int) = (albums?.let { it[position].id } ?: 0).toLong()
override fun onBindViewHolder(holder: AlbumViewHolder, position: Int) {
albums?.let {
holder.bind(it[position])

View File

@ -27,8 +27,14 @@ class ThumbnailAdapter(
private var recyclerView: RecyclerView? = null
init {
setHasStableIds(true)
}
override fun getItemCount() = super.getItemCount() + headersPositions.size
override fun getItemId(position: Int) = getIdFromMediaStore(position)
override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
super.onAttachedToRecyclerView(recyclerView)
@ -121,6 +127,13 @@ class ThumbnailAdapter(
recyclerView?.post { notifyItemInserted(newItemCount) }
}
private fun getIdFromMediaStore(position: Int): Long {
val cursor = cursor ?: return 0
val idIndex = cursor.getColumnIndex(MediaStore.Files.FileColumns._ID)
cursor.moveToPosition(getTruePosition(position))
return cursor.getLong(idIndex)
}
private fun getMediaFromMediaStore(position: Int): Media? {
val cursor = cursor ?: return null