mirror of https://github.com/tasks/tasks
Move date time preferences to their own fragment
parent
cf22d6dae8
commit
25142fe9aa
@ -0,0 +1,175 @@
|
|||||||
|
package org.tasks.preferences.fragments
|
||||||
|
|
||||||
|
import android.app.Activity.RESULT_OK
|
||||||
|
import android.content.Intent
|
||||||
|
import android.os.Bundle
|
||||||
|
import android.widget.Toast
|
||||||
|
import androidx.preference.ListPreference
|
||||||
|
import androidx.preference.Preference
|
||||||
|
import org.tasks.R
|
||||||
|
import org.tasks.dialogs.MyTimePickerDialog.newTimePicker
|
||||||
|
import org.tasks.injection.FragmentComponent
|
||||||
|
import org.tasks.injection.InjectingPreferenceFragment
|
||||||
|
import org.tasks.locale.Locale
|
||||||
|
import org.tasks.preferences.Preferences
|
||||||
|
import org.tasks.time.DateTime
|
||||||
|
import org.tasks.ui.TimePreference
|
||||||
|
import org.threeten.bp.DayOfWeek
|
||||||
|
import org.threeten.bp.format.TextStyle
|
||||||
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
private const val REQUEST_MORNING = 10007
|
||||||
|
private const val REQUEST_AFTERNOON = 10008
|
||||||
|
private const val REQUEST_EVENING = 10009
|
||||||
|
private const val REQUEST_NIGHT = 10010
|
||||||
|
|
||||||
|
class DateAndTime : InjectingPreferenceFragment(), Preference.OnPreferenceChangeListener {
|
||||||
|
|
||||||
|
@Inject lateinit var preferences: Preferences
|
||||||
|
@Inject lateinit var locale: Locale
|
||||||
|
|
||||||
|
override fun getPreferenceXml() = R.xml.preferences_date_and_time
|
||||||
|
|
||||||
|
override fun setupPreferences(savedInstanceState: Bundle?) {
|
||||||
|
val startOfWeekPreference: ListPreference = getStartOfWeekPreference()
|
||||||
|
startOfWeekPreference.entries = getWeekdayEntries()
|
||||||
|
startOfWeekPreference.onPreferenceChangeListener = this
|
||||||
|
|
||||||
|
initializeTimePreference(getMorningPreference(), REQUEST_MORNING)
|
||||||
|
initializeTimePreference(getAfternoonPreference(), REQUEST_AFTERNOON)
|
||||||
|
initializeTimePreference(getEveningPreference(), REQUEST_EVENING)
|
||||||
|
initializeTimePreference(getNightPreference(), REQUEST_NIGHT)
|
||||||
|
|
||||||
|
updateStartOfWeek(preferences.getStringValue(R.string.p_start_of_week)!!)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
||||||
|
if (requestCode == REQUEST_MORNING) {
|
||||||
|
if (resultCode == RESULT_OK) {
|
||||||
|
getMorningPreference().handleTimePickerActivityIntent(data)
|
||||||
|
}
|
||||||
|
} else if (requestCode == REQUEST_AFTERNOON) {
|
||||||
|
if (resultCode == RESULT_OK) {
|
||||||
|
getAfternoonPreference().handleTimePickerActivityIntent(data)
|
||||||
|
}
|
||||||
|
} else if (requestCode == REQUEST_EVENING) {
|
||||||
|
if (resultCode == RESULT_OK) {
|
||||||
|
getEveningPreference().handleTimePickerActivityIntent(data)
|
||||||
|
}
|
||||||
|
} else if (requestCode == REQUEST_NIGHT) {
|
||||||
|
if (resultCode == RESULT_OK) {
|
||||||
|
getNightPreference().handleTimePickerActivityIntent(data)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
super.onActivityResult(requestCode, resultCode, data)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun inject(component: FragmentComponent) = component.inject(this)
|
||||||
|
|
||||||
|
private fun initializeTimePreference(preference: TimePreference, requestCode: Int) {
|
||||||
|
preference.onPreferenceChangeListener = this
|
||||||
|
preference.onPreferenceClickListener = Preference.OnPreferenceClickListener {
|
||||||
|
val current = DateTime().withMillisOfDay(preference.millisOfDay)
|
||||||
|
newTimePicker(this, requestCode, current.millis)
|
||||||
|
.show(parentFragmentManager, FRAG_TAG_TIME_PICKER)
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onPreferenceChange(preference: Preference?, newValue: Any?): Boolean {
|
||||||
|
if (preference == getStartOfWeekPreference()) {
|
||||||
|
updateStartOfWeek(newValue.toString())
|
||||||
|
} else {
|
||||||
|
val millisOfDay = newValue as Int
|
||||||
|
if (preference == getMorningPreference()) {
|
||||||
|
if (millisOfDay >= getAfternoonPreference().millisOfDay) {
|
||||||
|
mustComeBefore(R.string.date_shortcut_morning, R.string.date_shortcut_afternoon)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
} else if (preference == getAfternoonPreference()) {
|
||||||
|
if (millisOfDay <= getMorningPreference().millisOfDay) {
|
||||||
|
mustComeAfter(R.string.date_shortcut_afternoon, R.string.date_shortcut_morning)
|
||||||
|
return false
|
||||||
|
} else if (millisOfDay >= getEveningPreference().millisOfDay) {
|
||||||
|
mustComeBefore(R.string.date_shortcut_afternoon, R.string.date_shortcut_evening)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
} else if (preference == getEveningPreference()) {
|
||||||
|
if (millisOfDay <= getAfternoonPreference().millisOfDay) {
|
||||||
|
mustComeAfter(R.string.date_shortcut_evening, R.string.date_shortcut_afternoon)
|
||||||
|
return false
|
||||||
|
} else if (millisOfDay >= getNightPreference().millisOfDay) {
|
||||||
|
mustComeBefore(R.string.date_shortcut_evening, R.string.date_shortcut_night)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
} else if (preference == getNightPreference()) {
|
||||||
|
if (millisOfDay <= getEveningPreference().millisOfDay) {
|
||||||
|
mustComeAfter(R.string.date_shortcut_night, R.string.date_shortcut_evening)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun mustComeBefore(settingResId: Int, relativeResId: Int) {
|
||||||
|
invalidSetting(R.string.date_shortcut_must_come_before, settingResId, relativeResId)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun mustComeAfter(settingResId: Int, relativeResId: Int) {
|
||||||
|
invalidSetting(R.string.date_shortcut_must_come_after, settingResId, relativeResId)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun invalidSetting(errorResId: Int, settingResId: Int, relativeResId: Int) {
|
||||||
|
Toast.makeText(
|
||||||
|
context,
|
||||||
|
getString(errorResId, getString(settingResId), getString(relativeResId)),
|
||||||
|
Toast.LENGTH_SHORT
|
||||||
|
)
|
||||||
|
.show()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun updateStartOfWeek(value: String) {
|
||||||
|
val preference = getStartOfWeekPreference()
|
||||||
|
val index = preference.findIndexOfValue(value)
|
||||||
|
val summary: String? = getWeekdayEntries()?.get(index)
|
||||||
|
preference.summary = summary
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getStartOfWeekPreference(): ListPreference {
|
||||||
|
return findPreference(R.string.p_start_of_week) as ListPreference
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getWeekdayDisplayName(dayOfWeek: DayOfWeek): String {
|
||||||
|
return dayOfWeek.getDisplayName(TextStyle.FULL, locale.locale)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getMorningPreference(): TimePreference {
|
||||||
|
return getTimePreference(R.string.p_date_shortcut_morning)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getAfternoonPreference(): TimePreference {
|
||||||
|
return getTimePreference(R.string.p_date_shortcut_afternoon)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getEveningPreference(): TimePreference {
|
||||||
|
return getTimePreference(R.string.p_date_shortcut_evening)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getNightPreference(): TimePreference {
|
||||||
|
return getTimePreference(R.string.p_date_shortcut_night)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getTimePreference(resId: Int): TimePreference {
|
||||||
|
return findPreference(resId) as TimePreference
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getWeekdayEntries(): Array<String?>? {
|
||||||
|
return arrayOf(
|
||||||
|
getString(R.string.use_locale_default),
|
||||||
|
getWeekdayDisplayName(DayOfWeek.SUNDAY),
|
||||||
|
getWeekdayDisplayName(DayOfWeek.MONDAY)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,49 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:key="@string/preference_screen">
|
||||||
|
|
||||||
|
<ListPreference
|
||||||
|
android:defaultValue="0"
|
||||||
|
android:entryValues="@array/day_of_week_values"
|
||||||
|
android:key="@string/p_start_of_week"
|
||||||
|
android:title="@string/start_of_week" />
|
||||||
|
|
||||||
|
<org.tasks.ui.TimePreference
|
||||||
|
android:defaultValue="@integer/default_morning"
|
||||||
|
android:key="@string/p_date_shortcut_morning"
|
||||||
|
android:title="@string/date_shortcut_morning" />
|
||||||
|
|
||||||
|
<org.tasks.ui.TimePreference
|
||||||
|
android:defaultValue="@integer/default_afternoon"
|
||||||
|
android:key="@string/p_date_shortcut_afternoon"
|
||||||
|
android:title="@string/date_shortcut_afternoon" />
|
||||||
|
|
||||||
|
<org.tasks.ui.TimePreference
|
||||||
|
android:defaultValue="@integer/default_evening"
|
||||||
|
android:key="@string/p_date_shortcut_evening"
|
||||||
|
android:title="@string/date_shortcut_evening" />
|
||||||
|
|
||||||
|
<org.tasks.ui.TimePreference
|
||||||
|
android:defaultValue="@integer/default_night"
|
||||||
|
android:key="@string/p_date_shortcut_night"
|
||||||
|
android:title="@string/date_shortcut_night" />
|
||||||
|
|
||||||
|
<PreferenceCategory android:title="@string/auto_dismiss_datetime">
|
||||||
|
|
||||||
|
<Preference
|
||||||
|
android:summary="@string/auto_dismiss_datetime_summary"
|
||||||
|
android:selectable="false" />
|
||||||
|
|
||||||
|
<SwitchPreferenceCompat
|
||||||
|
android:defaultValue="false"
|
||||||
|
android:key="@string/p_auto_dismiss_datetime_list_screen"
|
||||||
|
android:title="@string/auto_dismiss_datetime_list" />
|
||||||
|
|
||||||
|
<SwitchPreferenceCompat
|
||||||
|
android:defaultValue="false"
|
||||||
|
android:key="@string/p_auto_dismiss_datetime_edit_screen"
|
||||||
|
android:title="@string/auto_dismiss_datetime_edit" />
|
||||||
|
|
||||||
|
</PreferenceCategory>
|
||||||
|
|
||||||
|
</PreferenceScreen>
|
Loading…
Reference in New Issue