Convert RefreshScheduler to Kotlin

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

@ -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<Long> 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<Long> upcoming = jobs.tailSet(now);
boolean reschedule = upcoming.isEmpty() || timestamp < upcoming.first();
jobs.add(timestamp);
if (reschedule) {
scheduleNext();
}
}
}
public synchronized void scheduleNext() {
List<Long> lapsed = ImmutableList.copyOf(jobs.headSet(currentTimeMillis() + 1));
jobs.removeAll(lapsed);
if (!jobs.isEmpty()) {
workManager.scheduleRefresh(jobs.first());
}
}
}

@ -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<Long> = 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()
}
}
}
}
Loading…
Cancel
Save