From 94cc4bf57298cfbde4ca9a396faebbc71b88fc83 Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Sat, 11 Jul 2020 08:35:09 -0500 Subject: [PATCH] Use coroutines in preferences --- .../preferences/DefaultFilterProvider.kt | 7 ++-- .../tasks/preferences/fragments/Advanced.kt | 34 +++++++------------ .../preferences/fragments/LookAndFeel.kt | 22 +++++++----- .../preferences/fragments/TaskDefaults.kt | 18 ++++++---- .../res/xml/preferences_task_defaults.xml | 3 +- 5 files changed, 41 insertions(+), 43 deletions(-) diff --git a/app/src/main/java/org/tasks/preferences/DefaultFilterProvider.kt b/app/src/main/java/org/tasks/preferences/DefaultFilterProvider.kt index 4dfac57e6..5b377bb21 100644 --- a/app/src/main/java/org/tasks/preferences/DefaultFilterProvider.kt +++ b/app/src/main/java/org/tasks/preferences/DefaultFilterProvider.kt @@ -32,10 +32,6 @@ class DefaultFilterProvider @Inject constructor( @Deprecated("use coroutines") get() = runBlocking { getBadgeFilter() } set(filter) = setFilterPreference(filter, R.string.p_badge_list) - var defaultOpenFilter: Filter - @Deprecated("use coroutines") get() = runBlocking { getDefaultOpenFilter() } - set(filter) = setFilterPreference(filter, R.string.p_default_open_filter) - var lastViewedFilter: Filter @Deprecated("use coroutines") get() = runBlocking { getFilterFromPreference(R.string.p_last_viewed_list) } set(filter) = setFilterPreference(filter, R.string.p_last_viewed_list) @@ -58,6 +54,9 @@ class DefaultFilterProvider @Inject constructor( suspend fun getDefaultOpenFilter() = getFilterFromPreference(R.string.p_default_open_filter) + fun setDefaultOpenFilter(filter: Filter) = + setFilterPreference(filter, R.string.p_default_open_filter) + suspend fun getStartupFilter(): Filter { return if (preferences.getBoolean(R.string.p_open_last_viewed_list, true)) { getLastViewedFilter() diff --git a/app/src/main/java/org/tasks/preferences/fragments/Advanced.kt b/app/src/main/java/org/tasks/preferences/fragments/Advanced.kt index 19b347246..1f4e90d97 100644 --- a/app/src/main/java/org/tasks/preferences/fragments/Advanced.kt +++ b/app/src/main/java/org/tasks/preferences/fragments/Advanced.kt @@ -3,16 +3,14 @@ package org.tasks.preferences.fragments import android.app.Activity import android.content.Intent import android.os.Bundle -import androidx.annotation.StringRes +import androidx.lifecycle.lifecycleScope import androidx.preference.Preference import androidx.preference.SwitchPreferenceCompat import com.todoroo.astrid.dao.Database -import com.todoroo.astrid.dao.TaskDaoBlocking +import com.todoroo.astrid.dao.TaskDao import dagger.hilt.android.AndroidEntryPoint -import io.reactivex.Single -import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.disposables.CompositeDisposable -import io.reactivex.schedulers.Schedulers +import kotlinx.coroutines.launch import org.tasks.LocalBroadcastManager import org.tasks.PermissionUtil import org.tasks.R @@ -25,7 +23,6 @@ import org.tasks.preferences.PermissionRequestor import org.tasks.preferences.Preferences import org.tasks.scheduling.CalendarNotificationIntentService import org.tasks.ui.Toaster -import java.util.concurrent.Callable import javax.inject.Inject private const val REQUEST_CODE_FILES_DIR = 10000 @@ -35,7 +32,7 @@ class Advanced : InjectingPreferenceFragment() { @Inject lateinit var preferences: Preferences @Inject lateinit var database: Database - @Inject lateinit var taskDao: TaskDaoBlocking + @Inject lateinit var taskDao: TaskDao @Inject lateinit var calendarEventProvider: CalendarEventProvider @Inject lateinit var toaster: Toaster @Inject lateinit var permissionRequester: FragmentPermissionRequestor @@ -163,12 +160,10 @@ class Advanced : InjectingPreferenceFragment() { .newDialog() .setMessage(R.string.EPr_manage_delete_completed_gcal_message) .setPositiveButton(android.R.string.ok) { _, _ -> - performAction( - R.string.EPr_manage_delete_completed_gcal_status, - Callable { - calendarEventProvider.deleteEvents(taskDao.getCompletedCalendarEvents()) - taskDao.clearCompletedCalendarEvents() - }) + performAction(R.string.EPr_manage_delete_completed_gcal_status) { + calendarEventProvider.deleteEvents(taskDao.getCompletedCalendarEvents()) + taskDao.clearCompletedCalendarEvents() + } } .setNegativeButton(android.R.string.cancel, null) .show() @@ -180,22 +175,17 @@ class Advanced : InjectingPreferenceFragment() { .setMessage(R.string.EPr_manage_delete_all_gcal_message) .setPositiveButton(android.R.string.ok) { _, _ -> performAction( - R.string.EPr_manage_delete_all_gcal_status, - Callable { + R.string.EPr_manage_delete_all_gcal_status) { calendarEventProvider.deleteEvents(taskDao.getAllCalendarEvents()) taskDao.clearAllCalendarEvents() - }) + } } .setNegativeButton(android.R.string.cancel, null) .show() } - private fun performAction(@StringRes message: Int, callable: Callable) { - disposables.add( - Single.fromCallable(callable) - .subscribeOn(Schedulers.io()) - .observeOn(AndroidSchedulers.mainThread()) - .subscribe { c: Int? -> toaster.longToastUnformatted(message, c!!) }) + private fun performAction(message: Int, callable: suspend () -> Int) = lifecycleScope.launch { + toaster.longToastUnformatted(message, callable.invoke()) } private fun resetPreferences() { 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 ea84b2e19..fa0ecf003 100644 --- a/app/src/main/java/org/tasks/preferences/fragments/LookAndFeel.kt +++ b/app/src/main/java/org/tasks/preferences/fragments/LookAndFeel.kt @@ -9,10 +9,12 @@ import android.content.res.Configuration import android.os.Bundle import android.os.Handler import androidx.annotation.StringRes +import androidx.lifecycle.lifecycleScope import androidx.preference.Preference import androidx.preference.SwitchPreferenceCompat import com.todoroo.astrid.api.Filter import dagger.hilt.android.AndroidEntryPoint +import kotlinx.coroutines.launch import org.tasks.BuildConfig import org.tasks.LocalBroadcastManager import org.tasks.R @@ -106,16 +108,18 @@ class LookAndFeel : InjectingPreferenceFragment() { } val defaultList = findPreference(R.string.p_default_open_filter) - val filter = defaultFilterProvider.defaultOpenFilter + val filter = defaultFilterProvider.getDefaultOpenFilter() defaultList.summary = filter.listingTitle defaultList.onPreferenceClickListener = Preference.OnPreferenceClickListener { - val intent = Intent(context, FilterSelectionActivity::class.java) - intent.putExtra( - FilterSelectionActivity.EXTRA_FILTER, - defaultFilterProvider.defaultOpenFilter - ) - intent.putExtra(FilterSelectionActivity.EXTRA_RETURN_FILTER, true) - startActivityForResult(intent, REQUEST_DEFAULT_LIST) + lifecycleScope.launch { + val intent = Intent(context, FilterSelectionActivity::class.java) + intent.putExtra( + FilterSelectionActivity.EXTRA_FILTER, + defaultFilterProvider.getDefaultOpenFilter() + ) + intent.putExtra(FilterSelectionActivity.EXTRA_RETURN_FILTER, true) + startActivityForResult(intent, REQUEST_DEFAULT_LIST) + } true } @@ -277,7 +281,7 @@ class LookAndFeel : InjectingPreferenceFragment() { if (resultCode == RESULT_OK) { val filter: Filter = data!!.getParcelableExtra(FilterSelectionActivity.EXTRA_FILTER)!! - defaultFilterProvider.defaultOpenFilter = filter + defaultFilterProvider.setDefaultOpenFilter(filter) findPreference(R.string.p_default_open_filter).summary = filter.listingTitle localBroadcastManager.broadcastRefresh() } diff --git a/app/src/main/java/org/tasks/preferences/fragments/TaskDefaults.kt b/app/src/main/java/org/tasks/preferences/fragments/TaskDefaults.kt index abb790d1a..58b479f87 100644 --- a/app/src/main/java/org/tasks/preferences/fragments/TaskDefaults.kt +++ b/app/src/main/java/org/tasks/preferences/fragments/TaskDefaults.kt @@ -3,11 +3,13 @@ package org.tasks.preferences.fragments import android.app.Activity.RESULT_OK import android.content.Intent import android.os.Bundle +import androidx.lifecycle.lifecycleScope import androidx.preference.Preference import com.todoroo.astrid.api.CaldavFilter import com.todoroo.astrid.api.Filter import com.todoroo.astrid.api.GtasksFilter import dagger.hilt.android.AndroidEntryPoint +import kotlinx.coroutines.launch import org.tasks.R import org.tasks.activities.ListPicker import org.tasks.calendars.CalendarPicker @@ -47,11 +49,13 @@ class TaskDefaults : InjectingPreferenceFragment() { findPreference(R.string.p_default_list) .setOnPreferenceClickListener { - ListPicker.newListPicker( - defaultFilterProvider.defaultList, - this, - REQUEST_DEFAULT_LIST) - .show(parentFragmentManager, FRAG_TAG_DEFAULT_LIST_SELECTION) + lifecycleScope.launch { + ListPicker.newListPicker( + defaultFilterProvider.getDefaultList(), + this@TaskDefaults, + REQUEST_DEFAULT_LIST) + .show(parentFragmentManager, FRAG_TAG_DEFAULT_LIST_SELECTION) + } false } updateRemoteListSummary() @@ -87,8 +91,8 @@ class TaskDefaults : InjectingPreferenceFragment() { return calendarProvider.getCalendar(calendarId)?.name } - private fun updateRemoteListSummary() { - val defaultFilter = defaultFilterProvider.defaultList + private fun updateRemoteListSummary() = lifecycleScope.launch { + val defaultFilter = defaultFilterProvider.getDefaultList() findPreference(R.string.p_default_list).summary = defaultFilter.listingTitle } } \ No newline at end of file diff --git a/app/src/main/res/xml/preferences_task_defaults.xml b/app/src/main/res/xml/preferences_task_defaults.xml index a1c9bfbbb..95bfe57c0 100644 --- a/app/src/main/res/xml/preferences_task_defaults.xml +++ b/app/src/main/res/xml/preferences_task_defaults.xml @@ -11,7 +11,8 @@ + android:title="@string/default_list" + android:summary=" "/>