Use coroutines in preferences

pull/1051/head
Alex Baker 4 years ago
parent a89580577b
commit 94cc4bf572

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

@ -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<Int>) {
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() {

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

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

@ -11,7 +11,8 @@
<Preference
android:icon="@drawable/ic_list_24px"
android:key="@string/p_default_list"
android:title="@string/default_list" />
android:title="@string/default_list"
android:summary=" "/>
<ListPreference
android:defaultValue="2"

Loading…
Cancel
Save