diff --git a/Shared/Account/AccountLoginView.swift b/Shared/Account/AccountLoginView.swift index d5ce962..1e3aa30 100644 --- a/Shared/Account/AccountLoginView.swift +++ b/Shared/Account/AccountLoginView.swift @@ -1,84 +1,84 @@ import SwiftUI struct AccountLoginView: View { @EnvironmentObject var model: WriteFreelyModel - @State private var isShowingAlert: Bool = false @State private var alertMessage: String = "" @State private var username: String = "" @State private var password: String = "" @State private var server: String = "" var body: some View { VStack { HStack { Image(systemName: "person.circle") .foregroundColor(.gray) #if os(iOS) TextField("Username", text: $username) .autocapitalization(.none) .disableAutocorrection(true) .textFieldStyle(RoundedBorderTextFieldStyle()) #else TextField("Username", text: $username) #endif } HStack { Image(systemName: "lock.circle") .foregroundColor(.gray) #if os(iOS) SecureField("Password", text: $password) .autocapitalization(.none) .disableAutocorrection(true) .textFieldStyle(RoundedBorderTextFieldStyle()) #else SecureField("Password", text: $password) #endif } HStack { Image(systemName: "link.circle") .foregroundColor(.gray) #if os(iOS) TextField("Server URL", text: $server) .keyboardType(.URL) .autocapitalization(.none) .disableAutocorrection(true) .textFieldStyle(RoundedBorderTextFieldStyle()) #else TextField("Server URL", text: $server) #endif } Spacer() if model.isLoggingIn { ProgressView("Logging in...") .padding() } else { Button(action: { model.login( to: URL(string: server)!, as: username, password: password ) }, label: { Text("Login") }) .disabled( model.account.isLoggedIn || (username.isEmpty || password.isEmpty || server.isEmpty) ) .padding() } } - .alert(isPresented: $isShowingAlert) { - Alert( + .alert(isPresented: $model.account.hasError) { + guard let accountError = model.account.currentError else { fatalError() } + return Alert( title: Text("Error Logging In"), message: Text(accountError.localizedDescription), dismissButton: .default(Text("OK")) ) } } } struct AccountLoginView_Previews: PreviewProvider { static var previews: some View { AccountLoginView() .environmentObject(WriteFreelyModel()) } } diff --git a/Shared/Account/AccountModel.swift b/Shared/Account/AccountModel.swift index 22a9a74..9f20628 100644 --- a/Shared/Account/AccountModel.swift +++ b/Shared/Account/AccountModel.swift @@ -1,46 +1,52 @@ import Foundation import WriteFreely enum AccountError: Error { case invalidPassword case usernameNotFound case serverNotFound } extension AccountError: LocalizedError { public var errorDescription: String? { switch self { case .serverNotFound: return NSLocalizedString( "The server could not be found. Please check the information you've entered and try again.", comment: "" ) case .invalidPassword: return NSLocalizedString( "Invalid password. Please check that you've entered your password correctly and try logging in again.", comment: "" ) case .usernameNotFound: return NSLocalizedString( "Username not found. Did you use your email address by mistake?", comment: "" ) } } } struct AccountModel { var server: String = "" + var hasError: Bool = false + var currentError: AccountError? { + didSet { + hasError = true + } + } private(set) var user: WFUser? private(set) var isLoggedIn: Bool = false mutating func login(_ user: WFUser) { self.user = user self.isLoggedIn = true } mutating func logout() { self.user = nil self.isLoggedIn = false } }