mirror of
https://github.com/SideStore/SideStore.git
synced 2026-02-09 06:43:25 +01:00
refactor: Reduce duplication code with UIApplication.keyWindow and .topController and improve alert function
This commit is contained in:
@@ -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!")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
47
AltStore/Extensions/UIApplication+SideStore.swift
Normal file
47
AltStore/Extensions/UIApplication+SideStore.swift
Normal file
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -156,14 +156,8 @@ final class FetchAnisetteDataOperation: ResultOperation<ALTAnisetteData>, 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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -187,11 +187,7 @@ final class InstallAppOperation: ResultOperation<InstalledApp>
|
||||
}))
|
||||
|
||||
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")
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)")
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user