Remove useless ListAdapters
Remove unnecessary ListAdapter implementation from AlbumDataAdapter/SongDataAdapter and replace it with a normal adapter.
This commit is contained in:
parent
eafc89a294
commit
53710ca3f0
10 changed files with 44 additions and 90 deletions
|
@ -36,7 +36,7 @@ class MainFragment : Fragment() {
|
|||
inflater, R.layout.fragment_main, container, false
|
||||
)
|
||||
|
||||
val adapter = FragmentAdapter(requireActivity())
|
||||
val adapter = PagerAdapter(requireActivity())
|
||||
binding.viewPager.adapter = adapter
|
||||
|
||||
Log.d(this::class.simpleName, "Fragment Created.")
|
||||
|
@ -47,8 +47,8 @@ class MainFragment : Fragment() {
|
|||
private fun getFragment(pos: Int): Fragment {
|
||||
if (shownFragments.contains(pos)) {
|
||||
return when (pos) {
|
||||
0 -> libraryFragment
|
||||
1 -> songsFragment
|
||||
1 -> libraryFragment
|
||||
0 -> songsFragment
|
||||
|
||||
else -> libraryFragment
|
||||
}
|
||||
|
@ -63,7 +63,7 @@ class MainFragment : Fragment() {
|
|||
return libraryFragment
|
||||
}
|
||||
|
||||
inner class FragmentAdapter(activity: FragmentActivity) : FragmentStateAdapter(activity) {
|
||||
private inner class PagerAdapter(activity: FragmentActivity) : FragmentStateAdapter(activity) {
|
||||
override fun getItemCount(): Int = shownFragments.size
|
||||
|
||||
override fun createFragment(position: Int): Fragment {
|
||||
|
|
|
@ -7,11 +7,10 @@ import android.view.View
|
|||
import android.view.ViewGroup
|
||||
import androidx.databinding.DataBindingUtil
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.lifecycle.Observer
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import org.oxycblt.auxio.R
|
||||
import org.oxycblt.auxio.databinding.FragmentLibraryBinding
|
||||
import org.oxycblt.auxio.library.recycler.AlbumDataAdapter
|
||||
import org.oxycblt.auxio.recycler.adapters.AlbumDataAdapter
|
||||
import org.oxycblt.auxio.recycler.applyDivider
|
||||
|
||||
class LibraryFragment : Fragment() {
|
||||
|
@ -29,18 +28,12 @@ class LibraryFragment : Fragment() {
|
|||
inflater, R.layout.fragment_library, container, false
|
||||
)
|
||||
|
||||
val adapter = AlbumDataAdapter()
|
||||
val adapter = AlbumDataAdapter(libraryModel.albums.value!!)
|
||||
|
||||
binding.libraryRecycler.adapter = adapter
|
||||
binding.libraryRecycler.applyDivider()
|
||||
binding.libraryRecycler.setHasFixedSize(true)
|
||||
|
||||
libraryModel.albums.observe(
|
||||
viewLifecycleOwner,
|
||||
Observer {
|
||||
adapter.data = it
|
||||
}
|
||||
)
|
||||
|
||||
Log.d(this::class.simpleName, "Fragment created.")
|
||||
|
||||
return binding.root
|
||||
|
|
|
@ -1,40 +0,0 @@
|
|||
package org.oxycblt.auxio.library.recycler
|
||||
|
||||
import android.view.LayoutInflater
|
||||
import android.view.ViewGroup
|
||||
import androidx.recyclerview.widget.DiffUtil
|
||||
import androidx.recyclerview.widget.ListAdapter
|
||||
import org.oxycblt.auxio.databinding.AlbumItemBinding
|
||||
import org.oxycblt.auxio.music.models.Album
|
||||
import org.oxycblt.auxio.recycler.AlbumViewHolder
|
||||
|
||||
class AlbumDataAdapter : ListAdapter<Album, AlbumViewHolder>(DiffCallback) {
|
||||
|
||||
var data = listOf<Album>()
|
||||
set(newData) {
|
||||
field = newData
|
||||
submitList(data)
|
||||
}
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AlbumViewHolder {
|
||||
return AlbumViewHolder(
|
||||
AlbumItemBinding.inflate(LayoutInflater.from(parent.context))
|
||||
)
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(holder: AlbumViewHolder, position: Int) {
|
||||
val album = getItem(position)
|
||||
|
||||
holder.bind(album)
|
||||
}
|
||||
|
||||
companion object DiffCallback : DiffUtil.ItemCallback<Album>() {
|
||||
override fun areItemsTheSame(oldItem: Album, newItem: Album): Boolean {
|
||||
return oldItem == newItem
|
||||
}
|
||||
|
||||
override fun areContentsTheSame(oldItem: Album, newItem: Album): Boolean {
|
||||
return oldItem.id == newItem.id
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package org.oxycblt.auxio.recycler.adapters
|
||||
|
||||
import android.view.LayoutInflater
|
||||
import android.view.ViewGroup
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import org.oxycblt.auxio.databinding.AlbumItemBinding
|
||||
import org.oxycblt.auxio.music.models.Album
|
||||
import org.oxycblt.auxio.recycler.viewholders.AlbumViewHolder
|
||||
|
||||
class AlbumDataAdapter(val data: List<Album>) : RecyclerView.Adapter<AlbumViewHolder>() {
|
||||
|
||||
override fun getItemCount(): Int = data.size
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AlbumViewHolder {
|
||||
return AlbumViewHolder(
|
||||
AlbumItemBinding.inflate(LayoutInflater.from(parent.context))
|
||||
)
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(holder: AlbumViewHolder, position: Int) {
|
||||
val album = data[position]
|
||||
|
||||
holder.bind(album)
|
||||
}
|
||||
}
|
|
@ -1,21 +1,15 @@
|
|||
package org.oxycblt.auxio.songs
|
||||
package org.oxycblt.auxio.recycler.adapters
|
||||
|
||||
import android.view.LayoutInflater
|
||||
import android.view.ViewGroup
|
||||
import androidx.recyclerview.widget.DiffUtil
|
||||
import androidx.recyclerview.widget.ListAdapter
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import org.oxycblt.auxio.databinding.SongItemBinding
|
||||
import org.oxycblt.auxio.music.models.Song
|
||||
import org.oxycblt.auxio.recycler.SongViewHolder
|
||||
import org.oxycblt.auxio.recycler.viewholders.SongViewHolder
|
||||
|
||||
class SongDataAdapter : ListAdapter<Song, SongViewHolder>(DiffCallback) {
|
||||
class SongDataAdapter(val data: List<Song>) : RecyclerView.Adapter<SongViewHolder>() {
|
||||
|
||||
var data = listOf<Song>()
|
||||
set(newData) {
|
||||
field = newData
|
||||
submitList(data)
|
||||
}
|
||||
override fun getItemCount(): Int = data.size
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SongViewHolder {
|
||||
val binding = SongItemBinding.inflate(LayoutInflater.from(parent.context))
|
||||
|
@ -29,18 +23,8 @@ class SongDataAdapter : ListAdapter<Song, SongViewHolder>(DiffCallback) {
|
|||
}
|
||||
|
||||
override fun onBindViewHolder(holder: SongViewHolder, position: Int) {
|
||||
val song = getItem(position)
|
||||
val song = data[position]
|
||||
|
||||
holder.bind(song)
|
||||
}
|
||||
|
||||
companion object DiffCallback : DiffUtil.ItemCallback<Song>() {
|
||||
override fun areItemsTheSame(oldItem: Song, newItem: Song): Boolean {
|
||||
return oldItem == newItem
|
||||
}
|
||||
|
||||
override fun areContentsTheSame(oldItem: Song, newItem: Song): Boolean {
|
||||
return oldItem.id == newItem.id
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package org.oxycblt.auxio.recycler
|
||||
package org.oxycblt.auxio.recycler.viewholders
|
||||
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import coil.load
|
|
@ -1,4 +1,4 @@
|
|||
package org.oxycblt.auxio.recycler
|
||||
package org.oxycblt.auxio.recycler.viewholders
|
||||
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import coil.load
|
|
@ -7,13 +7,14 @@ import android.view.View
|
|||
import android.view.ViewGroup
|
||||
import androidx.databinding.DataBindingUtil
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.lifecycle.Observer
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import org.oxycblt.auxio.R
|
||||
import org.oxycblt.auxio.databinding.FragmentSongsBinding
|
||||
import org.oxycblt.auxio.recycler.adapters.SongDataAdapter
|
||||
import org.oxycblt.auxio.recycler.applyDivider
|
||||
|
||||
class SongsFragment : Fragment() {
|
||||
|
||||
private val songsModel: SongsViewModel by lazy {
|
||||
ViewModelProvider(this).get(SongsViewModel::class.java)
|
||||
}
|
||||
|
@ -27,18 +28,11 @@ class SongsFragment : Fragment() {
|
|||
inflater, R.layout.fragment_songs, container, false
|
||||
)
|
||||
|
||||
val adapter = SongDataAdapter()
|
||||
val adapter = SongDataAdapter(songsModel.songs.value!!)
|
||||
binding.songRecycler.adapter = adapter
|
||||
binding.songRecycler.applyDivider()
|
||||
binding.songRecycler.setHasFixedSize(true)
|
||||
|
||||
songsModel.songs.observe(
|
||||
viewLifecycleOwner,
|
||||
Observer {
|
||||
adapter.data = it
|
||||
}
|
||||
)
|
||||
|
||||
Log.d(this::class.simpleName, "Fragment created.")
|
||||
|
||||
return binding.root
|
||||
|
|
|
@ -23,8 +23,7 @@
|
|||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
tools:src="@tools:sample/backgrounds/scenic"
|
||||
tools:srcCompat="@tools:sample/backgrounds/scenic" />
|
||||
tools:src="@tools:sample/backgrounds/scenic" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/album_name"
|
||||
|
|
|
@ -23,8 +23,7 @@
|
|||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
tools:src="@tools:sample/backgrounds/scenic"
|
||||
tools:srcCompat="@tools:sample/backgrounds/scenic" />
|
||||
tools:src="@tools:sample/backgrounds/scenic" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/song_name"
|
||||
|
|
Loading…
Reference in a new issue