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
This commit is contained in:
Andreas 2021-08-15 23:07:48 +02:00 committed by GitHub
parent 91fbccdbaa
commit 57a5862840
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 56 additions and 1 deletions

View File

@ -182,6 +182,7 @@ object PreferenceKeys {
const val libraryDisplayMode = "pref_display_mode_library" const val libraryDisplayMode = "pref_display_mode_library"
const val relativeTime: String = "relative_time"
const val dateFormat = "app_date_format" const val dateFormat = "app_date_format"
const val defaultCategory = "default_category" const val defaultCategory = "default_category"

View File

@ -208,6 +208,8 @@ class PreferencesHelper(val context: Context) {
fun backupsDirectory() = flowPrefs.getString(Keys.backupDirectory, defaultBackupDir.toString()) 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) { fun dateFormat(format: String = flowPrefs.getString(Keys.dateFormat, "").get()): DateFormat = when (format) {
"" -> DateFormat.getDateInstance(DateFormat.SHORT) "" -> DateFormat.getDateInstance(DateFormat.SHORT)
else -> SimpleDateFormat(format, Locale.getDefault()) else -> SimpleDateFormat(format, Locale.getDefault())

View File

@ -10,6 +10,7 @@ import eu.kanade.tachiyomi.data.database.models.Manga
import eu.kanade.tachiyomi.databinding.ChaptersItemBinding import eu.kanade.tachiyomi.databinding.ChaptersItemBinding
import eu.kanade.tachiyomi.source.LocalSource import eu.kanade.tachiyomi.source.LocalSource
import eu.kanade.tachiyomi.ui.manga.chapter.base.BaseChapterHolder import eu.kanade.tachiyomi.ui.manga.chapter.base.BaseChapterHolder
import eu.kanade.tachiyomi.util.lang.toRelativeString
import java.util.Date import java.util.Date
class ChapterHolder( class ChapterHolder(
@ -56,7 +57,7 @@ class ChapterHolder(
val descriptions = mutableListOf<CharSequence>() val descriptions = mutableListOf<CharSequence>()
if (chapter.date_upload > 0) { 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) { if (!chapter.read && chapter.last_page_read > 0) {
val lastPageRead = buildSpannedString { val lastPageRead = buildSpannedString {

View File

@ -32,6 +32,7 @@ class ChaptersAdapter(
.apply { decimalSeparator = '.' } .apply { decimalSeparator = '.' }
) )
val relativeTime: Int = preferences.relativeTime().get()
val dateFormat: DateFormat = preferences.dateFormat() val dateFormat: DateFormat = preferences.dateFormat()
override fun updateDataSet(items: List<ChapterItem>?) { override fun updateDataSet(items: List<ChapterItem>?) {

View File

@ -21,6 +21,7 @@ import kotlinx.coroutines.flow.launchIn
import java.util.Date import java.util.Date
import eu.kanade.tachiyomi.data.preference.PreferenceKeys as Keys import eu.kanade.tachiyomi.data.preference.PreferenceKeys as Keys
import eu.kanade.tachiyomi.data.preference.PreferenceValues as Values import eu.kanade.tachiyomi.data.preference.PreferenceValues as Values
import androidx.preference.Preference
class SettingsGeneralController : SettingsController() { 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 { listPreference {
key = Keys.dateFormat key = Keys.dateFormat
titleRes = R.string.pref_date_format titleRes = R.string.pref_date_format

View File

@ -1,5 +1,7 @@
package eu.kanade.tachiyomi.util.lang package eu.kanade.tachiyomi.util.lang
import android.content.Context
import eu.kanade.tachiyomi.R
import java.text.DateFormat import java.text.DateFormat
import java.util.Calendar import java.util.Calendar
import java.util.Date 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)
}
}

View File

@ -186,6 +186,16 @@
<string name="pref_label_nsfw_extension">Label in extensions list</string> <string name="pref_label_nsfw_extension">Label in extensions list</string>
<string name="parental_controls_info">This does not prevent unofficial or potentially incorrectly flagged extensions from surfacing NSFW (18+) content within the app.</string> <string name="parental_controls_info">This does not prevent unofficial or potentially incorrectly flagged extensions from surfacing NSFW (18+) content within the app.</string>
<string name="recently">Recently</string>
<plurals name="relative_time">
<item quantity="zero">Today</item>
<item quantity="one">Yesterday</item>
<item quantity="other">%1$.0f days ago</item>
</plurals>
<string name="pref_relative_format">Relative timestamps</string>
<string name="pref_relative_time_short">Short (Today, Yesterday)</string>
<string name="pref_relative_time_long">Long (Short+, n days ago)</string>
<!-- Library section --> <!-- Library section -->
<string name="pref_category_display">Display</string> <string name="pref_category_display">Display</string>
<string name="pref_library_columns">Items per row</string> <string name="pref_library_columns">Items per row</string>