UnitFormat android implementation that follows locale on device
This commit is contained in:
@@ -1,30 +1,56 @@
|
|||||||
package app.klottr.util
|
package app.klottr.util
|
||||||
|
|
||||||
import android.text.format.DateFormat
|
import android.os.Build
|
||||||
|
import java.text.DateFormat
|
||||||
import java.util.Calendar
|
import java.util.Calendar
|
||||||
|
import java.util.Date
|
||||||
|
import java.util.Locale
|
||||||
|
|
||||||
// Android-actual: samma beteende som tidigare.
|
|
||||||
actual fun formatRelativeOrTime(ms: Long): String {
|
actual fun formatRelativeOrTime(ms: Long): String {
|
||||||
val calNow = Calendar.getInstance()
|
val locale: Locale = Locale.getDefault()
|
||||||
val calTime = Calendar.getInstance().apply { timeInMillis = ms }
|
val date = Date(ms)
|
||||||
|
|
||||||
val sameYear = calNow.get(Calendar.YEAR) == calTime.get(Calendar.YEAR)
|
// Detect same day / yesterday once.
|
||||||
val sameDay = sameYear &&
|
val nowCal = Calendar.getInstance()
|
||||||
calNow.get(Calendar.DAY_OF_YEAR) == calTime.get(Calendar.DAY_OF_YEAR)
|
val thatCal = Calendar.getInstance().apply { timeInMillis = ms }
|
||||||
|
|
||||||
val yesterday = sameYear &&
|
val sameDay =
|
||||||
calNow.get(Calendar.DAY_OF_YEAR) - calTime.get(Calendar.DAY_OF_YEAR) == 1
|
nowCal.get(Calendar.YEAR) == thatCal.get(Calendar.YEAR) &&
|
||||||
|
nowCal.get(Calendar.DAY_OF_YEAR) == thatCal.get(Calendar.DAY_OF_YEAR)
|
||||||
|
|
||||||
|
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 {
|
return when {
|
||||||
sameDay -> {
|
sameDay -> {
|
||||||
"idag " + DateFormat.format("HH:mm", ms).toString()
|
val day = rfmt.format(
|
||||||
|
android.icu.text.RelativeDateTimeFormatter.Direction.THIS,
|
||||||
|
android.icu.text.RelativeDateTimeFormatter.AbsoluteUnit.DAY
|
||||||
|
)
|
||||||
|
rfmt.combineDateAndTime(day, timeStr)
|
||||||
}
|
}
|
||||||
yesterday -> {
|
yesterday -> {
|
||||||
"igår " + DateFormat.format("HH:mm", ms).toString()
|
val day = rfmt.format(
|
||||||
|
android.icu.text.RelativeDateTimeFormatter.Direction.LAST,
|
||||||
|
android.icu.text.RelativeDateTimeFormatter.AbsoluteUnit.DAY
|
||||||
|
)
|
||||||
|
rfmt.combineDateAndTime(day, timeStr)
|
||||||
}
|
}
|
||||||
else -> {
|
else -> {
|
||||||
val pattern = if (sameYear) "d MMM HH:mm" else "yyyy-MM-dd HH:mm"
|
DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT, locale)
|
||||||
DateFormat.format(pattern, ms).toString()
|
.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