Move date time preferences to their own fragment

pull/996/head
Alex Baker 5 years ago
parent cf22d6dae8
commit 25142fe9aa

@ -13,6 +13,7 @@ import org.tasks.fragments.CommentBarFragment;
import org.tasks.preferences.fragments.Advanced; import org.tasks.preferences.fragments.Advanced;
import org.tasks.preferences.fragments.Backups; import org.tasks.preferences.fragments.Backups;
import org.tasks.preferences.fragments.DashClock; import org.tasks.preferences.fragments.DashClock;
import org.tasks.preferences.fragments.DateAndTime;
import org.tasks.preferences.fragments.Debug; import org.tasks.preferences.fragments.Debug;
import org.tasks.preferences.fragments.HelpAndFeedback; import org.tasks.preferences.fragments.HelpAndFeedback;
import org.tasks.preferences.fragments.LookAndFeel; import org.tasks.preferences.fragments.LookAndFeel;
@ -103,4 +104,6 @@ public interface FragmentComponent {
void inject(NavigationDrawer navigationDrawer); void inject(NavigationDrawer navigationDrawer);
void inject(Widgets widgets); void inject(Widgets widgets);
void inject(DateAndTime dateAndTime);
} }

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

@ -57,15 +57,11 @@ private const val REQUEST_ACCENT_PICKER = 10003
private const val REQUEST_LAUNCHER_PICKER = 10004 private const val REQUEST_LAUNCHER_PICKER = 10004
private const val REQUEST_DEFAULT_LIST = 10005 private const val REQUEST_DEFAULT_LIST = 10005
private const val REQUEST_LOCALE = 10006 private const val REQUEST_LOCALE = 10006
private const val REQUEST_MORNING = 10007
private const val REQUEST_AFTERNOON = 10008
private const val REQUEST_EVENING = 10009
private const val REQUEST_NIGHT = 10010
private const val FRAG_TAG_LOCALE_PICKER = "frag_tag_locale_picker" private const val FRAG_TAG_LOCALE_PICKER = "frag_tag_locale_picker"
private const val FRAG_TAG_THEME_PICKER = "frag_tag_theme_picker" private const val FRAG_TAG_THEME_PICKER = "frag_tag_theme_picker"
private const val FRAG_TAG_COLOR_PICKER = "frag_tag_color_picker" private const val FRAG_TAG_COLOR_PICKER = "frag_tag_color_picker"
class LookAndFeel : InjectingPreferenceFragment(), Preference.OnPreferenceChangeListener { class LookAndFeel : InjectingPreferenceFragment() {
@Inject lateinit var themeBase: ThemeBase @Inject lateinit var themeBase: ThemeBase
@Inject lateinit var themeColor: ThemeColor @Inject lateinit var themeColor: ThemeColor
@ -137,17 +133,6 @@ class LookAndFeel : InjectingPreferenceFragment(), Preference.OnPreferenceChange
false false
} }
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)!!)
requires(R.string.task_list_options, atLeastLollipop(), R.string.p_show_subtasks) requires(R.string.task_list_options, atLeastLollipop(), R.string.p_show_subtasks)
@Suppress("ConstantConditionIf") @Suppress("ConstantConditionIf")
@ -351,22 +336,6 @@ class LookAndFeel : InjectingPreferenceFragment(), Preference.OnPreferenceChange
showRestartDialog() showRestartDialog()
} }
} }
} else 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 { } else {
super.onActivityResult(requestCode, resultCode, data) super.onActivityResult(requestCode, resultCode, data)
} }
@ -410,110 +379,4 @@ class LookAndFeel : InjectingPreferenceFragment(), Preference.OnPreferenceChange
false false
} }
} }
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)
)
}
} }

@ -588,6 +588,8 @@ File %1$s contained %2$s.\n\n
<string name="shortcut_pick_time">Pick time</string> <string name="shortcut_pick_time">Pick time</string>
<string name="shortcut_pick_date">Pick date</string> <string name="shortcut_pick_date">Pick date</string>
<string name="auto_dismiss_datetime">Autoclose date time picker</string> <string name="auto_dismiss_datetime">Autoclose date time picker</string>
<string name="auto_dismiss_datetime_list">Autoclose from list screen</string>
<string name="auto_dismiss_datetime_edit">Autoclose from edit screen</string>
<string name="auto_dismiss_datetime_summary">Close date time picker after selecting a date or time</string> <string name="auto_dismiss_datetime_summary">Close date time picker after selecting a date or time</string>
<string name="calendar_event_created">Calendar event created for %s</string> <string name="calendar_event_created">Calendar event created for %s</string>
<string name="select_all">Select all</string> <string name="select_all">Select all</string>

@ -22,6 +22,11 @@
app:icon="@drawable/ic_outline_edit_24px" app:icon="@drawable/ic_outline_edit_24px"
app:title="@string/task_defaults" /> app:title="@string/task_defaults" />
<Preference
app:fragment="org.tasks.preferences.fragments.DateAndTime"
app:icon="@drawable/ic_outline_schedule_24px"
app:title="@string/date_and_time" />
<Preference <Preference
app:fragment="org.tasks.preferences.fragments.NavigationDrawer" app:fragment="org.tasks.preferences.fragments.NavigationDrawer"
app:icon="@drawable/ic_outline_menu_24px" app:icon="@drawable/ic_outline_menu_24px"

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

@ -77,12 +77,6 @@
android:key="@string/p_show_list_indicators" android:key="@string/p_show_list_indicators"
android:title="@string/show_list_indicators" /> android:title="@string/show_list_indicators" />
<SwitchPreferenceCompat
android:defaultValue="false"
android:key="@string/p_auto_dismiss_datetime_list_screen"
android:title="@string/auto_dismiss_datetime"
android:summary="@string/auto_dismiss_datetime_summary" />
<SwitchPreferenceCompat <SwitchPreferenceCompat
android:defaultValue="false" android:defaultValue="false"
android:key="@string/p_linkify_task_list" android:key="@string/p_linkify_task_list"
@ -130,12 +124,6 @@
android:targetPackage="@string/app_package" /> android:targetPackage="@string/app_package" />
</Preference> </Preference>
<SwitchPreferenceCompat
android:defaultValue="false"
android:key="@string/p_auto_dismiss_datetime_edit_screen"
android:title="@string/auto_dismiss_datetime"
android:summary="@string/auto_dismiss_datetime_summary" />
<SwitchPreferenceCompat <SwitchPreferenceCompat
android:defaultValue="false" android:defaultValue="false"
android:key="@string/p_linkify_task_edit" android:key="@string/p_linkify_task_edit"
@ -159,36 +147,6 @@
</PreferenceCategory> </PreferenceCategory>
<PreferenceCategory android:title="@string/date_and_time">
<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>
<PreferenceCategory <PreferenceCategory
android:key="@string/TEA_control_location" android:key="@string/TEA_control_location"
android:title="@string/TEA_control_location"> android:title="@string/TEA_control_location">

Loading…
Cancel
Save