1 // Copyright 2015-present 650 Industries. All rights reserved.
2 
3 import Foundation
4 
5 @objc
6 public class EXDevLauncherErrorManager: NSObject {
7   internal weak var controller: EXDevLauncherController?
8   private weak var currentVC: EXDevLauncherErrorViewController?
9   private var error: EXDevLauncherAppError?
10   private static let VIEW_TAG = 6634
11 
12   @objc
13   public init(controller: EXDevLauncherController) {
14     self.controller = controller
15     EXDevLauncherRedBoxInterceptor.isInstalled = true
16   }
17 
18   @objc
consumeErrornull19   public func consumeError() -> EXDevLauncherAppError {
20     let result = error!
21     error = nil
22     return result
23   }
24 
25   @objc
showErrornull26   public func showError(_ error: EXDevLauncherAppError) {
27     guard let nextViewController = getNextErrorViewController() else {
28       currentVC = nil
29       return
30     }
31 
32     self.error = error
33     currentVC = nextViewController
34     controller?.currentWindow()?.rootViewController = currentVC
35     controller?.currentWindow()?.makeKeyAndVisible()
36 
37     // remove splash screen
38     currentVC?.view.subviews.forEach {
39       if ($0.tag != EXDevLauncherErrorManager.VIEW_TAG) {
40         $0.removeFromSuperview()
41       }
42     }
43   }
44 
getNextErrorViewControllernull45   private func getNextErrorViewController() -> EXDevLauncherErrorViewController? {
46     if currentVC == nil || controller?.currentWindow()?.rootViewController != currentVC {
47       return EXDevLauncherErrorViewController.create(forManager: self)
48     }
49 
50     return currentVC
51   }
52 }
53