Glimpse: Switch AlbumThumbnailAdapter to `var data`
A bit nicer instead of nullable list Change-Id: If56620b5883c0e3b53a7fa0d62311c2e0999ca66
This commit is contained in:
parent
d4b1bfd6b2
commit
2790d4977d
|
@ -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()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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(
|
||||||
|
|
Loading…
Reference in New Issue