From 9090f877564547e7b18ef97e32b6c76e16f5b280 Mon Sep 17 00:00:00 2001 From: Jays2Kings Date: Mon, 22 Mar 2021 01:34:55 -0400 Subject: [PATCH] Suggested Start/Completed Date for Trackers based on reading history --- .../tachiyomi/ui/library/LibraryPresenter.kt | 2 +- .../ui/manga/MangaDetailsPresenter.kt | 10 +++ .../manga/track/SetTrackReadingDatesDialog.kt | 64 ++++++++++++++++++- .../ui/manga/track/TrackingBottomSheet.kt | 16 ++++- .../util/view/MaterialDialogExtensions.kt | 12 ++++ app/src/main/res/values/strings.xml | 1 + 6 files changed, 101 insertions(+), 4 deletions(-) create mode 100644 app/src/main/java/eu/kanade/tachiyomi/util/view/MaterialDialogExtensions.kt diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt index 3c49103380..39d77cc429 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt @@ -929,7 +929,7 @@ class LibraryPresenter( libraryManga.forEach { manga -> if (manga.date_added == 0L) { val chapters = db.getChapters(manga).executeAsBlocking() - manga.date_added = chapters.minBy { it.date_fetch }?.date_fetch ?: 0L + manga.date_added = chapters.minByOrNull { it.date_fetch }?.date_fetch ?: 0L db.insertManga(manga).executeAsBlocking() } db.resetMangaInfo(manga).executeAsBlocking() diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaDetailsPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaDetailsPresenter.kt index 6d09aab45c..730879bfa0 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaDetailsPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaDetailsPresenter.kt @@ -30,6 +30,7 @@ import eu.kanade.tachiyomi.source.Source import eu.kanade.tachiyomi.source.model.toSChapter import eu.kanade.tachiyomi.source.model.toSManga import eu.kanade.tachiyomi.ui.manga.chapter.ChapterItem +import eu.kanade.tachiyomi.ui.manga.track.SetTrackReadingDatesDialog import eu.kanade.tachiyomi.ui.manga.track.TrackItem import eu.kanade.tachiyomi.ui.security.SecureActivityDelegate import eu.kanade.tachiyomi.util.chapter.ChapterFilter @@ -895,6 +896,15 @@ class MangaDetailsPresenter( updateRemote(track, item.service) } + fun getSuggestedDate(readingDate: SetTrackReadingDatesDialog.ReadingDate): Long? { + val chapters = db.getHistoryByMangaId(manga.id ?: 0L).executeAsBlocking() + val date = when (readingDate) { + SetTrackReadingDatesDialog.ReadingDate.Start -> chapters.minByOrNull { it.last_read }?.last_read + SetTrackReadingDatesDialog.ReadingDate.Finish -> chapters.maxByOrNull { it.last_read }?.last_read + } ?: return null + return if (date <= 0L) null else date + } + companion object { const val MULTIPLE_VOLUMES = 1 const val TENS_OF_CHAPTERS = 2 diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/track/SetTrackReadingDatesDialog.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/track/SetTrackReadingDatesDialog.kt index 21539ba5fc..63c01a552d 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/track/SetTrackReadingDatesDialog.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/track/SetTrackReadingDatesDialog.kt @@ -3,15 +3,22 @@ package eu.kanade.tachiyomi.ui.manga.track import android.app.Dialog import android.os.Bundle import androidx.core.os.bundleOf +import com.afollestad.date.dayOfMonth +import com.afollestad.date.month +import com.afollestad.date.year import com.afollestad.materialdialogs.MaterialDialog import com.afollestad.materialdialogs.datetime.datePicker import com.bluelinelabs.conductor.Controller import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.database.models.Track +import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.data.track.TrackManager import eu.kanade.tachiyomi.ui.base.controller.DialogController +import eu.kanade.tachiyomi.util.view.setDate import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get +import uy.kohesive.injekt.injectLazy +import java.text.DateFormat import java.util.Calendar class SetTrackReadingDatesDialog : DialogController @@ -20,16 +27,23 @@ class SetTrackReadingDatesDialog : DialogController private val item: TrackItem private val dateToUpdate: ReadingDate + private val suggestedDate: Long? + + private val preferences: PreferencesHelper by injectLazy() + private val dateFormat: DateFormat by lazy { + preferences.dateFormat() + } private lateinit var listener: Listener - constructor(target: T, listener: Listener, dateToUpdate: ReadingDate, item: TrackItem) : super( + constructor(target: T, listener: Listener, dateToUpdate: ReadingDate, item: TrackItem, suggestedDate: Long?) : super( bundleOf(KEY_ITEM_TRACK to item.track) ) { targetController = target this.listener = listener this.item = item this.dateToUpdate = dateToUpdate + this.suggestedDate = suggestedDate } @Suppress("unused") @@ -38,6 +52,7 @@ class SetTrackReadingDatesDialog : DialogController val service = Injekt.get().getService(track.sync_id)!! item = TrackItem(track, service) dateToUpdate = ReadingDate.Start + suggestedDate = null } override fun onCreateDialog(savedViewState: Bundle?): Dialog { @@ -53,12 +68,59 @@ class SetTrackReadingDatesDialog : DialogController } .neutralButton(R.string.remove) { listener.setReadingDate(item, dateToUpdate, 0L) + }.apply { + getSuggestedDate()?.let { + message(text = it, + applySettings = { + messageTextView.setOnClickListener { + this@apply.setDate(suggestedDate ?: 0L) + } + }) + } } } + private fun getSuggestedDate(): String? { + item.track ?: return null + val date = when (dateToUpdate) { + ReadingDate.Start -> item.track.started_reading_date + ReadingDate.Finish -> item.track.finished_reading_date + } + if (date != 0L) { + if (suggestedDate != null) { + val calendar = Calendar.getInstance() + calendar.timeInMillis = date + val suggestedCalendar = Calendar.getInstance() + suggestedCalendar.timeInMillis = suggestedDate + return if (date > suggestedDate && + (suggestedCalendar.year != calendar.year || + suggestedCalendar.month != calendar.month || + suggestedCalendar.dayOfMonth != calendar.dayOfMonth) + ) { + activity?.getString( + R.string.suggested_date_, + dateFormat.format(suggestedDate) + ) + } else { + null + } + } + } + suggestedDate?.let { + return activity?.getString( + R.string.suggested_date_, + dateFormat.format(suggestedDate) + ) + } + return null + } + private fun getCurrentDate(): Calendar { // Today if no date is set, otherwise the already set date return Calendar.getInstance().apply { + suggestedDate?.let { + timeInMillis = it + } item.track?.let { val date = when (dateToUpdate) { ReadingDate.Start -> it.started_reading_date diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/track/TrackingBottomSheet.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/track/TrackingBottomSheet.kt index 04ba0e3cbe..273d5facee 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/track/TrackingBottomSheet.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/track/TrackingBottomSheet.kt @@ -187,14 +187,26 @@ class TrackingBottomSheet(private val controller: MangaDetailsController) : val item = adapter?.getItem(position) ?: return if (item.track == null) return - SetTrackReadingDatesDialog(controller, this, SetTrackReadingDatesDialog.ReadingDate.Start, item).showDialog(controller.router) + val suggestedDate = presenter.getSuggestedDate(SetTrackReadingDatesDialog.ReadingDate.Start) + SetTrackReadingDatesDialog(controller, + this, + SetTrackReadingDatesDialog.ReadingDate.Start, + item, + suggestedDate) + .showDialog(controller.router) } override fun onFinishDateClick(position: Int) { val item = adapter?.getItem(position) ?: return if (item.track == null) return - SetTrackReadingDatesDialog(controller, this, SetTrackReadingDatesDialog.ReadingDate.Finish, item).showDialog(controller.router) + val suggestedDate = presenter.getSuggestedDate(SetTrackReadingDatesDialog.ReadingDate.Finish) + SetTrackReadingDatesDialog(controller, + this, + SetTrackReadingDatesDialog.ReadingDate.Finish, + item, + suggestedDate) + .showDialog(controller.router) } override fun setStatus(item: TrackItem, selection: Int) { diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/view/MaterialDialogExtensions.kt b/app/src/main/java/eu/kanade/tachiyomi/util/view/MaterialDialogExtensions.kt new file mode 100644 index 0000000000..27c289b644 --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/util/view/MaterialDialogExtensions.kt @@ -0,0 +1,12 @@ +package eu.kanade.tachiyomi.util.view + +import com.afollestad.date.DatePicker +import com.afollestad.materialdialogs.MaterialDialog +import java.util.Calendar + +fun MaterialDialog.setDate(date: Long) { + val datePicker = findViewById(com.afollestad.materialdialogs.datetime.R.id.datetimeDatePicker) ?: return + val calendar = Calendar.getInstance() + calendar.timeInMillis = date + datePicker.setDate(calendar) +} diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index b3bfe6cf70..51561191e1 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -436,6 +436,7 @@ Started reading date Finished reading date Please login to MAL again + Suggested date: %1$s Select sources