taler-ios

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

View+dismissTop.swift (3464B)


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