musikr: decouple date from auxio

This commit is contained in:
Alexander Capehart 2024-12-14 15:48:05 -05:00
parent c5cd404393
commit e3146647d3
No known key found for this signature in database
GPG key ID: 37DBE3621FE9AD47
3 changed files with 40 additions and 37 deletions

View file

@ -39,6 +39,7 @@ import org.oxycblt.auxio.list.recycler.FastScrollRecyclerView
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

View file

@ -19,12 +19,16 @@
package org.oxycblt.auxio.music
import android.content.Context
import java.text.ParseException
import java.text.SimpleDateFormat
import kotlin.math.max
import org.oxycblt.auxio.R
import org.oxycblt.auxio.util.concatLocalized
import org.oxycblt.musikr.Music
import org.oxycblt.musikr.tag.Date
import org.oxycblt.musikr.tag.Name
import org.oxycblt.musikr.tag.Placeholder
import timber.log.Timber
fun Name.resolve(context: Context) =
when (this) {
@ -65,3 +69,32 @@ fun <T : Music> List<T>.areNamesTheSame(other: List<T>): Boolean {
return true
}
/**
* Resolve this instance into a human-readable date.
*
* @param context [Context] required to get human-readable names.
* @return If the [Date] has a valid month and year value, a more fine-grained date (ex. "Jan 2020")
* will be returned. Otherwise, a plain year value (ex. "2020") is returned. Dates will be
* properly localized.
*/
fun Date.resolve(context: Context) =
// Unable to create fine-grained date, just format as a year.
month?.let { resolveFineGrained() } ?: context.getString(R.string.fmt_number, year)
private fun Date.resolveFineGrained(): String? {
// We can't directly load a date with our own
val format = (SimpleDateFormat.getDateInstance() as SimpleDateFormat)
format.applyPattern("yyyy-MM")
val date =
try {
format.parse("$year-$month")
} catch (e: ParseException) {
Timber.e("Unable to parse fine-grained date: $e")
return null
}
// Reformat as a readable month and year
format.applyPattern("MMM yyyy")
return format.format(date)
}

View file

@ -19,13 +19,11 @@
package org.oxycblt.musikr.tag
import android.content.Context
import java.text.ParseException
import java.text.SimpleDateFormat
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
import timber.log.Timber as L
/**
* An ISO-8601/RFC 3339 Date.
@ -38,40 +36,11 @@ import timber.log.Timber as L
*/
class Date private constructor(private val tokens: List<Int>) : Comparable<Date> {
val year = tokens[0]
private val month = tokens.getOrNull(1)
private val day = tokens.getOrNull(2)
private val hour = tokens.getOrNull(3)
private val minute = tokens.getOrNull(4)
private val second = tokens.getOrNull(5)
/**
* Resolve this instance into a human-readable date.
*
* @param context [Context] required to get human-readable names.
* @return If the [Date] has a valid month and year value, a more fine-grained date (ex. "Jan
* 2020") will be returned. Otherwise, a plain year value (ex. "2020") is returned. Dates will
* be properly localized.
*/
fun resolve(context: Context) =
// Unable to create fine-grained date, just format as a year.
month?.let { resolveFineGrained() } ?: context.getString(R.string.fmt_number, year)
private fun resolveFineGrained(): String? {
// We can't directly load a date with our own
val format = (SimpleDateFormat.getDateInstance() as SimpleDateFormat)
format.applyPattern("yyyy-MM")
val date =
try {
format.parse("$year-$month")
} catch (e: ParseException) {
L.e("Unable to parse fine-grained date: $e")
return null
}
// Reformat as a readable month and year
format.applyPattern("MMM yyyy")
return format.format(date)
}
val month = tokens.getOrNull(1)
val day = tokens.getOrNull(2)
val hour = tokens.getOrNull(3)
val minute = tokens.getOrNull(4)
val second = tokens.getOrNull(5)
override fun equals(other: Any?) = other is Date && compareTo(other) == 0