Use coroutines in MainActivity

pull/1043/head
Alex Baker 4 years ago
parent a4c3023c1a
commit 236f9ae0e0

@ -22,16 +22,12 @@ import com.todoroo.andlib.utility.AndroidUtilities
import com.todoroo.astrid.activity.TaskEditFragment.TaskEditFragmentCallbackHandler import com.todoroo.astrid.activity.TaskEditFragment.TaskEditFragmentCallbackHandler
import com.todoroo.astrid.activity.TaskListFragment.TaskListFragmentCallbackHandler import com.todoroo.astrid.activity.TaskListFragment.TaskListFragmentCallbackHandler
import com.todoroo.astrid.api.Filter 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.data.Task
import com.todoroo.astrid.service.TaskCreator import com.todoroo.astrid.service.TaskCreator
import com.todoroo.astrid.timers.TimerControlSet.TimerControlSetCallback import com.todoroo.astrid.timers.TimerControlSet.TimerControlSetCallback
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import io.reactivex.Single import kotlinx.coroutines.launch
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.schedulers.Schedulers
import org.tasks.BuildConfig
import org.tasks.LocalBroadcastManager import org.tasks.LocalBroadcastManager
import org.tasks.R import org.tasks.R
import org.tasks.activities.TagSettingsActivity import org.tasks.activities.TagSettingsActivity
@ -65,13 +61,12 @@ class MainActivity : InjectingAppCompatActivity(), TaskListFragmentCallbackHandl
@Inject lateinit var repeatConfirmationReceiver: RepeatConfirmationReceiver @Inject lateinit var repeatConfirmationReceiver: RepeatConfirmationReceiver
@Inject lateinit var defaultFilterProvider: DefaultFilterProvider @Inject lateinit var defaultFilterProvider: DefaultFilterProvider
@Inject lateinit var theme: Theme @Inject lateinit var theme: Theme
@Inject lateinit var taskDao: TaskDaoBlocking @Inject lateinit var taskDao: TaskDao
@Inject lateinit var localBroadcastManager: LocalBroadcastManager @Inject lateinit var localBroadcastManager: LocalBroadcastManager
@Inject lateinit var taskCreator: TaskCreator @Inject lateinit var taskCreator: TaskCreator
@Inject lateinit var playServices: PlayServices @Inject lateinit var playServices: PlayServices
@Inject lateinit var inventory: Inventory @Inject lateinit var inventory: Inventory
@Inject lateinit var colorProvider: ColorProvider @Inject lateinit var colorProvider: ColorProvider
private var disposables: CompositeDisposable? = null
private lateinit var navigationDrawer: NavigationDrawerFragment private lateinit var navigationDrawer: NavigationDrawerFragment
private var currentNightMode = 0 private var currentNightMode = 0
private var currentPro = false private var currentPro = false
@ -176,23 +171,18 @@ class MainActivity : InjectingAppCompatActivity(), TaskListFragmentCallbackHandl
tef.save() tef.save()
} }
if (loadFilter || !openFilter && filter == null) { if (loadFilter || !openFilter && filter == null) {
disposables!!.add( lifecycleScope.launch {
Single.fromCallable { val filterString = intent.getStringExtra(LOAD_FILTER)
val filter = intent.getStringExtra(LOAD_FILTER) intent.removeExtra(LOAD_FILTER)
intent.removeExtra(LOAD_FILTER) val filter = if (filterString.isNullOrBlank()) {
if (filter.isNullOrBlank()) { defaultFilterProvider.getStartupFilter()
defaultFilterProvider.startupFilter } else {
} else { defaultFilterProvider.getFilterFromPreference(filterString)
defaultFilterProvider.getFilterFromPreference(filter) }
} clearUi()
} openTaskListFragment(filter)
.subscribeOn(Schedulers.io()) openTask(filter)
.observeOn(AndroidSchedulers.mainThread()) }
.subscribe { filter: Filter? ->
clearUi()
openTaskListFragment(filter)
openTask(filter)
})
} else if (openFilter) { } else if (openFilter) {
val filter: Filter? = intent.getParcelableExtra(OPEN_FILTER) val filter: Filter? = intent.getParcelableExtra(OPEN_FILTER)
intent.removeExtra(OPEN_FILTER) intent.removeExtra(OPEN_FILTER)
@ -279,8 +269,6 @@ class MainActivity : InjectingAppCompatActivity(), TaskListFragmentCallbackHandl
return return
} }
localBroadcastManager.registerRepeatReceiver(repeatConfirmationReceiver) 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_just_updated, false)) {
if (preferences.getBoolean(R.string.p_show_whats_new, true)) { if (preferences.getBoolean(R.string.p_show_whats_new, true)) {
val fragmentManager = supportFragmentManager val fragmentManager = supportFragmentManager
@ -303,7 +291,6 @@ class MainActivity : InjectingAppCompatActivity(), TaskListFragmentCallbackHandl
override fun onPause() { override fun onPause() {
super.onPause() super.onPause()
localBroadcastManager.unregisterReceiver(repeatConfirmationReceiver) localBroadcastManager.unregisterReceiver(repeatConfirmationReceiver)
disposables?.dispose()
} }
override fun onTaskListItemClicked(task: Task?) { override fun onTaskListItemClicked(task: Task?) {
@ -317,11 +304,9 @@ class MainActivity : InjectingAppCompatActivity(), TaskListFragmentCallbackHandl
if (task.isNew) { if (task.isNew) {
openTask(task) openTask(task)
} else { } else {
disposables!!.add( lifecycleScope.launch {
Single.fromCallable { taskDao.fetchBlocking(task.id) } openTask(taskDao.fetch(task.id))
.subscribeOn(Schedulers.io()) }
.observeOn(AndroidSchedulers.mainThread())
.subscribe { t: Task? -> this.openTask(t) })
} }
} }

@ -54,7 +54,7 @@ public class DashClockExtension extends com.google.android.apps.dashclock.api.Da
private void refresh() { private void refresh() {
final String filterPreference = preferences.getStringValue(R.string.p_dashclock_filter); 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); int count = taskDao.count(filter);

@ -25,10 +25,6 @@ class FilterDaoBlocking @Inject constructor(private val dao: FilterDao) {
dao.getFilters() dao.getFilters()
} }
fun getById(id: Long): Filter? = runBlocking {
dao.getById(id)
}
fun getAll(): List<Filter> = runBlocking { fun getAll(): List<Filter> = runBlocking {
dao.getAll() dao.getAll()
} }

@ -31,7 +31,7 @@ public class TaskerIntentService extends InjectingJobIntentService {
if (ListNotificationBundle.isBundleValid(bundle)) { if (ListNotificationBundle.isBundleValid(bundle)) {
Filter filter = Filter filter =
defaultFilterProvider.getFilterFromPreference( defaultFilterProvider.getFilterFromPreferenceBlocking(
bundle.getString(ListNotificationBundle.BUNDLE_EXTRA_STRING_FILTER)); bundle.getString(ListNotificationBundle.BUNDLE_EXTRA_STRING_FILTER));
notifier.triggerFilterNotification(filter); notifier.triggerFilterNotification(filter);
} else if (TaskCreationBundle.isBundleValid(bundle)) { } else if (TaskCreationBundle.isBundleValid(bundle)) {

@ -6,6 +6,7 @@ import com.todoroo.astrid.api.Filter
import com.todoroo.astrid.core.BuiltInFilterExposer import com.todoroo.astrid.core.BuiltInFilterExposer
import com.todoroo.astrid.core.BuiltInFilterExposer.getMyTasksFilter import com.todoroo.astrid.core.BuiltInFilterExposer.getMyTasksFilter
import dagger.hilt.android.qualifiers.ApplicationContext import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.runBlocking
import org.tasks.R import org.tasks.R
import org.tasks.Strings.isNullOrEmpty import org.tasks.Strings.isNullOrEmpty
import org.tasks.data.* import org.tasks.data.*
@ -16,62 +17,88 @@ import javax.inject.Inject
class DefaultFilterProvider @Inject constructor( class DefaultFilterProvider @Inject constructor(
@param:ApplicationContext private val context: Context, @param:ApplicationContext private val context: Context,
private val preferences: Preferences, private val preferences: Preferences,
private val filterDao: FilterDaoBlocking, private val filterDao: FilterDao,
private val tagDataDao: TagDataDaoBlocking, private val tagDataDao: TagDataDao,
private val googleTaskListDao: GoogleTaskListDaoBlocking, private val googleTaskListDao: GoogleTaskListDao,
private val caldavDao: CaldavDaoBlocking, private val caldavDao: CaldavDao,
private val locationDao: LocationDaoBlocking) { private val locationDao: LocationDao) {
@Deprecated("use coroutines")
var dashclockFilter: Filter 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) set(filter) = setFilterPreference(filter, R.string.p_dashclock_filter)
@Deprecated("use coroutines")
var badgeFilter: Filter 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) set(filter) = setFilterPreference(filter, R.string.p_badge_list)
@Deprecated("use coroutines")
var defaultOpenFilter: Filter var defaultOpenFilter: Filter
get() = getFilterFromPreference(R.string.p_default_open_filter) get() = runBlocking { getDefaultOpenFilter() }
set(filter) = setFilterPreference(filter, R.string.p_default_open_filter) set(filter) = setFilterPreference(filter, R.string.p_default_open_filter)
@Deprecated("use coroutines")
var lastViewedFilter: Filter 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) set(filter) = setFilterPreference(filter, R.string.p_last_viewed_list)
@Deprecated("use coroutines")
var defaultList: Filter 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) set(filter) = setFilterPreference(filter, R.string.p_default_list)
@Deprecated("use coroutines")
val startupFilter: Filter val startupFilter: Filter
get() { get() = runBlocking { getStartupFilter() }
return if (preferences.getBoolean(R.string.p_open_last_viewed_list, true)) {
lastViewedFilter suspend fun getDefaultList() =
} else { getFilterFromPreference(preferences.getStringValue(R.string.p_default_list), null)
defaultOpenFilter ?: 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)) getFilterFromPreference(preferences.getStringValue(resId))
fun getFilterFromPreference(prefString: String?): Filter = suspend fun getFilterFromPreference(prefString: String?): Filter =
getFilterFromPreference(prefString, getMyTasksFilter(context.resources))!! getFilterFromPreference(prefString, getMyTasksFilter(context.resources))!!
private fun getAnyList(): Filter { private suspend fun getAnyList(): Filter {
val filter = googleTaskListDao.getAllLists().getOrNull(0)?.let(::GtasksFilter) val filter = googleTaskListDao.getAllLists().getOrNull(0)?.let(::GtasksFilter)
?: caldavDao.getCalendars().getOrElse(0) { caldavDao.getLocalList(context) }.let(::CaldavFilter) ?: caldavDao.getCalendars().getOrElse(0) { caldavDao.getLocalList(context) }.let(::CaldavFilter)
defaultList = filter defaultList = filter
return filter return filter
} }
private fun getFilterFromPreference(preferenceValue: String?, def: Filter?) = try { private suspend fun getFilterFromPreference(preferenceValue: String?, def: Filter?) = try {
preferenceValue?.let(this::loadFilter) ?: def preferenceValue?.let { loadFilter(it) } ?: def
} catch (e: Exception) { } catch (e: Exception) {
Timber.e(e) Timber.e(e)
def def
} }
private fun loadFilter(preferenceValue: String): Filter? { private suspend fun loadFilter(preferenceValue: String): Filter? {
val split = preferenceValue.split(":") val split = preferenceValue.split(":")
return when (split[0].toInt()) { return when (split[0].toInt()) {
TYPE_FILTER -> getBuiltInFilter(split[1].toInt()) TYPE_FILTER -> getBuiltInFilter(split[1].toInt())

@ -52,7 +52,7 @@ class DashClock : InjectingPreferenceFragment() {
} }
private fun refreshPreferences() { 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 findPreference(R.string.p_dashclock_filter).summary = filter?.listingTitle
} }
} }

@ -171,7 +171,7 @@ class ScrollableWidget : InjectingPreferenceFragment() {
} }
private fun getFilter(): Filter? { private fun getFilter(): Filter? {
return defaultFilterProvider.getFilterFromPreference(widgetPreferences.filterId) return defaultFilterProvider.getFilterFromPreferenceBlocking(widgetPreferences.filterId)
} }
private fun setupSlider(resId: Int, defValue: Int): SeekBarPreference { private fun setupSlider(resId: Int, defValue: Int): SeekBarPreference {

@ -41,7 +41,7 @@ class TaskerListNotification : InjectingPreferenceFragment() {
override fun setupPreferences(savedInstanceState: Bundle?) { override fun setupPreferences(savedInstanceState: Bundle?) {
filter = if (savedInstanceState == null) { filter = if (savedInstanceState == null) {
defaultFilterProvider.getFilterFromPreference(arguments?.getString(EXTRA_FILTER)) defaultFilterProvider.getFilterFromPreferenceBlocking(arguments?.getString(EXTRA_FILTER))
} else { } else {
savedInstanceState.getParcelable(EXTRA_FILTER)!! savedInstanceState.getParcelable(EXTRA_FILTER)!!
} }

@ -32,7 +32,7 @@ class Widgets : InjectingPreferenceFragment() {
appWidgetManager.widgetIds.forEach { appWidgetManager.widgetIds.forEach {
val widgetPrefs = WidgetPreferences(context, preferences, it) val widgetPrefs = WidgetPreferences(context, preferences, it)
val pref = Preference(context) val pref = Preference(context)
val filter = defaultFilterProvider.getFilterFromPreference(widgetPrefs.filterId) val filter = defaultFilterProvider.getFilterFromPreferenceBlocking(widgetPrefs.filterId)
pref.title = filter?.listingTitle pref.title = filter?.listingTitle
pref.summary = getString(R.string.widget_id, it) pref.summary = getString(R.string.widget_id, it)
val intent = Intent(context, WidgetConfigActivity::class.java) val intent = Intent(context, WidgetConfigActivity::class.java)

@ -307,7 +307,7 @@ class ScrollableViewsFactory implements RemoteViewsService.RemoteViewsFactory {
showCheckboxes = widgetPreferences.showCheckboxes(); showCheckboxes = widgetPreferences.showCheckboxes();
textSize = widgetPreferences.getFontSize(); textSize = widgetPreferences.getFontSize();
dueDateTextSize = Math.max(10, textSize - 2); dueDateTextSize = Math.max(10, textSize - 2);
filter = defaultFilterProvider.getFilterFromPreference(widgetPreferences.getFilterId()); filter = defaultFilterProvider.getFilterFromPreferenceBlocking(widgetPreferences.getFilterId());
showDividers = widgetPreferences.showDividers(); showDividers = widgetPreferences.showDividers();
showSubtasks = widgetPreferences.showSubtasks(); showSubtasks = widgetPreferences.showSubtasks();
isRtl = locale.getDirectionality() == View.LAYOUT_DIRECTION_RTL; isRtl = locale.getDirectionality() == View.LAYOUT_DIRECTION_RTL;

@ -88,7 +88,7 @@ public class TasksWidget extends AppWidgetProvider {
"setBackgroundColor", "setBackgroundColor",
ColorUtils.setAlphaComponent(bgColor, widgetPreferences.getFooterOpacity())); ColorUtils.setAlphaComponent(bgColor, widgetPreferences.getFooterOpacity()));
Filter filter = defaultFilterProvider.getFilterFromPreference(filterId); Filter filter = defaultFilterProvider.getFilterFromPreferenceBlocking(filterId);
remoteViews.setTextViewText(R.id.widget_title, filter.listingTitle); remoteViews.setTextViewText(R.id.widget_title, filter.listingTitle);
Uri cacheBuster = Uri.parse("tasks://widget/" + System.currentTimeMillis()); Uri cacheBuster = Uri.parse("tasks://widget/" + System.currentTimeMillis());

Loading…
Cancel
Save