From 57a5862840eafc0848c5226376f2f22c48693e5a Mon Sep 17 00:00:00 2001 From: Andreas Date: Sun, 15 Aug 2021 23:07:48 +0200 Subject: [PATCH] Use relative time in ChapterHolder (#5719) * Use relative time in ChapterHolder Similar to how J2K does it * Use custom implementation for relative time * Changes based on review comments --- .../data/preference/PreferenceKeys.kt | 1 + .../data/preference/PreferencesHelper.kt | 2 ++ .../ui/manga/chapter/ChapterHolder.kt | 3 ++- .../ui/manga/chapter/ChaptersAdapter.kt | 1 + .../ui/setting/SettingsGeneralController.kt | 17 ++++++++++++++ .../tachiyomi/util/lang/DateExtensions.kt | 23 +++++++++++++++++++ app/src/main/res/values/strings.xml | 10 ++++++++ 7 files changed, 56 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt index a7515c2934..3770a3f0dd 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt @@ -182,6 +182,7 @@ object PreferenceKeys { const val libraryDisplayMode = "pref_display_mode_library" + const val relativeTime: String = "relative_time" const val dateFormat = "app_date_format" const val defaultCategory = "default_category" diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt index 06c6e8dd1e..ee2e29d7bc 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt @@ -208,6 +208,8 @@ class PreferencesHelper(val context: Context) { fun backupsDirectory() = flowPrefs.getString(Keys.backupDirectory, defaultBackupDir.toString()) + fun relativeTime() = flowPrefs.getInt(Keys.relativeTime, 7) + fun dateFormat(format: String = flowPrefs.getString(Keys.dateFormat, "").get()): DateFormat = when (format) { "" -> DateFormat.getDateInstance(DateFormat.SHORT) else -> SimpleDateFormat(format, Locale.getDefault()) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChapterHolder.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChapterHolder.kt index 3e9588d9a0..9275a11ac3 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChapterHolder.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChapterHolder.kt @@ -10,6 +10,7 @@ import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.databinding.ChaptersItemBinding import eu.kanade.tachiyomi.source.LocalSource import eu.kanade.tachiyomi.ui.manga.chapter.base.BaseChapterHolder +import eu.kanade.tachiyomi.util.lang.toRelativeString import java.util.Date class ChapterHolder( @@ -56,7 +57,7 @@ class ChapterHolder( val descriptions = mutableListOf() if (chapter.date_upload > 0) { - descriptions.add(adapter.dateFormat.format(Date(chapter.date_upload))) + descriptions.add(Date(chapter.date_upload).toRelativeString(itemView.context, adapter.relativeTime, adapter.dateFormat)) } if (!chapter.read && chapter.last_page_read > 0) { val lastPageRead = buildSpannedString { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersAdapter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersAdapter.kt index 2117290292..d96ff53254 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersAdapter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersAdapter.kt @@ -32,6 +32,7 @@ class ChaptersAdapter( .apply { decimalSeparator = '.' } ) + val relativeTime: Int = preferences.relativeTime().get() val dateFormat: DateFormat = preferences.dateFormat() override fun updateDataSet(items: List?) { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralController.kt index 1747223b24..47e8cead67 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralController.kt @@ -21,6 +21,7 @@ import kotlinx.coroutines.flow.launchIn import java.util.Date import eu.kanade.tachiyomi.data.preference.PreferenceKeys as Keys import eu.kanade.tachiyomi.data.preference.PreferenceValues as Values +import androidx.preference.Preference class SettingsGeneralController : SettingsController() { @@ -78,6 +79,22 @@ class SettingsGeneralController : SettingsController() { } } } + intListPreference { + key = Keys.relativeTime + titleRes = R.string.pref_relative_format + val values = arrayOf("0", "2", "7") + entryValues = values + entries = values.map { + when (it) { + "0" -> context.getString(R.string.off) + "2" -> context.getString(R.string.pref_relative_time_short) + else -> context.getString(R.string.pref_relative_time_long) + } + }.toTypedArray() + defaultValue = "7" + summary = "%s" + } + listPreference { key = Keys.dateFormat titleRes = R.string.pref_date_format 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 8f8682ede9..f4614fcb94 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,5 +1,7 @@ package eu.kanade.tachiyomi.util.lang +import android.content.Context +import eu.kanade.tachiyomi.R import java.text.DateFormat import java.util.Calendar import java.util.Date @@ -94,3 +96,24 @@ fun Long.toLocalCalendar(): Calendar? { ) } } + +private const val MILLISECONDS_IN_DAY = 86_400_000.0 + +fun Date.toRelativeString( + context: Context, + range: Int = 7, + dateFormat: DateFormat = DateFormat.getDateInstance(DateFormat.SHORT) +): String { + val now = Date() + val difference = now.time - this.time + val days = difference / MILLISECONDS_IN_DAY + return when { + difference < 0 -> context.getString(R.string.recently) + difference < MILLISECONDS_IN_DAY.times(range) -> context.resources.getQuantityString( + R.plurals.relative_time, + days.toInt(), + days + ) + else -> dateFormat.format(this) + } +} diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 2c67a26984..3b3f8e86f5 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -186,6 +186,16 @@ Label in extensions list This does not prevent unofficial or potentially incorrectly flagged extensions from surfacing NSFW (18+) content within the app. + Recently + + Today + Yesterday + %1$.0f days ago + + Relative timestamps + Short (Today, Yesterday) + Long (Short+, n days ago) + Display Items per row