From 8df89db77b56229457c4402fdc43218cbe77edcd Mon Sep 17 00:00:00 2001 From: Alexander Capehart Date: Sat, 19 Nov 2022 16:34:00 -0700 Subject: [PATCH] music: backport full dates to older versions Backport the code for full "Month + Year" dates to older versions with the legacy Date API. For the same of not missing bugs on newer devices, this is now what will be used in Auxio. --- CHANGELOG.md | 2 +- .../main/java/org/oxycblt/auxio/music/Tags.kt | 52 ++++++++----------- .../ui/fastscroll/FastScrollRecyclerView.kt | 2 +- 3 files changed, 23 insertions(+), 33 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 54eea76c7..230124c51 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,7 +24,7 @@ - Reshuffling the queue will no longer drop any songs you have added/removed - Allowed light/dark theme to be customized on Android 12+ - All information now scrolls in the playback view -- A month is now shown for song/album dates when available (Android O+ only) +- A month is now shown for song/album dates when available #### What's Fixed - Fixed issue where the scroll popup would not display correctly in landscape mode [#230] diff --git a/app/src/main/java/org/oxycblt/auxio/music/Tags.kt b/app/src/main/java/org/oxycblt/auxio/music/Tags.kt index 00cdbfa5a..d9db455e5 100644 --- a/app/src/main/java/org/oxycblt/auxio/music/Tags.kt +++ b/app/src/main/java/org/oxycblt/auxio/music/Tags.kt @@ -31,6 +31,7 @@ import org.oxycblt.auxio.R import org.oxycblt.auxio.util.inRangeOrNull import org.oxycblt.auxio.util.logE import org.oxycblt.auxio.util.nonZeroOrNull +import java.text.SimpleDateFormat /** * An ISO-8601/RFC 3339 Date. @@ -80,37 +81,26 @@ class Date private constructor(private val tokens: List) : Comparable * Resolve this date into a string. This could result in a year string formatted as "YYYY", or a * month and year string formatted as "MMM YYYY" depending on the situation. */ - fun resolveDate(context: Context): String { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - return try { - resolveFullDate(context) - } catch (e: Exception) { - logE("Failed to format a full date") - logE(e.stackTraceToString()) - return resolveYear(context) - } - } else { - return resolveYear(context) + fun resolveDate(context: Context) = + try { + resolveFullDate(context) + } catch (e: Exception) { + logE("Failed to format a full date") + logE(e.stackTraceToString()) + resolveYear(context) } - } - @RequiresApi(Build.VERSION_CODES.O) - private fun resolveFullDate(context: Context) = - if (month != null) { - val temporal = - DateTimeFormatter.ISO_DATE.parse( - "$year-$month-${day ?: 1}", TemporalQueries.localDate()) - - // When it comes to songs, we only want to show the month and year. This - // cannot be done with DateUtils due to it's dynamic nature, so instead - // it's done with the built-in date formatter. Since the legacy date API - // is awful, we only use instant and limit it to Android 8 onwards. - temporal - .atStartOfDay(ZoneId.systemDefault()) - .format(DateTimeFormatter.ofPattern("MMM yyyy", Locale.getDefault())) + private fun resolveFullDate(context: Context): String { + return if (month != null) { + val format = (SimpleDateFormat.getDateInstance() as SimpleDateFormat) + format.applyPattern("yyyy-MM-dd") + val date = format.parse("$year-$month-${day ?: 1}") ?: return resolveYear(context) + format.applyPattern("MMM yyyy") + format.format(date) } else { resolveYear(context) } + } /** Resolve the year field in a way suitable for the UI. */ private fun resolveYear(context: Context) = context.getString(R.string.fmt_number, year) @@ -143,11 +133,11 @@ class Date private constructor(private val tokens: List) : Comparable private fun StringBuilder.appendDate(): StringBuilder { append(year.toFixedString(4)) - append("-${(month ?: 1).toFixedString(2)}") - append("-${(day ?: 1).toFixedString(2)}") - append("T${(hour ?: 0).toFixedString(2)}") - append(":${(minute ?: 0).toFixedString(2)}") - append(":${(second ?: 0).toFixedString(2)}") + append("-${(month ?: return this).toFixedString(2)}") + append("-${(day ?: return this).toFixedString(2)}") + append("T${(hour ?: return this).toFixedString(2)}") + append(":${(minute ?: return this.append('Z')).toFixedString(2)}") + append(":${(second ?: return this.append('Z')).toFixedString(2)}") return this.append('Z') } diff --git a/app/src/main/java/org/oxycblt/auxio/ui/fastscroll/FastScrollRecyclerView.kt b/app/src/main/java/org/oxycblt/auxio/ui/fastscroll/FastScrollRecyclerView.kt index 760928958..ebc2c02d9 100644 --- a/app/src/main/java/org/oxycblt/auxio/ui/fastscroll/FastScrollRecyclerView.kt +++ b/app/src/main/java/org/oxycblt/auxio/ui/fastscroll/FastScrollRecyclerView.kt @@ -67,7 +67,7 @@ import org.oxycblt.auxio.util.systemBarInsetsCompat * * TODO: Add vibration when popup changes * - * TODO: Improve this for variably sized items + * TODO: Improve support for variably sized items * * @author Hai Zhang, OxygenCobalt */