From b08d604d2a8ae09e623a4375a75cbc844d26a0fa Mon Sep 17 00:00:00 2001 From: arkon Date: Wed, 20 Sep 2023 22:49:15 -0400 Subject: [PATCH] Consistently use absolute date strings everywhere Closes #9781 --- .../components/RelativeDateHeader.kt | 5 +- .../kanade/presentation/manga/MangaScreen.kt | 3 +- .../ui/updates/UpdatesScreenModel.kt | 3 +- .../tachiyomi/util/lang/DateExtensions.kt | 101 ------------------ i18n/src/main/res/values/strings.xml | 6 -- 5 files changed, 3 insertions(+), 115 deletions(-) diff --git a/app/src/main/java/eu/kanade/presentation/components/RelativeDateHeader.kt b/app/src/main/java/eu/kanade/presentation/components/RelativeDateHeader.kt index 89bb61b4f7..598bad83e5 100644 --- a/app/src/main/java/eu/kanade/presentation/components/RelativeDateHeader.kt +++ b/app/src/main/java/eu/kanade/presentation/components/RelativeDateHeader.kt @@ -3,8 +3,6 @@ package eu.kanade.presentation.components import androidx.compose.runtime.Composable import androidx.compose.runtime.remember import androidx.compose.ui.Modifier -import androidx.compose.ui.platform.LocalContext -import eu.kanade.tachiyomi.util.lang.toRelativeString import tachiyomi.presentation.core.components.ListGroupHeader import java.text.DateFormat import java.util.Date @@ -15,11 +13,10 @@ fun RelativeDateHeader( date: Date, dateFormat: DateFormat, ) { - val context = LocalContext.current ListGroupHeader( modifier = modifier, text = remember { - date.toRelativeString(context, dateFormat) + dateFormat.format(date) }, ) } diff --git a/app/src/main/java/eu/kanade/presentation/manga/MangaScreen.kt b/app/src/main/java/eu/kanade/presentation/manga/MangaScreen.kt index 740ece4a26..352e6aedb5 100644 --- a/app/src/main/java/eu/kanade/presentation/manga/MangaScreen.kt +++ b/app/src/main/java/eu/kanade/presentation/manga/MangaScreen.kt @@ -63,7 +63,6 @@ import eu.kanade.tachiyomi.data.download.model.Download import eu.kanade.tachiyomi.source.getNameForMangaInfo import eu.kanade.tachiyomi.ui.manga.ChapterItem import eu.kanade.tachiyomi.ui.manga.MangaScreenModel -import eu.kanade.tachiyomi.util.lang.toRelativeString import eu.kanade.tachiyomi.util.system.copyToClipboard import tachiyomi.domain.chapter.model.Chapter import tachiyomi.domain.chapter.service.missingChaptersCount @@ -740,7 +739,7 @@ private fun LazyListScope.sharedChapterItems( date = chapterItem.chapter.dateUpload .takeIf { it > 0L } ?.let { - Date(it).toRelativeString(context, dateFormat) + dateFormat.format(Date(it)) }, readProgress = chapterItem.chapter.lastPageRead .takeIf { !chapterItem.chapter.read && it > 0L } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/updates/UpdatesScreenModel.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/updates/UpdatesScreenModel.kt index 6459d6525c..6e0c1c4742 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/updates/UpdatesScreenModel.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/updates/UpdatesScreenModel.kt @@ -20,7 +20,6 @@ import eu.kanade.tachiyomi.data.download.DownloadManager import eu.kanade.tachiyomi.data.download.model.Download import eu.kanade.tachiyomi.data.library.LibraryUpdateJob import eu.kanade.tachiyomi.util.lang.toDateKey -import eu.kanade.tachiyomi.util.lang.toRelativeString import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.catch @@ -384,7 +383,7 @@ class UpdatesScreenModel( val afterDate = after?.item?.update?.dateFetch?.toDateKey() ?: Date(0) when { beforeDate.time != afterDate.time && afterDate.time != 0L -> { - val text = afterDate.toRelativeString(context, dateFormat) + val text = dateFormat.format(afterDate) UpdatesUiModel.Header(text) } // Return null to avoid adding a separator between two items. diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/lang/DateExtensions.kt b/app/src/main/java/eu/kanade/tachiyomi/util/lang/DateExtensions.kt index 9efcaaa9fb..88477e8d57 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/lang/DateExtensions.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/lang/DateExtensions.kt @@ -1,14 +1,11 @@ package eu.kanade.tachiyomi.util.lang -import android.content.Context -import eu.kanade.tachiyomi.R import java.text.DateFormat import java.time.Instant import java.time.LocalDateTime import java.time.ZoneId import java.util.Calendar import java.util.Date -import java.util.TimeZone fun Date.toDateTimestampString(dateFormatter: DateFormat): String { val date = dateFormatter.format(this) @@ -45,101 +42,3 @@ fun Long.toDateKey(): Date { cal[Calendar.MILLISECOND] = 0 return cal.time } - -/** - * Convert epoch long to Calendar instance - * - * @return Calendar instance at supplied epoch time. Null if epoch was 0. - */ -fun Long.toCalendar(): Calendar? { - if (this == 0L) { - return null - } - val cal = Calendar.getInstance() - cal.timeInMillis = this - return cal -} - -/** - * Convert local time millisecond value to Calendar instance in UTC - * - * @return UTC Calendar instance at supplied time. Null if time is 0. - */ -fun Long.toUtcCalendar(): Calendar? { - if (this == 0L) { - return null - } - val rawCalendar = Calendar.getInstance().apply { - timeInMillis = this@toUtcCalendar - } - return Calendar.getInstance(TimeZone.getTimeZone("UTC")).apply { - clear() - set( - rawCalendar.get(Calendar.YEAR), - rawCalendar.get(Calendar.MONTH), - rawCalendar.get(Calendar.DAY_OF_MONTH), - rawCalendar.get(Calendar.HOUR_OF_DAY), - rawCalendar.get(Calendar.MINUTE), - rawCalendar.get(Calendar.SECOND), - ) - } -} - -/** - * Convert UTC time millisecond to Calendar instance in local time zone - * - * @return local Calendar instance at supplied UTC time. Null if time is 0. - */ -fun Long.toLocalCalendar(): Calendar? { - if (this == 0L) { - return null - } - val rawCalendar = Calendar.getInstance(TimeZone.getTimeZone("UTC")).apply { - timeInMillis = this@toLocalCalendar - } - return Calendar.getInstance().apply { - clear() - set( - rawCalendar.get(Calendar.YEAR), - rawCalendar.get(Calendar.MONTH), - rawCalendar.get(Calendar.DAY_OF_MONTH), - rawCalendar.get(Calendar.HOUR_OF_DAY), - rawCalendar.get(Calendar.MINUTE), - rawCalendar.get(Calendar.SECOND), - ) - } -} - -private const val MILLISECONDS_IN_DAY = 86_400_000L - -fun Date.toRelativeString( - context: Context, - dateFormat: DateFormat = DateFormat.getDateInstance(DateFormat.SHORT), -): String { - val now = Date() - val difference = now.timeWithOffset.floorNearest(MILLISECONDS_IN_DAY) - this.timeWithOffset.floorNearest(MILLISECONDS_IN_DAY) - val days = difference.floorDiv(MILLISECONDS_IN_DAY).toInt() - return when { - difference < 0 -> dateFormat.format(this) - difference < MILLISECONDS_IN_DAY -> context.getString(R.string.relative_time_today) - difference < MILLISECONDS_IN_DAY.times(7) -> context.resources.getQuantityString( - R.plurals.relative_time, - days, - days, - ) - else -> dateFormat.format(this) - } -} - -private val Date.timeWithOffset: Long - get() { - return Calendar.getInstance().run { - time = this@timeWithOffset - val dstOffset = get(Calendar.DST_OFFSET) - this@timeWithOffset.time + timeZone.rawOffset + dstOffset - } - } - -fun Long.floorNearest(to: Long): Long { - return this.floorDiv(to) * to -} diff --git a/i18n/src/main/res/values/strings.xml b/i18n/src/main/res/values/strings.xml index 35e9cb2752..67f33acca5 100644 --- a/i18n/src/main/res/values/strings.xml +++ b/i18n/src/main/res/values/strings.xml @@ -223,12 +223,6 @@ Show in sources and extensions lists This does not prevent unofficial or potentially incorrectly flagged extensions from surfacing NSFW (18+) content within the app. - Today - - Yesterday - %1$d days ago - - Display Grid size