Simplify login error-handling process

This commit is contained in:
Angelo Stavrow 2020-09-25 11:00:51 -04:00
parent 0bde76c7d8
commit be69225253
No known key found for this signature in database
GPG Key ID: 1A49C7064E060EEE
3 changed files with 18 additions and 13 deletions

View File

@ -65,11 +65,10 @@ struct AccountLoginView: View {
.padding()
}
}
.alert(isPresented: $model.account.hasError) {
guard let accountError = model.account.currentError else { fatalError() }
return Alert(
.alert(isPresented: $model.isPresentingLoginErrorAlert) {
Alert(
title: Text("Error Logging In"),
message: Text(accountError.localizedDescription),
message: Text(model.loginErrorMessage ?? "An unknown error occurred while trying to login."),
dismissButton: .default(Text("OK"))
)
}

View File

@ -37,12 +37,7 @@ struct AccountModel {
var server: String = ""
var username: String = ""
var hasError: Bool = false
var currentError: AccountError? {
didSet {
hasError = true
}
}
private(set) var user: WFUser?
private(set) var isLoggedIn: Bool = false

View File

@ -26,11 +26,14 @@ class WriteFreelyModel: ObservableObject {
}
}
@Published var isPresentingDeleteAlert: Bool = false
@Published var isPresentingLoginErrorAlert: Bool = false
@Published var postToDelete: WFAPost?
#if os(iOS)
@Published var isPresentingSettingsView: Bool = false
#endif
var loginErrorMessage: String?
// swiftlint:disable line_length
let helpURL = URL(string: "https://discuss.write.as/c/help/5")!
let licensesURL = URL(string: "https://github.com/writeas/writefreely-swiftui-multiplatform/tree/main/Shared/Resources/Licenses")!
@ -188,17 +191,25 @@ private extension WriteFreelyModel {
}
} catch WFError.notFound {
DispatchQueue.main.async {
self.account.currentError = AccountError.usernameNotFound
self.loginErrorMessage = AccountError.usernameNotFound.localizedDescription
self.isPresentingLoginErrorAlert = true
}
} catch WFError.unauthorized {
DispatchQueue.main.async {
self.account.currentError = AccountError.invalidPassword
self.loginErrorMessage = AccountError.invalidPassword.localizedDescription
self.isPresentingLoginErrorAlert = true
}
} catch {
if (error as NSError).domain == NSURLErrorDomain,
(error as NSError).code == -1003 {
DispatchQueue.main.async {
self.account.currentError = AccountError.serverNotFound
self.loginErrorMessage = AccountError.serverNotFound.localizedDescription
self.isPresentingLoginErrorAlert = true
}
} else {
DispatchQueue.main.async {
self.loginErrorMessage = error.localizedDescription
self.isPresentingLoginErrorAlert = true
}
}
}