From 41b60a07a46115a580dc7c64856df73b4ae5dc52 Mon Sep 17 00:00:00 2001 From: dankito Date: Sat, 5 Sep 2020 18:38:05 +0200 Subject: [PATCH] Extracted UIAlert and ActionSheet --- .../BankingiOSApp/TabBarController.swift | 4 +-- .../BankingiOSApp/ui/UIKit/ActionSheet.swift | 28 ++++++++++++++++ .../BankingiOSApp/ui/UIKit/UIAlert.swift | 23 +++++++++++++ .../BankingiOSApp/ui/UIKit/UIAlertBase.swift | 33 +++++++++++++++++++ .../BankingiOSApp/ui/UIKitExtensions.swift | 28 ++++++++++++++++ .../BankingiOSApp/ui/ViewExtensions.swift | 5 +++ .../BankingiOSApp/ui/views/BankListItem.swift | 16 ++++----- 7 files changed, 124 insertions(+), 13 deletions(-) create mode 100644 ui/BankingiOSApp/BankingiOSApp/ui/UIKit/ActionSheet.swift create mode 100644 ui/BankingiOSApp/BankingiOSApp/ui/UIKit/UIAlert.swift create mode 100644 ui/BankingiOSApp/BankingiOSApp/ui/UIKit/UIAlertBase.swift diff --git a/ui/BankingiOSApp/BankingiOSApp/TabBarController.swift b/ui/BankingiOSApp/BankingiOSApp/TabBarController.swift index f1169666..8109889b 100644 --- a/ui/BankingiOSApp/BankingiOSApp/TabBarController.swift +++ b/ui/BankingiOSApp/BankingiOSApp/TabBarController.swift @@ -88,9 +88,7 @@ class TabBarController : UITabBarController, UITabBarControllerDelegate { private func showNewOptionsActionSheet() { - let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet) - - let transferMoneyAction = UIAlertAction(title: "Show transfer money dialog".localize(), style: .default, handler: { _ in self.showView(TransferMoneyDialog()) }) + let transferMoneyAction = UIAlertAction.default("Show transfer money dialog".localize()) { self.showView(TransferMoneyDialog()) } transferMoneyAction.isEnabled = data.hasAccountsThatSupportTransferringMoney alert.addAction(transferMoneyAction) diff --git a/ui/BankingiOSApp/BankingiOSApp/ui/UIKit/ActionSheet.swift b/ui/BankingiOSApp/BankingiOSApp/ui/UIKit/ActionSheet.swift new file mode 100644 index 00000000..fce95a1c --- /dev/null +++ b/ui/BankingiOSApp/BankingiOSApp/ui/UIKit/ActionSheet.swift @@ -0,0 +1,28 @@ +import SwiftUI + + +class ActionSheet : UIAlertBase { + + convenience init(_ title: String? = nil, _ actions: UIAlertAction...) { + self.init(title, actions) + } + + init(_ title: String? = nil, _ actions: [UIAlertAction] = []) { + super.init(title: title, message: nil, .actionSheet, actions) + } + + + func show(_ sourceView: UIView, _ sourceRectX: CGFloat, _ sourceRectY: CGFloat) { + if let rootViewController = SceneDelegate.rootViewController { + let alert = createAlertController() + + if let popoverController = alert.popoverPresentationController { + popoverController.sourceView = sourceView + popoverController.sourceRect = CGRect(x: sourceRectX, y: sourceRectY, width: 0, height: 0) + } + + rootViewController.present(alert, animated: true, completion: nil) + } + } + +} diff --git a/ui/BankingiOSApp/BankingiOSApp/ui/UIKit/UIAlert.swift b/ui/BankingiOSApp/BankingiOSApp/ui/UIKit/UIAlert.swift new file mode 100644 index 00000000..6b892897 --- /dev/null +++ b/ui/BankingiOSApp/BankingiOSApp/ui/UIKit/UIAlert.swift @@ -0,0 +1,23 @@ +import SwiftUI + + +class UIAlert : UIAlertBase { + + convenience init(_ title: String? = nil, _ message: String? = nil, _ actions: UIAlertAction...) { + self.init(title, message, actions) + } + + init(_ title: String? = nil, _ message: String? = nil, _ actions: [UIAlertAction] = []) { + super.init(title: title, message: message, .alert, actions) + } + + + func show() { + if let rootViewController = SceneDelegate.rootViewController { + let alert = createAlertController() + + rootViewController.present(alert, animated: true) + } + } + +} diff --git a/ui/BankingiOSApp/BankingiOSApp/ui/UIKit/UIAlertBase.swift b/ui/BankingiOSApp/BankingiOSApp/ui/UIKit/UIAlertBase.swift new file mode 100644 index 00000000..6537dee8 --- /dev/null +++ b/ui/BankingiOSApp/BankingiOSApp/ui/UIKit/UIAlertBase.swift @@ -0,0 +1,33 @@ +import SwiftUI + + +class UIAlertBase { + + private var title: String? = nil + + private var message: String? = nil + + private var preferredStyle: UIAlertController.Style = .alert + + private var actions: [UIAlertAction] = [] + + + init(title: String? = nil, message: String? = nil, _ preferredStyle: UIAlertController.Style = .alert, _ actions: [UIAlertAction] = []) { + self.title = title + self.message = message + self.preferredStyle = preferredStyle + self.actions = actions + } + + + func createAlertController() -> UIAlertController { + let alert = UIAlertController(title: title?.localize(), message: message?.localize(), preferredStyle: preferredStyle) + + for action in actions { + alert.addAction(action) + } + + return alert + } + +} diff --git a/ui/BankingiOSApp/BankingiOSApp/ui/UIKitExtensions.swift b/ui/BankingiOSApp/BankingiOSApp/ui/UIKitExtensions.swift index f5520009..48907b3b 100644 --- a/ui/BankingiOSApp/BankingiOSApp/ui/UIKitExtensions.swift +++ b/ui/BankingiOSApp/BankingiOSApp/ui/UIKitExtensions.swift @@ -57,6 +57,34 @@ extension UIDevice { } +extension UIAlertAction { + + static func ok(_ handler: (() -> Void)? = nil) -> UIAlertAction { + return `default`("OK", handler) + } + + static func `default`(_ title: String, _ handler: (() -> Void)? = nil) -> UIAlertAction { + return UIAlertAction(title, .default, handler) + } + + static func destructive(_ title: String, _ handler: (() -> Void)? = nil) -> UIAlertAction { + return UIAlertAction(title, .destructive, handler) + } + + static func cancel(_ handler: (() -> Void)? = nil) -> UIAlertAction { + return cancel("Cancel", handler) + } + + static func cancel(_ title: String, _ handler: (() -> Void)? = nil) -> UIAlertAction { + return UIAlertAction(title, .cancel, handler) + } + + convenience init(_ title: String, _ style: UIAlertAction.Style = .default, _ handler: (() -> Void)? = nil) { + self.init(title: title.localize(), style: style, handler: { _ in handler?() }) + } +} + + extension UserDefaults { func string(forKey key: String, defaultValue: String) -> String { diff --git a/ui/BankingiOSApp/BankingiOSApp/ui/ViewExtensions.swift b/ui/BankingiOSApp/BankingiOSApp/ui/ViewExtensions.swift index 6e12eb5c..fc2cf225 100644 --- a/ui/BankingiOSApp/BankingiOSApp/ui/ViewExtensions.swift +++ b/ui/BankingiOSApp/BankingiOSApp/ui/ViewExtensions.swift @@ -86,6 +86,11 @@ extension View { } + func alignVertically(_ alignment: Alignment) -> some View { + return self.frame(maxWidth: .infinity, alignment: alignment) + } + + func turnAnimationOff() -> some View { return self.animation(nil) } diff --git a/ui/BankingiOSApp/BankingiOSApp/ui/views/BankListItem.swift b/ui/BankingiOSApp/BankingiOSApp/ui/views/BankListItem.swift index 9bc18bb4..7d540910 100644 --- a/ui/BankingiOSApp/BankingiOSApp/ui/views/BankListItem.swift +++ b/ui/BankingiOSApp/BankingiOSApp/ui/views/BankListItem.swift @@ -49,16 +49,12 @@ struct BankListItem : View { func askUserToDeleteAccount() { // couldn't believe it, .alert() didn't work as SwiftUI resetted @State variable to dislpay it instantly, therefore Alert never got displayed // TODO: use values from Message.createAskUserToDeleteAccountMessage(self.bank, self.deleteAccount) - let alert = UIAlertController(title: "Really delete account '%@'?".localize(bank.displayName), - message: "All data for this account will be permanently deleted locally.".localize(), - preferredStyle: .alert) - - alert.addAction(UIAlertAction(title: "Delete".localize(), style: .destructive, handler: { _ in self.deleteAccount(self.bank) } )) - alert.addAction(UIAlertAction(title: "Cancel".localize(), style: .cancel, handler: nil)) - - if let rootViewController = SceneDelegate.rootNavigationController { - rootViewController.present(alert, animated: true) - } + UIAlert( + "Really delete account '%@'?".localize(bank.displayName), + "All data for this account will be permanently deleted locally.", + UIAlertAction.destructive("Delete") { self.deleteAccount(self.bank) }, + UIAlertAction.cancel() + ).show() } func deleteAccount(_ bank: Customer) {