taler-ios

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

View+dismissTop.swift (3562B)


      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? = nil, animated: Bool = true) -> Bool {
     27         let windows = UIApplication.shared.connectedScenes.compactMap {
     28             // TODO: iPad might have more than 1 window
     29             // TODO: see UIScreen+screenSize for keyWindow
     30             ($0 as? UIWindowScene)?.keyWindow
     31         }
     32         if var topController = windows.first?.rootViewController {
     33             var gotPresented = false
     34             var currentController = topController
     35             while let presentedViewController = currentController.presentedViewController {
     36                 currentController = presentedViewController
     37                 gotPresented = true
     38             }
     39             if gotPresented {
     40                 currentController.dismiss(animated: animated)
     41                 return true
     42             } else if let navController = Self.findNavigationController(viewController: topController) {
     43                 navController.popToRootViewController(animated: animated)
     44                 return true
     45             } else {
     46                 print("Yikes❗️ There is no navigationController!")
     47             }
     48         } else {
     49             print("Yikes❗️ There is no window/rootViewController!")
     50         }
     51         return false
     52     }
     53     @MainActor static func findNavigationController(viewController: UIViewController?) -> UINavigationController? {
     54         guard let viewController = viewController else {
     55             return nil
     56         }
     57 
     58         if let tabBarController = viewController as? UITabBarController {
     59             return findNavigationController(viewController: tabBarController.selectedViewController)
     60         }
     61 
     62         if let navigationController = viewController as? UINavigationController {
     63             return navigationController
     64         }
     65 
     66         for childViewController in viewController.children {
     67             return findNavigationController(viewController: childViewController)
     68         }
     69 
     70         return nil
     71     }
     72 }
     73 // MARK: -
     74 extension View {
     75     @MainActor
     76     static func endEditing() {
     77         UIApplication.shared.windows.forEach { $0.endEditing(true) }
     78     }
     79 }