onStart method
Callback when this executor is started. Returns true if successfully started, false otherwise.
Implementation
@override
Future<bool> onStart() async {
final from = taskControl.hasBeenScheduledUntil ?? DateTime.now();
final to = from.add(const Duration(days: 10)); // look 10 days ahead
final schedule = triggerExecutor.getSchedule(from, to, 10);
if (schedule.isEmpty) {
// Stop since the schedule is empty and there is not more to schedule.
stop();
} else {
// Enqueue the first 6 (max) app tasks in the future
var remainingNotifications =
NotificationController.PENDING_NOTIFICATION_LIMIT -
(await SmartPhoneClientManager()
.notificationController
?.pendingNotificationRequestsCount ??
0);
remainingNotifications = min(remainingNotifications, 6);
Iterator<DateTime> it = schedule.iterator;
var count = 0;
DateTime current = DateTime.now();
while (it.moveNext() && count++ < remainingNotifications) {
current = it.current;
await AppTaskController().enqueue(
taskExecutor,
triggerTime: current,
);
}
// Save timestamp
taskControl.hasBeenScheduledUntil = current;
// Now stop since the schedule has all been enqueued.
stop();
// .. but start again when the scheduled time has passed.
// This in the case where the app keeps running in the background
var duration = current.millisecondsSinceEpoch -
DateTime.now().millisecondsSinceEpoch;
Timer(Duration(milliseconds: duration), () => start());
}
return true;
}