Skip to content

Commit

Permalink
Merge pull request #291 from IFTTT/bugfix/event_registration_fixes2
Browse files Browse the repository at this point in the history
Event registration fixes
  • Loading branch information
ssathy2 committed Aug 18, 2021
2 parents ec6ee57 + 394c146 commit 38aa392
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 98 deletions.
7 changes: 5 additions & 2 deletions Examples/GroceryExpress/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
private let connectionRedirectHandler = ConnectionRedirectHandler(redirectURL: AppDelegate.connectionRedirectURL)

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

ConnectButtonController.synchronizationLoggingEnabled = true
ConnectButtonController.analyticsEnabled = true
ConnectButtonController.initialize(options: .init(enableSDKBackgroundProcess: true, showPermissionsPrompts: true))
ConnectButtonController.activate(connections: [DisplayInformation.locationConnection.connectionId])
if ConnectionCredentials(settings: .init()).isLoggedIn {
ConnectButtonController.activate(connections: [DisplayInformation.locationConnection.connectionId])
} else {
ConnectButtonController.deactivate()
}
ConnectButtonController.setBackgroundProcessClosures {
print("Background process started!")
} expirationHandler: {
Expand Down
5 changes: 5 additions & 0 deletions Examples/GroceryExpress/ConnectionCredentials.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,17 @@ class ConnectionCredentials: ConnectionCredentialProvider, CustomStringConvertib
Keys.token : token
]
UserDefaults.standard.set(user, forKey: Keys.user)
ConnectButtonController.activate(
connections: [DisplayInformation.locationConnection.connectionId],
lifecycleSynchronizationOptions: .all
)
}

/// Clears the active IFTTT session
func logout() {
userToken = nil
UserDefaults.standard.set(nil, forKey: Keys.user)
ConnectButtonController.deactivate()
}

/// Creates an instance of ConnectionCredentials
Expand Down
1 change: 0 additions & 1 deletion Examples/GroceryExpress/SettingsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ class SettingsViewController: UIViewController {
}
@IBAction func logoutTapped(_ sender: Any) {
ConnectionCredentials(settings: settings).logout()
ConnectButtonController.deactivate()
update()
}

