Implemented convenience method to show an Alert from a Message

This commit is contained in:
dankito 2020-09-07 16:16:45 +02:00
parent 7dc43aa369
commit 6b922fc2c6
9 changed files with 26 additions and 34 deletions

View File

@ -105,6 +105,19 @@ extension View {
method()
}
}
func alert(message: Binding<Message?>) -> some View {
return self.alert(item: message) { message in
if let seconaryButton = message.secondaryButton {
return Alert(title: message.title, message: message.message, primaryButton: message.primaryButton, secondaryButton: seconaryButton)
}
else {
return Alert(title: message.title, message: message.message, dismissButton: message.primaryButton)
}
}
}
}
@ -123,6 +136,8 @@ extension Color {
static let systemGroupedBackground = Color(UIColor.systemGroupedBackground)
// There are more..
static var destructive: Color {
if UIColor.responds(to: Selector(("_systemDestructiveTintColor"))) {
if let red = UIColor.perform(Selector(("_systemDestructiveTintColor")))?.takeUnretainedValue() as? UIColor {
@ -132,22 +147,20 @@ extension Color {
return Color.red
}
// There are more..
}
extension Alert.Button {
public static func `default`(_ label: String, _ action: (() -> Void)? = {}) -> Alert.Button {
static func `default`(_ label: String, _ action: (() -> Void)? = {}) -> Alert.Button {
return .default(Text(label), action: action)
}
public static func ok(_ action: (() -> Void)? = {}) -> Alert.Button {
static func ok(_ action: (() -> Void)? = {}) -> Alert.Button {
return .default("OK", action)
}
public static func discard(_ action: (() -> Void)? = {}) -> Alert.Button {
static func discard(_ action: (() -> Void)? = {}) -> Alert.Button {
return .destructive(Text("Discard"), action: action)
}

View File

@ -155,9 +155,7 @@ struct AccountTransactionsDialog: View {
.executeMutatingMethod {
self.showFetchAllTransactionsOverlay = self.shouldShowFetchAllTransactionsOverlay
}
.alert(item: $errorMessage) { message in
Alert(title: message.title, message: message.message, dismissButton: message.primaryButton)
}
.alert(message: $errorMessage)
.showNavigationBarTitle(LocalizedStringKey(title))
.navigationBarItems(trailing: UpdateButton { _, executingDone in self.updateTransactions(executingDone) })
}

View File

@ -58,9 +58,7 @@ struct AddAccountDialog: View {
}
}
.alert(item: $errorMessage) { message in
Alert(title: message.title, message: message.message, dismissButton: message.primaryButton)
}
.alert(message: $errorMessage)
.fixKeyboardCoversLowerPart()
.showNavigationBarTitle("Add account")
}

View File

@ -60,9 +60,7 @@ struct BankAccountSettingsDialog: View {
CheckmarkListItem("Supports Instant payment transfer", account.supportsInstantPaymentMoneyTransfer)
}
}
.alert(item: $unsavedChangesMessage) { message in
Alert(title: message.title, message: message.message, primaryButton: message.primaryButton, secondaryButton: message.secondaryButton!)
}
.alert(message: $unsavedChangesMessage)
.fixKeyboardCoversLowerPart()
.showNavigationBarTitle(LocalizedStringKey(account.displayName))
.setCancelAndDoneNavigationBarButtons(onCancelPressed: cancelPressed, onDonePressed: donePressed)

View File

@ -92,9 +92,7 @@ struct BankSettingsDialog: View {
Spacer()
}
}
.alert(item: $askUserToDeleteAccountOrSaveChangesMessage) { message in
Alert(title: message.title, message: message.message, primaryButton: message.primaryButton, secondaryButton: message.secondaryButton!)
}
.alert(message: $askUserToDeleteAccountOrSaveChangesMessage)
.fixKeyboardCoversLowerPart()
.showNavigationBarTitle(LocalizedStringKey(bank.displayName))
.setCancelAndDoneNavigationBarButtons(onCancelPressed: cancelPressed, onDonePressed: donePressed)

View File

@ -130,9 +130,7 @@ struct EnterTanDialog: View {
}
}
}
.alert(item: $errorMessage) { message in
Alert(title: message.title, message: message.message, dismissButton: message.primaryButton)
}
.alert(message: $errorMessage)
.fixKeyboardCoversLowerPart()
.showNavigationBarTitle("Enter TAN Dialog Title")
.customNavigationBarBackButton {

View File

@ -85,9 +85,7 @@ struct SelectBankDialog: View {
}
}
}
.alert(item: $errorMessage) { message in
Alert(title: message.title, message: message.message, dismissButton: message.primaryButton)
}
.alert(message: $errorMessage)
.fixKeyboardCoversLowerPart()
.showNavigationBarTitle("Select Bank Dialog Title")
}

View File

@ -38,9 +38,7 @@ struct SettingsTab: View {
self.navigateToProtectAppSettingsDialog()
}
}
.alert(item: $askToDeleteAccountMessage) { message in
Alert(title: message.title, message: message.message, primaryButton: message.primaryButton, secondaryButton: message.secondaryButton!)
}
.alert(message: $askToDeleteAccountMessage)
.showNavigationBarTitle("Settings")
}

View File

@ -207,14 +207,7 @@ struct TransferMoneyDialog: View {
}
}
}
.alert(item: $transferMoneyResponseMessage) { message in
if let secondaryButton = message.secondaryButton {
return Alert(title: message.title, message: message.message, primaryButton: message.primaryButton, secondaryButton: secondaryButton)
}
else {
return Alert(title: message.title, message: message.message, dismissButton: message.primaryButton)
}
}
.alert(message: $transferMoneyResponseMessage)
.fixKeyboardCoversLowerPart()
.showNavigationBarTitle("Transfer Money Dialog Title")
}