From ae1a76da2bf99d3fb06cede954f188c2169637fa Mon Sep 17 00:00:00 2001 From: arkon Date: Sun, 15 Aug 2021 17:42:06 -0400 Subject: [PATCH] Use toRelativeString in Updates and History headings --- .../tachiyomi/ui/recent/DateSectionItem.kt | 15 ++++++++------ .../ui/recent/history/HistoryPresenter.kt | 13 +++++++----- .../ui/recent/updates/UpdatesPresenter.kt | 20 +++++++++++-------- 3 files changed, 29 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/recent/DateSectionItem.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/recent/DateSectionItem.kt index 438ad285e..2a05ce607 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/recent/DateSectionItem.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/recent/DateSectionItem.kt @@ -1,6 +1,5 @@ package eu.kanade.tachiyomi.ui.recent -import android.text.format.DateUtils import android.view.View import androidx.recyclerview.widget.RecyclerView import eu.davidea.flexibleadapter.FlexibleAdapter @@ -9,9 +8,15 @@ import eu.davidea.flexibleadapter.items.IFlexible import eu.davidea.viewholders.FlexibleViewHolder import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.databinding.SectionHeaderItemBinding +import eu.kanade.tachiyomi.util.lang.toRelativeString +import java.text.DateFormat import java.util.Date -class DateSectionItem(val date: Date) : AbstractHeaderItem() { +class DateSectionItem( + private val date: Date, + private val range: Int, + private val dateFormat: DateFormat, +) : AbstractHeaderItem() { override fun getLayoutRes(): Int { return R.layout.section_header_item @@ -37,14 +42,12 @@ class DateSectionItem(val date: Date) : AbstractHeaderItem) : FlexibleViewHolder(view, adapter, true) { + inner class DateSectionItemHolder(private val view: View, adapter: FlexibleAdapter<*>) : FlexibleViewHolder(view, adapter, true) { private val binding = SectionHeaderItemBinding.bind(view) - private val now = Date().time - fun bind(item: DateSectionItem) { - binding.title.text = DateUtils.getRelativeTimeSpanString(item.date.time, now, DateUtils.DAY_IN_MILLIS) + binding.title.text = item.date.toRelativeString(view.context, range, dateFormat) } } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/recent/history/HistoryPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/recent/history/HistoryPresenter.kt index 0193d3df6..51025c1a4 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/recent/history/HistoryPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/recent/history/HistoryPresenter.kt @@ -6,6 +6,7 @@ import eu.kanade.tachiyomi.data.database.models.Chapter import eu.kanade.tachiyomi.data.database.models.History import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.data.database.models.MangaChapterHistory +import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.ui.base.presenter.BasePresenter import eu.kanade.tachiyomi.ui.recent.DateSectionItem import eu.kanade.tachiyomi.util.lang.toDateKey @@ -13,6 +14,7 @@ import rx.Observable import rx.Subscription import rx.android.schedulers.AndroidSchedulers import uy.kohesive.injekt.injectLazy +import java.text.DateFormat import java.util.Calendar import java.util.Date import java.util.TreeMap @@ -24,10 +26,11 @@ import java.util.TreeMap */ class HistoryPresenter : BasePresenter() { - /** - * Used to connect to database - */ - val db: DatabaseHelper by injectLazy() + private val db: DatabaseHelper by injectLazy() + private val preferences: PreferencesHelper by injectLazy() + + private val relativeTime: Int = preferences.relativeTime().get() + private val dateFormat: DateFormat = preferences.dateFormat() private var recentMangaSubscription: Subscription? = null @@ -65,7 +68,7 @@ class HistoryPresenter : BasePresenter() { val byDay = recents .groupByTo(map, { it.history.last_read.toDateKey() }) byDay.flatMap { entry -> - val dateItem = DateSectionItem(entry.key) + val dateItem = DateSectionItem(entry.key, relativeTime, dateFormat) entry.value.map { HistoryItem(it, dateItem) } } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/recent/updates/UpdatesPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/recent/updates/UpdatesPresenter.kt index dd2b501f0..c03be1417 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/recent/updates/UpdatesPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/recent/updates/UpdatesPresenter.kt @@ -15,18 +15,22 @@ import rx.Observable import rx.android.schedulers.AndroidSchedulers import rx.schedulers.Schedulers import timber.log.Timber -import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get +import uy.kohesive.injekt.injectLazy +import java.text.DateFormat import java.util.Calendar import java.util.Date import java.util.TreeMap -class UpdatesPresenter( - val preferences: PreferencesHelper = Injekt.get(), - private val db: DatabaseHelper = Injekt.get(), - private val downloadManager: DownloadManager = Injekt.get(), - private val sourceManager: SourceManager = Injekt.get() -) : BasePresenter() { +class UpdatesPresenter : BasePresenter() { + + val preferences: PreferencesHelper by injectLazy() + private val db: DatabaseHelper by injectLazy() + private val downloadManager: DownloadManager by injectLazy() + private val sourceManager: SourceManager by injectLazy() + + private val relativeTime: Int = preferences.relativeTime().get() + private val dateFormat: DateFormat = preferences.dateFormat() /** * List containing chapter and manga information @@ -82,7 +86,7 @@ class UpdatesPresenter( val byDay = mangaChapters .groupByTo(map, { it.chapter.date_fetch.toDateKey() }) byDay.flatMap { entry -> - val dateItem = DateSectionItem(entry.key) + val dateItem = DateSectionItem(entry.key, relativeTime, dateFormat) entry.value .sortedWith(compareBy({ it.chapter.date_fetch }, { it.chapter.chapter_number })).asReversed() .map { UpdatesItem(it.chapter, it.manga, dateItem) }