6d69903a56
* Replace java.time APIs with kotlin(x).(date)time kotlinx-datetime explicitly says in its README that it does not cover i18n, so we have to stick with using their toJavaX converters wherever we localise dates and times. Yes, some of these replacements are... questionable. But I wanted to try and maximise the replacement for now. * Bump kotlinx-datetime to 0.8.0 * More kotlin.time/kotlinx-datetime replacements * Remove redundant init block & make attributes val * Replace new use of java.time.Instant
46 lines
1.4 KiB
Kotlin
46 lines
1.4 KiB
Kotlin
package eu.kanade.presentation.components
|
|
|
|
import androidx.compose.runtime.Composable
|
|
import androidx.compose.runtime.remember
|
|
import androidx.compose.ui.platform.LocalContext
|
|
import eu.kanade.domain.ui.UiPreferences
|
|
import eu.kanade.tachiyomi.util.lang.toRelativeString
|
|
import kotlinx.datetime.LocalDate
|
|
import kotlinx.datetime.TimeZone
|
|
import kotlinx.datetime.toLocalDateTime
|
|
import tachiyomi.i18n.MR
|
|
import tachiyomi.presentation.core.i18n.stringResource
|
|
import uy.kohesive.injekt.Injekt
|
|
import uy.kohesive.injekt.api.get
|
|
import kotlin.time.Instant
|
|
|
|
@Composable
|
|
fun relativeDateText(
|
|
dateEpochMillis: Long,
|
|
): String {
|
|
return relativeDateText(
|
|
localDate = Instant.fromEpochMilliseconds(dateEpochMillis)
|
|
.toLocalDateTime(TimeZone.currentSystemDefault())
|
|
.date
|
|
.takeIf { dateEpochMillis != 0L },
|
|
)
|
|
}
|
|
|
|
@Composable
|
|
fun relativeDateText(
|
|
localDate: LocalDate?,
|
|
): String {
|
|
val context = LocalContext.current
|
|
|
|
val preferences = remember { Injekt.get<UiPreferences>() }
|
|
val relativeTime = remember { preferences.relativeTime.get() }
|
|
val dateFormat = remember { UiPreferences.dateFormat(preferences.dateFormat.get()) }
|
|
|
|
return localDate?.toRelativeString(
|
|
context = context,
|
|
relative = relativeTime,
|
|
dateFormat = dateFormat,
|
|
)
|
|
?: stringResource(MR.strings.not_applicable)
|
|
}
|