Remove excess zeroes from durations

Remove excess zeroes from the formatted durations.
This commit is contained in:
OxygenCobalt 2020-09-26 10:58:30 -06:00
parent 531825465f
commit fddf5e4472
6 changed files with 47 additions and 21 deletions

View file

@ -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()
}
}

View file

@ -4,10 +4,10 @@ package org.oxycblt.auxio.music.models
data class Artist(
val id: Long = -1,
var name: String,
val genres: MutableList<Genre> = mutableListOf()
val givenGenres: MutableList<Genre> = mutableListOf()
) {
val albums = mutableListOf<Album>()
var genre = ""
lateinit var genres: Map<String, List<Genre>>
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 }
}
}

View file

@ -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()
}

View file

@ -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(

View file

@ -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(

View file

@ -67,10 +67,11 @@
<TextView
android:id="@+id/duration"
android:layout_width="wrap_content"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@{song.formattedDuration}"
android:textAlignment="center"
android:textAppearance="?android:attr/textAppearanceListItemSecondary"
android:textColor="?android:attr/textColorTertiary"
app:layout_constraintBottom_toBottomOf="parent"