@@ -21,21 +21,14 @@ public class WorkmanagerPlugin: FlutterPluginAppLifeCycleDelegate, FlutterPlugin
2121 @available ( iOS 13 . 0 , * )
2222 private static func handleBGProcessingTask( identifier: String , task: BGProcessingTask ) {
2323 let operationQueue = OperationQueue ( )
24-
25- let operation = BackgroundTaskOperation (
26- task. identifier,
24+ let operation = createBackgroundOperation (
25+ identifier: task. identifier,
2726 inputData: nil ,
28- flutterPluginRegistrantCallback: flutterPluginRegistrantCallback,
2927 backgroundMode: . backgroundProcessingTask( identifier: identifier)
3028 )
3129
32- task. expirationHandler = {
33- operation. cancel ( )
34- }
35-
36- operation. completionBlock = {
37- task. setTaskCompleted ( success: !operation. isCancelled)
38- }
30+ task. expirationHandler = { operation. cancel ( ) }
31+ operation. completionBlock = { task. setTaskCompleted ( success: !operation. isCancelled) }
3932
4033 operationQueue. addOperation ( operation)
4134 }
@@ -53,38 +46,28 @@ public class WorkmanagerPlugin: FlutterPluginAppLifeCycleDelegate, FlutterPlugin
5346 schedulePeriodicTask ( taskIdentifier: task. identifier, earliestBeginInSeconds: earliestBeginInSeconds ?? ( 15 * 60 ) )
5447
5548 let operationQueue = OperationQueue ( )
56- let operation = BackgroundTaskOperation (
57- task. identifier,
49+ let operation = createBackgroundOperation (
50+ identifier : task. identifier,
5851 inputData: nil ,
59- flutterPluginRegistrantCallback: flutterPluginRegistrantCallback,
6052 backgroundMode: . backgroundPeriodicTask( identifier: identifier)
6153 )
6254
63- task. expirationHandler = {
64- operation. cancel ( )
65- }
66-
67- operation. completionBlock = {
68- task. setTaskCompleted ( success: !operation. isCancelled)
69- }
55+ task. expirationHandler = { operation. cancel ( ) }
56+ operation. completionBlock = { task. setTaskCompleted ( success: !operation. isCancelled) }
7057
7158 operationQueue. addOperation ( operation)
7259 }
7360
7461 @available ( iOS 13 . 0 , * )
7562 public static func startOneOffTask( identifier: String , taskIdentifier: UIBackgroundTaskIdentifier , inputData: [ String : Any ] ? , delaySeconds: Int64 ) {
7663 let operationQueue = OperationQueue ( )
77- let operation = BackgroundTaskOperation (
78- identifier,
64+ let operation = createBackgroundOperation (
65+ identifier: identifier ,
7966 inputData: inputData,
80- flutterPluginRegistrantCallback: flutterPluginRegistrantCallback,
8167 backgroundMode: . backgroundOneOffTask( identifier: identifier)
8268 )
8369
84- operation. completionBlock = {
85- UIApplication . shared. endBackgroundTask ( taskIdentifier)
86- }
87-
70+ operation. completionBlock = { UIApplication . shared. endBackgroundTask ( taskIdentifier) }
8871 operationQueue. addOperation ( operation)
8972 }
9073
@@ -177,7 +160,7 @@ public class WorkmanagerPlugin: FlutterPluginAppLifeCycleDelegate, FlutterPlugin
177160 return
178161 }
179162
180- if #available ( iOS 13 . 0 , * ) {
163+ executeIfSupportedVoid ( completion : completion , feature : " OneOffTask " ) {
181164 var taskIdentifier : UIBackgroundTaskIdentifier = . invalid
182165 let delaySeconds = request. initialDelaySeconds ?? 0
183166
@@ -191,13 +174,6 @@ public class WorkmanagerPlugin: FlutterPluginAppLifeCycleDelegate, FlutterPlugin
191174 inputData: request. inputData as? [ String : Any ] ,
192175 delaySeconds: delaySeconds
193176 )
194- completion ( . success( ( ) ) )
195- } else {
196- completion ( . failure( PigeonError (
197- code: " 99 " ,
198- message: " OneOffTask could not be registered " ,
199- details: " BGTaskScheduler tasks are only supported on iOS 13+ "
200- ) ) )
201177 }
202178 }
203179
@@ -207,20 +183,12 @@ public class WorkmanagerPlugin: FlutterPluginAppLifeCycleDelegate, FlutterPlugin
207183 return
208184 }
209185
210- if #available ( iOS 13 . 0 , * ) {
186+ executeIfSupportedVoid ( completion : completion , feature : " PeriodicTask " ) {
211187 let initialDelaySeconds = Double ( request. initialDelaySeconds ?? 0 )
212-
213188 WorkmanagerPlugin . schedulePeriodicTask (
214189 taskIdentifier: request. uniqueName,
215190 earliestBeginInSeconds: initialDelaySeconds
216191 )
217- completion ( . success( ( ) ) )
218- } else {
219- completion ( . failure( PigeonError (
220- code: " 99 " ,
221- message: " PeriodicTask could not be registered " ,
222- details: " BGAppRefreshTasks are only supported on iOS 13+. Instead you should use Background Fetch "
223- ) ) )
224192 }
225193 }
226194
@@ -230,48 +198,35 @@ public class WorkmanagerPlugin: FlutterPluginAppLifeCycleDelegate, FlutterPlugin
230198 return
231199 }
232200
233- if #available ( iOS 13 . 0 , * ) {
201+ executeIfSupportedVoid ( completion : completion , feature : " BackgroundProcessingTask " ) {
234202 let delaySeconds = Double ( request. initialDelaySeconds ?? 0 )
235203 let requiresCharging = request. requiresCharging ?? false
236-
237- var requiresNetwork = false
238- if let networkType = request. networkType,
239- networkType == . connected || networkType == . metered {
240- requiresNetwork = true
241- }
204+ let requiresNetwork = request. networkType == . connected || request. networkType == . metered
242205
243206 WorkmanagerPlugin . scheduleBackgroundProcessingTask (
244207 withIdentifier: request. uniqueName,
245208 earliestBeginInSeconds: delaySeconds,
246209 requiresNetworkConnectivity: requiresNetwork,
247210 requiresExternalPower: requiresCharging
248211 )
249- completion ( . success( ( ) ) )
250- } else {
251- completion ( . failure( PigeonError (
252- code: " 99 " ,
253- message: " BackgroundProcessingTask could not be registered " ,
254- details: " BGProcessingTasks are only supported on iOS 13+ "
255- ) ) )
256212 }
257213 }
258214
259215 func cancelByUniqueName( uniqueName: String , completion: @escaping ( Result < Void , Error > ) -> Void ) {
260- if #available ( iOS 13 . 0 , * ) {
216+ executeIfSupportedVoid ( completion : completion , feature : " cancelByUniqueName " ) {
261217 BGTaskScheduler . shared. cancel ( taskRequestWithIdentifier: uniqueName)
262218 }
263- completion ( . success( ( ) ) )
264219 }
265220
266221 func cancelByTag( tag: String , completion: @escaping ( Result < Void , Error > ) -> Void ) {
267- completion ( . failure( PigeonError ( code: " not implemented " , message: " not implemented " , details: nil ) ) )
222+ // iOS doesn't support canceling by tag - this is an Android-specific feature
223+ completion ( . success( ( ) ) )
268224 }
269225
270226 func cancelAll( completion: @escaping ( Result < Void , Error > ) -> Void ) {
271- if #available ( iOS 13 . 0 , * ) {
227+ executeIfSupportedVoid ( completion : completion , feature : " cancelAll " ) {
272228 BGTaskScheduler . shared. cancelAllTaskRequests ( )
273229 }
274- completion ( . success( ( ) ) )
275230 }
276231
277232 func isScheduledByUniqueName( uniqueName: String , completion: @escaping ( Result < Bool , Error > ) -> Void ) {
@@ -333,6 +288,59 @@ public class WorkmanagerPlugin: FlutterPluginAppLifeCycleDelegate, FlutterPlugin
333288 details: nil
334289 )
335290 }
291+
292+ private func createUnsupportedVersionError( feature: String ) -> PigeonError {
293+ return PigeonError (
294+ code: " 99 " ,
295+ message: " \( feature) could not be registered " ,
296+ details: " BGTaskScheduler tasks are only supported on iOS 13+ "
297+ )
298+ }
299+
300+ private func executeIfSupported< T> (
301+ completion: @escaping ( Result < T , Error > ) -> Void ,
302+ defaultValue: T ? = nil ,
303+ feature: String ,
304+ action: @escaping ( ) -> T
305+ ) {
306+ if #available( iOS 13 . 0 , * ) {
307+ let result = action ( )
308+ completion ( . success( result) )
309+ } else {
310+ if let defaultValue = defaultValue {
311+ completion ( . success( defaultValue) )
312+ } else {
313+ completion ( . failure( createUnsupportedVersionError ( feature: feature) ) )
314+ }
315+ }
316+ }
317+
318+ private func executeIfSupportedVoid(
319+ completion: @escaping ( Result < Void , Error > ) -> Void ,
320+ feature: String ,
321+ action: @escaping ( ) -> Void
322+ ) {
323+ if #available( iOS 13 . 0 , * ) {
324+ action ( )
325+ completion ( . success( ( ) ) )
326+ } else {
327+ completion ( . failure( createUnsupportedVersionError ( feature: feature) ) )
328+ }
329+ }
330+
331+ @available ( iOS 13 . 0 , * )
332+ private static func createBackgroundOperation(
333+ identifier: String ,
334+ inputData: [ String : Any ] ? ,
335+ backgroundMode: BackgroundMode
336+ ) -> BackgroundTaskOperation {
337+ return BackgroundTaskOperation (
338+ identifier,
339+ inputData: inputData,
340+ flutterPluginRegistrantCallback: flutterPluginRegistrantCallback,
341+ backgroundMode: backgroundMode
342+ )
343+ }
336344}
337345
338346// MARK: - FlutterPlugin conformance
0 commit comments