diff --git a/app/src/main/java/org/tasks/injection/FragmentComponent.java b/app/src/main/java/org/tasks/injection/FragmentComponent.java index bcb837d8c..964fac55e 100644 --- a/app/src/main/java/org/tasks/injection/FragmentComponent.java +++ b/app/src/main/java/org/tasks/injection/FragmentComponent.java @@ -13,6 +13,7 @@ import org.tasks.fragments.CommentBarFragment; import org.tasks.preferences.fragments.Advanced; import org.tasks.preferences.fragments.Backups; import org.tasks.preferences.fragments.DashClock; +import org.tasks.preferences.fragments.DateAndTime; import org.tasks.preferences.fragments.Debug; import org.tasks.preferences.fragments.HelpAndFeedback; import org.tasks.preferences.fragments.LookAndFeel; @@ -103,4 +104,6 @@ public interface FragmentComponent { void inject(NavigationDrawer navigationDrawer); void inject(Widgets widgets); + + void inject(DateAndTime dateAndTime); } diff --git a/app/src/main/java/org/tasks/preferences/fragments/DateAndTime.kt b/app/src/main/java/org/tasks/preferences/fragments/DateAndTime.kt new file mode 100644 index 000000000..09040acaa --- /dev/null +++ b/app/src/main/java/org/tasks/preferences/fragments/DateAndTime.kt @@ -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? { + return arrayOf( + getString(R.string.use_locale_default), + getWeekdayDisplayName(DayOfWeek.SUNDAY), + getWeekdayDisplayName(DayOfWeek.MONDAY) + ) + } +} \ No newline at end of file diff --git a/app/src/main/java/org/tasks/preferences/fragments/LookAndFeel.kt b/app/src/main/java/org/tasks/preferences/fragments/LookAndFeel.kt index e798705b7..1d05e7dc8 100644 --- a/app/src/main/java/org/tasks/preferences/fragments/LookAndFeel.kt +++ b/app/src/main/java/org/tasks/preferences/fragments/LookAndFeel.kt @@ -57,15 +57,11 @@ private const val REQUEST_ACCENT_PICKER = 10003 private const val REQUEST_LAUNCHER_PICKER = 10004 private const val REQUEST_DEFAULT_LIST = 10005 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_THEME_PICKER = "frag_tag_theme_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 themeColor: ThemeColor @@ -137,17 +133,6 @@ class LookAndFeel : InjectingPreferenceFragment(), Preference.OnPreferenceChange 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) @Suppress("ConstantConditionIf") @@ -351,22 +336,6 @@ class LookAndFeel : InjectingPreferenceFragment(), Preference.OnPreferenceChange 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 { super.onActivityResult(requestCode, resultCode, data) } @@ -410,110 +379,4 @@ class LookAndFeel : InjectingPreferenceFragment(), Preference.OnPreferenceChange 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? { - return arrayOf( - getString(R.string.use_locale_default), - getWeekdayDisplayName(DayOfWeek.SUNDAY), - getWeekdayDisplayName(DayOfWeek.MONDAY) - ) - } } \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a8040793e..faf8f792e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -588,6 +588,8 @@ File %1$s contained %2$s.\n\n Pick time Pick date Autoclose date time picker + Autoclose from list screen + Autoclose from edit screen Close date time picker after selecting a date or time Calendar event created for %s Select all diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index e24dfe5e4..5e2e03b51 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -22,6 +22,11 @@ app:icon="@drawable/ic_outline_edit_24px" app:title="@string/task_defaults" /> + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/xml/preferences_look_and_feel.xml b/app/src/main/res/xml/preferences_look_and_feel.xml index bce5cbd74..1a0295d29 100644 --- a/app/src/main/res/xml/preferences_look_and_feel.xml +++ b/app/src/main/res/xml/preferences_look_and_feel.xml @@ -77,12 +77,6 @@ android:key="@string/p_show_list_indicators" android:title="@string/show_list_indicators" /> - - - - - - - - - - - - - - - - - -