mirror of
https://github.com/writeas/writefreely-swiftui-multiplatform.git
synced 2024-11-15 01:11:02 +00:00
Simplify login error-handling process
This commit is contained in:
parent
0bde76c7d8
commit
be69225253
@ -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"))
|
||||
)
|
||||
}
|
||||
|
@ -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
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user