Glimpse: Switch AlbumThumbnailAdapter to `var data`

A bit nicer instead of nullable list

Change-Id: If56620b5883c0e3b53a7fa0d62311c2e0999ca66
This commit is contained in:
Luca Stefani 2023-08-15 19:23:56 +02:00 committed by Sebastiano Barezzi
parent d4b1bfd6b2
commit 2790d4977d
No known key found for this signature in database
GPG Key ID: 763BD3AE91A7A13F
2 changed files with 16 additions and 21 deletions

View File

@ -75,7 +75,7 @@ class AlbumsFragment : Fragment() {
viewLifecycleOwner.lifecycleScope.launch { viewLifecycleOwner.lifecycleScope.launch {
mediaViewModel.albums.collectLatest { mediaViewModel.albums.collectLatest {
albumThumbnailAdapter.changeArray(it.toTypedArray()) albumThumbnailAdapter.data = it.toTypedArray()
} }
} }
} }

View File

@ -5,7 +5,6 @@
package org.lineageos.glimpse.thumbnail package org.lineageos.glimpse.thumbnail
import android.annotation.SuppressLint
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
@ -22,7 +21,18 @@ import org.lineageos.glimpse.models.Album
class AlbumThumbnailAdapter( class AlbumThumbnailAdapter(
private val navController: NavController, private val navController: NavController,
) : RecyclerView.Adapter<AlbumThumbnailAdapter.AlbumViewHolder>() { ) : RecyclerView.Adapter<AlbumThumbnailAdapter.AlbumViewHolder>() {
private var albums: Array<Album>? = null var data: Array<Album> = arrayOf()
set(value) {
if (value.contentEquals(field)) {
return
}
field = value
field.let {
@Suppress("NotifyDataSetChanged") notifyDataSetChanged()
}
}
init { init {
setHasStableIds(true) setHasStableIds(true)
@ -36,27 +46,12 @@ class AlbumThumbnailAdapter(
return AlbumViewHolder(view, navController) return AlbumViewHolder(view, navController)
} }
override fun getItemCount() = albums?.size ?: 0 override fun getItemCount() = data.size
override fun getItemId(position: Int) = (albums?.let { it[position].id } ?: 0).toLong() override fun getItemId(position: Int) = data[position].id.toLong()
override fun onBindViewHolder(holder: AlbumViewHolder, position: Int) { override fun onBindViewHolder(holder: AlbumViewHolder, position: Int) {
albums?.let { holder.bind(data[position])
holder.bind(it[position])
}
}
@SuppressLint("NotifyDataSetChanged")
fun changeArray(array: Array<Album>?) {
if (albums.contentEquals(array)) {
return
}
albums = array
array?.let {
notifyDataSetChanged()
}
} }
class AlbumViewHolder( class AlbumViewHolder(