From 789836058dc1cb1fa4a4c7e5abc62affdaaf078b Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Tue, 28 Jul 2020 10:23:33 -0500 Subject: [PATCH] Use coroutines in geofence api --- .../astrid/reminders/ReminderServiceTest.kt | 4 ++-- .../java/org/tasks/location/GeofenceApi.kt | 13 ++++++------ app/src/main/java/org/tasks/Tasks.kt | 21 ++++++++++++------- 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/app/src/androidTest/java/com/todoroo/astrid/reminders/ReminderServiceTest.kt b/app/src/androidTest/java/com/todoroo/astrid/reminders/ReminderServiceTest.kt index ee8c68f42..2a723382f 100644 --- a/app/src/androidTest/java/com/todoroo/astrid/reminders/ReminderServiceTest.kt +++ b/app/src/androidTest/java/com/todoroo/astrid/reminders/ReminderServiceTest.kt @@ -2,7 +2,7 @@ package com.todoroo.astrid.reminders import com.natpryce.makeiteasy.MakeItEasy.with import com.todoroo.andlib.utility.DateUtilities -import com.todoroo.astrid.dao.TaskDaoBlocking +import com.todoroo.astrid.dao.TaskDao import com.todoroo.astrid.data.Task import dagger.hilt.android.testing.HiltAndroidTest import dagger.hilt.android.testing.UninstallModules @@ -38,7 +38,7 @@ import javax.inject.Inject @HiltAndroidTest class ReminderServiceTest : InjectingTestCase() { @Inject lateinit var preferences: Preferences - @Inject lateinit var taskDao: TaskDaoBlocking + @Inject lateinit var taskDao: TaskDao @Inject lateinit var jobs: NotificationQueue private lateinit var service: ReminderService diff --git a/app/src/googleplay/java/org/tasks/location/GeofenceApi.kt b/app/src/googleplay/java/org/tasks/location/GeofenceApi.kt index 954da9656..b262e4511 100644 --- a/app/src/googleplay/java/org/tasks/location/GeofenceApi.kt +++ b/app/src/googleplay/java/org/tasks/location/GeofenceApi.kt @@ -8,7 +8,7 @@ import com.google.android.gms.location.Geofence import com.google.android.gms.location.GeofencingRequest import com.google.android.gms.location.LocationServices import dagger.hilt.android.qualifiers.ApplicationContext -import org.tasks.data.LocationDaoBlocking +import org.tasks.data.LocationDao import org.tasks.data.MergedGeofence import org.tasks.data.Place import org.tasks.preferences.PermissionChecker @@ -18,16 +18,16 @@ import javax.inject.Inject class GeofenceApi @Inject constructor( @param:ApplicationContext private val context: Context, private val permissionChecker: PermissionChecker, - private val locationDao: LocationDaoBlocking) { + private val locationDao: LocationDao) { - fun registerAll() = locationDao.getPlacesWithGeofences().forEach(this::update) + suspend fun registerAll() = locationDao.getPlacesWithGeofences().forEach { update(it) } - fun update(taskId: Long) = locationDao.getPlaceForTask(taskId).apply(this::update) + suspend fun update(taskId: Long) = update(locationDao.getPlaceForTask(taskId)) - fun update(place: String) = locationDao.getPlace(place).apply(this::update) + suspend fun update(place: String) = update(locationDao.getPlace(place)) @SuppressLint("MissingPermission") - fun update(place: Place?) { + suspend fun update(place: Place?) { if (place == null || !permissionChecker.canAccessLocation()) { return } @@ -63,5 +63,4 @@ class GeofenceApi @Inject constructor( .setExpirationDuration(Geofence.NEVER_EXPIRE) .build() } - } \ No newline at end of file diff --git a/app/src/main/java/org/tasks/Tasks.kt b/app/src/main/java/org/tasks/Tasks.kt index cb43df620..ecf0f2f9b 100644 --- a/app/src/main/java/org/tasks/Tasks.kt +++ b/app/src/main/java/org/tasks/Tasks.kt @@ -12,8 +12,10 @@ import com.todoroo.astrid.service.Upgrader import dagger.Lazy import dagger.hilt.android.HiltAndroidApp import dagger.hilt.android.qualifiers.ApplicationContext -import io.reactivex.Completable -import io.reactivex.schedulers.Schedulers +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.SupervisorJob +import kotlinx.coroutines.launch import org.tasks.billing.BillingClient import org.tasks.billing.Inventory import org.tasks.files.FileHelper @@ -33,6 +35,7 @@ import javax.inject.Inject @HiltAndroidApp class Tasks : Application(), Configuration.Provider { + @Inject @ApplicationContext lateinit var context: Context @Inject lateinit var preferences: Preferences @Inject lateinit var buildSetup: BuildSetup @@ -54,7 +57,7 @@ class Tasks : Application(), Configuration.Provider { ThemeBase.getThemeBase(preferences, inventory, null).setDefaultNightMode() localBroadcastManager.registerRefreshReceiver(RefreshBroadcastReceiver()) Locale.getInstance(this).createConfigurationContext(applicationContext) - Completable.fromAction { doInBackground() }.subscribeOn(Schedulers.io()).subscribe() + backgroundWork() } private fun upgrade() { @@ -69,14 +72,16 @@ class Tasks : Application(), Configuration.Provider { } } - private fun doInBackground() { + private fun backgroundWork() = CoroutineScope(Dispatchers.IO + SupervisorJob()).launch { NotificationSchedulerIntentService.enqueueWork(context, false) CalendarNotificationIntentService.enqueueWork(context) refreshScheduler.get().scheduleAll() - workManager.get().updateBackgroundSync() - workManager.get().scheduleMidnightRefresh() - workManager.get().scheduleBackup() - workManager.get().scheduleConfigRefresh() + workManager.get().apply { + updateBackgroundSync() + scheduleMidnightRefresh() + scheduleBackup() + scheduleConfigRefresh() + } geofenceApi.get().registerAll() FileHelper.delete(context, preferences.cacheDirectory) billingClient.get().queryPurchases()