diff --git a/app/src/main/java/org/lineageos/glimpse/fragments/AlbumsFragment.kt b/app/src/main/java/org/lineageos/glimpse/fragments/AlbumsFragment.kt index 357d9a7..37398e1 100644 --- a/app/src/main/java/org/lineageos/glimpse/fragments/AlbumsFragment.kt +++ b/app/src/main/java/org/lineageos/glimpse/fragments/AlbumsFragment.kt @@ -75,7 +75,7 @@ class AlbumsFragment : Fragment() { viewLifecycleOwner.lifecycleScope.launch { mediaViewModel.albums.collectLatest { - albumThumbnailAdapter.changeArray(it.toTypedArray()) + albumThumbnailAdapter.data = it.toTypedArray() } } } diff --git a/app/src/main/java/org/lineageos/glimpse/thumbnail/AlbumThumbnailAdapter.kt b/app/src/main/java/org/lineageos/glimpse/thumbnail/AlbumThumbnailAdapter.kt index dd6dd35..30ff15a 100644 --- a/app/src/main/java/org/lineageos/glimpse/thumbnail/AlbumThumbnailAdapter.kt +++ b/app/src/main/java/org/lineageos/glimpse/thumbnail/AlbumThumbnailAdapter.kt @@ -5,7 +5,6 @@ package org.lineageos.glimpse.thumbnail -import android.annotation.SuppressLint import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -22,7 +21,18 @@ import org.lineageos.glimpse.models.Album class AlbumThumbnailAdapter( private val navController: NavController, ) : RecyclerView.Adapter() { - private var albums: Array? = null + var data: Array = arrayOf() + set(value) { + if (value.contentEquals(field)) { + return + } + + field = value + + field.let { + @Suppress("NotifyDataSetChanged") notifyDataSetChanged() + } + } init { setHasStableIds(true) @@ -36,27 +46,12 @@ class AlbumThumbnailAdapter( 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) { - albums?.let { - holder.bind(it[position]) - } - } - - @SuppressLint("NotifyDataSetChanged") - fun changeArray(array: Array?) { - if (albums.contentEquals(array)) { - return - } - - albums = array - - array?.let { - notifyDataSetChanged() - } + holder.bind(data[position]) } class AlbumViewHolder(