Fixed the progress indicator in the login button of the LoginView view for the Login module.

This commit is contained in:
Javier Cicchelli 2022-12-18 15:06:38 +01:00
parent 011401a855
commit 76814cfe8b

View File

@ -73,8 +73,7 @@ fileprivate extension LoginView {
VStack(spacing: 32) { VStack(spacing: 32) {
Text( Text(
"login.title.text", "login.title.text",
bundle: .module, bundle: .module
comment: "Login view title text."
) )
.font(.largeTitle) .font(.largeTitle)
.fontWeight(.bold) .fontWeight(.bold)
@ -94,13 +93,13 @@ fileprivate extension LoginView {
Label { Label {
Text( Text(
"login.button.log_in.text", "login.button.log_in.text",
bundle: .module, bundle: .module
comment: "Log in button text."
) )
.fontWeight(.semibold) .fontWeight(.semibold)
} icon: { } icon: {
if isAuthenticating { if isAuthenticating {
ProgressView() ProgressView()
.tint(.white)
.controlSize(.regular) .controlSize(.regular)
} else { } else {
EmptyView() EmptyView()
@ -128,7 +127,9 @@ private extension LoginView.LoginContainer {
// MARK: Computed // MARK: Computed
var isLoginDisabled: Bool { var isLoginDisabled: Bool {
username.isEmpty || password.isEmpty username.isEmpty
|| password.isEmpty
|| errorMessage != nil
} }
// MARK: Functions // MARK: Functions
@ -136,23 +137,28 @@ private extension LoginView.LoginContainer {
func authenticate() async { func authenticate() async {
guard isAuthenticating else { return } guard isAuthenticating else { return }
defer { isAuthenticating = false }
do { 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( authenticated(
.init( .init(
username: username, username: username,
password: password password: password
), ),
try await getUser( user
username: username,
password: password
)
) )
} catch APIClientError.authenticationFailed { } catch APIClientError.authenticationFailed {
errorMessage = "login.error.authentication_failed.text" errorMessage = "login.error.authentication_failed.text"
isAuthenticating = false
} catch { } catch {
errorMessage = "login.error.authentication_unknown.text" errorMessage = "login.error.authentication_unknown.text"
isAuthenticating = false
} }
} }
@ -162,6 +168,14 @@ private extension LoginView.LoginContainer {
public typealias AuthenticatedClosure = (Account, User) -> Void 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 // MARK: - Previews
struct LoginView_Previews: PreviewProvider { struct LoginView_Previews: PreviewProvider {