Extracted UIAlert and ActionSheet
This commit is contained in:
parent
c08b4e7b5d
commit
41b60a07a4
|
@ -88,9 +88,7 @@ class TabBarController : UITabBarController, UITabBarControllerDelegate {
|
||||||
|
|
||||||
|
|
||||||
private func showNewOptionsActionSheet() {
|
private func showNewOptionsActionSheet() {
|
||||||
let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
|
let transferMoneyAction = UIAlertAction.default("Show transfer money dialog".localize()) { self.showView(TransferMoneyDialog()) }
|
||||||
|
|
||||||
let transferMoneyAction = UIAlertAction(title: "Show transfer money dialog".localize(), style: .default, handler: { _ in self.showView(TransferMoneyDialog()) })
|
|
||||||
transferMoneyAction.isEnabled = data.hasAccountsThatSupportTransferringMoney
|
transferMoneyAction.isEnabled = data.hasAccountsThatSupportTransferringMoney
|
||||||
alert.addAction(transferMoneyAction)
|
alert.addAction(transferMoneyAction)
|
||||||
|
|
||||||
|
|
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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 {
|
extension UserDefaults {
|
||||||
|
|
||||||
func string(forKey key: String, defaultValue: String) -> String {
|
func string(forKey key: String, defaultValue: String) -> String {
|
||||||
|
|
|
@ -86,6 +86,11 @@ extension View {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func alignVertically(_ alignment: Alignment) -> some View {
|
||||||
|
return self.frame(maxWidth: .infinity, alignment: alignment)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
func turnAnimationOff() -> some View {
|
func turnAnimationOff() -> some View {
|
||||||
return self.animation(nil)
|
return self.animation(nil)
|
||||||
}
|
}
|
||||||
|
|
|
@ -49,16 +49,12 @@ struct BankListItem : View {
|
||||||
func askUserToDeleteAccount() {
|
func askUserToDeleteAccount() {
|
||||||
// couldn't believe it, .alert() didn't work as SwiftUI resetted @State variable to dislpay it instantly, therefore Alert never got displayed
|
// 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)
|
// TODO: use values from Message.createAskUserToDeleteAccountMessage(self.bank, self.deleteAccount)
|
||||||
let alert = UIAlertController(title: "Really delete account '%@'?".localize(bank.displayName),
|
UIAlert(
|
||||||
message: "All data for this account will be permanently deleted locally.".localize(),
|
"Really delete account '%@'?".localize(bank.displayName),
|
||||||
preferredStyle: .alert)
|
"All data for this account will be permanently deleted locally.",
|
||||||
|
UIAlertAction.destructive("Delete") { self.deleteAccount(self.bank) },
|
||||||
alert.addAction(UIAlertAction(title: "Delete".localize(), style: .destructive, handler: { _ in self.deleteAccount(self.bank) } ))
|
UIAlertAction.cancel()
|
||||||
alert.addAction(UIAlertAction(title: "Cancel".localize(), style: .cancel, handler: nil))
|
).show()
|
||||||
|
|
||||||
if let rootViewController = SceneDelegate.rootNavigationController {
|
|
||||||
rootViewController.present(alert, animated: true)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func deleteAccount(_ bank: Customer) {
|
func deleteAccount(_ bank: Customer) {
|
||||||
|
|
Loading…
Reference in New Issue