Use coroutines in geofence api

pull/1055/head
Alex Baker 4 years ago
parent 20353023d8
commit 789836058d

@ -2,7 +2,7 @@ package com.todoroo.astrid.reminders
import com.natpryce.makeiteasy.MakeItEasy.with import com.natpryce.makeiteasy.MakeItEasy.with
import com.todoroo.andlib.utility.DateUtilities 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 com.todoroo.astrid.data.Task
import dagger.hilt.android.testing.HiltAndroidTest import dagger.hilt.android.testing.HiltAndroidTest
import dagger.hilt.android.testing.UninstallModules import dagger.hilt.android.testing.UninstallModules
@ -38,7 +38,7 @@ import javax.inject.Inject
@HiltAndroidTest @HiltAndroidTest
class ReminderServiceTest : InjectingTestCase() { class ReminderServiceTest : InjectingTestCase() {
@Inject lateinit var preferences: Preferences @Inject lateinit var preferences: Preferences
@Inject lateinit var taskDao: TaskDaoBlocking @Inject lateinit var taskDao: TaskDao
@Inject lateinit var jobs: NotificationQueue @Inject lateinit var jobs: NotificationQueue
private lateinit var service: ReminderService private lateinit var service: ReminderService

@ -8,7 +8,7 @@ import com.google.android.gms.location.Geofence
import com.google.android.gms.location.GeofencingRequest import com.google.android.gms.location.GeofencingRequest
import com.google.android.gms.location.LocationServices import com.google.android.gms.location.LocationServices
import dagger.hilt.android.qualifiers.ApplicationContext 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.MergedGeofence
import org.tasks.data.Place import org.tasks.data.Place
import org.tasks.preferences.PermissionChecker import org.tasks.preferences.PermissionChecker
@ -18,16 +18,16 @@ import javax.inject.Inject
class GeofenceApi @Inject constructor( class GeofenceApi @Inject constructor(
@param:ApplicationContext private val context: Context, @param:ApplicationContext private val context: Context,
private val permissionChecker: PermissionChecker, 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") @SuppressLint("MissingPermission")
fun update(place: Place?) { suspend fun update(place: Place?) {
if (place == null || !permissionChecker.canAccessLocation()) { if (place == null || !permissionChecker.canAccessLocation()) {
return return
} }
@ -63,5 +63,4 @@ class GeofenceApi @Inject constructor(
.setExpirationDuration(Geofence.NEVER_EXPIRE) .setExpirationDuration(Geofence.NEVER_EXPIRE)
.build() .build()
} }
} }

@ -12,8 +12,10 @@ import com.todoroo.astrid.service.Upgrader
import dagger.Lazy import dagger.Lazy
import dagger.hilt.android.HiltAndroidApp import dagger.hilt.android.HiltAndroidApp
import dagger.hilt.android.qualifiers.ApplicationContext import dagger.hilt.android.qualifiers.ApplicationContext
import io.reactivex.Completable import kotlinx.coroutines.CoroutineScope
import io.reactivex.schedulers.Schedulers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.launch
import org.tasks.billing.BillingClient import org.tasks.billing.BillingClient
import org.tasks.billing.Inventory import org.tasks.billing.Inventory
import org.tasks.files.FileHelper import org.tasks.files.FileHelper
@ -33,6 +35,7 @@ import javax.inject.Inject
@HiltAndroidApp @HiltAndroidApp
class Tasks : Application(), Configuration.Provider { class Tasks : Application(), Configuration.Provider {
@Inject @ApplicationContext lateinit var context: Context @Inject @ApplicationContext lateinit var context: Context
@Inject lateinit var preferences: Preferences @Inject lateinit var preferences: Preferences
@Inject lateinit var buildSetup: BuildSetup @Inject lateinit var buildSetup: BuildSetup
@ -54,7 +57,7 @@ class Tasks : Application(), Configuration.Provider {
ThemeBase.getThemeBase(preferences, inventory, null).setDefaultNightMode() ThemeBase.getThemeBase(preferences, inventory, null).setDefaultNightMode()
localBroadcastManager.registerRefreshReceiver(RefreshBroadcastReceiver()) localBroadcastManager.registerRefreshReceiver(RefreshBroadcastReceiver())
Locale.getInstance(this).createConfigurationContext(applicationContext) Locale.getInstance(this).createConfigurationContext(applicationContext)
Completable.fromAction { doInBackground() }.subscribeOn(Schedulers.io()).subscribe() backgroundWork()
} }
private fun upgrade() { 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) NotificationSchedulerIntentService.enqueueWork(context, false)
CalendarNotificationIntentService.enqueueWork(context) CalendarNotificationIntentService.enqueueWork(context)
refreshScheduler.get().scheduleAll() refreshScheduler.get().scheduleAll()
workManager.get().updateBackgroundSync() workManager.get().apply {
workManager.get().scheduleMidnightRefresh() updateBackgroundSync()
workManager.get().scheduleBackup() scheduleMidnightRefresh()
workManager.get().scheduleConfigRefresh() scheduleBackup()
scheduleConfigRefresh()
}
geofenceApi.get().registerAll() geofenceApi.get().registerAll()
FileHelper.delete(context, preferences.cacheDirectory) FileHelper.delete(context, preferences.cacheDirectory)
billingClient.get().queryPurchases() billingClient.get().queryPurchases()

Loading…
Cancel
Save