Skip to content

Commit

Permalink
Merge pull request #273 from IFTTT/hotfix/crash_fix_notification_obse…
Browse files Browse the repository at this point in the history
…rver_change

Localization crash fix and notification observer cleanup fix
  • Loading branch information
ssathy2 authored Mar 16, 2021
2 parents 4af9d49 + bd56af7 commit 22814bc
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
27 changes: 15 additions & 12 deletions IFTTT SDK/ConnectButtonController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,6 @@ public class ConnectButtonController {
state: state)
let activation = ConnectionActivation(userToken: userToken,
connection: connection)
if connection.hasNativeTriggers {
connectionsRegistry.update(with: connection)
}
delegate?.connectButtonController(self, didFinishActivationWithResult: .success(activation))
}
}
Expand Down Expand Up @@ -268,7 +265,7 @@ public class ConnectButtonController {
}

if connection.hasNativeTriggers {
connectionsRegistry.update(with: connection)
connectionsRegistry.update(with: connection, shouldNotify: false)
}

Analytics.shared.track(.Impression,
Expand Down Expand Up @@ -423,6 +420,18 @@ public class ConnectButtonController {
return NSAttributedString(string: "IFTTT",
attributes: [.font : Constants.iftttWordmarkFont])
}

private func substituteIftttWordmark(with string: String) -> NSMutableAttributedString {
let iftttRange = (string as NSString).range(of: "IFTTT")
let text = NSMutableAttributedString(string: string,
attributes: [.font : Constants.footnoteFont])
if iftttRange.location == NSNotFound {
return text
}

text.replaceCharacters(in: iftttRange, with: iftttWordmark)
return text
}

var value: ConnectButton.LabelValue {
return .attributed(attributedString)
Expand All @@ -440,10 +449,7 @@ public class ConnectButtonController {

case .enterEmail:
let string = "button.footer.email.prefix".localized + " "
let text = NSMutableAttributedString(string: string,
attributes: [.font : Constants.footnoteFont])
let iftttRange = (string as NSString).range(of: "IFTTT")
text.replaceCharacters(in: iftttRange, with: iftttWordmark)
let text = substituteIftttWordmark(with: string)

text.append(NSAttributedString(string: " ")) // Adds a space before the underline starts
text.append(NSAttributedString(string: "button.footer.email.postfix".localized,
Expand All @@ -459,10 +465,7 @@ public class ConnectButtonController {

case let .creatingAccount(email):
let string = "button.footer.accountCreation".localized(with: email)
let text = NSMutableAttributedString(string: string,
attributes: [.font : Constants.footnoteFont])
let iftttRange = (string as NSString).range(of: "IFTTT")
text.replaceCharacters(in: iftttRange, with: iftttWordmark)
let text = substituteIftttWordmark(with: string)
return text

case .loadingFailed:
Expand Down
22 changes: 19 additions & 3 deletions IFTTT SDK/SynchronizationScheduler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ final class SynchronizationScheduler {
}

func setup(lifecycleSynchronizationOptions: ApplicationLifecycleSynchronizationOptions) {
removeLifecycleNotificationObservers()

// Start synchronization on system events
var appLifecycleEventTuples = [(NSNotification.Name, SynchronizationSource, Bool)]()

Expand All @@ -77,6 +79,9 @@ final class SynchronizationScheduler {
/// Performs registration for system and SDK generated events for kicking off synchronizations
/// Should get called when the scheduler is to start.
func start() {
// Remove any previous tokens that might still be aroind
removeSDKGeneratedNotificationObservers()

// Start the manager
manager.start()

Expand All @@ -101,9 +106,8 @@ final class SynchronizationScheduler {

// Unregister from background process
// Remove observers from notification center
[applicationLifecycleNotificationCenterTokens + sdkGeneratedNotificationCenterTokens].forEach {
NotificationCenter.default.removeObserver($0)
}
removeLifecycleNotificationObservers()
removeSDKGeneratedNotificationObservers()

applicationLifecycleNotificationCenterTokens = []
sdkGeneratedNotificationCenterTokens = []
Expand All @@ -119,6 +123,18 @@ final class SynchronizationScheduler {
subscriberToken = nil
}

private func removeLifecycleNotificationObservers() {
applicationLifecycleNotificationCenterTokens.forEach {
NotificationCenter.default.removeObserver($0)
}
}

private func removeSDKGeneratedNotificationObservers() {
sdkGeneratedNotificationCenterTokens.forEach {
NotificationCenter.default.removeObserver($0)
}
}

/// Sets up subscriber with app generated triggers.
private func setupSubscribers() {
guard subscriberToken == nil else { return }
Expand Down

0 comments on commit 22814bc

Please sign in to comment.