From aae6820fdcadd78d09e3e640e30e98d212fa31d4 Mon Sep 17 00:00:00 2001 From: Hawk of the Death Date: Fri, 24 Apr 2020 00:28:41 +0200 Subject: [PATCH] Replace Track Reading Dates date picker with MaterialDialog's one (#2935) * Replaced datepicker with MaterialDialog's one * Removed max date due to library bug (afollestad/material-dialogs#1973) * Removed old date picker view. --- app/build.gradle | 1 + .../manga/track/SetTrackReadingDatesDialog.kt | 78 ++++--------------- app/src/main/res/layout/track_date_dialog.xml | 37 --------- 3 files changed, 17 insertions(+), 99 deletions(-) delete mode 100644 app/src/main/res/layout/track_date_dialog.xml diff --git a/app/build.gradle b/app/build.gradle index 203c1aa335..89c1ba5a90 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -228,6 +228,7 @@ dependencies { final material_dialogs_version = '3.3.0' implementation "com.afollestad.material-dialogs:core:$material_dialogs_version" implementation "com.afollestad.material-dialogs:input:$material_dialogs_version" + implementation "com.afollestad.material-dialogs:datetime:$material_dialogs_version" // Conductor implementation 'com.bluelinelabs:conductor:2.1.5' 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 988d29868a..4f458b009d 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 @@ -2,17 +2,13 @@ package eu.kanade.tachiyomi.ui.manga.track import android.app.Dialog import android.os.Bundle -import android.widget.NumberPicker import com.afollestad.materialdialogs.MaterialDialog -import com.afollestad.materialdialogs.customview.customView -import com.afollestad.materialdialogs.customview.getCustomView +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.track.TrackManager import eu.kanade.tachiyomi.ui.base.controller.DialogController -import eu.kanade.tachiyomi.util.system.toast -import java.text.DateFormatSymbols import java.util.Calendar import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get @@ -41,74 +37,32 @@ class SetTrackReadingDatesDialog : DialogController } override fun onCreateDialog(savedViewState: Bundle?): Dialog { - val item = item + val listener = (targetController as? Listener) - val dialog = MaterialDialog(activity!!) + return MaterialDialog(activity!!) .title(when (dateToUpdate) { ReadingDate.Start -> R.string.track_started_reading_date ReadingDate.Finish -> R.string.track_finished_reading_date }) - .customView(R.layout.track_date_dialog, dialogWrapContent = false) - .positiveButton(android.R.string.ok) { dialog -> - onDialogConfirm(dialog) + .datePicker(currentDate = getCurrentDate()) { _, date -> + listener?.setReadingDate(item, dateToUpdate, date.timeInMillis) } - .negativeButton(android.R.string.cancel) { dialog -> - dialog.dismiss() - } - .neutralButton(R.string.action_remove) { dialog -> - val listener = (targetController as? Listener) + .neutralButton(R.string.action_remove) { listener?.setReadingDate(item, dateToUpdate, 0L) - dialog.dismiss() } - .noAutoDismiss() - - onDialogCreated(dialog) - - return dialog } - private fun onDialogCreated(dialog: MaterialDialog) { - val view = dialog.getCustomView() - - val dayPicker: NumberPicker = view.findViewById(R.id.day_picker) - val monthPicker: NumberPicker = view.findViewById(R.id.month_picker) - val yearPicker: NumberPicker = view.findViewById(R.id.year_picker) - - val monthNames: Array = DateFormatSymbols().months - monthPicker.displayedValues = monthNames - - val calendar = Calendar.getInstance() - item.track?.let { - val date = when (dateToUpdate) { - ReadingDate.Start -> it.started_reading_date - ReadingDate.Finish -> it.finished_reading_date + private fun getCurrentDate(): Calendar { + // Today if no date is set, otherwise the already set date + return Calendar.getInstance().apply { + item.track?.let { + val date = when (dateToUpdate) { + ReadingDate.Start -> it.started_reading_date + ReadingDate.Finish -> it.finished_reading_date + } + if (date != 0L) + timeInMillis = date } - if (date != 0L) - calendar.timeInMillis = date - } - dayPicker.value = calendar[Calendar.DAY_OF_MONTH] - monthPicker.value = calendar[Calendar.MONTH] - yearPicker.maxValue = calendar[Calendar.YEAR] - yearPicker.value = calendar[Calendar.YEAR] - } - - private fun onDialogConfirm(dialog: MaterialDialog) { - val view = dialog.getCustomView() - - val dayPicker: NumberPicker = view.findViewById(R.id.day_picker) - val monthPicker: NumberPicker = view.findViewById(R.id.month_picker) - val yearPicker: NumberPicker = view.findViewById(R.id.year_picker) - - try { - val calendar = Calendar.getInstance().apply { isLenient = false } - calendar.set(yearPicker.value, monthPicker.value, dayPicker.value) - calendar.time = calendar.time // Throws if invalid - - val listener = (targetController as? Listener) - listener?.setReadingDate(item, dateToUpdate, calendar.timeInMillis) - dialog.dismiss() - } catch (e: Exception) { - activity?.toast(R.string.error_invalid_date_supplied) } } diff --git a/app/src/main/res/layout/track_date_dialog.xml b/app/src/main/res/layout/track_date_dialog.xml deleted file mode 100644 index 6ff1195417..0000000000 --- a/app/src/main/res/layout/track_date_dialog.xml +++ /dev/null @@ -1,37 +0,0 @@ - - - - - - - - - -