Files
SideStore/SideStoreApp/Sources/SideStoreUIKit/TabBarController.swift

122 lines
4.6 KiB
Swift
Raw Normal View History

//
// TabBarController.swift
// AltStore
//
// Created by Riley Testut on 9/19/19.
// Copyright © 2019 Riley Testut. All rights reserved.
//
2023-03-01 00:48:36 -05:00
import SideStoreCore
import UIKit
2023-03-01 00:48:36 -05:00
extension TabBarController {
private enum Tab: Int, CaseIterable {
case news
case browse
case myApps
case settings
}
}
2023-03-01 19:09:33 -05:00
public final class TabBarController: UITabBarController {
private var initialSegue: (identifier: String, sender: Any?)?
2023-03-01 00:48:36 -05:00
private var _viewDidAppear = false
2023-03-01 00:48:36 -05:00
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
2023-03-01 00:48:36 -05:00
2023-03-01 19:09:33 -05:00
NotificationCenter.default.addObserver(self, selector: #selector(TabBarController.openPatreonSettings(_:)), name: SideStoreAppDelegate.openPatreonSettingsDeepLinkNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(TabBarController.importApp(_:)), name: SideStoreAppDelegate.importAppDeepLinkNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(TabBarController.presentSources(_:)), name: SideStoreAppDelegate.addSourceDeepLinkNotification, object: nil)
}
2023-03-01 00:48:36 -05:00
2023-03-01 19:09:33 -05:00
public override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
2023-03-01 00:48:36 -05:00
_viewDidAppear = true
2023-03-01 00:48:36 -05:00
if let (identifier, sender) = initialSegue {
initialSegue = nil
performSegue(withIdentifier: identifier, sender: sender)
} else if let patchedApps = UserDefaults.standard.patchedApps, !patchedApps.isEmpty {
// Check if we need to finish installing untethered jailbreak.
let activeApps = InstalledApp.fetchActiveApps(in: DatabaseManager.shared.viewContext)
guard let patchedApp = activeApps.first(where: { patchedApps.contains($0.bundleIdentifier) }) else { return }
2023-03-01 00:48:36 -05:00
performSegue(withIdentifier: "finishJailbreak", sender: patchedApp)
}
}
2023-03-01 00:48:36 -05:00
2023-03-01 19:09:33 -05:00
public override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard let identifier = segue.identifier else { return }
2023-03-01 00:48:36 -05:00
switch identifier {
case "presentSources":
guard let notification = sender as? Notification,
2023-03-01 19:09:33 -05:00
let sourceURL = notification.userInfo?[SideStoreAppDelegate.addSourceDeepLinkURLKey] as? URL
else { return }
2023-03-01 00:48:36 -05:00
let navigationController = segue.destination as! UINavigationController
let sourcesViewController = navigationController.viewControllers.first as! SourcesViewController
sourcesViewController.deepLinkSourceURL = sourceURL
2023-03-01 00:48:36 -05:00
case "finishJailbreak":
guard let installedApp = sender as? InstalledApp, #available(iOS 14, *) else { return }
2023-03-01 00:48:36 -05:00
let navigationController = segue.destination as! UINavigationController
2023-03-01 00:48:36 -05:00
let patchViewController = navigationController.viewControllers.first as! PatchViewController
patchViewController.installedApp = installedApp
patchViewController.completionHandler = { [weak self] _ in
self?.dismiss(animated: true, completion: nil)
}
2023-03-01 00:48:36 -05:00
default: break
}
}
2023-03-01 00:48:36 -05:00
2023-03-01 19:09:33 -05:00
public override func performSegue(withIdentifier identifier: String, sender: Any?) {
guard _viewDidAppear else {
2023-03-01 00:48:36 -05:00
initialSegue = (identifier, sender)
return
}
2023-03-01 00:48:36 -05:00
super.performSegue(withIdentifier: identifier, sender: sender)
}
}
2023-03-01 00:48:36 -05:00
extension TabBarController {
@objc func presentSources(_ sender: Any) {
if let presentedViewController = presentedViewController {
if let navigationController = presentedViewController as? UINavigationController,
2023-03-01 00:48:36 -05:00
let sourcesViewController = navigationController.viewControllers.first as? SourcesViewController {
if let notification = (sender as? Notification),
2023-03-01 19:09:33 -05:00
let sourceURL = notification.userInfo?[SideStoreAppDelegate.addSourceDeepLinkURLKey] as? URL {
sourcesViewController.deepLinkSourceURL = sourceURL
2023-03-01 00:48:36 -05:00
} else {
// Don't dismiss SourcesViewController if it's already presented.
}
2023-03-01 00:48:36 -05:00
} else {
presentedViewController.dismiss(animated: true) {
self.presentSources(sender)
}
}
2023-03-01 00:48:36 -05:00
return
}
2023-03-01 00:48:36 -05:00
performSegue(withIdentifier: "presentSources", sender: sender)
}
}
2023-03-01 00:48:36 -05:00
private extension TabBarController {
@objc func openPatreonSettings(_: Notification) {
selectedIndex = Tab.settings.rawValue
}
2023-03-01 00:48:36 -05:00
@objc func importApp(_: Notification) {
selectedIndex = Tab.myApps.rawValue
}
}