Skip to content

Commit

Permalink
refactor finding specific view controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
phillipthelen committed Oct 18, 2024
1 parent d581461 commit 809aeca
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 45 deletions.
File renamed without changes.
41 changes: 1 addition & 40 deletions HabitRPG/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class HabiticaAppDelegate: UIResponder, MessagingDelegate, UIApplicationDelegate
private let contentRepository = ContentRepository()
let taskRepository = TaskRepository()
private let socialRepository = SocialRepository()
private let configRepository = ConfigRepository.shared
internal let configRepository = ConfigRepository.shared

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
Measurements.start(identifier: "didFinishLaunchingWithOptions")
Expand Down Expand Up @@ -431,42 +431,3 @@ class HabiticaAppDelegate: UIResponder, MessagingDelegate, UIApplicationDelegate
}
}

// Maintenance
extension HabiticaAppDelegate {
func handleMaintenanceScreen() -> Bool {
let maintenanceData = configRepository.dictionary(variable: .maintenanceData)
if let title = maintenanceData["title"] as? String, let descriptionString = maintenanceData["description"] as? String {
displayMaintenanceScreen(title: title, descriptionString: descriptionString)
return true
} else {
hideMaintenanceScreen()
}
return false
}

func displayMaintenanceScreen(title: String, descriptionString: String) {
if findMaintenanceScreen() == nil {
let maintenanceController = MaintenanceViewController()
maintenanceController.configure(title: title, descriptionString: descriptionString, showAppstoreButton: false)
maintenanceController.modalPresentationStyle = .fullScreen
maintenanceController.modalTransitionStyle = .crossDissolve
UIApplication.topViewController()?.present(maintenanceController, animated: true, completion: nil)
}
}

func hideMaintenanceScreen() {
findMaintenanceScreen()?.dismiss(animated: true, completion: nil)
}

private func findMaintenanceScreen() -> MaintenanceViewController? {
var viewController: UIViewController? = UIApplication.shared.findKeyWindow()?.rootViewController
while viewController != nil {
if let maintenanceController = viewController as? MaintenanceViewController {
return maintenanceController
} else {
viewController = viewController?.presentedViewController
}
}
return nil
}
}
41 changes: 41 additions & 0 deletions HabitRPG/AppDelege+Maintenance.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//
// AppDelege+Maintenance.swift
// Habitica
//
// Created by Phillip Thelen on 18.10.24.
// Copyright © 2024 HabitRPG Inc. All rights reserved.
//

import UIKit

// Maintenance
extension HabiticaAppDelegate {
func handleMaintenanceScreen() -> Bool {
let maintenanceData = configRepository.dictionary(variable: .maintenanceData)
if let title = maintenanceData["title"] as? String, let descriptionString = maintenanceData["description"] as? String {
displayMaintenanceScreen(title: title, descriptionString: descriptionString)
return true
} else {
hideMaintenanceScreen()
}
return false
}

func displayMaintenanceScreen(title: String, descriptionString: String) {
if findMaintenanceScreen() == nil {
let maintenanceController = MaintenanceViewController()
maintenanceController.configure(title: title, descriptionString: descriptionString, showAppstoreButton: false)
maintenanceController.modalPresentationStyle = .fullScreen
maintenanceController.modalTransitionStyle = .crossDissolve
UIApplication.topViewController()?.present(maintenanceController, animated: true, completion: nil)
}
}

func hideMaintenanceScreen() {
findMaintenanceScreen()?.dismiss(animated: true, completion: nil)
}

private func findMaintenanceScreen() -> MaintenanceViewController? {
return UIWindow.findViewController()
}
}
6 changes: 1 addition & 5 deletions HabitRPG/Utilities/RouterHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -397,11 +397,7 @@ class RouterHandler {
}

private var tabbarController: MainTabBarController? {
var viewController = UIApplication.shared.findKeyWindow()?.rootViewController
while viewController != nil && viewController as? MainTabBarController == nil {
viewController = viewController?.presentedViewController
}
return viewController as? MainTabBarController
return UIWindow.findViewController()
}

private var selectedNavigationController: UINavigationController? {
Expand Down
13 changes: 13 additions & 0 deletions HabitRPG/Utilities/UIWindow+VisibleController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,17 @@ extension UIWindow {
}
}
}

static func findViewController<VC: UIViewController>(from rootViewController: UIViewController? = nil) -> VC? {
var viewController: UIViewController? = rootViewController ?? UIApplication.shared.findKeyWindow()?.rootViewController
while viewController != nil {
if let vc = viewController as? VC {
return vc
} else {
viewController = viewController?.presentedViewController
}
}
return nil
}
}

0 comments on commit 809aeca

Please sign in to comment.