Skip to content

Commit

Permalink
add isScheduled call: checks whether a period task is scheduled (andr…
Browse files Browse the repository at this point in the history
…oid only) (#535)

Co-authored-by: Sebastian Roth <[email protected]>
  • Loading branch information
mx1up and ened authored Apr 8, 2024
1 parent bdd61b3 commit 6a09b41
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import androidx.work.PeriodicWorkRequest
import androidx.work.WorkRequest
import dev.fluttercommunity.workmanager.WorkManagerCall.CancelTask.ByTag.KEYS.UNREGISTER_TASK_TAG_KEY
import dev.fluttercommunity.workmanager.WorkManagerCall.CancelTask.ByUniqueName.KEYS.UNREGISTER_TASK_UNIQUE_NAME_KEY
import dev.fluttercommunity.workmanager.WorkManagerCall.IsScheduled.ByUniqueName.KEYS.IS_SCHEDULED_UNIQUE_NAME_KEY
import dev.fluttercommunity.workmanager.WorkManagerCall.Initialize.KEYS.INITIALIZE_TASK_CALL_HANDLE_KEY
import dev.fluttercommunity.workmanager.WorkManagerCall.Initialize.KEYS.INITIALIZE_TASK_IS_IN_DEBUG_MODE_KEY
import dev.fluttercommunity.workmanager.WorkManagerCall.RegisterTask.KEYS.REGISTER_TASK_BACK_OFF_POLICY_DELAY_MILLIS_KEY
Expand Down Expand Up @@ -131,6 +132,14 @@ sealed class WorkManagerCall {
}
}

sealed class IsScheduled : WorkManagerCall() {
data class ByUniqueName(val uniqueName: String) : IsScheduled() {
companion object KEYS {
const val IS_SCHEDULED_UNIQUE_NAME_KEY = "uniqueName"
}
}
}

sealed class CancelTask : WorkManagerCall() {
data class ByUniqueName(val uniqueName: String) : CancelTask() {
companion object KEYS {
Expand Down Expand Up @@ -164,6 +173,8 @@ object Extractor {
REGISTER_ONE_OFF_TASK("registerOneOffTask"),
REGISTER_PERIODIC_TASK("registerPeriodicTask"),

IS_SCHEDULED_BY_UNIQUE_NAME("isScheduledByUniqueName"),

CANCEL_TASK_BY_UNIQUE_NAME("cancelTaskByUniqueName"),
CANCEL_TASK_BY_TAG("cancelTaskByTag"),
CANCEL_ALL("cancelAllTasks"),
Expand Down Expand Up @@ -228,6 +239,12 @@ object Extractor {
)
}

PossibleWorkManagerCall.IS_SCHEDULED_BY_UNIQUE_NAME -> {
WorkManagerCall.IsScheduled.ByUniqueName(
call.argument(IS_SCHEDULED_UNIQUE_NAME_KEY)!!
)
}

PossibleWorkManagerCall.CANCEL_TASK_BY_UNIQUE_NAME -> WorkManagerCall.CancelTask.ByUniqueName(
call.argument(UNREGISTER_TASK_UNIQUE_NAME_KEY)!!
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import androidx.work.ExistingWorkPolicy
import androidx.work.OneTimeWorkRequest
import androidx.work.OutOfQuotaPolicy
import androidx.work.PeriodicWorkRequest
import androidx.work.WorkInfo
import androidx.work.WorkManager
import dev.fluttercommunity.workmanager.BackgroundWorker.Companion.DART_TASK_KEY
import dev.fluttercommunity.workmanager.BackgroundWorker.Companion.IS_IN_DEBUG_MODE_KEY
Expand Down Expand Up @@ -36,6 +37,11 @@ class WorkmanagerCallHandler(private val ctx: Context) : MethodChannel.MethodCal
extractedCall,
result
)
is WorkManagerCall.IsScheduled -> IsScheduledHandler.handle(
ctx,
extractedCall,
result
)
is WorkManagerCall.CancelTask -> UnregisterTaskHandler.handle(
ctx,
extractedCall,
Expand Down Expand Up @@ -140,6 +146,23 @@ private object RegisterTaskHandler : CallHandler<WorkManagerCall.RegisterTask> {
}
}

private object IsScheduledHandler : CallHandler<WorkManagerCall.IsScheduled> {
override fun handle(
context: Context,
convertedCall: WorkManagerCall.IsScheduled,
result: MethodChannel.Result
) {
when (convertedCall) {
is WorkManagerCall.IsScheduled.ByUniqueName -> {
val workInfos = WM.getWorkInfoByUniqueName(context, convertedCall.uniqueName).get()
val scheduled = workInfos.isNotEmpty()
&& workInfos.all { it.state == WorkInfo.State.ENQUEUED || it.state == WorkInfo.State.RUNNING }
return result.success(scheduled)
}
}
}
}

private object UnregisterTaskHandler : CallHandler<WorkManagerCall.CancelTask> {
override fun handle(
context: Context,
Expand Down Expand Up @@ -274,6 +297,9 @@ object WM {
.build()
}

fun getWorkInfoByUniqueName(context: Context, uniqueWorkName: String) =
context.workManager().getWorkInfosForUniqueWork(uniqueWorkName)

fun cancelByUniqueName(context: Context, uniqueWorkName: String) =
context.workManager().cancelUniqueWork(uniqueWorkName)

Expand Down
21 changes: 20 additions & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,11 @@ class _MyAppState extends State<MyApp> {
}
},
),
SizedBox(height: 16),
SizedBox(height: 8),
Text(
"Register task",
style: Theme.of(context).textTheme.headlineSmall,
),

//This task runs once.
//Most likely this will trigger immediately
Expand Down Expand Up @@ -194,6 +198,10 @@ class _MyAppState extends State<MyApp> {
);
}),
SizedBox(height: 8),
Text(
"Register periodic task (android only)",
style: Theme.of(context).textTheme.headlineSmall,
),
//This task runs periodically
//It will wait at least 10 seconds before its first launch
//Since we have not provided a frequency it will be the default 15 minutes
Expand Down Expand Up @@ -265,6 +273,17 @@ class _MyAppState extends State<MyApp> {
: null,
),
SizedBox(height: 16),
ElevatedButton(
child: Text("isscheduled (Android)"),
onPressed: Platform.isAndroid
? () async {
final workInfo = await Workmanager().isScheduledByUniqueName(
simplePeriodicTask,
);
print('isscheduled = $workInfo');
}
: null),
SizedBox(height: 8),
Text(
"Task cancellation",
style: Theme.of(context).textTheme.headlineSmall,
Expand Down
11 changes: 11 additions & 0 deletions lib/src/workmanager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,17 @@ class Workmanager {
),
);

/// Checks whether a period task is scheduled by its [uniqueName].
///
/// Scheduled means the work state is either ENQUEUED or RUNNING
///
/// Only available on Android.
Future<bool> isScheduledByUniqueName(final String uniqueName) async {
return await _foregroundChannel.invokeMethod(
"isScheduledByUniqueName",
{"uniqueName": uniqueName},
);

/// Schedule a background long running task, currently only available on iOS.
///
/// Processing tasks are for long processes like data processing and app maintenance.
Expand Down

0 comments on commit 6a09b41

Please sign in to comment.