From 298c49f3ab7000ed0457aa177bba9a98072b1339 Mon Sep 17 00:00:00 2001 From: arkon Date: Sun, 29 Oct 2023 11:54:50 -0400 Subject: [PATCH] Abstract out library last updated timespan text So we can reuse it for showing last automatic backup time. --- .../presentation/updates/UpdatesScreen.kt | 4 +--- .../presentation/updates/UpdatesUiItem.kt | 20 ++----------------- .../util/{DurationUtils.kt => TimeUtils.kt} | 17 ++++++++++++++++ i18n/src/main/res/values/strings.xml | 1 + 4 files changed, 21 insertions(+), 21 deletions(-) rename app/src/main/java/eu/kanade/presentation/util/{DurationUtils.kt => TimeUtils.kt} (51%) diff --git a/app/src/main/java/eu/kanade/presentation/updates/UpdatesScreen.kt b/app/src/main/java/eu/kanade/presentation/updates/UpdatesScreen.kt index 1572faff4f..5391d56bb0 100644 --- a/app/src/main/java/eu/kanade/presentation/updates/UpdatesScreen.kt +++ b/app/src/main/java/eu/kanade/presentation/updates/UpdatesScreen.kt @@ -109,9 +109,7 @@ fun UpdateScreen( FastScrollLazyColumn( contentPadding = contentPadding, ) { - if (lastUpdated > 0L) { - updatesLastUpdatedItem(lastUpdated) - } + updatesLastUpdatedItem(lastUpdated) updatesUiItems( uiModels = state.getUiModel(context, relativeTime), diff --git a/app/src/main/java/eu/kanade/presentation/updates/UpdatesUiItem.kt b/app/src/main/java/eu/kanade/presentation/updates/UpdatesUiItem.kt index 50ef6840ba..9be12b6bb0 100644 --- a/app/src/main/java/eu/kanade/presentation/updates/UpdatesUiItem.kt +++ b/app/src/main/java/eu/kanade/presentation/updates/UpdatesUiItem.kt @@ -1,6 +1,5 @@ package eu.kanade.presentation.updates -import android.text.format.DateUtils import androidx.compose.foundation.combinedClickable import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column @@ -27,7 +26,6 @@ import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier -import androidx.compose.ui.draw.alpha import androidx.compose.ui.hapticfeedback.HapticFeedbackType import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.platform.LocalHapticFeedback @@ -39,6 +37,7 @@ import eu.kanade.presentation.manga.components.ChapterDownloadAction import eu.kanade.presentation.manga.components.ChapterDownloadIndicator import eu.kanade.presentation.manga.components.DotSeparatorText import eu.kanade.presentation.manga.components.MangaCover +import eu.kanade.presentation.util.relativeTimeSpanString import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.download.model.Download import eu.kanade.tachiyomi.ui.updates.UpdatesItem @@ -47,33 +46,18 @@ import tachiyomi.presentation.core.components.ListGroupHeader import tachiyomi.presentation.core.components.material.ReadItemAlpha import tachiyomi.presentation.core.components.material.padding import tachiyomi.presentation.core.util.selectedBackground -import java.util.Date -import kotlin.time.Duration.Companion.minutes internal fun LazyListScope.updatesLastUpdatedItem( lastUpdated: Long, ) { item(key = "updates-lastUpdated") { - val time = remember(lastUpdated) { - val now = Date().time - if (now - lastUpdated < 1.minutes.inWholeMilliseconds) { - null - } else { - DateUtils.getRelativeTimeSpanString(lastUpdated, now, DateUtils.MINUTE_IN_MILLIS) - } - } - Box( modifier = Modifier .animateItemPlacement() .padding(horizontal = MaterialTheme.padding.medium, vertical = MaterialTheme.padding.small), ) { Text( - text = if (time.isNullOrEmpty()) { - stringResource(R.string.updates_last_update_info, stringResource(R.string.updates_last_update_info_just_now)) - } else { - stringResource(R.string.updates_last_update_info, time) - }, + text = stringResource(R.string.updates_last_update_info, relativeTimeSpanString(lastUpdated)), fontStyle = FontStyle.Italic, ) } diff --git a/app/src/main/java/eu/kanade/presentation/util/DurationUtils.kt b/app/src/main/java/eu/kanade/presentation/util/TimeUtils.kt similarity index 51% rename from app/src/main/java/eu/kanade/presentation/util/DurationUtils.kt rename to app/src/main/java/eu/kanade/presentation/util/TimeUtils.kt index 644f5ca13e..e98374fd87 100644 --- a/app/src/main/java/eu/kanade/presentation/util/DurationUtils.kt +++ b/app/src/main/java/eu/kanade/presentation/util/TimeUtils.kt @@ -1,8 +1,14 @@ package eu.kanade.presentation.util import android.content.Context +import android.text.format.DateUtils +import androidx.compose.runtime.Composable +import androidx.compose.runtime.ReadOnlyComposable +import androidx.compose.ui.res.stringResource import eu.kanade.tachiyomi.R +import java.util.Date import kotlin.time.Duration +import kotlin.time.Duration.Companion.minutes fun Duration.toDurationString(context: Context, fallback: String): String { return toComponents { days, hours, minutes, seconds, _ -> @@ -14,3 +20,14 @@ fun Duration.toDurationString(context: Context, fallback: String): String { }.joinToString(" ").ifBlank { fallback } } } + +@Composable +@ReadOnlyComposable +fun relativeTimeSpanString(epochMillis: Long): String { + val now = Date().time + return when { + epochMillis <= 0L -> stringResource(R.string.relative_time_span_never) + now - epochMillis < 1.minutes.inWholeMilliseconds -> stringResource(R.string.updates_last_update_info_just_now) + else -> DateUtils.getRelativeTimeSpanString(epochMillis, now, DateUtils.MINUTE_IN_MILLIS).toString() + } +} diff --git a/i18n/src/main/res/values/strings.xml b/i18n/src/main/res/values/strings.xml index 642f4ce7d3..7085c9d4c3 100644 --- a/i18n/src/main/res/values/strings.xml +++ b/i18n/src/main/res/values/strings.xml @@ -782,6 +782,7 @@ Unable to open last read chapter Library last updated: %s Just now + Never Ch. %1$s - %2$s