mirror of
				https://github.com/mihonapp/mihon.git
				synced 2025-10-31 14:27:57 +01:00 
			
		
		
		
	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:
		| @@ -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" | ||||
|   | ||||
| @@ -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()) | ||||
|   | ||||
| @@ -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<CharSequence>() | ||||
|  | ||||
|         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 { | ||||
|   | ||||
| @@ -32,6 +32,7 @@ class ChaptersAdapter( | ||||
|             .apply { decimalSeparator = '.' } | ||||
|     ) | ||||
|  | ||||
|     val relativeTime: Int = preferences.relativeTime().get() | ||||
|     val dateFormat: DateFormat = preferences.dateFormat() | ||||
|  | ||||
|     override fun updateDataSet(items: List<ChapterItem>?) { | ||||
|   | ||||
| @@ -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 | ||||
|   | ||||
| @@ -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) | ||||
|     } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user