musikr: decouple date range from auxio

This commit is contained in:
Alexander Capehart 2024-12-14 15:51:52 -05:00
parent cb84b2db17
commit e9c15bfbef
No known key found for this signature in database
GPG key ID: 37DBE3621FE9AD47
5 changed files with 19 additions and 23 deletions

View file

@ -132,7 +132,7 @@ class AlbumDetailFragment : DetailFragment<Album, Song>() {
// Date, song count, and duration map to the info text
binding.detailInfo.apply {
// Fall back to a friendlier "No date" text if the album doesn't have date information
val date = album.dates?.resolveDate(context) ?: context.getString(R.string.def_date)
val date = album.dates?.resolve(context) ?: context.getString(R.string.def_date)
val songCount = context.getPlural(R.plurals.fmt_song_count, album.songs.size)
val duration = album.durationMs.formatDurationMs(true)
text = context.getString(R.string.fmt_three, date, songCount, duration)

View file

@ -105,8 +105,7 @@ private class ArtistAlbumViewHolder private constructor(private val binding: Ite
binding.parentName.text = album.name.resolve(binding.context)
binding.parentInfo.text =
// Fall back to a friendlier "No date" text if the album doesn't have date information
album.dates?.resolveDate(binding.context)
?: binding.context.getString(R.string.def_date)
album.dates?.resolve(binding.context) ?: binding.context.getString(R.string.def_date)
}
override fun updatePlayingIndicator(isActive: Boolean, isPlaying: Boolean) {

View file

@ -38,6 +38,7 @@ import org.oxycblt.auxio.list.recycler.SongViewHolder
import org.oxycblt.auxio.list.sort.Sort
import org.oxycblt.auxio.music.IndexingState
import org.oxycblt.auxio.music.MusicViewModel
import org.oxycblt.auxio.music.resolve
import org.oxycblt.auxio.playback.PlaybackViewModel
import org.oxycblt.auxio.playback.formatDurationMs
import org.oxycblt.auxio.playback.secsToMs
@ -114,7 +115,7 @@ class SongListFragment :
is Sort.Mode.ByAlbum -> song.album.name.thumb()
// Year -> Use Full Year
is Sort.Mode.ByDate -> song.album.dates?.resolveDate(requireContext())
is Sort.Mode.ByDate -> song.album.dates?.resolve(requireContext())
// Duration -> Use formatted duration
is Sort.Mode.ByDuration -> song.durationMs.formatDurationMs(false)

View file

@ -103,3 +103,18 @@ private fun Date.resolveFineGrained(): String? {
fun Disc?.resolve(context: Context) =
this?.run { context.getString(R.string.fmt_disc_no, number) }
?: context.getString(R.string.def_disc)
/**
* Resolve this instance into a human-readable date range.
*
* @param context [Context] required to get human-readable names.
* @return If the date has a maximum value, then a `min - max` formatted string will be returned
* with the formatted [Date]s of the minimum and maximum dates respectively. Otherwise, the
* formatted name of the minimum [Date] will be returned.
*/
fun Date.Range.resolve(context: Context) =
if (min != max) {
context.getString(R.string.fmt_date_range, min.resolve(context), max.resolve(context))
} else {
min.resolve(context)
}

View file

@ -18,10 +18,7 @@
package org.oxycblt.musikr.tag
import android.content.Context
import kotlin.math.max
import org.oxycblt.auxio.R
import org.oxycblt.auxio.music.resolve
import org.oxycblt.musikr.util.inRangeOrNull
import org.oxycblt.musikr.util.positiveOrNull
@ -98,22 +95,6 @@ class Date private constructor(private val tokens: List<Int>) : Comparable<Date>
check(min <= max) { "Min date must be <= max date" }
}
/**
* Resolve this instance into a human-readable date range.
*
* @param context [Context] required to get human-readable names.
* @return If the date has a maximum value, then a `min - max` formatted string will be
* returned with the formatted [Date]s of the minimum and maximum dates respectively.
* Otherwise, the formatted name of the minimum [Date] will be returned.
*/
fun resolveDate(context: Context) =
if (min != max) {
context.getString(
R.string.fmt_date_range, min.resolve(context), max.resolve(context))
} else {
min.resolve(context)
}
override fun equals(other: Any?) = other is Range && min == other.min && max == other.max
override fun hashCode() = 31 * max.hashCode() + min.hashCode()