Skip to content

Commit

Permalink
fix: use existing registerBackgroundService
Browse files Browse the repository at this point in the history
  • Loading branch information
janvennemann committed May 6, 2020
1 parent cd6f844 commit 2eaf407
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 45 deletions.
3 changes: 2 additions & 1 deletion iphone/Classes/TiAppiOSBackgroundServiceProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@

#ifdef USE_TI_APPIOS

#import "Titanium-Swift.h"
#import <TitaniumKit/KrollBridge.h>
#import <TitaniumKit/TiProxy.h>

@interface TiAppiOSBackgroundServiceProxy : TiProxy {
@interface TiAppiOSBackgroundServiceProxy : TiProxy <BackgroundTaskProxy> {

@private
KrollBridge *bridge;
Expand Down
51 changes: 32 additions & 19 deletions iphone/Classes/TiAppiOSBackgroundTaskProxy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,18 @@ import TitaniumKit
import Foundation.NSOperation
import BackgroundTasks

@available(iOS 13.0, *)
@objc
public class TiAppiOSBackgroundTaskProxy : TiProxy {
public protocol BackgroundTaskProxy {
@objc(stop:)
func stop(args: Any)

@objc(unregister:)
func unregister(args: Any)
}

@available(iOS 13.0, *)
@objc
public class TiAppiOSBackgroundTaskProxy : TiProxy, BackgroundTaskProxy {
@objc
public var type: String!

Expand All @@ -23,7 +31,7 @@ public class TiAppiOSBackgroundTaskProxy : TiProxy {
public var url: String!

@objc
public var interval: NSNumber!
public var interval: NSNumber?

@objc
public var `repeat`: NSNumber {
Expand All @@ -41,8 +49,8 @@ public class TiAppiOSBackgroundTaskProxy : TiProxy {
private var _repeat: Bool!

public override func _init(withProperties properties: [AnyHashable : Any]!) {
guard let type = properties["type"] as? String else {
self.throwException("Invalid task", subreason: "No type specified", location: CODELOCATION)
guard let type = properties["type"] as? String, (type == "refresh" || type == "process") else {
self.throwException("Invalid task", subreason: "Missing or invalid type", location: CODELOCATION)
return;
}
guard let identifier = properties["identifier"] as? String else {
Expand All @@ -53,19 +61,8 @@ public class TiAppiOSBackgroundTaskProxy : TiProxy {
self.throwException("Invalid task", subreason: "No url specified", location: CODELOCATION)
return;
}
guard let interval = properties["interval"] as? NSNumber else {
self.throwException("Invalid task", subreason: "No interval specified", location: CODELOCATION)
return;
}

// TODO: validate type and options
if let options = properties["options"] as? [String] {
self.options = options
}
self.type = type
self.identifier = identifier
self.url = url;
self.interval = interval
super._init(withProperties: properties)
}

@objc
Expand All @@ -83,14 +80,24 @@ public class TiAppiOSBackgroundTaskProxy : TiProxy {
}
request = processingRequest
}
request.earliestBeginDate = Date(timeIntervalSinceNow: self.interval.doubleValue)
if let interval = self.interval {
request.earliestBeginDate = Date(timeIntervalSinceNow: interval.doubleValue)
}

do {
try BGTaskScheduler.shared.submit(request)
} catch {
NSLog("Could not schedule background task: \(error)")
}
}

public func stop(args: Any) {
BGTaskScheduler.shared.cancel(taskRequestWithIdentifier: self.identifier)
}

public func unregister(args: Any) {
TiApp.sharedApp()?.unregisterBackgroundTask(self)
}
}

@available(iOS 13.0, *)
Expand All @@ -100,14 +107,20 @@ extension TiApp {
if (self.backgroundTasks == nil) {
self.backgroundTasks = NSMutableDictionary()
}
self.backgroundTasks.setObject(taskProxy, forKey: taskProxy.identifier as NSString);
self.backgroundTasks.setObject(taskProxy, forKey: taskProxy.identifier! as NSCopying);
BGTaskScheduler.shared.register(forTaskWithIdentifier: taskProxy.identifier, using: nil) { (task) in
self.handleBackgroundTask(task)
}
}

@objc(unregisterBackgroundTask:)
func unregisterBackgroundTask(_ taskProxy: TiAppiOSBackgroundTaskProxy) {
self.backgroundTasks.removeObject(forKey: taskProxy.identifier!);
}

func handleBackgroundTask(_ task: BGTask) {
let taskProxy = self.backgroundTasks[task.identifier] as! TiAppiOSBackgroundTaskProxy
taskProxy.schedule()
let url = taskProxy.url!
let queue = Foundation.OperationQueue()

Expand Down
44 changes: 20 additions & 24 deletions iphone/Classes/TiAppiOSProxy.m
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ - (TiAppiOSUserDefaultsProxy *)createUserDefaults:(id)args

#pragma mark - Background Services & Tasks

- (TiAppiOSBackgroundServiceProxy *)registerBackgroundService:(id)args
- (TiProxy *)registerBackgroundService:(id)args
{
NSDictionary *a = nil;
ENSURE_ARG_AT_INDEX(a, args, 0, NSDictionary)
Expand All @@ -403,35 +403,31 @@ - (TiAppiOSBackgroundServiceProxy *)registerBackgroundService:(id)args
return nil;
}

if (backgroundServices == nil) {
backgroundServices = [[NSMutableDictionary alloc] init];
}
NSString *identifier = [a objectForKey:@"identifier"];
if ([TiUtils isIOSVersionOrGreater:@"13.0"] && identifier != nil) {
if (self.backgroundTasks == nil) {
self.backgroundTasks = [NSMutableDictionary dictionary];
}

TiAppiOSBackgroundServiceProxy *proxy = [backgroundServices objectForKey:urlString];
TiAppiOSBackgroundTaskProxy *task = [[[TiAppiOSBackgroundTaskProxy alloc] _initWithPageContext:self.executionContext args:args] autorelease];
[TiApp.app registerBackgroundTask:task];

if (proxy == nil) {
proxy = [[[TiAppiOSBackgroundServiceProxy alloc] _initWithPageContext:[self executionContext] args:args] autorelease];
[backgroundServices setValue:proxy forKey:urlString];
}
return task;
} else {
if (backgroundServices == nil) {
backgroundServices = [[NSMutableDictionary alloc] init];
}

[[TiApp app] registerBackgroundService:proxy];
return proxy;
}
TiAppiOSBackgroundServiceProxy *proxy = [backgroundServices objectForKey:urlString];

- (TiAppiOSBackgroundTaskProxy *)registerBackgroundTask:(id)args API_AVAILABLE(ios(13))
{
NSDictionary *options = nil;
ENSURE_ARG_AT_INDEX(options, args, 0, NSDictionary);
NSString *identifier = options[@"identifier"];
if (proxy == nil) {
proxy = [[[TiAppiOSBackgroundServiceProxy alloc] _initWithPageContext:[self executionContext] args:args] autorelease];
[backgroundServices setValue:proxy forKey:urlString];
}

if (self.backgroundTasks == nil) {
self.backgroundTasks = [NSMutableDictionary dictionary];
[[TiApp app] registerBackgroundService:proxy];
return proxy;
}

TiAppiOSBackgroundTaskProxy *task = [[[TiAppiOSBackgroundTaskProxy alloc] _initWithPageContext:self.executionContext args:args] autorelease];
[TiApp.app registerBackgroundTask:task];

return task;
}

- (void)registerUserNotificationSettings:(id)args
Expand Down
2 changes: 1 addition & 1 deletion iphone/Resources/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* to trigger a log that is displayed in the Xcode console.
*/

Ti.App.iOS.registerBackgroundTask({
Ti.App.iOS.registerBackgroundService({
type: 'refresh',
identifier: 'com.appc.test',
interval: 15 * 60,
Expand Down
1 change: 1 addition & 0 deletions iphone/TitaniumKit/TitaniumKit/Sources/API/TiApp.m
Original file line number Diff line number Diff line change
Expand Up @@ -1164,6 +1164,7 @@ - (void)dealloc
RELEASE_TO_NIL(remoteNotification);
RELEASE_TO_NIL(splashScreenView);
RELEASE_TO_NIL(backgroundServices);
RELEASE_TO_NIL(_backgroundTasks);
RELEASE_TO_NIL(localNotification);
RELEASE_TO_NIL(uploadTaskResponses);
RELEASE_TO_NIL(queuedBootEvents);
Expand Down

0 comments on commit 2eaf407

Please sign in to comment.