taler-ios

iOS apps for GNU Taler (wallet)
Log | Files | Refs | README | LICENSE

View+dismissTop.swift (3490B)


      1 //  MIT License
      2 //  Copyright © Nicolai Harbo
      3 //
      4 //  Permission is hereby granted, free of charge, to any person obtaining a copy of this software
      5 //  and associated documentation files (the "Software"), to deal in the Software without restriction,
      6 //  including without limitation the rights to use, copy, modify, merge, publish, distribute,
      7 //  sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
      8 //  furnished to do so, subject to the following conditions:
      9 //
     10 //  The above copyright notice and this permission notice shall be included in all copies or
     11 //  substantial portions of the Software.
     12 //
     13 //  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
     14 //  BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
     15 //  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
     16 //  DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     17 //  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     18 //
     19 import SwiftUI
     20 
     21 /// This is just a workaround for a SwiftUI bug
     22 /// A presented sheet (SwiftUI view) doesn't always close when calling "dismiss()" provided by @Environment(\.dismiss),
     23 /// so we are walking the view stack to find the top presentedViewController (UIKit) and dismiss it.
     24 extension View {
     25     @MainActor    @discardableResult
     26     func dismissTop(_ stack: CallStack, animated: Bool = true) -> Bool {
     27         let windows = UIApplication.shared.connectedScenes.compactMap {
     28             ($0 as? UIWindowScene)?.keyWindow       // TODO: iPad might have more than 1 window
     29         }
     30         if var topController = windows.first?.rootViewController {
     31             var gotPresented = false
     32             var currentController = topController
     33             while let presentedViewController = currentController.presentedViewController {
     34                 currentController = presentedViewController
     35                 gotPresented = true
     36             }
     37             if gotPresented {
     38                 currentController.dismiss(animated: animated)
     39                 return true
     40             } else if let navController = Self.findNavigationController(viewController: topController) {
     41                 navController.popToRootViewController(animated: animated)
     42                 return true
     43             } else {
     44                 print("Yikes❗️ There is no navigationController!")
     45             }
     46         } else {
     47             print("Yikes❗️ There is no window/rootViewController!")
     48         }
     49         return false
     50     }
     51     @MainActor static func findNavigationController(viewController: UIViewController?) -> UINavigationController? {
     52         guard let viewController = viewController else {
     53             return nil
     54         }
     55 
     56         if let tabBarController = viewController as? UITabBarController {
     57             return findNavigationController(viewController: tabBarController.selectedViewController)
     58         }
     59 
     60         if let navigationController = viewController as? UINavigationController {
     61             return navigationController
     62         }
     63 
     64         for childViewController in viewController.children {
     65             return findNavigationController(viewController: childViewController)
     66         }
     67 
     68         return nil
     69     }
     70 }
     71 // MARK: -
     72 extension View {
     73     @MainActor
     74     static func endEditing() {
     75         UIApplication.shared.windows.forEach { $0.endEditing(true) }
     76     }
     77 }