fix: Dates in the Vault / Send list are incorrectly formatted

This commit is contained in:
Artem Chepurnoy 2024-01-09 22:03:58 +02:00
parent f8986be2ba
commit 12aec0cbc5
No known key found for this signature in database
GPG Key ID: FAC37D0CF674043E

View File

@ -1,21 +1,30 @@
package com.artemchep.keyguard.copy package com.artemchep.keyguard.copy
import com.artemchep.keyguard.common.usecase.DateFormatter import com.artemchep.keyguard.common.usecase.DateFormatter
import com.artemchep.keyguard.feature.datepicker.getMonthTitleStringRes
import com.artemchep.keyguard.feature.localization.textResource
import com.artemchep.keyguard.platform.LeContext
import kotlinx.datetime.Instant import kotlinx.datetime.Instant
import kotlinx.datetime.TimeZone
import kotlinx.datetime.toLocalDateTime
import org.kodein.di.DirectDI import org.kodein.di.DirectDI
import org.kodein.di.instance
import java.text.DateFormat import java.text.DateFormat
import java.text.SimpleDateFormat
import java.util.Date import java.util.Date
class DateFormatterAndroid( class DateFormatterAndroid(
private val context: LeContext,
) : DateFormatter { ) : DateFormatter {
private val formatterDateTime = private val formatterDateTime =
DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.SHORT) DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.SHORT)
private val formatterDate = DateFormat.getDateInstance(DateFormat.LONG) private val formatterDate = DateFormat.getDateInstance(DateFormat.LONG)
private val formatterDateShort = SimpleDateFormat("MMMM yyyy")
constructor(directDI: DirectDI) : this() constructor(
directDI: DirectDI,
) : this(
context = directDI.instance(),
)
override fun formatDateTime( override fun formatDateTime(
instant: Instant, instant: Instant,
@ -30,7 +39,16 @@ class DateFormatterAndroid(
} }
override fun formatDateShort(instant: Instant): String { override fun formatDateShort(instant: Instant): String {
val date = instant.toEpochMilliseconds().let(::Date) val tz = TimeZone.currentSystemDefault()
return formatterDateShort.format(date) val dt = instant.toLocalDateTime(tz)
// Manually format the date. Using the "MMMM yyyy" format
// doesn't work correctly for some locales.
val year = dt.year.toString()
val month = kotlin.run {
val res = getMonthTitleStringRes(dt.monthNumber)
textResource(res, context)
}
return "$month $year"
} }
} }