Skip to content

Commit

Permalink
some improvements over the alert class
Browse files Browse the repository at this point in the history
  • Loading branch information
c22dev committed Sep 15, 2024
1 parent 29f9b38 commit f6bee97
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 89 deletions.
2 changes: 1 addition & 1 deletion Geranium/Cleaner/CleanerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ struct CleanerView: View {
.onAppear {
if ProcessInfo().operatingSystemVersion.majorVersion == 15, appSettings.firstCleanerTime {
appSettings.firstCleanerTime = false
UIApplication.shared.yesoubiennon(title: "⚠️ You are on iOS 15 ⚠️", body: "Cleaning on iOS 15 might break notifications, and some app permissions. Do you want to enable measures that will keep your phone safe ? You might not get everything completly cleaned up. Pressing yes on iOS 15 will keep your device safe.", onOK: {
UIApplication.shared.confirmAlert(title: "⚠️ You are on iOS 15 ⚠️", body: "Cleaning on iOS 15 might break notifications, and some app permissions. Do you want to enable measures that will keep your phone safe ? You might not get everything completly cleaned up. Pressing yes on iOS 15 will keep your device safe.", onOK: {
appSettings.tmpClean = false
}, noCancel: false, onCancel: {
appSettings.tmpClean = true
Expand Down
14 changes: 8 additions & 6 deletions Geranium/Cleaner/CustomPaths.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,16 @@ struct CustomPaths: View {
}
ToolbarItem(placement: .navigationBarTrailing) {
Button(action: {
UIApplication.shared.TextFieldAlert(title: "Enter a path you want to add to your list :", textFieldPlaceHolder: "/var/mobile/DCIM/") { chemin in
if chemin == ""{
UIApplication.shared.alert(title:"Empty text.", body: "Please enter a path.")
}
else {
paths.append(chemin ?? "")
UIApplication.shared.TextFieldAlert(
title: "Enter a path you want to add to your list:",
textFieldPlaceHolder: "/var/mobile/DCIM/"
) { chemin, _ in
if let chemin = chemin, !chemin.isEmpty {
paths.append(chemin)
savePaths()
isAddingPath.toggle()
} else {
UIApplication.shared.alert(title: "Empty input !!", body: "Please enter a path.")
}
}
}) {
Expand Down
2 changes: 1 addition & 1 deletion Geranium/GeraniumApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ struct GeraniumApp: App {
}
task.resume()
}
RootHelper.loadMCM()
_ = RootHelper.loadMCM()
}
.sheet(isPresented: $appSettings.isFirstRun) {
if #available(iOS 16.0, *) {
Expand Down
90 changes: 16 additions & 74 deletions Geranium/Libs/Addon.swift
Original file line number Diff line number Diff line change
Expand Up @@ -149,87 +149,40 @@ extension UIApplication {
onCancel?()
}))
}
if !yes {
currentUIAlertController?.addAction(.init(title: "OK", style: noCancel ? .cancel : .default, handler: { _ in
onOK()
}))
}
if yes {
currentUIAlertController?.addAction(.init(title: "Yes", style: noCancel ? .cancel : .default, handler: { _ in
onOK()
}))
}
self.present(alert: currentUIAlertController!)
}
}

func yesoubiennon(title: String = "Error", body: String, onOK: @escaping () -> (), noCancel: Bool, onCancel: (() -> ())? = nil, yes: Bool) {
DispatchQueue.main.async {
currentUIAlertController = UIAlertController(title: title, message: body, preferredStyle: .alert)
if !noCancel {
currentUIAlertController?.addAction(.init(title: "No", style: .cancel, handler: { _ in
onCancel?()
}))
}
if !yes {
currentUIAlertController?.addAction(.init(title: "OK", style: noCancel ? .cancel : .default, handler: { _ in
onOK()
}))
}
if yes {
currentUIAlertController?.addAction(.init(title: "Yes", style: noCancel ? .cancel : .default, handler: { _ in
onOK()
}))
}

let okActionTitle = yes ? "Yes" : "OK"
currentUIAlertController?.addAction(.init(title: okActionTitle, style: noCancel ? .cancel : .default, handler: { _ in
onOK()
}))

self.present(alert: currentUIAlertController!)
}
}


func TextFieldAlert(title: String, textFieldPlaceHolder: String, completion: @escaping (String?) -> Void) {
let alertController = UIAlertController(title: title, message: nil, preferredStyle: .alert)
alertController.addTextField { (textField) in
textField.placeholder = textFieldPlaceHolder
}
let okAction = UIAlertAction(title: "OK", style: .default) { (action) in
if let text = alertController.textFields?.first?.text {
completion(text)
} else {
completion(nil)
}
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alertController.addAction(okAction)
alertController.addAction(cancelAction)
present(alert: alertController)
}

// bad programming practices but im lazy

func DualTextFieldAlert(title: String, message: String, textFieldPlaceHolder: String, secondTextFieldPlaceHolder: String, completion: @escaping (String?, String?) -> Void) {
func TextFieldAlert(title: String, message: String? = nil, textFieldPlaceHolder: String, secondTextFieldPlaceHolder: String? = nil, completion: @escaping (String?, String?) -> Void) {
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)

alertController.addTextField { (textField) in
textField.placeholder = textFieldPlaceHolder
textField.keyboardType = .decimalPad
}

alertController.addTextField { (textField) in
textField.placeholder = secondTextFieldPlaceHolder
textField.keyboardType = .decimalPad
if let secondPlaceholder = secondTextFieldPlaceHolder {
alertController.addTextField { (textField) in
textField.placeholder = secondPlaceholder
}
}

let okAction = UIAlertAction(title: "OK", style: .default) { _ in
let latText = alertController.textFields?[0].text
let longText = alertController.textFields?[1].text
completion(latText, longText)
let firstText = alertController.textFields?.first?.text
let secondText = secondTextFieldPlaceHolder != nil ? alertController.textFields?[1].text : nil
completion(firstText, secondText)
}

let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alertController.addAction(okAction)
alertController.addAction(cancelAction)

presentDual(alertController: alertController)
present(alert: alertController)
}

func change(title: String = "Error", body: String) {
Expand All @@ -240,22 +193,11 @@ extension UIApplication {
}

func present(alert: UIAlertController) {
if var topController = self.windows[0].rootViewController {
while let presentedViewController = topController.presentedViewController {
topController = presentedViewController
}

topController.present(alert, animated: true)
// topController should now be your topmost view controller
}
}

func presentDual(alertController: UIAlertController) {
if var topController = self.windows.first?.rootViewController {
while let presentedViewController = topController.presentedViewController {
topController = presentedViewController
}
topController.present(alertController, animated: true)
topController.present(alert, animated: true)
}
}

Expand Down
13 changes: 9 additions & 4 deletions Geranium/LocSim/BookMark/BookMarkSlider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,20 @@ struct BookMarkSlider: View {
ToolbarItem(placement: .navigationBarTrailing) {
Button(action: {
if lat != 0.00 && long != 0.00 {
UIApplication.shared.TextFieldAlert(title: "Enter bookmark name", textFieldPlaceHolder: "Example...", completion: { enteredText in
result = BookMarkSave(lat: lat, long: long, name: enteredText ?? "Unknown")
UIApplication.shared.TextFieldAlert(
title: "Enter bookmark name",
textFieldPlaceHolder: "Example..."
) { enteredText, _ in
let bookmarkName = enteredText ?? "Unknown"
result = BookMarkSave(lat: lat, long: long, name: bookmarkName)
bookmarks = BookMarkRetrieve().map {
Bookmark(name: $0["name"] as! String, lat: $0["lat"] as! Double, long: $0["long"] as! Double)
}
})
}

} else {
UIApplication.shared.confirmAlert(title: "Your position is set to 0", body: "Are you sure you want to continue? This might be a mistake; try picking somewhere on the map", onOK: {
UIApplication.shared.TextFieldAlert(title: "Enter bookmark name", textFieldPlaceHolder: "Example...", completion: { enteredText in
UIApplication.shared.TextFieldAlert(title: "Enter bookmark name", textFieldPlaceHolder: "Example...", completion: { enteredText, _ in
result = BookMarkSave(lat: lat, long: long, name: enteredText ?? "Unknown")
bookmarks = BookMarkRetrieve().map {
Bookmark(name: $0["name"] as! String, lat: $0["lat"] as! Double, long: $0["long"] as! Double)
Expand Down
8 changes: 5 additions & 3 deletions Geranium/LocSim/LocSimView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,16 @@ struct LocSimView: View {
}
ToolbarItem(placement: .navigationBarTrailing) {
Button(action: {
UIApplication.shared.DualTextFieldAlert(
UIApplication.shared.TextFieldAlert(
title: "Enter Coordinates",
message: "The location will be simulated on device\nPro tip: Press wherever on the map to move there.",
textFieldPlaceHolder: "Latitude",
secondTextFieldPlaceHolder: "Longitude"
) { latText, longText in
if let lat = Double(latText ?? ""), let long = Double(longText ?? "") {
LocSimManager.startLocSim(location: .init(latitude: lat, longitude: long))
if let latDouble = Double(latText ?? ""), let longDouble = Double(longText ?? "") {
lat = latDouble
long = longDouble
LocSimManager.startLocSim(location: .init(latitude: latDouble, longitude: longDouble))
} else {
UIApplication.shared.alert(body: "Those are invalid coordinates mate !")
}
Expand Down

0 comments on commit f6bee97

Please sign in to comment.