From 236f9ae0e0da05535fbaa5c79b9244acf59492b4 Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Mon, 29 Jun 2020 15:47:41 -0500 Subject: [PATCH] Use coroutines in MainActivity --- .../todoroo/astrid/activity/MainActivity.kt | 51 +++++-------- .../tasks/dashclock/DashClockExtension.java | 2 +- .../java/org/tasks/data/FilterDaoBlocking.kt | 4 -- .../locale/receiver/TaskerIntentService.java | 2 +- .../preferences/DefaultFilterProvider.kt | 71 +++++++++++++------ .../tasks/preferences/fragments/DashClock.kt | 2 +- .../preferences/fragments/ScrollableWidget.kt | 2 +- .../fragments/TaskerListNotification.kt | 2 +- .../tasks/preferences/fragments/Widgets.kt | 2 +- .../tasks/widget/ScrollableViewsFactory.java | 2 +- .../java/org/tasks/widget/TasksWidget.java | 2 +- 11 files changed, 75 insertions(+), 67 deletions(-) diff --git a/app/src/main/java/com/todoroo/astrid/activity/MainActivity.kt b/app/src/main/java/com/todoroo/astrid/activity/MainActivity.kt index 470ab4504..624ded33f 100644 --- a/app/src/main/java/com/todoroo/astrid/activity/MainActivity.kt +++ b/app/src/main/java/com/todoroo/astrid/activity/MainActivity.kt @@ -22,16 +22,12 @@ import com.todoroo.andlib.utility.AndroidUtilities import com.todoroo.astrid.activity.TaskEditFragment.TaskEditFragmentCallbackHandler import com.todoroo.astrid.activity.TaskListFragment.TaskListFragmentCallbackHandler import com.todoroo.astrid.api.Filter -import com.todoroo.astrid.dao.TaskDaoBlocking +import com.todoroo.astrid.dao.TaskDao import com.todoroo.astrid.data.Task import com.todoroo.astrid.service.TaskCreator import com.todoroo.astrid.timers.TimerControlSet.TimerControlSetCallback 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 org.tasks.BuildConfig +import kotlinx.coroutines.launch import org.tasks.LocalBroadcastManager import org.tasks.R import org.tasks.activities.TagSettingsActivity @@ -65,13 +61,12 @@ class MainActivity : InjectingAppCompatActivity(), TaskListFragmentCallbackHandl @Inject lateinit var repeatConfirmationReceiver: RepeatConfirmationReceiver @Inject lateinit var defaultFilterProvider: DefaultFilterProvider @Inject lateinit var theme: Theme - @Inject lateinit var taskDao: TaskDaoBlocking + @Inject lateinit var taskDao: TaskDao @Inject lateinit var localBroadcastManager: LocalBroadcastManager @Inject lateinit var taskCreator: TaskCreator @Inject lateinit var playServices: PlayServices @Inject lateinit var inventory: Inventory @Inject lateinit var colorProvider: ColorProvider - private var disposables: CompositeDisposable? = null private lateinit var navigationDrawer: NavigationDrawerFragment private var currentNightMode = 0 private var currentPro = false @@ -176,23 +171,18 @@ class MainActivity : InjectingAppCompatActivity(), TaskListFragmentCallbackHandl tef.save() } if (loadFilter || !openFilter && filter == null) { - disposables!!.add( - Single.fromCallable { - val filter = intent.getStringExtra(LOAD_FILTER) - intent.removeExtra(LOAD_FILTER) - if (filter.isNullOrBlank()) { - defaultFilterProvider.startupFilter - } else { - defaultFilterProvider.getFilterFromPreference(filter) - } - } - .subscribeOn(Schedulers.io()) - .observeOn(AndroidSchedulers.mainThread()) - .subscribe { filter: Filter? -> - clearUi() - openTaskListFragment(filter) - openTask(filter) - }) + lifecycleScope.launch { + val filterString = intent.getStringExtra(LOAD_FILTER) + intent.removeExtra(LOAD_FILTER) + val filter = if (filterString.isNullOrBlank()) { + defaultFilterProvider.getStartupFilter() + } else { + defaultFilterProvider.getFilterFromPreference(filterString) + } + clearUi() + openTaskListFragment(filter) + openTask(filter) + } } else if (openFilter) { val filter: Filter? = intent.getParcelableExtra(OPEN_FILTER) intent.removeExtra(OPEN_FILTER) @@ -279,8 +269,6 @@ class MainActivity : InjectingAppCompatActivity(), TaskListFragmentCallbackHandl return } localBroadcastManager.registerRepeatReceiver(repeatConfirmationReceiver) - check(!(BuildConfig.DEBUG && disposables != null && !disposables!!.isDisposed)) - disposables = CompositeDisposable() if (preferences.getBoolean(R.string.p_just_updated, false)) { if (preferences.getBoolean(R.string.p_show_whats_new, true)) { val fragmentManager = supportFragmentManager @@ -303,7 +291,6 @@ class MainActivity : InjectingAppCompatActivity(), TaskListFragmentCallbackHandl override fun onPause() { super.onPause() localBroadcastManager.unregisterReceiver(repeatConfirmationReceiver) - disposables?.dispose() } override fun onTaskListItemClicked(task: Task?) { @@ -317,11 +304,9 @@ class MainActivity : InjectingAppCompatActivity(), TaskListFragmentCallbackHandl if (task.isNew) { openTask(task) } else { - disposables!!.add( - Single.fromCallable { taskDao.fetchBlocking(task.id) } - .subscribeOn(Schedulers.io()) - .observeOn(AndroidSchedulers.mainThread()) - .subscribe { t: Task? -> this.openTask(t) }) + lifecycleScope.launch { + openTask(taskDao.fetch(task.id)) + } } } diff --git a/app/src/main/java/org/tasks/dashclock/DashClockExtension.java b/app/src/main/java/org/tasks/dashclock/DashClockExtension.java index f4b0b6b4d..2bc71bcaf 100644 --- a/app/src/main/java/org/tasks/dashclock/DashClockExtension.java +++ b/app/src/main/java/org/tasks/dashclock/DashClockExtension.java @@ -54,7 +54,7 @@ public class DashClockExtension extends com.google.android.apps.dashclock.api.Da private void refresh() { final String filterPreference = preferences.getStringValue(R.string.p_dashclock_filter); - Filter filter = defaultFilterProvider.getFilterFromPreference(filterPreference); + Filter filter = defaultFilterProvider.getFilterFromPreferenceBlocking(filterPreference); int count = taskDao.count(filter); diff --git a/app/src/main/java/org/tasks/data/FilterDaoBlocking.kt b/app/src/main/java/org/tasks/data/FilterDaoBlocking.kt index 0568e8a3d..192750de6 100644 --- a/app/src/main/java/org/tasks/data/FilterDaoBlocking.kt +++ b/app/src/main/java/org/tasks/data/FilterDaoBlocking.kt @@ -25,10 +25,6 @@ class FilterDaoBlocking @Inject constructor(private val dao: FilterDao) { dao.getFilters() } - fun getById(id: Long): Filter? = runBlocking { - dao.getById(id) - } - fun getAll(): List = runBlocking { dao.getAll() } diff --git a/app/src/main/java/org/tasks/locale/receiver/TaskerIntentService.java b/app/src/main/java/org/tasks/locale/receiver/TaskerIntentService.java index 6e4c6745a..95fa47f97 100644 --- a/app/src/main/java/org/tasks/locale/receiver/TaskerIntentService.java +++ b/app/src/main/java/org/tasks/locale/receiver/TaskerIntentService.java @@ -31,7 +31,7 @@ public class TaskerIntentService extends InjectingJobIntentService { if (ListNotificationBundle.isBundleValid(bundle)) { Filter filter = - defaultFilterProvider.getFilterFromPreference( + defaultFilterProvider.getFilterFromPreferenceBlocking( bundle.getString(ListNotificationBundle.BUNDLE_EXTRA_STRING_FILTER)); notifier.triggerFilterNotification(filter); } else if (TaskCreationBundle.isBundleValid(bundle)) { diff --git a/app/src/main/java/org/tasks/preferences/DefaultFilterProvider.kt b/app/src/main/java/org/tasks/preferences/DefaultFilterProvider.kt index 1bc251490..1dd2af609 100644 --- a/app/src/main/java/org/tasks/preferences/DefaultFilterProvider.kt +++ b/app/src/main/java/org/tasks/preferences/DefaultFilterProvider.kt @@ -6,6 +6,7 @@ import com.todoroo.astrid.api.Filter import com.todoroo.astrid.core.BuiltInFilterExposer import com.todoroo.astrid.core.BuiltInFilterExposer.getMyTasksFilter import dagger.hilt.android.qualifiers.ApplicationContext +import kotlinx.coroutines.runBlocking import org.tasks.R import org.tasks.Strings.isNullOrEmpty import org.tasks.data.* @@ -16,62 +17,88 @@ import javax.inject.Inject class DefaultFilterProvider @Inject constructor( @param:ApplicationContext private val context: Context, private val preferences: Preferences, - private val filterDao: FilterDaoBlocking, - private val tagDataDao: TagDataDaoBlocking, - private val googleTaskListDao: GoogleTaskListDaoBlocking, - private val caldavDao: CaldavDaoBlocking, - private val locationDao: LocationDaoBlocking) { + private val filterDao: FilterDao, + private val tagDataDao: TagDataDao, + private val googleTaskListDao: GoogleTaskListDao, + private val caldavDao: CaldavDao, + private val locationDao: LocationDao) { + @Deprecated("use coroutines") var dashclockFilter: Filter - get() = getFilterFromPreference(R.string.p_dashclock_filter) + get() = runBlocking { getFilterFromPreference(R.string.p_dashclock_filter) } set(filter) = setFilterPreference(filter, R.string.p_dashclock_filter) + @Deprecated("use coroutines") var badgeFilter: Filter - get() = getFilterFromPreference(R.string.p_badge_list) + get() = runBlocking { getFilterFromPreference(R.string.p_badge_list) } set(filter) = setFilterPreference(filter, R.string.p_badge_list) + @Deprecated("use coroutines") var defaultOpenFilter: Filter - get() = getFilterFromPreference(R.string.p_default_open_filter) + get() = runBlocking { getDefaultOpenFilter() } set(filter) = setFilterPreference(filter, R.string.p_default_open_filter) + @Deprecated("use coroutines") var lastViewedFilter: Filter - get() = getFilterFromPreference(R.string.p_last_viewed_list) + get() = runBlocking { getFilterFromPreference(R.string.p_last_viewed_list) } set(filter) = setFilterPreference(filter, R.string.p_last_viewed_list) + @Deprecated("use coroutines") var defaultList: Filter - get() = getFilterFromPreference(preferences.getStringValue(R.string.p_default_list), null) ?: getAnyList() + get() = runBlocking { getDefaultList() } set(filter) = setFilterPreference(filter, R.string.p_default_list) + @Deprecated("use coroutines") val startupFilter: Filter - get() { - return if (preferences.getBoolean(R.string.p_open_last_viewed_list, true)) { - lastViewedFilter - } else { - defaultOpenFilter - } + get() = runBlocking { getStartupFilter() } + + suspend fun getDefaultList() = + getFilterFromPreference(preferences.getStringValue(R.string.p_default_list), null) + ?: getAnyList() + + suspend fun getLastViewedFilter() = getFilterFromPreference(R.string.p_last_viewed_list) + + suspend fun getDefaultOpenFilter() = getFilterFromPreference(R.string.p_default_open_filter) + + suspend fun getStartupFilter(): Filter { + return if (preferences.getBoolean(R.string.p_open_last_viewed_list, true)) { + getLastViewedFilter() + } else { + getDefaultOpenFilter() } + } + + @Deprecated("use coroutines") + fun getFilterFromPreferenceBlocking(resId: Int) = runBlocking { + getFilterFromPreference(resId) + } + + @Deprecated("use coroutines") + fun getFilterFromPreferenceBlocking(prefString: String?) = runBlocking { + getFilterFromPreference(prefString) + } - fun getFilterFromPreference(resId: Int): Filter = + suspend fun getFilterFromPreference(resId: Int): Filter = getFilterFromPreference(preferences.getStringValue(resId)) - fun getFilterFromPreference(prefString: String?): Filter = + suspend fun getFilterFromPreference(prefString: String?): Filter = getFilterFromPreference(prefString, getMyTasksFilter(context.resources))!! - private fun getAnyList(): Filter { + private suspend fun getAnyList(): Filter { val filter = googleTaskListDao.getAllLists().getOrNull(0)?.let(::GtasksFilter) ?: caldavDao.getCalendars().getOrElse(0) { caldavDao.getLocalList(context) }.let(::CaldavFilter) defaultList = filter return filter } - private fun getFilterFromPreference(preferenceValue: String?, def: Filter?) = try { - preferenceValue?.let(this::loadFilter) ?: def + private suspend fun getFilterFromPreference(preferenceValue: String?, def: Filter?) = try { + preferenceValue?.let { loadFilter(it) } ?: def } catch (e: Exception) { Timber.e(e) def } - private fun loadFilter(preferenceValue: String): Filter? { + private suspend fun loadFilter(preferenceValue: String): Filter? { val split = preferenceValue.split(":") return when (split[0].toInt()) { TYPE_FILTER -> getBuiltInFilter(split[1].toInt()) diff --git a/app/src/main/java/org/tasks/preferences/fragments/DashClock.kt b/app/src/main/java/org/tasks/preferences/fragments/DashClock.kt index 60b2e5759..39b67f636 100644 --- a/app/src/main/java/org/tasks/preferences/fragments/DashClock.kt +++ b/app/src/main/java/org/tasks/preferences/fragments/DashClock.kt @@ -52,7 +52,7 @@ class DashClock : InjectingPreferenceFragment() { } private fun refreshPreferences() { - val filter = defaultFilterProvider.getFilterFromPreference(R.string.p_dashclock_filter) + val filter = defaultFilterProvider.getFilterFromPreferenceBlocking(R.string.p_dashclock_filter) findPreference(R.string.p_dashclock_filter).summary = filter?.listingTitle } } \ No newline at end of file diff --git a/app/src/main/java/org/tasks/preferences/fragments/ScrollableWidget.kt b/app/src/main/java/org/tasks/preferences/fragments/ScrollableWidget.kt index be848330b..5b453fcf1 100644 --- a/app/src/main/java/org/tasks/preferences/fragments/ScrollableWidget.kt +++ b/app/src/main/java/org/tasks/preferences/fragments/ScrollableWidget.kt @@ -171,7 +171,7 @@ class ScrollableWidget : InjectingPreferenceFragment() { } private fun getFilter(): Filter? { - return defaultFilterProvider.getFilterFromPreference(widgetPreferences.filterId) + return defaultFilterProvider.getFilterFromPreferenceBlocking(widgetPreferences.filterId) } private fun setupSlider(resId: Int, defValue: Int): SeekBarPreference { diff --git a/app/src/main/java/org/tasks/preferences/fragments/TaskerListNotification.kt b/app/src/main/java/org/tasks/preferences/fragments/TaskerListNotification.kt index d8062729c..f8b02c9f7 100644 --- a/app/src/main/java/org/tasks/preferences/fragments/TaskerListNotification.kt +++ b/app/src/main/java/org/tasks/preferences/fragments/TaskerListNotification.kt @@ -41,7 +41,7 @@ class TaskerListNotification : InjectingPreferenceFragment() { override fun setupPreferences(savedInstanceState: Bundle?) { filter = if (savedInstanceState == null) { - defaultFilterProvider.getFilterFromPreference(arguments?.getString(EXTRA_FILTER)) + defaultFilterProvider.getFilterFromPreferenceBlocking(arguments?.getString(EXTRA_FILTER)) } else { savedInstanceState.getParcelable(EXTRA_FILTER)!! } diff --git a/app/src/main/java/org/tasks/preferences/fragments/Widgets.kt b/app/src/main/java/org/tasks/preferences/fragments/Widgets.kt index 8ecbea35d..1d156d684 100644 --- a/app/src/main/java/org/tasks/preferences/fragments/Widgets.kt +++ b/app/src/main/java/org/tasks/preferences/fragments/Widgets.kt @@ -32,7 +32,7 @@ class Widgets : InjectingPreferenceFragment() { appWidgetManager.widgetIds.forEach { val widgetPrefs = WidgetPreferences(context, preferences, it) val pref = Preference(context) - val filter = defaultFilterProvider.getFilterFromPreference(widgetPrefs.filterId) + val filter = defaultFilterProvider.getFilterFromPreferenceBlocking(widgetPrefs.filterId) pref.title = filter?.listingTitle pref.summary = getString(R.string.widget_id, it) val intent = Intent(context, WidgetConfigActivity::class.java) diff --git a/app/src/main/java/org/tasks/widget/ScrollableViewsFactory.java b/app/src/main/java/org/tasks/widget/ScrollableViewsFactory.java index 0ab858437..4a9e15f67 100644 --- a/app/src/main/java/org/tasks/widget/ScrollableViewsFactory.java +++ b/app/src/main/java/org/tasks/widget/ScrollableViewsFactory.java @@ -307,7 +307,7 @@ class ScrollableViewsFactory implements RemoteViewsService.RemoteViewsFactory { showCheckboxes = widgetPreferences.showCheckboxes(); textSize = widgetPreferences.getFontSize(); dueDateTextSize = Math.max(10, textSize - 2); - filter = defaultFilterProvider.getFilterFromPreference(widgetPreferences.getFilterId()); + filter = defaultFilterProvider.getFilterFromPreferenceBlocking(widgetPreferences.getFilterId()); showDividers = widgetPreferences.showDividers(); showSubtasks = widgetPreferences.showSubtasks(); isRtl = locale.getDirectionality() == View.LAYOUT_DIRECTION_RTL; diff --git a/app/src/main/java/org/tasks/widget/TasksWidget.java b/app/src/main/java/org/tasks/widget/TasksWidget.java index e63af5f5c..11677646d 100644 --- a/app/src/main/java/org/tasks/widget/TasksWidget.java +++ b/app/src/main/java/org/tasks/widget/TasksWidget.java @@ -88,7 +88,7 @@ public class TasksWidget extends AppWidgetProvider { "setBackgroundColor", ColorUtils.setAlphaComponent(bgColor, widgetPreferences.getFooterOpacity())); - Filter filter = defaultFilterProvider.getFilterFromPreference(filterId); + Filter filter = defaultFilterProvider.getFilterFromPreferenceBlocking(filterId); remoteViews.setTextViewText(R.id.widget_title, filter.listingTitle); Uri cacheBuster = Uri.parse("tasks://widget/" + System.currentTimeMillis());