@@ -15,8 +15,6 @@ final class InPersonPaymentsMenuViewController: UIViewController {
1515 private let cardPresentPaymentsOnboardingUseCase : CardPresentPaymentsOnboardingUseCase
1616 private var cancellables : Set < AnyCancellable > = [ ]
1717
18- private var cardPresentPaymentsOnboardingPresenter : CardPresentPaymentsOnboardingPresenting ?
19-
2018 /// Main TableView
2119 ///
2220 private lazy var tableView : UITableView = {
@@ -75,41 +73,43 @@ private extension InPersonPaymentsMenuViewController {
7573 . debounce ( for: . milliseconds( 100 ) , scheduler: DispatchQueue . main)
7674 . removeDuplicates ( )
7775 . sink ( receiveValue: { [ weak self] state in
78- guard let self = self else { return }
79-
80- self . pluginState = nil
81-
82- guard state != . loading else {
83- self . activityIndicator? . startAnimating ( )
84- return
85- }
86-
87- switch state {
88- case let . completed( newPluginState) :
89- self . pluginState = newPluginState
90- self . dismissCardPresentPaymentsOnboardingNoticeIfPresent ( )
91- case let . selectPlugin( pluginSelectionWasCleared) :
92- // If it was cleared it means that we triggered it manually (e.g by tapping in this view on the plugin selection row)
93- // No need to show the onboarding notice
94- if !pluginSelectionWasCleared {
95- self . showCardPresentPaymentsOnboardingNotice ( )
96- }
97- default :
98- self . showCardPresentPaymentsOnboardingNotice ( )
99- }
100-
101- self . activityIndicator? . stopAnimating ( )
102- self . configureSections ( )
103- self . tableView. reloadData ( )
76+ self ? . refreshAfterNewOnboardingState ( state)
10477 } ) . store ( in: & cancellables)
10578 }
10679
80+ func refreshAfterNewOnboardingState( _ state: CardPresentPaymentOnboardingState ) {
81+ self . pluginState = nil
82+
83+ guard state != . loading else {
84+ self . activityIndicator? . startAnimating ( )
85+ return
86+ }
87+
88+ switch state {
89+ case let . completed( newPluginState) :
90+ self . pluginState = newPluginState
91+ self . dismissCardPresentPaymentsOnboardingNoticeIfPresent ( )
92+ case let . selectPlugin( pluginSelectionWasCleared) :
93+ // If it was cleared it means that we triggered it manually (e.g by tapping in this view on the plugin selection row)
94+ // No need to show the onboarding notice
95+ if !pluginSelectionWasCleared {
96+ self . showCardPresentPaymentsOnboardingNotice ( )
97+ }
98+ default :
99+ self . showCardPresentPaymentsOnboardingNotice ( )
100+ }
101+
102+ self . activityIndicator? . stopAnimating ( )
103+ self . configureSections ( )
104+ self . tableView. reloadData ( )
105+ }
106+
107107 func showCardPresentPaymentsOnboardingNotice( ) {
108108 let permanentNotice = PermanentNotice ( message: Localization . inPersonPaymentsSetupNotFinishedNotice,
109109 callToActionTitle: Localization . inPersonPaymentsSetupNotFinishedNoticeButtonTitle,
110110 callToActionHandler: { [ weak self] in
111111 ServiceLocator . analytics. track ( . paymentsMenuOnboardingErrorTapped)
112- self ? . showOnboardingIfRequired ( )
112+ self ? . showOnboarding ( )
113113 } )
114114
115115 permanentNoticePresenter. presentNotice ( notice: permanentNotice, from: self )
@@ -119,18 +119,23 @@ private extension InPersonPaymentsMenuViewController {
119119 permanentNoticePresenter. dismiss ( )
120120 }
121121
122- func showOnboardingIfRequired ( ) {
122+ func showOnboarding ( ) {
123123 guard let navigationController = self . navigationController else {
124124 return
125125 }
126126
127- // Recreating it ensures that the readiness state is up to date.
128- // Keeping a reference ensures that the callback closure is retained.
129- cardPresentPaymentsOnboardingPresenter = CardPresentPaymentsOnboardingPresenter ( )
130-
131- cardPresentPaymentsOnboardingPresenter? . showOnboardingIfRequired ( from: navigationController) { [ weak self] in
132- self ? . cardPresentPaymentsOnboardingUseCase. refresh ( )
127+ // Instead of using `CardPresentPaymentsOnboardingPresenter` we create the view directly because we already have the onboarding state in the use case.
128+ // That way we avoid triggering the onboarding check again that comes with the presenter.
129+ let onboardingViewModel = InPersonPaymentsViewModel ( useCase: cardPresentPaymentsOnboardingUseCase, showMenuOnCompletion: false )
130+ onboardingViewModel. onOnboardingCompletion = { [ weak self] plugin in
131+ self ? . refreshAfterNewOnboardingState ( . completed( plugin: plugin) )
132+ if navigationController. visibleViewController is InPersonPaymentsViewController {
133+ navigationController. popViewController ( animated: true )
134+ }
133135 }
136+
137+ let onboardingViewController = InPersonPaymentsViewController ( viewModel: onboardingViewModel)
138+ show ( onboardingViewController, sender: self )
134139 }
135140}
136141
0 commit comments