Add DetailFragment for genres

Add a DetailFragment for genres.
This commit is contained in:
OxygenCobalt
2020-09-27 09:47:53 -06:00
parent 21626d8d74
commit 9ffc194c4d
16 changed files with 497 additions and 16 deletions
@@ -5,6 +5,7 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import org.oxycblt.auxio.music.models.Album
import org.oxycblt.auxio.music.models.Artist
import org.oxycblt.auxio.music.models.Genre
import org.oxycblt.auxio.recycler.SortMode
class DetailViewModel : ViewModel() {
@@ -13,12 +14,16 @@ class DetailViewModel : ViewModel() {
private val mNavToParentArtist = MutableLiveData<Boolean>()
val navToParentArtist: LiveData<Boolean> get() = mNavToParentArtist
private val mGenreSortMode = MutableLiveData(SortMode.ALPHA_DOWN)
val genreSortMode: LiveData<SortMode> get() = mGenreSortMode
private val mArtistSortMode = MutableLiveData(SortMode.NUMERIC_DOWN)
val artistSortMode: LiveData<SortMode> get() = mArtistSortMode
private val mAlbumSortMode = MutableLiveData(SortMode.NUMERIC_DOWN)
val albumSortMode: LiveData<SortMode> get() = mAlbumSortMode
var currentGenre: Genre? = null
var currentArtist: Artist? = null
var currentAlbum: Album? = null
@@ -30,6 +35,15 @@ class DetailViewModel : ViewModel() {
mNavToParentArtist.value = false
}
fun incrementGenreSortMode() {
mGenreSortMode.value = when (mGenreSortMode.value) {
SortMode.ALPHA_DOWN -> SortMode.ALPHA_UP
SortMode.ALPHA_UP -> SortMode.ALPHA_DOWN
else -> SortMode.ALPHA_DOWN
}
}
fun incrementArtistSortMode() {
mArtistSortMode.value = when (mArtistSortMode.value) {
SortMode.NUMERIC_DOWN -> SortMode.NUMERIC_UP
@@ -0,0 +1,120 @@
package org.oxycblt.auxio.detail
import android.content.res.ColorStateList
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.navigation.fragment.findNavController
import androidx.navigation.fragment.navArgs
import org.oxycblt.auxio.R
import org.oxycblt.auxio.databinding.FragmentArtistDetailBinding
import org.oxycblt.auxio.databinding.FragmentGenreDetailBinding
import org.oxycblt.auxio.detail.adapters.DetailAlbumAdapter
import org.oxycblt.auxio.detail.adapters.DetailArtistAdapter
import org.oxycblt.auxio.music.MusicViewModel
import org.oxycblt.auxio.music.models.Album
import org.oxycblt.auxio.music.models.Artist
import org.oxycblt.auxio.recycler.ClickListener
import org.oxycblt.auxio.recycler.SortMode
import org.oxycblt.auxio.theme.applyDivider
import org.oxycblt.auxio.theme.toColor
class GenreDetailFragment : Fragment() {
private val args: GenreDetailFragmentArgs by navArgs()
private val detailModel: DetailViewModel by activityViewModels()
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val binding = FragmentGenreDetailBinding.inflate(inflater)
// If DetailViewModel isn't already storing the genre, get it from MusicViewModel
// using the ID given by the navigation arguments
if (detailModel.currentGenre == null) {
val musicModel: MusicViewModel by activityViewModels()
detailModel.currentGenre = musicModel.genres.value!!.find {
it.id == args.genreId
}!!
}
val albumAdapter = DetailArtistAdapter(
ClickListener {
navToArtist(it)
}
)
binding.lifecycleOwner = this
binding.detailModel = detailModel
binding.genre = detailModel.currentGenre!!
binding.albumRecycler.adapter = albumAdapter
binding.albumRecycler.applyDivider()
binding.albumRecycler.setHasFixedSize(true)
binding.toolbar.setNavigationOnClickListener {
findNavController().navigateUp()
}
detailModel.genreSortMode.observe(viewLifecycleOwner) { mode ->
// Update the current sort icon
binding.sortButton.setImageResource(mode.iconRes)
// Then update the sort mode of the album adapter.
albumAdapter.submitList(
detailModel.currentGenre!!.artists.sortedWith(
SortMode.artistSortComparators.getOrDefault(
mode,
// If any invalid value is given, just default to the normal sort order.
compareByDescending { it.name }
)
)
)
}
// Don't enable the sort button if there is only one artist [Or less]
if (detailModel.currentGenre!!.numArtists < 2) {
binding.sortButton.imageTintList = ColorStateList.valueOf(
R.color.inactive_color.toColor(requireContext())
)
binding.sortButton.isEnabled = false
}
Log.d(this::class.simpleName, "Fragment created.")
return binding.root
}
override fun onResume() {
super.onResume()
detailModel.isAlreadyNavigating = false
}
override fun onDestroy() {
super.onDestroy()
// Reset the stored artist so that the next instance of GenreDetailFragment
// will not read it.
detailModel.currentGenre = null
}
private fun navToArtist(artist: Artist) {
// Don't navigate if an item already has been selected.
if (!detailModel.isAlreadyNavigating) {
detailModel.isAlreadyNavigating = true
findNavController().navigate(
GenreDetailFragmentDirections.actionShowArtist(artist.id)
)
}
}
}
@@ -0,0 +1,54 @@
package org.oxycblt.auxio.detail.adapters
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import org.oxycblt.auxio.databinding.ItemArtistAlbumBinding
import org.oxycblt.auxio.databinding.ItemGenreArtistBinding
import org.oxycblt.auxio.music.models.Album
import org.oxycblt.auxio.music.models.Artist
import org.oxycblt.auxio.recycler.AlbumDiffCallback
import org.oxycblt.auxio.recycler.ArtistDiffCallback
import org.oxycblt.auxio.recycler.ClickListener
class DetailArtistAdapter(
private val listener: ClickListener<Artist>
) : ListAdapter<Artist, DetailArtistAdapter.ViewHolder>(ArtistDiffCallback()) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder(
ItemGenreArtistBinding.inflate(LayoutInflater.from(parent.context))
)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.bind(getItem(position))
}
// Generic ViewHolder for an album
inner class ViewHolder(
private val binding: ItemGenreArtistBinding
) : RecyclerView.ViewHolder(binding.root) {
init {
// Force the viewholder to *actually* be the screen width so ellipsizing can work.
binding.root.layoutParams = RecyclerView.LayoutParams(
RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT
)
}
// Bind the view w/new data
fun bind(artist: Artist) {
binding.artist = artist
binding.root.setOnClickListener {
listener.onClick(artist)
}
// Force-update the layout so ellipsizing works.
binding.artistImage.requestLayout()
binding.executePendingBindings()
}
}
}
@@ -16,6 +16,7 @@ import org.oxycblt.auxio.library.adapters.GenreAdapter
import org.oxycblt.auxio.music.MusicViewModel
import org.oxycblt.auxio.music.models.Album
import org.oxycblt.auxio.music.models.Artist
import org.oxycblt.auxio.music.models.Genre
import org.oxycblt.auxio.recycler.ClickListener
import org.oxycblt.auxio.theme.SHOW_ALBUMS
import org.oxycblt.auxio.theme.SHOW_ARTISTS
@@ -55,7 +56,7 @@ class LibraryFragment : Fragment() {
SHOW_GENRES -> GenreAdapter(
musicModel.genres.value!!,
ClickListener {
Log.d(this::class.simpleName, it.name)
navToGenre(it)
}
)
@@ -100,4 +101,16 @@ class LibraryFragment : Fragment() {
)
}
}
private fun navToGenre(genre: Genre) {
if (!libraryModel.isAlreadyNavigating) {
libraryModel.isAlreadyNavigating = true
findNavController().navigate(
MainFragmentDirections.actionShowGenre(
genre.id
)
)
}
}
}
@@ -78,8 +78,17 @@ fun ImageView.getGenreImage(genre: Genre) {
if (genre.numArtists >= 4) {
val uris = mutableListOf<Uri>()
// For each artist, get the nth album from them [if possible].
for (i in 0..3) {
uris.add(genre.artists[i].albums[0].coverUri)
val artist = genre.artists[i]
uris.add(
if (artist.albums.size > i) {
artist.albums[i].coverUri
} else {
artist.albums[0].coverUri
}
)
}
val fetcher = MosaicFetcher(context)
@@ -5,6 +5,7 @@ import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.net.Uri
import android.util.Log
import androidx.core.graphics.drawable.toDrawable
import coil.bitmap.BitmapPool
import coil.decode.DataSource
@@ -17,8 +18,11 @@ import coil.size.Size
import okio.buffer
import okio.source
import java.io.InputStream
import kotlin.math.pow
import kotlin.math.sqrt
const val MOSAIC_BITMAP_SIZE = 512
const val MOSAIC_BITMAP_INCREMENT = 256
class MosaicFetcher(private val context: Context) : Fetcher<List<Uri>> {
override suspend fun fetch(
@@ -59,25 +63,24 @@ class MosaicFetcher(private val context: Context) : Fetcher<List<Uri>> {
var x = 0
var y = 0
val increment = MOSAIC_BITMAP_SIZE / 2
// For each stream, create a bitmap scaled to 1/4th of the mosaics combined size
// and place it on a corner of the canvas.
for (stream in streams) {
val bitmap = Bitmap.createScaledBitmap(
BitmapFactory.decodeStream(stream),
increment,
increment,
MOSAIC_BITMAP_INCREMENT,
MOSAIC_BITMAP_INCREMENT,
true
)
canvas.drawBitmap(bitmap, x.toFloat(), y.toFloat(), null)
x += increment
x += MOSAIC_BITMAP_INCREMENT
if (x == MOSAIC_BITMAP_SIZE) {
x = 0
y += increment
y += MOSAIC_BITMAP_INCREMENT
if (y == MOSAIC_BITMAP_SIZE) {
break
@@ -14,4 +14,11 @@ data class Genre(
}
return num
}
val numSongs: Int get() {
var num = 0
artists.forEach {
num += it.numSongs
}
return num
}
}
@@ -158,8 +158,7 @@ class MusicSorter(
// Finalize music
private fun finalizeMusic() {
// Remove genre duplicates now, as there's a risk duplicates could be added during the
// sorting process.
// Remove genre duplicates now, as duplicate genres can be added during the sorting process.
genres = genres.distinctBy {
it.name
}.toMutableList()
@@ -3,6 +3,7 @@ package org.oxycblt.auxio.recycler
import androidx.recyclerview.widget.DiffUtil
import org.oxycblt.auxio.R
import org.oxycblt.auxio.music.models.Album
import org.oxycblt.auxio.music.models.Artist
import org.oxycblt.auxio.music.models.Song
// RecyclerView click listener
@@ -30,6 +31,16 @@ class AlbumDiffCallback : DiffUtil.ItemCallback<Album>() {
}
}
class ArtistDiffCallback : DiffUtil.ItemCallback<Artist>() {
override fun areContentsTheSame(oldItem: Artist, newItem: Artist): Boolean {
return oldItem.id == newItem.id
}
override fun areItemsTheSame(oldItem: Artist, newItem: Artist): Boolean {
return oldItem == newItem
}
}
// Sorting modes
enum class SortMode(val iconRes: Int) {
// Icons for each mode are assigned to the enums themselves
@@ -56,7 +67,17 @@ enum class SortMode(val iconRes: Int) {
) { it.name },
ALPHA_UP to compareBy(
String.CASE_INSENSITIVE_ORDER
) { it.name }
)
val artistSortComparators = mapOf<SortMode, Comparator<Artist>>(
// Alphabetic sorting needs to be case-insensitive
ALPHA_DOWN to compareBy(
String.CASE_INSENSITIVE_ORDER
) { it.name },
ALPHA_UP to compareByDescending(
String.CASE_INSENSITIVE_ORDER
) { it.name }
)
}
}