Abstract out library last updated timespan text

So we can reuse it for showing last automatic backup time.
This commit is contained in:
arkon 2023-10-29 11:54:50 -04:00
parent ce5e10be95
commit 298c49f3ab
4 changed files with 21 additions and 21 deletions

View File

@ -109,9 +109,7 @@ fun UpdateScreen(
FastScrollLazyColumn( FastScrollLazyColumn(
contentPadding = contentPadding, contentPadding = contentPadding,
) { ) {
if (lastUpdated > 0L) { updatesLastUpdatedItem(lastUpdated)
updatesLastUpdatedItem(lastUpdated)
}
updatesUiItems( updatesUiItems(
uiModels = state.getUiModel(context, relativeTime), uiModels = state.getUiModel(context, relativeTime),

View File

@ -1,6 +1,5 @@
package eu.kanade.presentation.updates package eu.kanade.presentation.updates
import android.text.format.DateUtils
import androidx.compose.foundation.combinedClickable import androidx.compose.foundation.combinedClickable
import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Column
@ -27,7 +26,6 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.hapticfeedback.HapticFeedbackType import androidx.compose.ui.hapticfeedback.HapticFeedbackType
import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.LocalHapticFeedback 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.ChapterDownloadIndicator
import eu.kanade.presentation.manga.components.DotSeparatorText import eu.kanade.presentation.manga.components.DotSeparatorText
import eu.kanade.presentation.manga.components.MangaCover import eu.kanade.presentation.manga.components.MangaCover
import eu.kanade.presentation.util.relativeTimeSpanString
import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.download.model.Download import eu.kanade.tachiyomi.data.download.model.Download
import eu.kanade.tachiyomi.ui.updates.UpdatesItem 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.ReadItemAlpha
import tachiyomi.presentation.core.components.material.padding import tachiyomi.presentation.core.components.material.padding
import tachiyomi.presentation.core.util.selectedBackground import tachiyomi.presentation.core.util.selectedBackground
import java.util.Date
import kotlin.time.Duration.Companion.minutes
internal fun LazyListScope.updatesLastUpdatedItem( internal fun LazyListScope.updatesLastUpdatedItem(
lastUpdated: Long, lastUpdated: Long,
) { ) {
item(key = "updates-lastUpdated") { 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( Box(
modifier = Modifier modifier = Modifier
.animateItemPlacement() .animateItemPlacement()
.padding(horizontal = MaterialTheme.padding.medium, vertical = MaterialTheme.padding.small), .padding(horizontal = MaterialTheme.padding.medium, vertical = MaterialTheme.padding.small),
) { ) {
Text( Text(
text = if (time.isNullOrEmpty()) { text = stringResource(R.string.updates_last_update_info, relativeTimeSpanString(lastUpdated)),
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)
},
fontStyle = FontStyle.Italic, fontStyle = FontStyle.Italic,
) )
} }

View File

@ -1,8 +1,14 @@
package eu.kanade.presentation.util package eu.kanade.presentation.util
import android.content.Context 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 eu.kanade.tachiyomi.R
import java.util.Date
import kotlin.time.Duration import kotlin.time.Duration
import kotlin.time.Duration.Companion.minutes
fun Duration.toDurationString(context: Context, fallback: String): String { fun Duration.toDurationString(context: Context, fallback: String): String {
return toComponents { days, hours, minutes, seconds, _ -> return toComponents { days, hours, minutes, seconds, _ ->
@ -14,3 +20,14 @@ fun Duration.toDurationString(context: Context, fallback: String): String {
}.joinToString(" ").ifBlank { fallback } }.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()
}
}

View File

@ -782,6 +782,7 @@
<string name="cant_open_last_read_chapter">Unable to open last read chapter</string> <string name="cant_open_last_read_chapter">Unable to open last read chapter</string>
<string name="updates_last_update_info">Library last updated: %s</string> <string name="updates_last_update_info">Library last updated: %s</string>
<string name="updates_last_update_info_just_now">Just now</string> <string name="updates_last_update_info_just_now">Just now</string>
<string name="relative_time_span_never">Never</string>
<!-- History --> <!-- History -->
<string name="recent_manga_time">Ch. %1$s - %2$s</string> <string name="recent_manga_time">Ch. %1$s - %2$s</string>