From 175b5bec959a727ff0e86044fc0a1fe9a2fee5af Mon Sep 17 00:00:00 2001 From: naturecodevoid <44983869+naturecodevoid@users.noreply.github.com> Date: Thu, 1 Jun 2023 07:35:07 -0700 Subject: [PATCH] refactor: Reduce duplication code with UIApplication.keyWindow and .topController and improve alert function --- AltStore/Extensions/UIApplication+Alert.swift | 26 ---------- .../Extensions/UIApplication+SideStore.swift | 47 +++++++++++++++++++ .../Operations/AuthenticationOperation.swift | 14 ++---- .../FetchAnisetteDataOperation.swift | 8 +--- AltStore/Operations/InstallAppOperation.swift | 6 +-- .../SwiftUI/Helper/SideloadingManager.swift | 3 +- .../View Components/AppPillButton.swift | 3 +- .../SwiftUI/Views/Settings/SettingsView.swift | 3 +- .../UnstableFeatures+SwiftUI.swift | 4 +- 9 files changed, 57 insertions(+), 57 deletions(-) delete mode 100644 AltStore/Extensions/UIApplication+Alert.swift create mode 100644 AltStore/Extensions/UIApplication+SideStore.swift diff --git a/AltStore/Extensions/UIApplication+Alert.swift b/AltStore/Extensions/UIApplication+Alert.swift deleted file mode 100644 index e237eda6..00000000 --- a/AltStore/Extensions/UIApplication+Alert.swift +++ /dev/null @@ -1,26 +0,0 @@ -// -// UIApplication+Alert.swift -// SideStore -// -// Created by naturecodevoid on 5/20/23. -// Copyright © 2023 SideStore. All rights reserved. -// - -extension UIApplication { - static func alertOk(title: String?, message: String?) { - let alert = UIAlertController(title: title, message: message, preferredStyle: .alert) - alert.addAction(UIAlertAction(title: NSLocalizedString("Ok", comment: ""), style: .default)) - - DispatchQueue.main.async { - let keyWindow = UIApplication.shared.windows.filter { $0.isKeyWindow }.first - if var topController = keyWindow?.rootViewController { - while let presentedViewController = topController.presentedViewController { - topController = presentedViewController - } - topController.present(alert, animated: true) - } else { - print("No key window!") - } - } - } -} diff --git a/AltStore/Extensions/UIApplication+SideStore.swift b/AltStore/Extensions/UIApplication+SideStore.swift new file mode 100644 index 00000000..748af0ed --- /dev/null +++ b/AltStore/Extensions/UIApplication+SideStore.swift @@ -0,0 +1,47 @@ +// +// UIApplication+SideStore.swift +// SideStore +// +// Created by naturecodevoid on 5/20/23. +// Copyright © 2023 SideStore. All rights reserved. +// + +extension UIApplication { + static var keyWindow: UIWindow? { + UIApplication.shared.windows.filter { $0.isKeyWindow }.first + } + + static var topController: UIViewController? { + if var topController = keyWindow?.rootViewController { + while let presentedViewController = topController.presentedViewController { + topController = presentedViewController + } + return topController + } + return nil + } + + static func alert( + title: String? = nil, + message: String? = nil, + leftButton: (text: String, action: ((UIAlertAction) -> Void)?)? = nil, + rightButton: (text: String, action: ((UIAlertAction) -> Void)?)? = nil, + leftButtonStyle: UIAlertAction.Style = .default, + rightButtonStyle: UIAlertAction.Style = .default + ) { + let alert = UIAlertController(title: title, message: message, preferredStyle: .alert) + if let leftButton = leftButton { + alert.addAction(UIAlertAction(title: leftButton.text, style: leftButtonStyle, handler: leftButton.action)) + } + if let rightButton = rightButton { + alert.addAction(UIAlertAction(title: rightButton.text, style: rightButtonStyle, handler: rightButton.action)) + } + if rightButton == nil && leftButton == nil { + alert.addAction(UIAlertAction(title: NSLocalizedString("Ok", comment: ""), style: .default)) + } + + DispatchQueue.main.async { + topController?.present(alert, animated: true) + } + } +} diff --git a/AltStore/Operations/AuthenticationOperation.swift b/AltStore/Operations/AuthenticationOperation.swift index a502903e..d0a7e4d5 100644 --- a/AltStore/Operations/AuthenticationOperation.swift +++ b/AltStore/Operations/AuthenticationOperation.swift @@ -297,7 +297,7 @@ private extension AuthenticationOperation func present(_ viewController: UIViewController) -> Bool { if UnstableFeatures.enabled(.swiftUI) { - UIApplication.shared.keyWindow?.rootViewController?.present(viewController, animated: true) + UIApplication.topController?.present(viewController, animated: true) } else { guard let presentingViewController = self.presentingViewController else { return false } @@ -324,7 +324,7 @@ private extension AuthenticationOperation if let presentingViewController { presentingViewController.dismiss(animated: true) } -// UIApplication.shared.keyWindow?.rootViewController?.presentedViewController?.dismiss(animated: true) +// UIApplication.topController?.dismiss(animated: true) } } @@ -450,15 +450,7 @@ private extension AuthenticationOperation completionHandler(nil) }) - let keyWindow = UIApplication.shared.windows.filter { $0.isKeyWindow }.first - - if var topController = keyWindow?.rootViewController { - while let presentedViewController = topController.presentedViewController { - topController = presentedViewController - } - - topController.present(alertController, animated: true, completion: nil) - } + UIApplication.topController?.present(alertController, animated: true, completion: nil) } } diff --git a/AltStore/Operations/FetchAnisetteDataOperation.swift b/AltStore/Operations/FetchAnisetteDataOperation.swift index a0b899c6..d6c15df9 100644 --- a/AltStore/Operations/FetchAnisetteDataOperation.swift +++ b/AltStore/Operations/FetchAnisetteDataOperation.swift @@ -156,14 +156,8 @@ final class FetchAnisetteDataOperation: ResultOperation, WebSoc self.finish(.failure(OperationError.cancelled)) })) - let keyWindow = UIApplication.shared.windows.filter { $0.isKeyWindow }.first - DispatchQueue.main.async { - if let presentingController = keyWindow?.rootViewController?.presentedViewController { - presentingController.present(alert, animated: true) - } else { - keyWindow?.rootViewController?.present(alert, animated: true) - } + UIApplication.topController?.present(alert, animated: true) } } diff --git a/AltStore/Operations/InstallAppOperation.swift b/AltStore/Operations/InstallAppOperation.swift index bc866498..c23b06ca 100644 --- a/AltStore/Operations/InstallAppOperation.swift +++ b/AltStore/Operations/InstallAppOperation.swift @@ -187,11 +187,7 @@ final class InstallAppOperation: ResultOperation })) DispatchQueue.main.async { - let keyWindow = UIApplication.shared.windows.filter { $0.isKeyWindow }.first - if var topController = keyWindow?.rootViewController { - while let presentedViewController = topController.presentedViewController { - topController = presentedViewController - } + if var topController = UIApplication.topController { topController.present(alert, animated: true) } else { print("No key window? Let's just open Safari") diff --git a/AltStore/SwiftUI/Helper/SideloadingManager.swift b/AltStore/SwiftUI/Helper/SideloadingManager.swift index 09dfcfb4..8075d62d 100644 --- a/AltStore/SwiftUI/Helper/SideloadingManager.swift +++ b/AltStore/SwiftUI/Helper/SideloadingManager.swift @@ -248,7 +248,6 @@ class SideloadingManager { } }) - let rootViewController = UIApplication.shared.keyWindow?.rootViewController - rootViewController?.present(alertController, animated: true, completion: nil) + UIApplication.topController?.present(alertController, animated: true, completion: nil) } } diff --git a/AltStore/SwiftUI/View Components/AppPillButton.swift b/AltStore/SwiftUI/View Components/AppPillButton.swift index b8ede832..34133af2 100644 --- a/AltStore/SwiftUI/View Components/AppPillButton.swift +++ b/AltStore/SwiftUI/View Components/AppPillButton.swift @@ -85,8 +85,7 @@ struct AppPillButton: View { return } - let _ = AppManager.shared.install(storeApp, presentingViewController: UIApplication.shared.keyWindow?.rootViewController) { result in - + let _ = AppManager.shared.install(storeApp, presentingViewController: UIApplication.topController) { result in switch result { case let .success(installedApp): print("Installed app: \(installedApp.bundleIdentifier)") diff --git a/AltStore/SwiftUI/Views/Settings/SettingsView.swift b/AltStore/SwiftUI/Views/Settings/SettingsView.swift index 60342d11..4d7f3b8c 100644 --- a/AltStore/SwiftUI/Views/Settings/SettingsView.swift +++ b/AltStore/SwiftUI/Views/Settings/SettingsView.swift @@ -225,7 +225,6 @@ struct SettingsView: View { Text(L10n.SettingsView.debug) } - Section {} footer: { Text("SideStore \(appVersion)") .multilineTextAlignment(.center) @@ -257,7 +256,7 @@ struct SettingsView: View { // } func connectAppleID() { - guard let rootViewController = UIApplication.shared.keyWindow?.rootViewController else { + guard let rootViewController = UIApplication.topController else { return } diff --git a/AltStore/Unstable Features/UnstableFeatures+SwiftUI.swift b/AltStore/Unstable Features/UnstableFeatures+SwiftUI.swift index 5b7f0cba..3d4f48cb 100644 --- a/AltStore/Unstable Features/UnstableFeatures+SwiftUI.swift +++ b/AltStore/Unstable Features/UnstableFeatures+SwiftUI.swift @@ -17,14 +17,14 @@ extension UnstableFeatures { let rootView = RootView() .environment(\.managedObjectContext, DatabaseManager.shared.viewContext) - UIApplication.shared.keyWindow?.rootViewController = UIHostingController(rootView: rootView) + UIApplication.topController = UIHostingController(rootView: rootView) } static func onDisable() { let storyboard = UIStoryboard(name: "Main", bundle: .main) let rootVC = storyboard.instantiateViewController(withIdentifier: "tabBarController") as! TabBarController - UIApplication.shared.keyWindow?.rootViewController = rootVC + UIApplication.topController = rootVC } } }