From fddf5e4472ab1baf7c371292fc603a6e4192ee0b Mon Sep 17 00:00:00 2001 From: OxygenCobalt Date: Sat, 26 Sep 2020 10:58:30 -0600 Subject: [PATCH] Remove excess zeroes from durations Remove excess zeroes from the formatted durations. --- .../org/oxycblt/auxio/music/MusicUtils.kt | 37 +++++++++++++++++-- .../org/oxycblt/auxio/music/models/Artist.kt | 15 ++------ .../org/oxycblt/auxio/music/models/Song.kt | 3 +- .../auxio/music/processing/MusicLoader.kt | 4 +- .../auxio/music/processing/MusicSorter.kt | 6 +-- app/src/main/res/layout/item_song.xml | 3 +- 6 files changed, 47 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/org/oxycblt/auxio/music/MusicUtils.kt b/app/src/main/java/org/oxycblt/auxio/music/MusicUtils.kt index cb59da50d..fd9a8ad7a 100644 --- a/app/src/main/java/org/oxycblt/auxio/music/MusicUtils.kt +++ b/app/src/main/java/org/oxycblt/auxio/music/MusicUtils.kt @@ -38,6 +38,8 @@ private val ID3_GENRES = arrayOf( const val PAREN_FILTER = "()" +// --- EXTENSION FUNCTIONS --- + // Convert legacy ID3 genres to a named genre fun String.toNamedGenre(): String? { // Strip the genres of any parentheses, and convert it to an int @@ -45,7 +47,7 @@ fun String.toNamedGenre(): String? { PAREN_FILTER.indexOf(it) > -1 }.toInt() - // If the conversion fails [Due to the genre using an extension that isn't from winamp], + // If the conversion fails [Due to the genre using an extension that Auxio doesn't have], // then return null. return ID3_GENRES.getOrNull(intGenre) } @@ -66,6 +68,27 @@ fun Long.toAlbumArtURI(): Uri { ) } +// Cut off excess zeros from a duration string +fun String.removeDurationZeroes(): String { + val split = this.chunked(1).toMutableList() + + // Iterate through the string and remove the first zero found + // If anything else is found, exit the loop. + for (i in 0 until length) { + if (this[i] == '0') { + split.removeAt(i) + + break + } else { + break + } + } + + return split.joinToString("") +} + +// --- BINDING ADAPTERS --- + // Format the amount of songs in an album @BindingAdapter("songCount") fun TextView.getAlbumSongs(album: Album) { @@ -87,8 +110,16 @@ fun TextView.bindArtistCounts(artist: Artist) { } // Get the artist genre. -// TODO: Stub, add option to list all genres instead of just the most prominent +// TODO: Add option to list all genres @BindingAdapter("artistGenre") fun TextView.getArtistGenre(artist: Artist) { - text = artist.genre + // 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() + } } diff --git a/app/src/main/java/org/oxycblt/auxio/music/models/Artist.kt b/app/src/main/java/org/oxycblt/auxio/music/models/Artist.kt index 2d2d7c097..b97bd7717 100644 --- a/app/src/main/java/org/oxycblt/auxio/music/models/Artist.kt +++ b/app/src/main/java/org/oxycblt/auxio/music/models/Artist.kt @@ -4,10 +4,10 @@ package org.oxycblt.auxio.music.models data class Artist( val id: Long = -1, var name: String, - val genres: MutableList = mutableListOf() + val givenGenres: MutableList = mutableListOf() ) { val albums = mutableListOf() - var genre = "" + lateinit var genres: Map> val numAlbums: Int get() = albums.size val numSongs: Int @@ -19,14 +19,7 @@ data class Artist( return num } - fun finalizeGenre() { - // If the artist has more than one genre, pick the most "Prominent" one. - genre = if (genres.size > 1) { - val groupGenres = genres.groupBy { it.name } - - groupGenres.keys.sortedByDescending { groupGenres[it]?.size }[0] - } else { - genres[0].name - } + fun finalizeGenres() { + genres = givenGenres.groupBy { it.name } } } diff --git a/app/src/main/java/org/oxycblt/auxio/music/models/Song.kt b/app/src/main/java/org/oxycblt/auxio/music/models/Song.kt index 5dcdc4d6b..2fc566d6b 100644 --- a/app/src/main/java/org/oxycblt/auxio/music/models/Song.kt +++ b/app/src/main/java/org/oxycblt/auxio/music/models/Song.kt @@ -1,6 +1,7 @@ package org.oxycblt.auxio.music.models import android.text.format.DateUtils +import org.oxycblt.auxio.music.removeDurationZeroes // Class containing all relevant values for a song. data class Song( @@ -13,5 +14,5 @@ data class Song( lateinit var album: Album val seconds = duration / 1000 - val formattedDuration: String = DateUtils.formatElapsedTime(seconds) + val formattedDuration: String = DateUtils.formatElapsedTime(seconds).removeDurationZeroes() } diff --git a/app/src/main/java/org/oxycblt/auxio/music/processing/MusicLoader.kt b/app/src/main/java/org/oxycblt/auxio/music/processing/MusicLoader.kt index af421988f..05afa685f 100644 --- a/app/src/main/java/org/oxycblt/auxio/music/processing/MusicLoader.kt +++ b/app/src/main/java/org/oxycblt/auxio/music/processing/MusicLoader.kt @@ -138,7 +138,7 @@ class MusicLoader( val existingArtist = artists.find { it.name == name } if (existingArtist != null) { - existingArtist.genres.add(genre) + existingArtist.givenGenres.add(genre) } else { artists.add( Artist( @@ -155,7 +155,7 @@ class MusicLoader( // Remove dupes [Just in case] artists = artists.distinctBy { - it.name to it.genres + it.name to it.givenGenres }.toMutableList() Log.d( diff --git a/app/src/main/java/org/oxycblt/auxio/music/processing/MusicSorter.kt b/app/src/main/java/org/oxycblt/auxio/music/processing/MusicSorter.kt index a87f1c7b8..cec217eb0 100644 --- a/app/src/main/java/org/oxycblt/auxio/music/processing/MusicSorter.kt +++ b/app/src/main/java/org/oxycblt/auxio/music/processing/MusicSorter.kt @@ -114,7 +114,7 @@ class MusicSorter( for (genre in genres) { // Find all artists that match the current genre val genreArtists = artists.filter { artist -> - artist.genres.any { + artist.givenGenres.any { it.name == genre.name } } @@ -132,7 +132,7 @@ class MusicSorter( ) for (artist in unknownArtists) { - artist.genres.add(unknownGenre) + artist.givenGenres.add(unknownGenre) unknownGenre.artists.add(artist) } genres.add(unknownGenre) @@ -147,7 +147,7 @@ class MusicSorter( // Finalize music private fun finalizeMusic() { // Finalize the genre for each artist - artists.forEach { it.finalizeGenre() } + artists.forEach { it.finalizeGenres() } // Then finally sort the music genres.sortWith( diff --git a/app/src/main/res/layout/item_song.xml b/app/src/main/res/layout/item_song.xml index e584a8973..a1a084278 100644 --- a/app/src/main/res/layout/item_song.xml +++ b/app/src/main/res/layout/item_song.xml @@ -67,10 +67,11 @@