Skip to content
This repository was archived by the owner on Feb 5, 2025. It is now read-only.

Commit 40549a6

Browse files
authored
Merge pull request #596 from wordpress-mobile/issue/wc-4034-fix-login-ui
Fix Site Address UI Bugs When Displaying In-Page Errors
2 parents e68ef09 + d6447e7 commit 40549a6

File tree

2 files changed

+50
-20
lines changed

2 files changed

+50
-20
lines changed

WordPressAuthenticator/Unified Auth/View Related/Site Address/SiteAddressViewController.swift

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ final class SiteAddressViewController: LoginViewController {
1919
private var errorMessage: String?
2020
private var shouldChangeVoiceOverFocus: Bool = false
2121

22+
/// A state variable that is `true` if network calls are currently happening and so the
23+
/// view should be showing a loading indicator.
24+
///
25+
/// This should only be modified within `configureViewLoading(_ loading:)`.
26+
///
27+
/// This state is mainly used in `configureSubmitButton()` to determine whether the button
28+
/// should show an activity indicator.
29+
private var viewIsLoading: Bool = false
30+
2231
// MARK: - Actions
2332
@IBAction func handleContinueButtonTapped(_ sender: NUXButton) {
2433
tracker.track(click: .submit)
@@ -36,15 +45,15 @@ final class SiteAddressViewController: LoginViewController {
3645
localizePrimaryButton()
3746
registerTableViewCells()
3847
loadRows()
39-
configureSubmitButton(animating: false)
48+
configureSubmitButton()
4049
configureForAccessibility()
4150
}
4251

4352
override func viewWillAppear(_ animated: Bool) {
4453
super.viewWillAppear(animated)
4554

4655
siteURLField?.text = loginFields.siteAddress
47-
configureSubmitButton(animating: false)
56+
configureSubmitButton()
4857
}
4958

5059
override func viewDidAppear(_ animated: Bool) {
@@ -82,6 +91,14 @@ final class SiteAddressViewController: LoginViewController {
8291
return WordPressAuthenticator.shared.unifiedStyle?.statusBarStyle ?? WordPressAuthenticator.shared.style.statusBarStyle
8392
}
8493

94+
/// Configures the appearance and state of the submit button.
95+
///
96+
/// Use this instead of the overridden `configureSubmitButton(animating:)` since this uses the
97+
/// _current_ `viewIsLoading` state.
98+
private func configureSubmitButton() {
99+
configureSubmitButton(animating: viewIsLoading)
100+
}
101+
85102
/// Configures the appearance and state of the submit button.
86103
///
87104
override func configureSubmitButton(animating: Bool) {
@@ -120,9 +137,11 @@ final class SiteAddressViewController: LoginViewController {
120137
/// - Parameter loading: True if the form should be configured to a "loading" state.
121138
///
122139
override func configureViewLoading(_ loading: Bool) {
140+
viewIsLoading = loading
141+
123142
siteURLField?.isEnabled = !loading
124143

125-
configureSubmitButton(animating: loading)
144+
configureSubmitButton()
126145
navigationItem.hidesBackButton = loading
127146
}
128147

@@ -183,21 +202,6 @@ extension SiteAddressViewController: UITableViewDataSource {
183202
}
184203
}
185204

186-
// MARK: - UITableViewDelegate conformance
187-
extension SiteAddressViewController: UITableViewDelegate {
188-
/// After the site address textfield cell is done displaying, remove the textfield reference.
189-
///
190-
func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
191-
guard let row = rows[safe: indexPath.row] else {
192-
return
193-
}
194-
195-
if row == .siteAddress {
196-
siteURLField = nil
197-
}
198-
}
199-
}
200-
201205
// MARK: - Keyboard Notifications
202206
extension SiteAddressViewController: NUXKeyboardResponder {
203207
@objc func handleKeyboardWillShow(_ notification: Foundation.Notification) {
@@ -304,9 +308,10 @@ private extension SiteAddressViewController {
304308
// Save a reference to the first textField so it can becomeFirstResponder.
305309
siteURLField = cell.textField
306310
cell.textField.delegate = self
311+
cell.textField.text = loginFields.siteAddress
307312
cell.onChangeSelectionHandler = { [weak self] textfield in
308313
self?.loginFields.siteAddress = textfield.nonNilTrimmedText()
309-
self?.configureSubmitButton(animating: false)
314+
self?.configureSubmitButton()
310315
}
311316

312317
SigninEditingState.signinEditingStateActive = true
@@ -462,7 +467,6 @@ private extension SiteAddressViewController {
462467
}
463468

464469
func fetchSiteInfo() {
465-
print("🔴 SAVC > fetchSiteInfo")
466470
let baseSiteUrl = WordPressAuthenticator.baseSiteURL(string: loginFields.siteAddress)
467471
let service = WordPressComBlogService()
468472

@@ -489,6 +493,26 @@ private extension SiteAddressViewController {
489493
}
490494

491495
func presentNextControllerIfPossible(siteInfo: WordPressComSiteInfo?) {
496+
497+
// Ensure that we're using the verified URL before passing the `loginFields` to the next
498+
// view controller.
499+
//
500+
// In some scenarios, the text field change callback in `configureTextField()` gets executed
501+
// right after we validated and modified `loginFields.siteAddress` in `validateForm()`. And
502+
// this causes the value of `loginFields.siteAddress` to be reset to what the user entered.
503+
//
504+
// Using the user-entered `loginFields.siteAddress` causes problems when we try to log
505+
// the user in especially if they just use a domain. For example, validating their
506+
// self-hosted site credentials fails because the
507+
// `WordPressOrgXMLRPCValidator.guessXMLRPCURLForSite` expects a complete site URL.
508+
//
509+
// This routine fixes that problem. We'll use what we already validated from
510+
// `fetchSiteInfo()`.
511+
//
512+
if let verifiedSiteAddress = siteInfo?.url {
513+
loginFields.siteAddress = verifiedSiteAddress
514+
}
515+
492516
guard siteInfo?.isWPCom == false else {
493517
showGetStarted()
494518
return

WordPressAuthenticatorTests/Authenticator/WordPressAuthenticatorTests.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ class WordPressAuthenticatorTests: XCTestCase {
4949
XCTAssert(url == punycode)
5050
}
5151

52+
func testBaseSiteURLKeepsHTTPSchemeForNonWPSites() {
53+
let url = "http://selfhostedsite.com"
54+
let correctedURL = WordPressAuthenticator.baseSiteURL(string: url)
55+
XCTAssertEqual(correctedURL, url)
56+
}
57+
5258
// MARK: WordPressAuthenticator Notification Tests
5359
func testDispatchesSupportPushNotificationReceived() {
5460
let authenticator = WordpressAuthenticatorProvider.getWordpressAuthenticator()

0 commit comments

Comments
 (0)