mirror of https://github.com/tasks/tasks
Replace refresh work with coroutines
parent
dc90e583e4
commit
7fb85b6da1
@ -1,25 +0,0 @@
|
||||
package org.tasks.jobs
|
||||
|
||||
import android.content.Context
|
||||
import androidx.hilt.work.HiltWorker
|
||||
import androidx.work.WorkerParameters
|
||||
import dagger.assisted.Assisted
|
||||
import dagger.assisted.AssistedInject
|
||||
import org.tasks.LocalBroadcastManager
|
||||
import org.tasks.analytics.Firebase
|
||||
|
||||
@HiltWorker
|
||||
class MidnightRefreshWork @AssistedInject constructor(
|
||||
@Assisted context: Context,
|
||||
@Assisted workerParams: WorkerParameters,
|
||||
firebase: Firebase,
|
||||
private val workManager: WorkManager,
|
||||
private val localBroadcastManager: LocalBroadcastManager) : RepeatingWorker(context, workerParams, firebase) {
|
||||
|
||||
override suspend fun run(): Result {
|
||||
localBroadcastManager.broadcastRefresh()
|
||||
return Result.success()
|
||||
}
|
||||
|
||||
override suspend fun scheduleNext() = workManager.scheduleMidnightRefresh()
|
||||
}
|
@ -1,26 +0,0 @@
|
||||
package org.tasks.jobs
|
||||
|
||||
import android.content.Context
|
||||
import androidx.hilt.work.HiltWorker
|
||||
import androidx.work.WorkerParameters
|
||||
import dagger.assisted.Assisted
|
||||
import dagger.assisted.AssistedInject
|
||||
import org.tasks.LocalBroadcastManager
|
||||
import org.tasks.analytics.Firebase
|
||||
import org.tasks.scheduling.RefreshScheduler
|
||||
|
||||
@HiltWorker
|
||||
class RefreshWork @AssistedInject constructor(
|
||||
@Assisted context: Context,
|
||||
@Assisted workerParams: WorkerParameters,
|
||||
firebase: Firebase,
|
||||
private val refreshScheduler: RefreshScheduler,
|
||||
private val localBroadcastManager: LocalBroadcastManager) : RepeatingWorker(context, workerParams, firebase) {
|
||||
|
||||
override suspend fun run(): Result {
|
||||
localBroadcastManager.broadcastRefresh()
|
||||
return Result.success()
|
||||
}
|
||||
|
||||
override suspend fun scheduleNext() = refreshScheduler.scheduleNext()
|
||||
}
|
@ -1,56 +0,0 @@
|
||||
package org.tasks.scheduling
|
||||
|
||||
import com.todoroo.astrid.data.Task
|
||||
import org.tasks.data.TaskDao
|
||||
import org.tasks.jobs.WorkManager
|
||||
import org.tasks.time.DateTimeUtils
|
||||
import java.util.SortedSet
|
||||
import java.util.TreeSet
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Singleton
|
||||
class RefreshScheduler @Inject internal constructor(
|
||||
private val workManager: WorkManager,
|
||||
private val taskDao: TaskDao,
|
||||
) {
|
||||
|
||||
private val jobs: SortedSet<Long> = TreeSet()
|
||||
|
||||
suspend fun scheduleAll() {
|
||||
for (task in taskDao.needsRefresh()) {
|
||||
scheduleRefresh(task)
|
||||
}
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
fun scheduleRefresh(task: Task) {
|
||||
if (task.hasDueDate()) {
|
||||
scheduleRefresh(task.dueDate)
|
||||
}
|
||||
if (task.hasStartDate()) {
|
||||
scheduleRefresh(task.hideUntil)
|
||||
}
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
fun scheduleNext() {
|
||||
val lapsed = jobs.headSet(DateTimeUtils.currentTimeMillis() + 1).toList()
|
||||
jobs.removeAll(lapsed)
|
||||
if (!jobs.isEmpty()) {
|
||||
workManager.scheduleRefresh(jobs.first())
|
||||
}
|
||||
}
|
||||
|
||||
private fun scheduleRefresh(timestamp: Long) {
|
||||
val now = DateTimeUtils.currentTimeMillis()
|
||||
if (now < timestamp) {
|
||||
val upcoming = jobs.tailSet(now)
|
||||
val reschedule = upcoming.isEmpty() || timestamp < upcoming.first()
|
||||
jobs.add(timestamp)
|
||||
if (reschedule) {
|
||||
scheduleNext()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue