From b44150c7f3e30fa8857b049b9030ea51886464e8 Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Tue, 28 Jul 2020 10:29:56 -0500 Subject: [PATCH] Convert RefreshScheduler to Kotlin --- .../tasks/scheduling/RefreshScheduler.java | 70 ------------------- .../org/tasks/scheduling/RefreshScheduler.kt | 63 +++++++++++++++++ 2 files changed, 63 insertions(+), 70 deletions(-) delete mode 100644 app/src/main/java/org/tasks/scheduling/RefreshScheduler.java create mode 100644 app/src/main/java/org/tasks/scheduling/RefreshScheduler.kt diff --git a/app/src/main/java/org/tasks/scheduling/RefreshScheduler.java b/app/src/main/java/org/tasks/scheduling/RefreshScheduler.java deleted file mode 100644 index 6f988d433..000000000 --- a/app/src/main/java/org/tasks/scheduling/RefreshScheduler.java +++ /dev/null @@ -1,70 +0,0 @@ -package org.tasks.scheduling; - -import static com.todoroo.andlib.utility.DateUtilities.ONE_MINUTE; -import static org.tasks.time.DateTimeUtils.currentTimeMillis; - -import com.google.common.collect.ImmutableList; -import com.todoroo.astrid.dao.TaskDaoBlocking; -import com.todoroo.astrid.data.Task; -import java.util.List; -import java.util.SortedSet; -import java.util.TreeSet; -import javax.inject.Inject; -import javax.inject.Singleton; -import org.tasks.R; -import org.tasks.jobs.WorkManager; -import org.tasks.preferences.Preferences; - -@Singleton -public class RefreshScheduler { - - private final Preferences preferences; - private final WorkManager workManager; - private final TaskDaoBlocking taskDao; - private final SortedSet jobs = new TreeSet<>(); - - @Inject - RefreshScheduler(Preferences preferences, WorkManager workManager, TaskDaoBlocking taskDao) { - this.preferences = preferences; - this.workManager = workManager; - this.taskDao = taskDao; - } - - public synchronized void scheduleAll() { - for (Task task : taskDao.needsRefresh()) { - scheduleRefresh(task); - } - } - - public synchronized void scheduleRefresh(Task task) { - if (task.isCompleted() - && preferences.getBoolean(R.string.p_temporarily_show_completed_tasks, false)) { - scheduleRefresh(task.getCompletionDate() + ONE_MINUTE); - } else if (task.hasDueDate()) { - scheduleRefresh(task.getDueDate()); - } - if (task.hasHideUntilDate()) { - scheduleRefresh(task.getHideUntil()); - } - } - - private void scheduleRefresh(Long timestamp) { - long now = currentTimeMillis(); - if (now < timestamp) { - SortedSet upcoming = jobs.tailSet(now); - boolean reschedule = upcoming.isEmpty() || timestamp < upcoming.first(); - jobs.add(timestamp); - if (reschedule) { - scheduleNext(); - } - } - } - - public synchronized void scheduleNext() { - List lapsed = ImmutableList.copyOf(jobs.headSet(currentTimeMillis() + 1)); - jobs.removeAll(lapsed); - if (!jobs.isEmpty()) { - workManager.scheduleRefresh(jobs.first()); - } - } -} diff --git a/app/src/main/java/org/tasks/scheduling/RefreshScheduler.kt b/app/src/main/java/org/tasks/scheduling/RefreshScheduler.kt new file mode 100644 index 000000000..2ea17e4c6 --- /dev/null +++ b/app/src/main/java/org/tasks/scheduling/RefreshScheduler.kt @@ -0,0 +1,63 @@ +package org.tasks.scheduling + +import com.todoroo.andlib.utility.DateUtilities +import com.todoroo.astrid.dao.TaskDao +import com.todoroo.astrid.data.Task +import kotlinx.collections.immutable.toImmutableList +import org.tasks.R +import org.tasks.jobs.WorkManager +import org.tasks.preferences.Preferences +import org.tasks.time.DateTimeUtils +import java.util.* +import javax.inject.Inject +import javax.inject.Singleton + +@Singleton +class RefreshScheduler @Inject internal constructor( + private val preferences: Preferences, + private val workManager: WorkManager, + private val taskDao: TaskDao) { + + private val jobs: SortedSet = TreeSet() + + @Synchronized + suspend fun scheduleAll() { + for (task in taskDao.needsRefresh()) { + scheduleRefresh(task) + } + } + + @Synchronized + fun scheduleRefresh(task: Task) { + if (task.isCompleted + && preferences.getBoolean(R.string.p_temporarily_show_completed_tasks, false)) { + scheduleRefresh(task.completionDate + DateUtilities.ONE_MINUTE) + } else if (task.hasDueDate()) { + scheduleRefresh(task.dueDate) + } + if (task.hasHideUntilDate()) { + scheduleRefresh(task.hideUntil) + } + } + + @Synchronized + fun scheduleNext() { + val lapsed = jobs.headSet(DateTimeUtils.currentTimeMillis() + 1).toImmutableList() + 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() + } + } + } +} \ No newline at end of file