Expand Down
4 changes: 4 additions & 0 deletions IFTTT SDK.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
B556033C25A851EE00A29A01 /* Localizable_cs.strings in Resources */ = {isa = PBXBuildFile; fileRef = B556032425A851EE00A29A01 /* Localizable_cs.strings */; };
B556033D25A851EE00A29A01 /* Localizable_nl.strings in Resources */ = {isa = PBXBuildFile; fileRef = B556032525A851EE00A29A01 /* Localizable_nl.strings */; };
DE25265623D8C49D0019C9CB /* Analytics.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE25265523D8C49D0019C9CB /* Analytics.swift */; };
DE260F6B26CAFC20004191D1 /* SynchronizationSchedulerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE260F6A26CAFC20004191D1 /* SynchronizationSchedulerTests.swift */; };
DE2906D3242BF66E00CC2825 /* Connection+Parsing.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE2906D2242BF66E00CC2825 /* Connection+Parsing.swift */; };
DE2F524A2429404200EF986A /* Connection+Location.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE2F52492429404200EF986A /* Connection+Location.swift */; };
DE2F524C242940AD00EF986A /* CLCircularRegion+Parsing.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE2F524B242940AD00EF986A /* CLCircularRegion+Parsing.swift */; };
Expand Down Expand Up @@ -226,6 +227,7 @@
B556032525A851EE00A29A01 /* Localizable_nl.strings */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.strings; name = Localizable_nl.strings; path = Resources/Localizable_nl.strings; sourceTree = "<group>"; };
DE1712BC2565B295000B13E6 /* RegionEventsRegistryTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RegionEventsRegistryTests.swift; sourceTree = "<group>"; };
DE25265523D8C49D0019C9CB /* Analytics.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Analytics.swift; sourceTree = "<group>"; };
DE260F6A26CAFC20004191D1 /* SynchronizationSchedulerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SynchronizationSchedulerTests.swift; sourceTree = "<group>"; };
DE2906D2242BF66E00CC2825 /* Connection+Parsing.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Connection+Parsing.swift"; sourceTree = "<group>"; };
DE2F52492429404200EF986A /* Connection+Location.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Connection+Location.swift"; sourceTree = "<group>"; };
DE2F524B242940AD00EF986A /* CLCircularRegion+Parsing.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CLCircularRegion+Parsing.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -439,6 +441,7 @@
DEC29DE525840DE300BF56EE /* LocationServiceTests.swift */,
DEC29EE7258419FC00BF56EE /* Info.plist */,
DEF4A4962587BA1A00735E98 /* ArrayHelpersTests.swift */,
DE260F6A26CAFC20004191D1 /* SynchronizationSchedulerTests.swift */,
);
path = SDKHostAppTests;
sourceTree = "<group>";
Expand Down Expand Up @@ -864,6 +867,7 @@
DEC29F0125841A0800BF56EE /* String_EmailDataDetectorTests.swift in Sources */,
DEC29F0525841A0800BF56EE /* RegionsMonitorTests.swift in Sources */,
DEC29F0625841A0800BF56EE /* Connection_ParsingTests.swift in Sources */,
DE260F6B26CAFC20004191D1 /* SynchronizationSchedulerTests.swift in Sources */,
DEF4A4972587BA1A00735E98 /* ArrayHelpersTests.swift in Sources */,
DEC29F0225841A0800BF56EE /* EventPublisherTests.swift in Sources */,
);
Expand Down
13 changes: 6 additions & 7 deletions IFTTT SDK/ConnectButtonController+Public.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,25 +70,24 @@ extension ConnectButtonController {
}
}

