From be28e0b559103960ba2515ecbe57f921a678e918 Mon Sep 17 00:00:00 2001 From: Ivan Iskandar <12537387+ivaniskandar@users.noreply.github.com> Date: Tue, 20 Jul 2021 04:45:46 +0700 Subject: [PATCH] Fix incorrect saved tracker dates (#5581) --- .../tachiyomi/ui/manga/track/TrackSheet.kt | 17 +++++++---- .../tachiyomi/util/lang/DateExtensions.kt | 29 +++++++++++++++++-- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/track/TrackSheet.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/track/TrackSheet.kt index b64086ab3..2c6e91a24 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/track/TrackSheet.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/track/TrackSheet.kt @@ -18,6 +18,7 @@ import eu.kanade.tachiyomi.source.SourceManager import eu.kanade.tachiyomi.ui.base.controller.openInBrowser import eu.kanade.tachiyomi.ui.manga.MangaController import eu.kanade.tachiyomi.util.lang.launchIO +import eu.kanade.tachiyomi.util.lang.toLocalCalendar import eu.kanade.tachiyomi.util.lang.toUtcCalendar import eu.kanade.tachiyomi.util.lang.withUIContext import eu.kanade.tachiyomi.util.system.copyToClipboard @@ -154,8 +155,11 @@ class TrackSheet( .setSelection(selection) .setCalendarConstraints(constraints) .build() - picker.addOnPositiveButtonClickListener { - controller.presenter.setTrackerStartDate(item, it) + picker.addOnPositiveButtonClickListener { utcMillis -> + val result = utcMillis.toLocalCalendar()?.timeInMillis + if (result != null) { + controller.presenter.setTrackerStartDate(item, result) + } } picker.show(fragmentManager, null) } @@ -171,7 +175,7 @@ class TrackSheet( val constraints = CalendarConstraints.Builder().apply { val startMillis = item.track.started_reading_date.toUtcCalendar()?.timeInMillis if (startMillis != null) { - setValidator(DateValidatorPointForward.from(item.track.started_reading_date)) + setValidator(DateValidatorPointForward.from(startMillis)) } }.build() @@ -180,8 +184,11 @@ class TrackSheet( .setSelection(selection) .setCalendarConstraints(constraints) .build() - picker.addOnPositiveButtonClickListener { - controller.presenter.setTrackerFinishDate(item, it) + picker.addOnPositiveButtonClickListener { utcMillis -> + val result = utcMillis.toLocalCalendar()?.timeInMillis + if (result != null) { + controller.presenter.setTrackerFinishDate(item, result) + } } picker.show(fragmentManager, null) } 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 310b698d0..8f8682ede 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 @@ -46,9 +46,9 @@ fun Long.toCalendar(): Calendar? { } /** - * Convert epoch long to Calendar instance in UTC + * Convert local time millisecond value to Calendar instance in UTC * - * @return UTC Calendar instance at supplied epoch time. Null if epoch was 0. + * @return UTC Calendar instance at supplied time. Null if time is 0. */ fun Long.toUtcCalendar(): Calendar? { if (this == 0L) { @@ -69,3 +69,28 @@ fun Long.toUtcCalendar(): Calendar? { ) } } + +/** + * Convert UTC time millisecond to Calendar instance in local time zone + * + * @return local Calendar instance at supplied UTC time. Null if time is 0. + */ +fun Long.toLocalCalendar(): Calendar? { + if (this == 0L) { + return null + } + val rawCalendar = Calendar.getInstance(TimeZone.getTimeZone("UTC")).apply { + timeInMillis = this@toLocalCalendar + } + return Calendar.getInstance().apply { + clear() + set( + rawCalendar.get(Calendar.YEAR), + rawCalendar.get(Calendar.MONTH), + rawCalendar.get(Calendar.DAY_OF_MONTH), + rawCalendar.get(Calendar.HOUR_OF_DAY), + rawCalendar.get(Calendar.MINUTE), + rawCalendar.get(Calendar.SECOND) + ) + } +}