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.
This commit is contained in:
Hawk of the Death 2020-04-24 00:28:41 +02:00 committed by GitHub
parent 2a4f35959b
commit aae6820fdc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 99 deletions

View File

@ -228,6 +228,7 @@ dependencies {
final material_dialogs_version = '3.3.0' final material_dialogs_version = '3.3.0'
implementation "com.afollestad.material-dialogs:core:$material_dialogs_version" implementation "com.afollestad.material-dialogs:core:$material_dialogs_version"
implementation "com.afollestad.material-dialogs:input:$material_dialogs_version" implementation "com.afollestad.material-dialogs:input:$material_dialogs_version"
implementation "com.afollestad.material-dialogs:datetime:$material_dialogs_version"
// Conductor // Conductor
implementation 'com.bluelinelabs:conductor:2.1.5' implementation 'com.bluelinelabs:conductor:2.1.5'

View File

@ -2,17 +2,13 @@ package eu.kanade.tachiyomi.ui.manga.track
import android.app.Dialog import android.app.Dialog
import android.os.Bundle import android.os.Bundle
import android.widget.NumberPicker
import com.afollestad.materialdialogs.MaterialDialog import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.customview.customView import com.afollestad.materialdialogs.datetime.datePicker
import com.afollestad.materialdialogs.customview.getCustomView
import com.bluelinelabs.conductor.Controller import com.bluelinelabs.conductor.Controller
import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.database.models.Track import eu.kanade.tachiyomi.data.database.models.Track
import eu.kanade.tachiyomi.data.track.TrackManager import eu.kanade.tachiyomi.data.track.TrackManager
import eu.kanade.tachiyomi.ui.base.controller.DialogController import eu.kanade.tachiyomi.ui.base.controller.DialogController
import eu.kanade.tachiyomi.util.system.toast
import java.text.DateFormatSymbols
import java.util.Calendar import java.util.Calendar
import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get import uy.kohesive.injekt.api.get
@ -41,74 +37,32 @@ class SetTrackReadingDatesDialog<T> : DialogController
} }
override fun onCreateDialog(savedViewState: Bundle?): Dialog { override fun onCreateDialog(savedViewState: Bundle?): Dialog {
val item = item val listener = (targetController as? Listener)
val dialog = MaterialDialog(activity!!) return MaterialDialog(activity!!)
.title(when (dateToUpdate) { .title(when (dateToUpdate) {
ReadingDate.Start -> R.string.track_started_reading_date ReadingDate.Start -> R.string.track_started_reading_date
ReadingDate.Finish -> R.string.track_finished_reading_date ReadingDate.Finish -> R.string.track_finished_reading_date
}) })
.customView(R.layout.track_date_dialog, dialogWrapContent = false) .datePicker(currentDate = getCurrentDate()) { _, date ->
.positiveButton(android.R.string.ok) { dialog -> listener?.setReadingDate(item, dateToUpdate, date.timeInMillis)
onDialogConfirm(dialog)
} }
.negativeButton(android.R.string.cancel) { dialog -> .neutralButton(R.string.action_remove) {
dialog.dismiss()
}
.neutralButton(R.string.action_remove) { dialog ->
val listener = (targetController as? Listener)
listener?.setReadingDate(item, dateToUpdate, 0L) listener?.setReadingDate(item, dateToUpdate, 0L)
dialog.dismiss()
} }
.noAutoDismiss()
onDialogCreated(dialog)
return dialog
} }
private fun onDialogCreated(dialog: MaterialDialog) { private fun getCurrentDate(): Calendar {
val view = dialog.getCustomView() // Today if no date is set, otherwise the already set date
return Calendar.getInstance().apply {
val dayPicker: NumberPicker = view.findViewById(R.id.day_picker) item.track?.let {
val monthPicker: NumberPicker = view.findViewById(R.id.month_picker) val date = when (dateToUpdate) {
val yearPicker: NumberPicker = view.findViewById(R.id.year_picker) ReadingDate.Start -> it.started_reading_date
ReadingDate.Finish -> it.finished_reading_date
val monthNames: Array<String> = DateFormatSymbols().months }
monthPicker.displayedValues = monthNames if (date != 0L)
timeInMillis = date
val calendar = Calendar.getInstance()
item.track?.let {
val date = when (dateToUpdate) {
ReadingDate.Start -> it.started_reading_date
ReadingDate.Finish -> it.finished_reading_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)
} }
} }

View File

@ -1,37 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<eu.kanade.tachiyomi.widget.MinMaxNumberPicker
android:id="@+id/day_picker"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="3dp"
android:descendantFocusability="blocksDescendants"
app:max="31"
app:min="1" />
<eu.kanade.tachiyomi.widget.MinMaxNumberPicker
android:id="@+id/month_picker"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="3dp"
android:descendantFocusability="blocksDescendants"
app:max="11"
app:min="0" />
<eu.kanade.tachiyomi.widget.MinMaxNumberPicker
android:id="@+id/year_picker"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="3dp"
android:descendantFocusability="blocksDescendants"
app:min="1900" />
</LinearLayout>