Add genres to LibraryFragment

Add a genre mode to LibraryFragment.
This commit is contained in:
OxygenCobalt 2020-09-27 08:24:11 -06:00
parent 5191220bc0
commit 21626d8d74
13 changed files with 283 additions and 37 deletions

40
AuxioTODO Normal file
View file

@ -0,0 +1,40 @@
TODO:
? - Could do with some more research.
! - Tried to do, but is completely broken.
/detail/
- Add genre detail
- ? Implement Toolbar update functionality ?
- ! Implement shared element transitions !
/music/
- Add option to show all genres
- ! Move genres to songs !
- ! Remove lists from music models !
- ! Dont determine track/album/artist counts on the fly !
/songs/
- ? Sorting ?
- ? Search ?
- ? Fast Scrolling ?
/library/
- Add genres
- ? Move into ViewPager ?
- Sorting
- Search
- ? Show Artists, Albums, and Songs in search ?
- Exit functionality
/other/
- Remove binding adapters
To be added:
/prefs/
/playback/

View file

@ -12,17 +12,20 @@ import org.oxycblt.auxio.MainFragmentDirections
import org.oxycblt.auxio.databinding.FragmentLibraryBinding
import org.oxycblt.auxio.library.adapters.AlbumAdapter
import org.oxycblt.auxio.library.adapters.ArtistAdapter
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.recycler.ClickListener
import org.oxycblt.auxio.theme.SHOW_ALBUMS
import org.oxycblt.auxio.theme.SHOW_ARTISTS
import org.oxycblt.auxio.theme.SHOW_GENRES
import org.oxycblt.auxio.theme.applyDivider
class LibraryFragment : Fragment() {
// FIXME: Temp value, remove when there are actual preferences
private val libraryMode = SHOW_ARTISTS
private val libraryMode = SHOW_GENRES
private val musicModel: MusicViewModel by activityViewModels()
private val libraryModel: LibraryViewModel by activityViewModels()
@ -42,12 +45,21 @@ class LibraryFragment : Fragment() {
}
)
else -> AlbumAdapter(
SHOW_ALBUMS -> AlbumAdapter(
musicModel.albums.value!!,
ClickListener {
navToAlbum(it)
}
)
SHOW_GENRES -> GenreAdapter(
musicModel.genres.value!!,
ClickListener {
Log.d(this::class.simpleName, it.name)
}
)
else -> null
}
binding.libraryRecycler.applyDivider()

View file

@ -0,0 +1,50 @@
package org.oxycblt.auxio.library.adapters
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import org.oxycblt.auxio.databinding.ItemGenreBinding
import org.oxycblt.auxio.music.models.Genre
import org.oxycblt.auxio.recycler.ClickListener
class GenreAdapter(
private val data: List<Genre>,
private val listener: ClickListener<Genre>
) : RecyclerView.Adapter<GenreAdapter.ViewHolder>() {
override fun getItemCount(): Int = data.size
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder(
ItemGenreBinding.inflate(LayoutInflater.from(parent.context))
)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.bind(data[position])
}
// Generic ViewHolder for an artist
inner class ViewHolder(
private val binding: ItemGenreBinding
) : 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(genre: Genre) {
binding.genre = genre
binding.root.setOnClickListener { listener.onClick(genre) }
// Force-update the layout so ellipsizing works.
binding.artistName.requestLayout()
binding.executePendingBindings()
}
}
}

View file

