mirror of https://github.com/tasks/tasks
Use coroutines in injecting service
parent
8a30fde2f2
commit
e2dffbe71a
@ -1,57 +0,0 @@
|
||||
package org.tasks.jobs;
|
||||
|
||||
import static com.google.common.collect.Lists.transform;
|
||||
import static com.todoroo.andlib.utility.AndroidUtilities.assertNotMainThread;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.os.IBinder;
|
||||
import androidx.annotation.Nullable;
|
||||
import dagger.hilt.android.AndroidEntryPoint;
|
||||
import java.util.List;
|
||||
import javax.inject.Inject;
|
||||
import org.tasks.Notifier;
|
||||
import org.tasks.R;
|
||||
import org.tasks.injection.InjectingService;
|
||||
import org.tasks.preferences.Preferences;
|
||||
|
||||
@AndroidEntryPoint
|
||||
public class NotificationService extends InjectingService {
|
||||
|
||||
@Inject Preferences preferences;
|
||||
@Inject Notifier notifier;
|
||||
@Inject NotificationQueue notificationQueue;
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getNotificationId() {
|
||||
return -1;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getNotificationBody() {
|
||||
return R.string.building_notifications;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected synchronized void doWork() {
|
||||
assertNotMainThread();
|
||||
|
||||
if (!preferences.isCurrentlyQuietHours()) {
|
||||
List<? extends NotificationQueueEntry> overdueJobs = notificationQueue.getOverdueJobs();
|
||||
if (!notificationQueue.remove(overdueJobs)) {
|
||||
throw new RuntimeException("Failed to remove jobs from queue");
|
||||
}
|
||||
notifier.triggerNotifications(transform(overdueJobs, NotificationQueueEntry::toNotification));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void scheduleNext() {
|
||||
notificationQueue.scheduleNext();
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
package org.tasks.jobs
|
||||
|
||||
import android.content.Intent
|
||||
import android.os.IBinder
|
||||
import com.google.common.collect.Lists
|
||||
import com.todoroo.andlib.utility.AndroidUtilities
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import org.tasks.Notifier
|
||||
import org.tasks.R
|
||||
import org.tasks.injection.InjectingService
|
||||
import org.tasks.preferences.Preferences
|
||||
import javax.inject.Inject
|
||||
|
||||
@AndroidEntryPoint
|
||||
class NotificationService : InjectingService() {
|
||||
@Inject lateinit var preferences: Preferences
|
||||
@Inject lateinit var notifier: Notifier
|
||||
@Inject lateinit var notificationQueue: NotificationQueue
|
||||
|
||||
override fun onBind(intent: Intent): IBinder? = null
|
||||
|
||||
override val notificationId = -1
|
||||
|
||||
override val notificationBody = R.string.building_notifications
|
||||
|
||||
@Synchronized
|
||||
override suspend fun doWork() {
|
||||
AndroidUtilities.assertNotMainThread()
|
||||
if (!preferences.isCurrentlyQuietHours) {
|
||||
val overdueJobs = notificationQueue.overdueJobs
|
||||
if (!notificationQueue.remove(overdueJobs)) {
|
||||
throw RuntimeException("Failed to remove jobs from queue")
|
||||
}
|
||||
notifier.triggerNotifications(Lists.transform(overdueJobs) { obj: NotificationQueueEntry? -> obj!!.toNotification() })
|
||||
}
|
||||
}
|
||||
|
||||
override fun scheduleNext() = notificationQueue.scheduleNext()
|
||||
}
|
Loading…
Reference in New Issue