-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathConnectButtonController.swift
907 lines (751 loc) · 41.3 KB
/
ConnectButtonController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
//
// ConnectButtonController.swift
// IFTTT SDK
//
// Copyright © 2019 IFTTT. All rights reserved.
//
import UIKit
import SafariServices
/// Bundles values when a Connection is activated
public struct ConnectionActivation {
/// The IFTTT service-level user token for your service.
public let userToken: String?
/// The Connection that was activated
public let connection: Connection
}
/// An error occurred, preventing a connect button from completing a service authentication with the `Connection`.
public enum ConnectButtonControllerError: Error {
/// For some reason we could not create an IFTTT account for a new user.
case iftttAccountCreationFailed
/// Some generic networking error occurred.
case networkError(ConnectionNetworkError)
/// A user cancelled the service authentication with the `Connection`. This happens when the user cancels from the sign in process on an authorization page in a safari view controller.
case canceled
/// A user cancelled the configuration of the connection in the IFTTT app.
case userCancelledConfiguration
/// Redirect parameters did not match what we expected. This should never happen. Verify you are using the latest SDK.
case unknownRedirect
/// Response parameters did not match what we expected. This should never happen. Verify you are using the latest SDK.
case unknownResponse
/// For some reason the `Connection` used by this controller has gone nil or could not be retrieved. This should never happen.
case unableToGetConnection
/// For some reason we could not redirect to the IFTTT app. This should never happen since we check if the app is installed first.
case iftttAppRedirectFailed
}
/// Defines the communication between ConnectButtonController and your app. It is required to implement this protocol.
public protocol ConnectButtonControllerDelegate: AnyObject {
/// The `ConnectButtonController` needs to present a view controller. This includes the About IFTTT page and Safari VC during Connection activation.
///
/// - Parameter connectButtonController: The `ConnectButtonController` controller that is sending the message.
/// - Returns: A `UIViewController` to present other view controllers on.
func presentingViewController(for connectButtonController: ConnectButtonController) -> UIViewController
/// Connection activation is finished.
///
/// On success, the controller transitions the button to its connected state. It is recommended that you fetch or refresh the user's IFTTT token after successfully authenticating a connection.
///
/// On failure, the controller resets the button to its initial state but does not present any error message.
/// It is up to you to decided how to present an error message to your user. You may use our message or one you provide.
///
/// For these two scenarios it's not neccessary for you to show additional confirmation, however, you may
/// want to update your app's UI in some way or ask they user why they canceled if you recieve a canceled error.
///
/// - Parameters:
/// - connectButtonController: The `ConnectButtonController` controller that is sending the message.
/// - result: A result of the connection activation request.
func connectButtonController(_ connectButtonController: ConnectButtonController,
didFinishActivationWithResult result: Result<ConnectionActivation, ConnectButtonControllerError>)
/// Connection deactivation is finished.
///
/// On success, the controller transitions the button to its deactivated initial state.
///
/// On failure, the controller will reset the Connection to the connected state but will not show any messaging. The user attempted to deactivate the Connection but something unexpected went wrong, likely a network failure. It is up to you to do this. This should be rare but should be handled.
///
/// - Parameters:
/// - connectButtonController: The `ConnectButtonController` controller that is sending the message.
/// - result: A result of the connection deactivation request.
func connectButtonController(_ connectButtonController: ConnectButtonController,
didFinishDeactivationWithResult result: Result<Connection, ConnectButtonControllerError>)
/// This is required as of iOS 13 to go through an authentication flow as a part of connection activation. This flow is shown if the user does not have the IFTTT app installed.
///
/// - Returns: A `UIWindow` to show the web authentication flow on.
@available(iOS 13.0, *)
func webAuthenticationPresentationAnchor() -> UIWindow
}
/// A controller that handles the `ConnectButton` when authenticating a `Connection`. It is mandatory that you interact with the ConnectButton only through this controller.
public class ConnectButtonController {
/// The connect button in this interaction
public let button: ConnectButton
/// The `Connection` the controller is handling. The controller may change the `Connection.Status` of the `Connection`.
public private(set) var connection: Connection?
/// Controls whether or not analytics events should be sent from the connect button.
public static var analyticsEnabled: Bool {
set {
Analytics.shared.enabled = newValue
}
get {
return Analytics.shared.enabled
}
}
/// The current `User`
/// This is set during the `identifyUser` step. It is required that we have this information for later steps in the flow.
private var user: User?
private func handleActivationFinished(userToken: String?) {
var state: AnalyticsState? = nil
if let status = connection?.status {
state = (status == .initial) ? .Created : .Enabled
}
if let userToken = userToken {
Keychain.userToken = userToken
}
connection?.status = .enabled
// If this connection has native triggers, update native trigger data
if connection?.hasNativeTriggers ?? false {
ConnectButtonController.update()
}
if let connection = connection {
Analytics.shared.track(.StateChange,
object: connection,
state: state)
let activation = ConnectionActivation(userToken: userToken,
connection: connection)
delegate?.connectButtonController(self, didFinishActivationWithResult: .success(activation))
}
}
private func handleActivationFailed(error: ConnectButtonControllerError) {
delegate?.connectButtonController(self, didFinishActivationWithResult: .failure(error))
}
private func handleDeactivationFinished() {
connection?.status = .disabled
if let connection = connection {
if connection.hasNativeTriggers {
connectionsRegistry.update(with: connection)
}
delegate?.connectButtonController(self, didFinishDeactivationWithResult: .success(connection))
}
}
private func handleDeactivationFailed(error: ConnectButtonControllerError) {
delegate?.connectButtonController(self, didFinishDeactivationWithResult: .failure(error))
}
private var credentialProvider: ConnectionCredentialProvider {
return connectionConfiguration.credentialProvider
}
/// An `ConnectButtonControllerDelegate` object that will recieved messages about events that happen on the `ConnectButtonController`.
public private(set) weak var delegate: ConnectButtonControllerDelegate?
private let connectionConfiguration: ConnectionConfiguration
private let connectionsRegistry = ConnectionsRegistry()
private let connectionHandoffFlow: ConnectionHandoffFlow
private let connectionNetworkController = ConnectionNetworkController()
private let serviceIconNetworkController = ServiceIconsNetworkController()
private let reachability = Reachability()
private let connectionVerificationSession: ConnectionVerificationSession
/// The locale that will be used for localizing the flow.
static var locale: Locale = {
return determineLocale()
}()
/// Determines the locale to use based on the user's preferred language and falls back to the current `Locale` chosen by the user.
///
/// - Returns: The `Locale` to use in localizing the flow.
public static func determineLocale() -> Locale {
guard let preferredLanguage = Locale.preferredLanguages.first else { return .current }
return Locale(identifier: preferredLanguage)
}
/// Creates a new `ConnectButtonController`.
///
/// - Parameters:
/// - connectButton: The `ConnectButton` that the controller is handling interaction for.
/// - connectionConfiguration: The `ConnectionConfiguration` with information for authenticating a `Connection`.
/// - locale: An overridable `Locale` that will get used to display strings in the SDK. The default is `nil`. If this parameter is set to `nil`, the user's current will be used.
/// - delegate: A `ConnectInteractionDelegate` to respond to various events that happen on the controller.
public init(connectButton: ConnectButton,
connectionConfiguration: ConnectionConfiguration,
locale: Locale = determineLocale(),
delegate: ConnectButtonControllerDelegate) {
self.button = connectButton
self.connectionConfiguration = connectionConfiguration
ConnectButtonController.locale = locale
self.connectionHandoffFlow = ConnectionHandoffFlow(connectionId: connectionConfiguration.connectionId,
credentialProvider: connectionConfiguration.credentialProvider,
activationRedirect: connectionConfiguration.redirectURL,
skipConnectionConfiguration: connectionConfiguration.skipConnectionConfiguration)
self.connection = connectionConfiguration.connection
self.delegate = delegate
self.connectionVerificationSession = ConnectionVerificationSession()
self.button.analyticsDelegate = self
setupKeychain(for: connectionConfiguration.credentialProvider)
setupConnection(for: connection, animated: false)
setupVerification()
}
private func setupKeychain(for credentialProvider: ConnectionCredentialProvider) {
Keychain.userToken = credentialProvider.userToken
Keychain.inviteCode = credentialProvider.inviteCode
ConnectButtonController.setup(with: credentialProvider)
}
private func setupVerification() {
connectionVerificationSession.completionHandler = { [weak self] (outcome) in
self?.handleOutcome(outcome)
}
}
private func handleOutcome(_ outcome: ConnectionVerificationSession.Outcome) {
var isUserCancelled = false
if case .error(let innerError) = outcome,
case .canceled = innerError {
isUserCancelled = true
}
connectionVerificationSession.dismiss(isUserCancelled: isUserCancelled) { [weak self] in
// Determine the next step based on the redirect result
let nextStep: ActivationStep = {
switch outcome {
case .error(let error):
return .failed(error)
case .token(let userToken):
return .activationComplete(userToken: userToken)
case .email:
return .enterEmail
}
}()
self?.transition(to: nextStep)
}
}
private func setupConnection(for connection: Connection?, animated: Bool) {
let emailValidator = EmailValidator()
if !ConnectionDeeplinkAction.isIftttAppAvailable && !emailValidator.validate(with: connectionConfiguration.suggestedUserEmail) {
assertionFailure("Unable to display connect button. This will only occur if the app is not installed and the email address is invalid.")
button.isHidden = true
return
}
button.minimumFooterLabelHeight = FooterMessages.estimatedMaximumTextHeight
guard let connection = connection else {
fetchConnection(for: connectionConfiguration.connectionId)
return
}
if connection.hasNativeTriggers {
connectionsRegistry.update(with: connection, shouldNotify: false)
}
Analytics.shared.track(.Impression,
location: .connectButtonImpression,
object: connection)
button.imageViewNetworkController = serviceIconNetworkController
serviceIconNetworkController.prefetchImages(for: connection)
button.configureEmailField(placeholderText: "button.email.placeholder".localized,
confirmButtonAsset: Assets.Button.emailConfirm)
switch connection.status {
case .initial, .unknown, .disabled:
// Disabled Connections are presented in the "Connect" state
transition(to: .initial(animated: animated))
case .enabled:
transition(to: .connected(animated: false))
}
}
private var connectionFetchingDataTask: URLSessionDataTask?
private func fetchConnection(for id: String,
numberOfRetries: Int = 3,
retryCount: Int = 0) {
button.animator(for: .buttonState(.loading(message: "button.state.loading".localized))).perform(animated: true)
button.animator(for: .footerValue(FooterMessages.worksWithIFTTT.value)).perform(animated: false)
connectionFetchingDataTask?.cancel()
connectionFetchingDataTask = nil
connectionFetchingDataTask = connectionNetworkController.start(request: .fetchConnection(for: id, credentialProvider: credentialProvider)) { [weak self] response in
guard let self = self else { return }
switch response.result {
case .success(let connection):
self.connection = connection
self.setupConnection(for: connection, animated: true)
case .failure:
if retryCount < numberOfRetries {
let count = retryCount + 1
DispatchQueue.main.asyncAfter(deadline: .now() + self.exponentialBackoffTiming(for: count)) {
self.fetchConnection(for: id, retryCount: count)
}
} else {
let footer = ConnectButtonController.FooterMessages.loadingFailed.value
self.button.animator(for: .buttonState(.loadingFailed, footerValue: footer)).perform(animated: true)
self.button.footerInteraction.isTapEnabled = true
self.button.footerInteraction.onSelect = { [weak self] in
self?.fetchConnection(for: id)
self?.reachability?.stopNotifier()
}
self.setupReachabilityForConnectionLoading(id: id)
}
}
}
}
private func setupReachabilityForConnectionLoading(id: String) {
reachability?.whenReachable = { [weak self] _ in
guard let self = self else {
return
}
self.fetchConnection(for: id)
self.reachability?.stopNotifier()
}
do {
try reachability?.startNotifier()
} catch {
assertionFailure("Reachability was unable to start notifications due to error: \(error).")
}
}
private func exponentialBackoffTiming(for retryCount: Int) -> DispatchTimeInterval {
let seconds = Int(pow(2, Double(retryCount)))
return .seconds(seconds)
}
private func buttonState(forConnectionStatus status: Connection.Status, service: Connection.Service, shouldAnimateKnob: Bool = true) -> ConnectButton.AnimationState {
switch status {
case .initial, .disabled, .unknown:
return .connect(service: service.connectButtonService,
message: "button.state.connect".localized(with: service.shortName))
case .enabled:
return .connected(service: service.connectButtonService,
message: "button.state.connected".localized,
shouldAnimateKnob: shouldAnimateKnob)
}
}
private func present(_ viewController: UIViewController) {
let presentingViewController = delegate?.presentingViewController(for: self)
presentingViewController?.present(viewController, animated: true, completion: nil)
}
// MARK: - Footer
/// Presents the about page
private func showAboutPage() {
guard let connection = connection else {
assertionFailure("It is expected and required that we have a non nil connection in this state.")
return
}
Analytics.shared.track(.Click,
location: Location(type: "connect_button", identifier: nil),
sourceLocation: nil,
object: AnalyticsObject.worksWithIFTTT,
state: nil)
let aboutViewController = AboutViewController(connection: connection, activationFlow: connectionHandoffFlow)
present(aboutViewController)
}
/// A comprehensive list of copy for the Connect Button's footer
///
/// - worksWithIFTTT: Default message used for most button states
/// - enterEmail: Message for the enter email step
/// - emailInvalid: Message when the entered email is invalid
private enum FooterMessages {
case worksWithIFTTT
case enterEmail
case emailInvalid
case creatingAccount(email: String)
case loadingFailed
private struct Constants {
static let errorTextColor = UIColor.red
static var footnoteFont: UIFont {
return StyleGuide.Font.footnote(weight: .demiBold)
}
static var iftttWordmarkFont: UIFont {
return StyleGuide.Font.footnote(weight: .heavy)
}
}
/// Our best guess of the maximum height of the footer label
fileprivate static var estimatedMaximumTextHeight: CGFloat {
// We are estimating that the text will never exceed 1 lines (plus some line spacing)
return 1.1 * Constants.footnoteFont.lineHeight
}
private var iftttWordmark: NSAttributedString {
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)
}
var attributedString: NSAttributedString {
switch self {
case .worksWithIFTTT:
let string = "button.footer.works_with".localized + " "
let text = NSMutableAttributedString(string: string,
attributes: [.font : Constants.footnoteFont])
text.append(iftttWordmark)
return text
case .enterEmail:
let string = "button.footer.email.prefix".localized + " "
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,
attributes: [.font : Constants.footnoteFont,
.underlineStyle : NSUnderlineStyle.single.rawValue]))
return text
case .emailInvalid:
let text = "button.footer.email.invalid".localized
return NSAttributedString(string: text,
attributes: [.font : Constants.footnoteFont,
.foregroundColor : Constants.errorTextColor])
case let .creatingAccount(email):
let string = "button.footer.accountCreation".localized(with: email)
let text = substituteIftttWordmark(with: string)
return text
case .loadingFailed:
let text = NSMutableAttributedString(string: "button.footer.loading.failed.prefix".localized,
attributes: [.font : Constants.footnoteFont,
.foregroundColor : Constants.errorTextColor])
text.append(NSAttributedString(string: " ")) // Adds a space before the underline starts
text.append(NSAttributedString(string: "button.footer.loading.failed.postfix".localized,
attributes: [.font : Constants.footnoteFont,
.foregroundColor : Constants.errorTextColor,
.underlineStyle : NSUnderlineStyle.single.rawValue]))
return text
}
}
}
// MARK: - Connection activation & deactivation
/// Defines the `Connection` authorization state machine
///
/// - initial: The `Connection` is in the initial state (not authorized)
/// - appHandoff: Links to the IFTTT app to complete the connection flow
/// - enterEmail: If we don't yet know the user, show the email field
/// - identifyUser: We will have an email address or an IFTTT service token. Use this information to determine if they are already an IFTTT user. Obviously they are if we have an IFTTT token, but we still need to get their username.
/// - activateConnection: Go to the web or the IFTTT app to activate this Connection. RedirectImmediately will cause the button to skip showing the going to service progress bar.
/// - activationComplete: Connection activation was successful. This always originates from a redirect. Passes the user token if it was included in the redirect.
/// - failed: The `Connection` could not be authorized due to some error.
/// - canceled: The `Connection` authorization was canceled.
/// - connected: The `Connection` was successfully authorized.
/// - processDisconnect: Disable the `Connection`.
/// - disconnected: The `Connection` was disabled.
enum ActivationStep {
case initial(animated: Bool)
case appHandoff(url: URL, redirectImmediately: Bool)
case enterEmail
case identifyUser(User.LookupMethod)
case activateConnection(user: User, redirectImmediately: Bool)
case activationComplete(userToken: String?)
case failed(ConnectButtonControllerError)
case canceled
case connected(animated: Bool)
case processDisconnect
case disconnected
}
/// State machine handling Applet activation and deactivation
private func transition(to step: ActivationStep) {
guard let connection = connection else {
assertionFailure("It is required to have a non nil `Connection` in order to handle activation and deactivation.")
return
}
// Cleanup
button.toggleInteraction = .init()
button.emailInteraction = .init()
button.footerInteraction.isTapEnabled = false // Don't clear the select block
switch step {
case .initial(let animated):
transitionToInitalization(connection: connection, animated: animated)
case .appHandoff(let url, let redirectImmediately):
transitionToAppHandoff(url: url, redirectImmediately: redirectImmediately)
case .enterEmail:
self.transition(to: .initial(animated: false))
self.button.animator(for: .buttonState(.enterEmail(service: connection.connectingService.connectButtonService, suggestedEmail: self.connectionConfiguration.suggestedUserEmail), footerValue: FooterMessages.enterEmail.value)).perform()
case .identifyUser(let lookupMethod):
transitionToIdentifyUser(connection: connection, lookupMethod: lookupMethod)
case .activateConnection(let user, let redirectImmediately):
transitionToActivate(connection: connection, user: user, redirectImmediately: redirectImmediately)
case .activationComplete(let userToken):
handleActivationFinished(userToken: userToken)
transitionToActivationComplete(service: connection.connectingService)
case .failed(let error):
transitionToFailed(error: error)
case .canceled:
transitionToCanceled(connection: connection)
case .connected(let animated):
transitionToConnected(connection: connection, animated: animated)
case .processDisconnect:
transitionToProcessDisconnect()
case .disconnected:
handleDeactivationFinished()
transitionToDisconnected(connection: connection)
}
}
private func transitionToInitalization(connection: Connection, animated: Bool) {
connectionVerificationSession.endActivationWebFlow()
button.footerInteraction.isTapEnabled = true
button.footerInteraction.onSelect = { [weak self] in
self?.showAboutPage()
}
let initialButtonState = ConnectButton.Transition.buttonState(buttonState(forConnectionStatus: connection.status,
service: connection.connectingService),
footerValue: FooterMessages.worksWithIFTTT.value)
button.animator(for: initialButtonState).perform(animated: animated)
button.toggleInteraction.isTapEnabled = true
button.toggleInteraction.isDragEnabled = true
let transition: (() -> ConnectButton.Transition)? = { [weak self] in
guard let self = self else {
return initialButtonState
}
if self.connectionHandoffFlow.isAppHandoffAvailable || self.credentialProvider.userToken != nil {
return .buttonState(.slideToConnect(service: nil, message: "button.state.verifying".localized))
} else {
return .buttonState(.enterEmail(service: connection.connectingService.connectButtonService, suggestedEmail: self.connectionConfiguration.suggestedUserEmail), footerValue: FooterMessages.enterEmail.value, duration: 0.5)
}
}
let toggle: VoidClosure = { [weak self] in
guard let self = self else { return }
if let token = self.credentialProvider.userToken {
self.transition(to: .identifyUser(.token(token)))
} else if let handoffURL = self.connectionHandoffFlow.appHandoffUrl(userId: nil) {
self.transition(to: .appHandoff(url: handoffURL, redirectImmediately: false))
}
}
button.toggleInteraction.toggleDragTransition = transition
button.toggleInteraction.toggleTapTransition = transition
button.toggleInteraction.onToggleTap = toggle
button.toggleInteraction.onToggleDrag = toggle
button.emailInteraction.onConfirm = { [weak self] email in
guard let self = self else {
assertionFailure("It is expected that `self` is not nil here.")
return
}
self.emailInteractionConfirmation(email: email)
}
}
/// Redirects to the IFTTT app for the app handoff connection flow
///
/// - Parameters:
/// - url: The handoff URL
/// - redirectImmediately: When true, do redirect immediately. Or delay to show progress bar.
private func transitionToAppHandoff(url: URL, redirectImmediately: Bool) {
let redirect = {
UIApplication.shared.open(url, options: [:]) { success in
if !success {
self.transition(to: .failed(.iftttAppRedirectFailed))
}
}
}
// Resets the button state after a app handoff
// Should the user return without completing the flow, they can just start over
// We will handle initial -> complete transition via the redirect
var token: Any?
token = NotificationCenter.default.addObserver(forName: UIApplication.didEnterBackgroundNotification,
object: nil,
queue: .main) { _ in
self.transition(to: .initial(animated: false))
if let token = token {
// This is one-time use. Remove it immediately.
NotificationCenter.default.removeObserver(token)
}
}
guard redirectImmediately == false else {
redirect()
return
}
let progress = button.showProgress(duration: 1)
progress.addCompletion { _ in
redirect()
}
progress.startAnimation()
}
private func transitionToIdentifyUser(connection: Connection, lookupMethod: User.LookupMethod) {
connectionVerificationSession.beginObservingSafariRedirects(with: lookupMethod)
button.animator(for: .buttonState(.verifying(message: "button.state.verifying".localized), footerValue: FooterMessages.worksWithIFTTT.value)).perform()
button.footerInteraction.isTapEnabled = true
// Pause at halfway here since we may follow up with the user account creation message
let progress = ProgressBarController(progressBar: button, pauseAt: 0.5)
progress.begin()
let dataTask = connectionNetworkController.fetchUser(lookupMethod: lookupMethod) { [weak self] result in
guard let self = self else {
return
}
switch result {
case .success(let user):
self.user = user
if user.isExistingUser {
// This is an existing IFTTT user but we don't have a token
// Send on to service authentication, web will take care of getting the user signed in
progress.finish {
if let handoffURL = self.connectionHandoffFlow.appHandoffUrl(userId: user.id) {
self.transition(to: .appHandoff(url: handoffURL, redirectImmediately: true))
} else {
self.transition(to: .activateConnection(user: user, redirectImmediately: true))
}
}
} else {
// There is no account for this user
// Show a fake message that we are creating an account
// Then move to the first step of the service connection flow
progress.wait(until: 0.5) {
self.button.animator(for: .buttonState(
.createAccount(message: "button.state.creating_account".localized),
footerValue: FooterMessages.creatingAccount(email: user.id.value).value)
).perform()
progress.finish(extendingDurationBy: 1.5) {
self.transition(to: .activateConnection(user: user, redirectImmediately: false))
}
}
}
case .failure(let error):
self.transition(to: .failed(.networkError(.init(networkControllerError: error))))
}
}
dataTask?.resume()
button.emailInteraction.onConfirm = { [weak self] email in
guard let self = self else {
assertionFailure("It is expected that `self` is not nil here.")
return
}
self.emailInteractionConfirmation(email: email)
}
}
private func transitionToActivate(connection: Connection, user: User, redirectImmediately: Bool) {
Analytics.shared.track(.Click,
location: .connectButtonLocation(connection),
object: connection)
let url = connectionHandoffFlow.webFlowUrl(user: user)
if redirectImmediately {
startWebConnectionVerification(with: url)
return
}
let service = connection.connectingService
button.footerInteraction.isTapEnabled = true
button.animator(for: .buttonState(.continueToService(service: service.connectButtonService,
message: "button.state.sign_in".localized(with: service.name)),
footerValue: FooterMessages.worksWithIFTTT.value)).perform()
let timeout = 2.0
button.showProgress(duration: timeout).startAnimation()
Timer.scheduledTimer(withTimeInterval: timeout, repeats: false) { [weak self] timer in
self?.startWebConnectionVerification(with: url)
timer.invalidate()
}
}
private func startWebConnectionVerification(with url: URL) {
if #available(iOS 13.0, *) {
guard let presentationContext = delegate?.webAuthenticationPresentationAnchor() else {
assertionFailure("It is expected for the web flow in iOS versions >= 13.0 to implement the `webAuthenticationPresentationAnchor()` delegate method of `ConnectButtonControllerDelegate`.")
return
}
connectionVerificationSession.start(with: url, in: presentationContext)
} else {
guard let viewController = delegate?.presentingViewController(for: self) else {
assertionFailure("It is expected for the web flow in iOS versions < 12 to implement the `presentingViewController(for connectButtonController: ConnectButtonController)` delegate method of `ConnectButtonControllerDelegate`.")
return
}
connectionVerificationSession.start(from: viewController, with: url)
}
}
private func transitionToActivationComplete(service: Connection.Service) {
Analytics.shared.track(.PageView,
location: .connectButtonLocation(connection))
button.animator(for: .buttonState(.connecting(service: service.connectButtonService, message: "button.state.connecting".localized),
footerValue: FooterMessages.worksWithIFTTT.value)).perform()
let progress = button.showProgress(duration: 2)
progress.perform()
progress.addCompletion { _ in
self.button.animator(for: .buttonState(.checkmark(service: service.connectButtonService))).perform()
DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
self.transition(to: .connected(animated: true))
}
}
}
private func transitionToFailed(error: ConnectButtonControllerError) {
handleActivationFailed(error: error)
transition(to: .initial(animated: false))
}
private func transitionToCanceled(connection: Connection) {
handleActivationFailed(error: .canceled)
transitionToInitalization(connection: connection, animated: true)
}
private func transitionToConnected(connection: Connection, animated: Bool) {
button.animator(for: .buttonState(buttonState(forConnectionStatus: .enabled, service: connection.connectingService, shouldAnimateKnob: animated), footerValue: FooterMessages.worksWithIFTTT.value)).perform()
button.footerInteraction.isTapEnabled = true
button.footerInteraction.onSelect = { [weak self] in
self?.showAboutPage()
}
// Toggle from here goes to disconnection confirmation
// When the user taps the switch, they are asked to confirm disconnection by dragging the switch into the off position
button.toggleInteraction.isTapEnabled = true
button.toggleInteraction.isDragEnabled = true
button.toggleInteraction.resistance = .heavy
let toggleTransition: () -> ConnectButton.Transition = {
return .buttonState(.disconnecting(message: "button.state.disconnecting".localized),
footerValue: FooterMessages.worksWithIFTTT.value)
}
let onToggleProcess: VoidClosure = { [weak self] in
self?.transition(to: .processDisconnect)
}
button.toggleInteraction.toggleTapTransition = toggleTransition
button.toggleInteraction.toggleDragTransition = toggleTransition
button.toggleInteraction.onToggleTap = onToggleProcess
button.toggleInteraction.onToggleDrag = onToggleProcess
button.toggleInteraction.onReverseDrag = { [weak self] in
self?.transition(to: .connected(animated: false))
}
}
private func transitionToProcessDisconnect() {
guard let connection = connection else {
assertionFailure("It is expected and required that we have a non nil connection in this state.")
return
}
Analytics.shared.track(.Click,
location: .connectButtonLocation(connection),
object: connection)
button.toggleInteraction = .init()
let progress = ProgressBarController(progressBar: button)
progress.begin()
let request = Connection.Request.disconnectConnection(with: connection.id, credentialProvider: credentialProvider)
connectionNetworkController.start(urlRequest: request.urlRequest) { response in
progress.finish {
switch response.result {
case .success(let connection):
self.connection = connection
self.transition(to: .disconnected)
case .failure(let error):
self.handleDeactivationFailed(error: .networkError(error))
self.transition(to: .connected(animated: true))
}
}
}
}
private func transitionToDisconnected(connection: Connection) {
button.animator(for: .buttonState(.disconnected(message: "button.state.disconnected".localized))).perform()
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
self.transition(to: .initial(animated: true))
}
}
private var emailFooterTimer: Timer?
private func emailInteractionConfirmation(email: String) {
emailFooterTimer?.invalidate()
emailFooterTimer = nil
Analytics.shared.track(.Click,
location: .connectButtonLocation(connection),
object: AnalyticsObject.email)
if email.isValidEmail {
self.transition(to: .identifyUser(.email(email)))
} else {
self.button.animator(for: .footerValue(FooterMessages.emailInvalid.value)).perform()
self.button.performInvalidEmailAnimation()
emailFooterTimer = Timer.scheduledTimer(withTimeInterval: 2.0, repeats: false) { [weak self] timer in
self?.button.animator(for: .footerValue(FooterMessages.enterEmail.value)).perform()
timer.invalidate()
}
}
}
}
extension ConnectButtonController: ConnectButtonAnalyticsDelegate {
func trackSuggestedEmailImpression() {
Analytics.shared.track(.Impression,
location: .connectButtonLocation(connection),
object: AnalyticsObject.email)
}
}
// MARK: - Convenience
private extension Connection.Service {
var connectButtonService: ConnectButton.Service {
return ConnectButton.Service(iconURL: templateIconURL, brandColor: brandColor)
}
}
private extension UIViewPropertyAnimator {
func perform(animated: Bool = true) {
if animated {
startAnimation()
} else {
startAnimation()
stopAnimation(false)
finishAnimation(at: .end)
}
}
}