/// Performs setup of the SDK. Starts the synchronization of in the SDK. Registers background process with the system if desired.
/// Performs setup of the SDK.Activated synchronization
///
/// - Parameters:
/// - credentials: An optional object conforming to `ConnectionCredentialProvider` which is used to setup the SDK. If this is nil, the SDK will attempt to use cached values.
/// - lifecycleSynchronizationOptions: An instance of `ApplicationLifecycleSynchronizationOptions` that defines which app lifecycle events the synchronization should occur on. If this parameter is not set, a default value of `ApplicationLifecycleSynchronizationOptions.all` will be used.
public static func setup(with credentials: ConnectionCredentialProvider?,
lifecycleSynchronizationOptions: ApplicationLifecycleSynchronizationOptions = .all) {
public static func setup(with credentials: ConnectionCredentialProvider?) {
if let credentials = credentials {
Keychain.update(with: credentials)
}
ConnectionsSynchronizer.shared.setup(lifecycleSynchronizationOptions: lifecycleSynchronizationOptions)
}

/// Call this method to activate the synchronization. This starts synchronization for the parameter connections.
///
/// - Parameters:
/// - connections: An optional list of `Connection` to activate synchronization with.
public static func activate(connections ids: [String]? = nil) {
ConnectionsSynchronizer.shared.activate(connections: ids)
/// - lifecycleSynchronizationOptions: An instance of `ApplicationLifecycleSynchronizationOptions` that defines which app lifecycle events the synchronization should occur on. If this parameter is not set, a default value of `ApplicationLifecycleSynchronizationOptions.all` will be used.
public static func activate(connections ids: [String]? = nil,
lifecycleSynchronizationOptions: ApplicationLifecycleSynchronizationOptions = .all) {
ConnectionsSynchronizer.shared.activate(connections: ids, lifecycleSynchronizationOptions: lifecycleSynchronizationOptions)
}

/// Call this method to deactivate the synchronization of connection and native service data. This stops synchronization and performs cleanup of any stored data. This will also remove any registered geofences.
Expand Down
40 changes: 8 additions & 32 deletions IFTTT SDK/ConnectionsSynchronizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,6 @@ final class ConnectionsSynchronizer {
location.start()
}

/// Performs basic setup of the SDK with Application lifecycle synchronization options.
///
/// - Parameters:
/// - lifecycleSynchronizationOptions: The synchronization options to use in setting up App Lifecycle notification observers.
func setup(lifecycleSynchronizationOptions: ApplicationLifecycleSynchronizationOptions) {
scheduler.setup(lifecycleSynchronizationOptions: lifecycleSynchronizationOptions)
}

/// Can be used to force a synchronization.
///
/// - Parameters:
Expand All @@ -151,19 +143,20 @@ final class ConnectionsSynchronizer {
eventPublisher.onNext(event)
}

/// Used to start the synchronization with an optional list of connection ids to monitor.
/// Used to start the synchronization.
///
/// - Parameters:
/// - connections: An optional list of connections to start monitoring.
func activate(connections ids: [String]? = nil) {
/// - lifecycleSynchronizationOptions: The app lifecycle synchronization options to use with the scheduler
func activate(connections ids: [String]? = nil, lifecycleSynchronizationOptions: ApplicationLifecycleSynchronizationOptions) {
start(lifecycleSynchronizationOptions: lifecycleSynchronizationOptions)
update(isActivation: true)
if let ids = ids {
registry.addConnections(with: ids, shouldNotify: false)
ConnectButtonController.synchronizationLog("Activated synchronization with connection ids: \(ids)")
} else {
ConnectButtonController.synchronizationLog("Activated synchronization")
}
start()
update(isActivation: true)
}

/// Used to deactivate and stop synchronization.
Expand All @@ -174,19 +167,17 @@ final class ConnectionsSynchronizer {
}

/// Call this to start the synchronization. Safe to be called multiple times.
private func start() {
private func start(lifecycleSynchronizationOptions: ApplicationLifecycleSynchronizationOptions) {
if state == .running { return }

setupNotifications()
performPreflightChecks()
Keychain.resetIfNecessary(force: false)
scheduler.start()
scheduler.start(lifecycleSynchronizationOptions: lifecycleSynchronizationOptions)
state = .running
}

/// Call this to stop the synchronization completely. Safe to be called multiple times.
private func stop() {
stopNotifications()
Keychain.resetIfNecessary(force: true)
scheduler.stop()
state = .stopped
Expand All @@ -198,22 +189,7 @@ final class ConnectionsSynchronizer {
ConnectButtonController.synchronizationLog("Background location not enabled for this target! Enable background location to allow location updates to be delivered to the app in the background.")
}
}

/// Peforms internal setup to allow the SDK to perform work in response to notification center notifications.
private func setupNotifications() {
if #available(iOS 13.0, *) {
NotificationCenter.default.addObserver(self,
selector: #selector(applicationDidEnterBackground),
name: UIApplication.didEnterBackgroundNotification,
object: nil)
}
}

/// Stops notification observation
private func stopNotifications() {
NotificationCenter.default.removeObserver(self)
}


private func setupRegistryNotifications() {
NotificationCenter.default.addObserver(forName: .UpdateConnectionsName,
object: nil,
Expand Down
4 changes: 2 additions & 2 deletions IFTTT SDK/SynchronizationManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ final class SynchronizationManager {
/// Creates a `SyncManager`
///
/// - Parameter subscribers: The `SyncSubscribers to perform during syncs
init(subscribers: Array<SynchronizationSubscriber>) {
init(subscribers: [SynchronizationSubscriber]) {
self.subscribers = subscribers
}

Expand Down Expand Up @@ -126,7 +126,7 @@ extension SynchronizationManager {
finish()
}

private(set) var subscribers: Array<SynchronizationSubscriber> = []
private(set) var subscribers = [SynchronizationSubscriber]()

private var resultsBySubscriber: [String : UIBackgroundFetchResult] = [:]

Expand Down
Loading

0 comments on commit 38aa392

Please sign in to comment.