Update RecyclerView item layouts
Update the song/album items to have the cover art be loaded by a BindingAdapter, and remove unnecessary chains.
This commit is contained in:
@@ -3,7 +3,6 @@ package org.oxycblt.auxio
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.appcompat.app.AppCompatDelegate
|
||||
|
||||
class MainActivity : AppCompatActivity() {
|
||||
|
||||
@@ -11,7 +10,7 @@ class MainActivity : AppCompatActivity() {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(R.layout.activity_main)
|
||||
|
||||
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
|
||||
// AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
|
||||
|
||||
Log.d(this::class.simpleName, "Activity Created.")
|
||||
}
|
||||
|
||||
@@ -47,8 +47,8 @@ class MainFragment : Fragment() {
|
||||
private fun getFragment(pos: Int): Fragment {
|
||||
if (shownFragments.contains(pos)) {
|
||||
return when (pos) {
|
||||
1 -> libraryFragment
|
||||
0 -> songsFragment
|
||||
0 -> libraryFragment
|
||||
1 -> songsFragment
|
||||
|
||||
else -> libraryFragment
|
||||
}
|
||||
@@ -67,6 +67,7 @@ class MainFragment : Fragment() {
|
||||
override fun getItemCount(): Int = shownFragments.size
|
||||
|
||||
override fun createFragment(position: Int): Fragment {
|
||||
Log.d(this::class.simpleName, "Switching to fragment $position.")
|
||||
return getFragment(position)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ import androidx.fragment.app.Fragment
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import org.oxycblt.auxio.R
|
||||
import org.oxycblt.auxio.databinding.FragmentLibraryBinding
|
||||
import org.oxycblt.auxio.recycler.adapters.AlbumDataAdapter
|
||||
import org.oxycblt.auxio.recycler.adapters.AlbumAdapter
|
||||
import org.oxycblt.auxio.recycler.applyDivider
|
||||
|
||||
class LibraryFragment : Fragment() {
|
||||
@@ -28,7 +28,7 @@ class LibraryFragment : Fragment() {
|
||||
inflater, R.layout.fragment_library, container, false
|
||||
)
|
||||
|
||||
val adapter = AlbumDataAdapter(libraryModel.albums.value!!)
|
||||
val adapter = AlbumAdapter(libraryModel.albums.value!!)
|
||||
|
||||
binding.libraryRecycler.adapter = adapter
|
||||
binding.libraryRecycler.applyDivider()
|
||||
|
||||
@@ -3,8 +3,10 @@ package org.oxycblt.auxio.music
|
||||
import android.content.ContentUris
|
||||
import android.net.Uri
|
||||
import android.provider.MediaStore
|
||||
import android.widget.ImageView
|
||||
import android.widget.TextView
|
||||
import androidx.databinding.BindingAdapter
|
||||
import coil.load
|
||||
import org.oxycblt.auxio.R
|
||||
import org.oxycblt.auxio.music.models.Album
|
||||
import org.oxycblt.auxio.music.models.Song
|
||||
@@ -62,6 +64,25 @@ fun Long.toAlbumArtURI(): Uri {
|
||||
)
|
||||
}
|
||||
|
||||
// Get the cover art
|
||||
@BindingAdapter("coverArt")
|
||||
fun ImageView.getCoverArt(any: Any) {
|
||||
val uri = when (any) {
|
||||
is Song -> any.album.coverUri
|
||||
is Album -> any.coverUri
|
||||
|
||||
// TODO: Artist images
|
||||
|
||||
else -> Uri.EMPTY
|
||||
}
|
||||
|
||||
load(uri) {
|
||||
crossfade(true)
|
||||
placeholder(android.R.color.transparent)
|
||||
error(android.R.color.transparent)
|
||||
}
|
||||
}
|
||||
|
||||
// Format the amount of songs in an album
|
||||
@BindingAdapter("songCount")
|
||||
fun TextView.getAlbumSongs(album: Album) {
|
||||
@@ -71,13 +92,3 @@ fun TextView.getAlbumSongs(album: Album) {
|
||||
context.getString(R.string.format_multi_song_count, album.numSongs.toString())
|
||||
}
|
||||
}
|
||||
|
||||
// Format the artist/album data for a song
|
||||
@BindingAdapter("songData")
|
||||
fun TextView.getSongData(song: Song) {
|
||||
text = context.getString(
|
||||
R.string.format_song_data,
|
||||
song.album.artist.name,
|
||||
song.album.title
|
||||
)
|
||||
}
|
||||
|
||||
+4
-2
@@ -7,13 +7,15 @@ 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>() {
|
||||
class AlbumAdapter(private 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))
|
||||
AlbumItemBinding.inflate(
|
||||
LayoutInflater.from(parent.context)
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
+1
-1
@@ -7,7 +7,7 @@ import org.oxycblt.auxio.databinding.SongItemBinding
|
||||
import org.oxycblt.auxio.music.models.Song
|
||||
import org.oxycblt.auxio.recycler.viewholders.SongViewHolder
|
||||
|
||||
class SongDataAdapter(val data: List<Song>) : RecyclerView.Adapter<SongViewHolder>() {
|
||||
class SongAdapter(private val data: List<Song>) : RecyclerView.Adapter<SongViewHolder>() {
|
||||
|
||||
override fun getItemCount(): Int = data.size
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package org.oxycblt.auxio.recycler.viewholders
|
||||
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import coil.load
|
||||
import org.oxycblt.auxio.databinding.SongItemBinding
|
||||
import org.oxycblt.auxio.music.models.Song
|
||||
|
||||
@@ -14,13 +13,6 @@ class SongViewHolder(
|
||||
fun bind(song: Song) {
|
||||
binding.song = song
|
||||
|
||||
// Load the album cover
|
||||
binding.cover.load(song.album.coverUri) {
|
||||
crossfade(true)
|
||||
placeholder(android.R.color.transparent)
|
||||
error(android.R.color.transparent)
|
||||
}
|
||||
|
||||
binding.executePendingBindings()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ import androidx.fragment.app.Fragment
|
||||
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.adapters.SongAdapter
|
||||
import org.oxycblt.auxio.recycler.applyDivider
|
||||
|
||||
class SongsFragment : Fragment() {
|
||||
@@ -28,8 +28,7 @@ class SongsFragment : Fragment() {
|
||||
inflater, R.layout.fragment_songs, container, false
|
||||
)
|
||||
|
||||
val adapter = SongDataAdapter(songsModel.songs.value!!)
|
||||
binding.songRecycler.adapter = adapter
|
||||
binding.songRecycler.adapter = SongAdapter(songsModel.songs.value!!)
|
||||
binding.songRecycler.applyDivider()
|
||||
binding.songRecycler.setHasFixedSize(true)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user