From 76814cfe8bbdcd84f028c457189145cd68e16e47 Mon Sep 17 00:00:00 2001 From: Javier Cicchelli Date: Sun, 18 Dec 2022 15:06:38 +0100 Subject: [PATCH] Fixed the progress indicator in the login button of the LoginView view for the Login module. --- .../Sources/Login/UI/Views/LoginView.swift | 36 +++++++++++++------ 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/Modules/Sources/Login/UI/Views/LoginView.swift b/Modules/Sources/Login/UI/Views/LoginView.swift index a88dade..e2977b4 100644 --- a/Modules/Sources/Login/UI/Views/LoginView.swift +++ b/Modules/Sources/Login/UI/Views/LoginView.swift @@ -73,8 +73,7 @@ fileprivate extension LoginView { VStack(spacing: 32) { Text( "login.title.text", - bundle: .module, - comment: "Login view title text." + bundle: .module ) .font(.largeTitle) .fontWeight(.bold) @@ -94,13 +93,13 @@ fileprivate extension LoginView { Label { Text( "login.button.log_in.text", - bundle: .module, - comment: "Log in button text." + bundle: .module ) .fontWeight(.semibold) } icon: { if isAuthenticating { ProgressView() + .tint(.white) .controlSize(.regular) } else { EmptyView() @@ -128,7 +127,9 @@ private extension LoginView.LoginContainer { // MARK: Computed var isLoginDisabled: Bool { - username.isEmpty || password.isEmpty + username.isEmpty + || password.isEmpty + || errorMessage != nil } // MARK: Functions @@ -136,23 +137,28 @@ private extension LoginView.LoginContainer { func authenticate() async { guard isAuthenticating else { return } + defer { isAuthenticating = false } + do { + let user = try await getUser( + username: username, + password: password + ) + + // Added some throttle (1 second) not to hide the loading indicator right away. + try await Task.sleep(nanoseconds: .Constants.secondInNanoseconds) + authenticated( .init( username: username, password: password ), - try await getUser( - username: username, - password: password - ) + user ) } catch APIClientError.authenticationFailed { errorMessage = "login.error.authentication_failed.text" - isAuthenticating = false } catch { errorMessage = "login.error.authentication_unknown.text" - isAuthenticating = false } } @@ -162,6 +168,14 @@ private extension LoginView.LoginContainer { public typealias AuthenticatedClosure = (Account, User) -> Void +// MARK: - UInt64+Constants + +private extension UInt64 { + enum Constants { + static let secondInNanoseconds = UInt64(1 * Double(NSEC_PER_SEC)) + } +} + // MARK: - Previews struct LoginView_Previews: PreviewProvider {