@ -10,6 +10,7 @@ import androidx.databinding.BindingAdapter
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.Genre
// List of ID3 genres + Winamp extensions, each index corresponds to their int value.
// There are a lot more int-genre extensions as far as Im aware, but this works for most cases.
@ -70,7 +71,7 @@ fun Long.toAlbumArtURI(): Uri {
)
}
// Convert a string into its duration
// Convert seconds into its string duration
fun Long.toDuration(): String {
val durationString = DateUtils.formatElapsedTime(this)
@ -99,37 +100,36 @@ fun getAlbumSongCount(album: Album, context: Context): String {
)
}
// Format the amount of songs in an album
@BindingAdapter("songCount")
fun TextView.bindAlbumSongs(album: Album) {
text = getAlbumSongCount(album, context)
}
@BindingAdapter("genreCounts")
fun TextView.bindGenreCounts(genre: Genre) {
val artists = context.resources.getQuantityString(
R.plurals.format_artist_count, genre.numArtists, genre.numArtists
)
@BindingAdapter("artistCounts")
fun TextView.bindArtistCounts(artist: Artist) {
val albums = context.resources.getQuantityString(
R.plurals.format_albums, artist.numAlbums, artist.numAlbums
)
val songs = context.resources.getQuantityString(
R.plurals.format_song_count, artist.numSongs, artist.numSongs
R.plurals.format_album_count, genre.numAlbums, genre.numAlbums
)
text = context.getString(R.string.format_double_counts, albums, songs)
text = context.getString(R.string.format_double_counts, artists, albums)
}
// Get the artist genre.
// TODO: Add option to list all genres
@BindingAdapter("artistGenre")
fun TextView.bindArtistGenre(artist: Artist) {
// If there are multiple genres, then pick the most "Prominent" one,
// Otherwise just pick the first one
if (artist.genres.keys.size > 1) {
text = artist.genres.keys.sortedByDescending {
artist.genres[it]?.size
}[0]
} else {
text = artist.genres.keys.first()
text = artist.genres[0].name
}
@BindingAdapter("artistCounts")
fun TextView.bindArtistCounts(artist: Artist) {
val albums = context.resources.getQuantityString(
R.plurals.format_album_count, artist.numAlbums, artist.numAlbums
)
val songs = context.resources.getQuantityString(
R.plurals.format_song_count, artist.numSongs, artist.numSongs
)
text = context.getString(R.string.format_double_counts, albums, songs)
}
// Get a bunch of miscellaneous album information [Year, Songs, Duration] and combine them
@ -142,3 +142,9 @@ fun TextView.bindAlbumDetails(album: Album) {
album.totalDuration
)
}
// Format the amount of songs in an album
@BindingAdapter("songCount")
fun TextView.bindAlbumSongs(album: Album) {
text = getAlbumSongCount(album, context)
}

View file

@ -21,6 +21,9 @@ import org.oxycblt.auxio.music.processing.MusicLoaderResponse
import org.oxycblt.auxio.music.processing.MusicSorter
// ViewModel for music storage. May also be a god object.
// FIXME: This class can be improved in multiple ways
// - Remove lists/parents from models so that they can be parcelable
// - Move genre usage to songs [If there's a way to find songs without a genre]
class MusicViewModel(private val app: Application) : ViewModel() {
// Coroutine

View file

@ -9,6 +9,7 @@ import coil.request.ImageRequest
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.Genre
import org.oxycblt.auxio.music.models.Song
// Get the cover art for a song or album
@ -45,7 +46,7 @@ fun ImageView.getArtistImage(artist: Artist) {
uris.add(artist.albums[i].coverUri)
}
val fetcher = ArtistImageFetcher(context)
val fetcher = MosaicFetcher(context)
request = getDefaultRequest(context, this)
.data(uris)
@ -70,6 +71,40 @@ fun ImageView.getArtistImage(artist: Artist) {
Coil.imageLoader(context).enqueue(request)
}
@BindingAdapter("genreImage")
fun ImageView.getGenreImage(genre: Genre) {
val request: ImageRequest
if (genre.numArtists >= 4) {
val uris = mutableListOf<Uri>()
for (i in 0..3) {
uris.add(genre.artists[i].albums[0].coverUri)
}
val fetcher = MosaicFetcher(context)
request = getDefaultRequest(context, this)
.data(uris)
.fetcher(fetcher)
.error(R.drawable.ic_genre)
.build()
} else {
if (genre.artists.isNotEmpty()) {
request = getDefaultRequest(context, this)
.data(genre.artists[0].albums[0].coverUri)
.error(R.drawable.ic_genre)
.build()
} else {
setImageResource(R.drawable.ic_genre)
return
}
}
Coil.imageLoader(context).enqueue(request)
}
// Get the base request used across the app.
private fun getDefaultRequest(context: Context, imageView: ImageView): ImageRequest.Builder {
return ImageRequest.Builder(context)

View file

@ -20,7 +20,7 @@ import java.io.InputStream
const val MOSAIC_BITMAP_SIZE = 512
class ArtistImageFetcher(private val context: Context) : Fetcher<List<Uri>> {
class MosaicFetcher(private val context: Context) : Fetcher<List<Uri>> {
override suspend fun fetch(
pool: BitmapPool,
data: List<Uri>,

View file

@ -7,7 +7,7 @@ data class Artist(
val givenGenres: MutableList<Genre> = mutableListOf()
) {
val albums = mutableListOf<Album>()
lateinit var genres: Map<String, List<Genre>>
val genres = mutableListOf<Genre>()
val numAlbums: Int get() = albums.size
val numSongs: Int
@ -18,8 +18,4 @@ data class Artist(
}
return num
}
fun finalizeGenres() {
genres = givenGenres.groupBy { it.name }
}
}

View file

@ -5,4 +5,13 @@ data class Genre(
var name: String,
) {
val artists = mutableListOf<Artist>()
val numArtists: Int get() = artists.size
val numAlbums: Int get() {
var num = 0
artists.forEach {
num += it.numAlbums
}
return num
}
}

View file

@ -7,7 +7,7 @@ import org.oxycblt.auxio.music.models.Genre
import org.oxycblt.auxio.music.models.Song
class MusicSorter(
val genres: MutableList<Genre>,
var genres: MutableList<Genre>,
val artists: MutableList<Artist>,
val albums: MutableList<Album>,
val songs: MutableList<Song>,
@ -81,6 +81,18 @@ class MusicSorter(
artist.albums.add(album)
}
// Then group the artist's genres and sort them by "Prominence"
// A.K.A Who has the most map entries
val groupedGenres = artist.givenGenres.groupBy { it.name }
groupedGenres.keys.sortedByDescending { key ->
groupedGenres[key]?.size
}.forEach { key ->
groupedGenres[key]?.get(0)?.let {
artist.genres.add(it)
}
}
unknownAlbums.removeAll(artistAlbums)
}
@ -127,7 +139,7 @@ class MusicSorter(
if (unknownArtists.size > 0) {
// Reuse an existing unknown genre if one is found
val unknownGenre = Genre(
val unknownGenre = genres.find { it.name == genrePlaceholder } ?: Genre(
name = genrePlaceholder
)
@ -146,8 +158,11 @@ class MusicSorter(
// Finalize music
private fun finalizeMusic() {
// Finalize the genre for each artist
artists.forEach { it.finalizeGenres() }
// Remove genre duplicates now, as there's a risk duplicates could be added during the
// sorting process.
genres = genres.distinctBy {
it.name
}.toMutableList()
// Then finally sort the music
genres.sortWith(

View file

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorPrimary">
<path
android:fillColor="@android:color/white"
android:pathData="M7,14c-1.66,0 -3,1.34 -3,3 0,1.31 -1.16,2 -2,2 0.92,1.22 2.49,2 4,2 2.21,0 4,-1.79 4,-4 0,-1.66 -1.34,-3 -3,-3zM20.71,4.63l-1.34,-1.34c-0.39,-0.39 -1.02,-0.39 -1.41,0L9,12.25 11.75,15l8.96,-8.96c0.39,-0.39 0.39,-1.02 0,-1.41z"/>
</vector>

View file

@ -0,0 +1,63 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="genre"
type="org.oxycblt.auxio.music.models.Genre" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/ripple"
android:clickable="true"
android:focusable="true"
android:padding="@dimen/padding_medium">
<ImageView
android:id="@+id/artist_image"
android:layout_width="@dimen/cover_size_normal"
android:layout_height="@dimen/cover_size_normal"
app:genreImage="@{genre}"
android:contentDescription="@{@string/description_genre_image(genre.name)}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:src="@drawable/ic_genre" />
<TextView
android:id="@+id/artist_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:text="@{genre.name}"
android:layout_marginStart="@dimen/margin_medium"
android:textAppearance="?android:attr/textAppearanceListItem"
android:textColor="?android:attr/textColorPrimary"
app:layout_constraintBottom_toTopOf="@+id/album_song_count"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/artist_image"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed"
tools:text="Genre Name" />
<TextView
android:id="@+id/album_song_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSecondary"
android:layout_marginStart="@dimen/margin_medium"
android:textColor="?android:attr/textColorSecondary"
app:genreCounts="@{genre}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/artist_image"
app:layout_constraintTop_toBottomOf="@+id/artist_name"
tools:text="2 Artists, 4 Albums" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

View file

@ -15,7 +15,8 @@
<string name="label_songs">Songs</string>
<string name="description_album_cover">Album Cover for %s</string>
<string name="description_artist_image">Artist Cover for %s</string>
<string name="description_artist_image">Artist Image for %s</string>
<string name="description_genre_image">Genre Image for %s</string>
<string name="description_track_number">Track %s</string>
<string name="description_error">Error</string>
<string name="description_sort_button">Change Sorting Mode</string>
@ -34,8 +35,13 @@
<item quantity="other">%s Songs</item>
</plurals>
<plurals name="format_albums">
<plurals name="format_album_count">
<item quantity="one">%s Album</item>
<item quantity="other">%s Albums</item>
</plurals>
<plurals name="format_artist_count">
<item quantity="one">%s Artist</item>
<item quantity="other">%s Artists</item>
</plurals>
</resources>