Future Plans for KTX Scheduling Functions for Workers
Closed this issue · 0 comments
evilthreads669966 commented
in the app implementation would look like this
- however the problem is that you have to be inside the ktx of WorkScheduler to have access
fun WorkScheduler.scheduleWork() {
PersistentWorker().schedulePersistent()
FirstFutureWorker().scheduleFuture(10000L, true, true)
}
I'd love to see it look like this for the user of Boot Laces taking away the need for the scheduler
onCreate(...){
PersistentWorker().schedulePersistent()
FirstFutureWorker().scheduleFuture(10000L, true, true)
}
prototype of public and internal API but I would have to get scheduler into it's own component so it could just be a container fo alarm manager and intent factory and it would be internal instead of public how it is now...really I mean WorkScheduler is it's own component already but I need to decouple it from actually scheduling and rename it to something else
class WorkScheduler @Inject constructor(@ApplicationContext val ctx: Context, val alarmMgr: AlarmManager, val factory: IntentFactory) {
fun Worker.schedulePersistent() = schedule(ctx, factory)
fun Worker.scheduleNow(): Boolean = schedule(factory, alarmMgr, 0L, false, false)
fun Worker.scheduleFuture(delay: Long, repeating: Boolean = false, wakeupIfIdle: Boolean = false): Boolean =
schedule(factory, alarmMgr, delay, repeating, wakeupIfIdle)
fun Worker.scheduleHour(repeating: Boolean = false, wakeupIfIdle: Boolean = false): Boolean =
schedule(factory, alarmMgr, AlarmManager.INTERVAL_HOUR, repeating, wakeupIfIdle)
fun Worker.scheduleQuarterDay(repeating: Boolean = false, wakeupIfIdle: Boolean = false): Boolean =
schedule(factory, alarmMgr, AlarmManager.INTERVAL_HOUR * 6, repeating, wakeupIfIdle)
}
internal fun Worker.schedule(factory: IntentFactory, alarmMgr: AlarmManager, interval: Long, repeating: Boolean, wakeupIfIdle: Boolean): Boolean{
val pendingIntent = factory.createPendingIntent(this.toWork()) ?: return false
var alarmTimeType: Int = AlarmManager.RTC
val triggerTime = System.currentTimeMillis() + interval
if(wakeupIfIdle)
alarmTimeType = AlarmManager.RTC_WAKEUP
if(repeating)
alarmMgr.setRepeating(alarmTimeType, triggerTime, interval, pendingIntent)
else
if(wakeupIfIdle)
alarmMgr.setExactAndAllowWhileIdle(alarmTimeType, triggerTime, pendingIntent)
else
alarmMgr.setExact(alarmTimeType, triggerTime, pendingIntent)
return true
}
///TWO Internal KTX functions for internal API one schedules non-persistent and the other persistent
internal fun Worker.schedule(ctx: Context, factory: IntentFactory){
val intent = factory.createWorkIntent(this.toWork(), Actions.ACTION_WORK_PERSISTENT)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
ctx.startForegroundService(intent)
else
ctx.startService(intent)
WorkService.persist(ctx)
}