Backport: Update alert

The alert backport was updated for simplicity. Reflect this inside
Ferrite's source code.

Signed-off-by: kingbri <bdashore3@proton.me>
This commit is contained in:
kingbri 2023-01-11 12:23:19 -05:00
parent e31f9a07fe
commit 6b0f90178b
4 changed files with 8 additions and 12 deletions

View file

@ -29,9 +29,9 @@ struct AlertButton: Identifiable {
} }
// Used for buttons with no action // Used for buttons with no action
init(_ label: String = "Cancel", role: Role? = nil) { init(_ label: String? = nil, role: Role? = nil) {
id = UUID() id = UUID()
self.label = label self.label = label ?? (role == .cancel ? "Cancel" : "OK")
action = {} action = {}
self.role = role self.role = role
} }

View file

@ -20,7 +20,7 @@ extension View {
} }
extension Backport where Content: View { extension Backport where Content: View {
@ViewBuilder func alert(isPresented: Binding<Bool>, title: String, message: String?, buttons: [AlertButton]) -> some View { @ViewBuilder func alert(isPresented: Binding<Bool>, title: String, message: String?, buttons: [AlertButton] = []) -> some View {
if #available(iOS 15, *) { if #available(iOS 15, *) {
content content
.alert( .alert(
@ -55,7 +55,7 @@ extension Backport where Content: View {
return Alert( return Alert(
title: Text(title), title: Text(title),
message: message.map { Text($0) } ?? nil, message: message.map { Text($0) } ?? nil,
dismissButton: buttons[0].toActionButton() dismissButton: buttons[safe: 0].map { $0.toActionButton() } ?? .cancel()
) )
} }
} }

View file

@ -35,8 +35,7 @@ struct SourceListEditorView: View {
.backport.alert( .backport.alert(
isPresented: $sourceManager.showUrlErrorAlert, isPresented: $sourceManager.showUrlErrorAlert,
title: "Error", title: "Error",
message: sourceManager.urlErrorAlertText, message: sourceManager.urlErrorAlertText
buttons: [AlertButton("OK")]
) )
.navigationTitle("Editing source list") .navigationTitle("Editing source list")
.navigationBarTitleDisplayMode(.inline) .navigationBarTitleDisplayMode(.inline)

View file

@ -118,10 +118,7 @@ struct MainView: View {
title: "Backup restored", title: "Backup restored",
message: backupManager.backupSourceNames.isEmpty ? message: backupManager.backupSourceNames.isEmpty ?
"No sources need to be reinstalled" : "No sources need to be reinstalled" :
"Reinstall sources: \(backupManager.backupSourceNames.joined(separator: ", "))", "Reinstall sources: \(backupManager.backupSourceNames.joined(separator: ", "))"
buttons: [
.init("OK") {}
]
) )
// Updater alert // Updater alert
.backport.alert( .backport.alert(
@ -129,14 +126,14 @@ struct MainView: View {
title: "Update available", title: "Update available",
message: "Ferrite \(releaseVersionString) can be downloaded. \n\n This alert can be disabled in Settings.", message: "Ferrite \(releaseVersionString) can be downloaded. \n\n This alert can be disabled in Settings.",
buttons: [ buttons: [
AlertButton("Download") { .init("Download") {
guard let releaseUrl = URL(string: releaseUrlString) else { guard let releaseUrl = URL(string: releaseUrlString) else {
return return
} }
UIApplication.shared.open(releaseUrl) UIApplication.shared.open(releaseUrl)
}, },
AlertButton(role: .cancel) .init(role: .cancel)
] ]
) )
.overlay { .overlay {