Fix incorrect saved tracker dates (#5581)

This commit is contained in:
Ivan Iskandar 2021-07-20 04:45:46 +07:00 committed by GitHub
parent 116fec208b
commit be28e0b559
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 7 deletions

View file

@ -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)
}

View file

@ -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)
)
}
}