Skip to content

Commit 78e298a

Browse files
committed
Update WrongAccountErrorViewModel to conform new changes in ULAccountMismatchViewModel
1 parent d0d97cb commit 78e298a

File tree

1 file changed

+16
-28
lines changed

1 file changed

+16
-28
lines changed

WooCommerce/Classes/Authentication/Navigation Exceptions/WrongAccountErrorViewModel.swift

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,10 @@ final class WrongAccountErrorViewModel: ULAccountMismatchViewModel {
2424
private var siteUsername: String = ""
2525
private var jetpackConnectionURL: URL?
2626

27-
private let primaryButtonHiddenSubject = CurrentValueSubject<Bool, Never>(true)
28-
private let primaryButtonLoadingSubject = CurrentValueSubject<Bool, Never>(false)
29-
private let activityIndicatorLoadingSubject = CurrentValueSubject<Bool, Never>(false)
27+
@Published private var isSelfHostedSite = false
28+
@Published private var primaryButtonLoading = false
3029

31-
private var primaryButtonSubscription: AnyCancellable?
30+
private var siteInfoSubscription: AnyCancellable?
3231

3332
init(siteURL: String?,
3433
showsConnectedStores: Bool,
@@ -96,19 +95,12 @@ final class WrongAccountErrorViewModel: ULAccountMismatchViewModel {
9695

9796
let secondaryButtonTitle = Localization.secondaryButtonTitle
9897

99-
var isPrimaryButtonHidden: AnyPublisher<Bool, Never> {
100-
primaryButtonHiddenSubject.eraseToAnyPublisher()
101-
}
102-
10398
var isPrimaryButtonLoading: AnyPublisher<Bool, Never> {
104-
primaryButtonLoadingSubject.eraseToAnyPublisher()
99+
$primaryButtonLoading.eraseToAnyPublisher()
105100
}
106101

107102
var isSecondaryButtonHidden: Bool { !showsConnectedStores }
108103

109-
var isShowingActivityIndicator: AnyPublisher<Bool, Never> {
110-
activityIndicatorLoadingSubject.eraseToAnyPublisher()
111-
}
112104

113105
// Configures `Help` button title
114106
var rightBarButtonItemTitle: String? {
@@ -117,19 +109,16 @@ final class WrongAccountErrorViewModel: ULAccountMismatchViewModel {
117109

118110
// MARK: - Actions
119111
func viewDidLoad(_ viewController: UIViewController?) {
120-
primaryButtonSubscription = primaryButtonHiddenSubject
112+
siteInfoSubscription = $isSelfHostedSite
121113
.dropFirst() // ignores first element
122-
.sink { [weak self] isHidden in
114+
.sink { [weak self] isSelfHosted in
123115
// if the button is hidden, the site is not self-hosted.
124-
self?.analytics.track(event: .LoginJetpackConnection.jetpackConnectionErrorShown(selfHostedSite: !isHidden))
116+
self?.analytics.track(event: .LoginJetpackConnection.jetpackConnectionErrorShown(selfHostedSite: isSelfHosted))
125117
}
126118

127119
// Fetches site info if we're not sure whether the site is self-hosted.
128120
if siteXMLRPC.isEmpty {
129121
fetchSiteInfo()
130-
} else {
131-
// Shows the Connect Jetpack button if the site is self-hosted
132-
primaryButtonHiddenSubject.send(false)
133122
}
134123
}
135124

@@ -140,7 +129,10 @@ final class WrongAccountErrorViewModel: ULAccountMismatchViewModel {
140129
}
141130

142131
guard let url = jetpackConnectionURL else {
143-
return showSiteCredentialLoginAndJetpackConnection(from: viewController)
132+
if isSelfHostedSite {
133+
return showSiteCredentialLoginAndJetpackConnection(from: viewController)
134+
}
135+
return // TODO: show alert
144136
}
145137

146138
showJetpackConnectionWebView(url: url, from: viewController)
@@ -182,18 +174,14 @@ private extension WrongAccountErrorViewModel {
182174
/// If the site is self-hosted, make the Connect Jetpack button visible.
183175
///
184176
func fetchSiteInfo() {
185-
activityIndicatorLoadingSubject.send(true)
177+
primaryButtonLoading = true
186178
authenticatorType.fetchSiteInfo(for: siteURL) { [weak self] result in
187179
guard let self = self else { return }
188-
self.activityIndicatorLoadingSubject.send(false)
180+
self.primaryButtonLoading = false
189181

190182
switch result {
191183
case .success(let siteInfo):
192-
if siteInfo.isWPCom == false {
193-
self.primaryButtonHiddenSubject.send(false)
194-
} else {
195-
self.primaryButtonHiddenSubject.send(true)
196-
}
184+
self.isSelfHostedSite = !siteInfo.isWPCom
197185
case .failure(let error):
198186
DDLogWarn("⚠️ Error fetching site info: \(error)")
199187
}
@@ -214,10 +202,10 @@ private extension WrongAccountErrorViewModel {
214202
/// Fetches the URL for handling Jetpack connection in a web view
215203
///
216204
func fetchJetpackConnectionURL(onCompletion: ((URL) -> Void)? = nil) {
217-
primaryButtonLoadingSubject.send(true)
205+
primaryButtonLoading = true
218206
let action = JetpackConnectionAction.fetchJetpackConnectionURL { [weak self] result in
219207
guard let self = self else { return }
220-
self.primaryButtonLoadingSubject.send(false)
208+
self.primaryButtonLoading = false
221209
switch result {
222210
case .success(let url):
223211
onCompletion?(url)

0 commit comments

Comments
 (0)