Skip to content

Commit

Permalink
Merge pull request #90 from kishikawakatsumi/alert
Browse files Browse the repository at this point in the history
Improve alert message
  • Loading branch information
kishikawakatsumi authored Jul 27, 2024
2 parents 92548f8 + 32f8be3 commit 07beb67
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 12 deletions.
25 changes: 17 additions & 8 deletions Examples/FileBrowser/FileBrowser (iOS)/ConnectServiceView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import SwiftUI
import SMBClient

struct ConnectServiceView: View {
enum FocusedField {
private enum FocusedField {
case username
case password
}
Expand All @@ -19,6 +19,9 @@ struct ConnectServiceView: View {
@FocusState
private var focusedField: FocusedField?

@State private var presentLocalizedAlert: Bool = false
@State private var localizedError: ErrorResponse? = nil

@State private var presentAlert: Bool = false
@State private var error: Error? = nil

Expand Down Expand Up @@ -82,12 +85,15 @@ struct ConnectServiceView: View {
guard canSubmit else { return }
submit()
}
.alert(isPresented: $presentAlert) {
if let error {
Alert(title: Text("Login Failed"), message: Text(error.localizedDescription), dismissButton: .default(Text("Close")))
} else {
Alert(title: Text("Login Failed"), dismissButton: .default(Text("Close")))
}
.alert(isPresented: $presentLocalizedAlert, error: localizedError) { _ in
Button("Close") {}
} message: { error in
Text(error.failureReason ?? error.recoverySuggestion ?? "")
}
.alert("", isPresented: $presentAlert) {
Button("Close") {}
} message: {
Text(error?.localizedDescription ?? "")
}
}
}
Expand All @@ -100,9 +106,12 @@ struct ConnectServiceView: View {

dismiss()
onSuccess(username, password, client)
} catch let error as ErrorResponse {
self.localizedError = error
presentLocalizedAlert = true
} catch {
presentAlert = true
self.error = error
presentAlert = true
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,15 @@ class DocumentViewController: UIViewController {

extension DocumentViewController: WKNavigationDelegate {
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: any Error) {
let controller = UIAlertController(title: "", message: error.localizedDescription, preferredStyle: .alert)
controller.addAction(UIAlertAction(title: NSLocalizedString("Close", comment: ""), style: .default))
present(controller, animated: true)
if let error = error as? LocalizedError {
let controller = UIAlertController(title: error.errorDescription, message: error.failureReason, preferredStyle: .alert)
controller.addAction(UIAlertAction(title: NSLocalizedString("Close", comment: ""), style: .default))
present(controller, animated: true)
} else {
let controller = UIAlertController(title: "", message: error.localizedDescription, preferredStyle: .alert)
controller.addAction(UIAlertAction(title: NSLocalizedString("Close", comment: ""), style: .default))
present(controller, animated: true)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ class FilesViewController: UIViewController, UITableViewDataSource, UITableViewD
.sorted { $0.name < $1.name }
self.files.append(contentsOf: files)
tableView.reloadData()
} catch let error as LocalizedError {
let controller = UIAlertController(title: error.errorDescription, message: error.failureReason, preferredStyle: .alert)
controller.addAction(UIAlertAction(title: NSLocalizedString("Close", comment: ""), style: .default))
present(controller, animated: true)
} catch {
let controller = UIAlertController(title: "", message: error.localizedDescription, preferredStyle: .alert)
controller.addAction(UIAlertAction(title: NSLocalizedString("Close", comment: ""), style: .default))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ class SharesViewController: UIViewController, UITableViewDataSource, UITableView
self.shares.append(contentsOf: shares)

tableView.reloadData()
} catch let error as LocalizedError {
let controller = UIAlertController(title: error.errorDescription, message: error.failureReason, preferredStyle: .alert)
controller.addAction(UIAlertAction(title: NSLocalizedString("Close", comment: ""), style: .default))
present(controller, animated: true)
} catch {
let controller = UIAlertController(title: "", message: error.localizedDescription, preferredStyle: .alert)
controller.addAction(UIAlertAction(title: NSLocalizedString("Close", comment: ""), style: .default))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"location" : "https://github.com/kishikawakatsumi/SMBClient.git",
"state" : {
"branch" : "main",
"revision" : "f1710f78e9ed3a7f4de60c29f28294f62aa95f8d"
"revision" : "92548f8cb10f5d741af0a7ab02acddf2bbacea1c"
}
}
],
Expand Down

0 comments on commit 07beb67

Please sign in to comment.