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.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))
}
}
}

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

@ -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<Filter> = runBlocking {
dao.getAll()
}

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

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

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

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

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

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

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

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

Loading…
Cancel
Save