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.
This commit is contained in:
parent
c13a57f694
commit
8df89db77b
3 changed files with 23 additions and 33 deletions
|
|
@ -24,7 +24,7 @@
|
||||||
- Reshuffling the queue will no longer drop any songs you have added/removed
|
- Reshuffling the queue will no longer drop any songs you have added/removed
|
||||||
- Allowed light/dark theme to be customized on Android 12+
|
- Allowed light/dark theme to be customized on Android 12+
|
||||||
- All information now scrolls in the playback view
|
- 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
|
#### What's Fixed
|
||||||
- Fixed issue where the scroll popup would not display correctly in landscape mode [#230]
|
- Fixed issue where the scroll popup would not display correctly in landscape mode [#230]
|
||||||
|
|
|
||||||
|
|
@ -31,6 +31,7 @@ import org.oxycblt.auxio.R
|
||||||
import org.oxycblt.auxio.util.inRangeOrNull
|
import org.oxycblt.auxio.util.inRangeOrNull
|
||||||
import org.oxycblt.auxio.util.logE
|
import org.oxycblt.auxio.util.logE
|
||||||
import org.oxycblt.auxio.util.nonZeroOrNull
|
import org.oxycblt.auxio.util.nonZeroOrNull
|
||||||
|
import java.text.SimpleDateFormat
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An ISO-8601/RFC 3339 Date.
|
* An ISO-8601/RFC 3339 Date.
|
||||||
|
|
@ -80,37 +81,26 @@ class Date private constructor(private val tokens: List<Int>) : Comparable<Date>
|
||||||
* Resolve this date into a string. This could result in a year string formatted as "YYYY", or a
|
* 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.
|
* month and year string formatted as "MMM YYYY" depending on the situation.
|
||||||
*/
|
*/
|
||||||
fun resolveDate(context: Context): String {
|
fun resolveDate(context: Context) =
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
try {
|
||||||
return try {
|
resolveFullDate(context)
|
||||||
resolveFullDate(context)
|
} catch (e: Exception) {
|
||||||
} catch (e: Exception) {
|
logE("Failed to format a full date")
|
||||||
logE("Failed to format a full date")
|
logE(e.stackTraceToString())
|
||||||
logE(e.stackTraceToString())
|
resolveYear(context)
|
||||||
return resolveYear(context)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return resolveYear(context)
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@RequiresApi(Build.VERSION_CODES.O)
|
private fun resolveFullDate(context: Context): String {
|
||||||
private fun resolveFullDate(context: Context) =
|
return if (month != null) {
|
||||||
if (month != null) {
|
val format = (SimpleDateFormat.getDateInstance() as SimpleDateFormat)
|
||||||
val temporal =
|
format.applyPattern("yyyy-MM-dd")
|
||||||
DateTimeFormatter.ISO_DATE.parse(
|
val date = format.parse("$year-$month-${day ?: 1}") ?: return resolveYear(context)
|
||||||
"$year-$month-${day ?: 1}", TemporalQueries.localDate())
|
format.applyPattern("MMM yyyy")
|
||||||
|
format.format(date)
|
||||||
// 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()))
|
|
||||||
} else {
|
} else {
|
||||||
resolveYear(context)
|
resolveYear(context)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** Resolve the year field in a way suitable for the UI. */
|
/** Resolve the year field in a way suitable for the UI. */
|
||||||
private fun resolveYear(context: Context) = context.getString(R.string.fmt_number, year)
|
private fun resolveYear(context: Context) = context.getString(R.string.fmt_number, year)
|
||||||
|
|
@ -143,11 +133,11 @@ class Date private constructor(private val tokens: List<Int>) : Comparable<Date>
|
||||||
|
|
||||||
private fun StringBuilder.appendDate(): StringBuilder {
|
private fun StringBuilder.appendDate(): StringBuilder {
|
||||||
append(year.toFixedString(4))
|
append(year.toFixedString(4))
|
||||||
append("-${(month ?: 1).toFixedString(2)}")
|
append("-${(month ?: return this).toFixedString(2)}")
|
||||||
append("-${(day ?: 1).toFixedString(2)}")
|
append("-${(day ?: return this).toFixedString(2)}")
|
||||||
append("T${(hour ?: 0).toFixedString(2)}")
|
append("T${(hour ?: return this).toFixedString(2)}")
|
||||||
append(":${(minute ?: 0).toFixedString(2)}")
|
append(":${(minute ?: return this.append('Z')).toFixedString(2)}")
|
||||||
append(":${(second ?: 0).toFixedString(2)}")
|
append(":${(second ?: return this.append('Z')).toFixedString(2)}")
|
||||||
return this.append('Z')
|
return this.append('Z')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -67,7 +67,7 @@ import org.oxycblt.auxio.util.systemBarInsetsCompat
|
||||||
*
|
*
|
||||||
* TODO: Add vibration when popup changes
|
* TODO: Add vibration when popup changes
|
||||||
*
|
*
|
||||||
* TODO: Improve this for variably sized items
|
* TODO: Improve support for variably sized items
|
||||||
*
|
*
|
||||||
* @author Hai Zhang, OxygenCobalt
|
* @author Hai Zhang, OxygenCobalt
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue