UnitFormat android implementation that follows locale on device
This commit is contained in:
@@ -1,30 +1,56 @@
|
||||
package app.klottr.util
|
||||
|
||||
import android.text.format.DateFormat
|
||||
import android.os.Build
|
||||
import java.text.DateFormat
|
||||
import java.util.Calendar
|
||||
import java.util.Date
|
||||
import java.util.Locale
|
||||
|
||||
// Android-actual: samma beteende som tidigare.
|
||||
actual fun formatRelativeOrTime(ms: Long): String {
|
||||
val calNow = Calendar.getInstance()
|
||||
val calTime = Calendar.getInstance().apply { timeInMillis = ms }
|
||||
val locale: Locale = Locale.getDefault()
|
||||
val date = Date(ms)
|
||||
|
||||
val sameYear = calNow.get(Calendar.YEAR) == calTime.get(Calendar.YEAR)
|
||||
val sameDay = sameYear &&
|
||||
calNow.get(Calendar.DAY_OF_YEAR) == calTime.get(Calendar.DAY_OF_YEAR)
|
||||
// Detect same day / yesterday once.
|
||||
val nowCal = Calendar.getInstance()
|
||||
val thatCal = Calendar.getInstance().apply { timeInMillis = ms }
|
||||
|
||||
val yesterday = sameYear &&
|
||||
calNow.get(Calendar.DAY_OF_YEAR) - calTime.get(Calendar.DAY_OF_YEAR) == 1
|
||||
val sameDay =
|
||||
nowCal.get(Calendar.YEAR) == thatCal.get(Calendar.YEAR) &&
|
||||
nowCal.get(Calendar.DAY_OF_YEAR) == thatCal.get(Calendar.DAY_OF_YEAR)
|
||||
|
||||
return when {
|
||||
sameDay -> {
|
||||
"idag " + DateFormat.format("HH:mm", ms).toString()
|
||||
}
|
||||
yesterday -> {
|
||||
"igår " + DateFormat.format("HH:mm", ms).toString()
|
||||
}
|
||||
else -> {
|
||||
val pattern = if (sameYear) "d MMM HH:mm" else "yyyy-MM-dd HH:mm"
|
||||
DateFormat.format(pattern, ms).toString()
|
||||
val yestCal = Calendar.getInstance().apply { add(Calendar.DAY_OF_YEAR, -1) }
|
||||
val yesterday =
|
||||
yestCal.get(Calendar.YEAR) == thatCal.get(Calendar.YEAR) &&
|
||||
yestCal.get(Calendar.DAY_OF_YEAR) == thatCal.get(Calendar.DAY_OF_YEAR)
|
||||
|
||||
if (Build.VERSION.SDK_INT >= 24) {
|
||||
// API 24+: Use ICU for truly localized "Today/Yesterday" + locale-correct joining.
|
||||
val timeStr = DateFormat.getTimeInstance(DateFormat.SHORT, locale).format(date)
|
||||
val rfmt = android.icu.text.RelativeDateTimeFormatter.getInstance(locale)
|
||||
|
||||
return when {
|
||||
sameDay -> {
|
||||
val day = rfmt.format(
|
||||
android.icu.text.RelativeDateTimeFormatter.Direction.THIS,
|
||||
android.icu.text.RelativeDateTimeFormatter.AbsoluteUnit.DAY
|
||||
)
|
||||
rfmt.combineDateAndTime(day, timeStr)
|
||||
}
|
||||
yesterday -> {
|
||||
val day = rfmt.format(
|
||||
android.icu.text.RelativeDateTimeFormatter.Direction.LAST,
|
||||
android.icu.text.RelativeDateTimeFormatter.AbsoluteUnit.DAY
|
||||
)
|
||||
rfmt.combineDateAndTime(day, timeStr)
|
||||
}
|
||||
else -> {
|
||||
DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT, locale)
|
||||
.format(date)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// < API 24: Fallback to localized date+time (no ICU-relative words available without Context).
|
||||
return DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT, locale)
|
||||
.format(date)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user