diff --git a/Delta/AppDelegate.swift b/Delta/AppDelegate.swift index a2c4930..4ad9156 100644 --- a/Delta/AppDelegate.swift +++ b/Delta/AppDelegate.swift @@ -36,11 +36,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate gestureRecognizer.delaysTouchesBegan = false } - // Database - DatabaseManager.shared.start { (error) in - print("Database started with error:", error as Any) - } - // Controllers ExternalGameControllerManager.shared.startMonitoring() diff --git a/Delta/Database/DatabaseManager.swift b/Delta/Database/DatabaseManager.swift index 1a9008a..61be585 100644 --- a/Delta/Database/DatabaseManager.swift +++ b/Delta/Database/DatabaseManager.swift @@ -74,6 +74,8 @@ final class DatabaseManager: RSTPersistentContainer else { fatalError("Core Data model cannot be found. Aborting.") } super.init(name: "Delta", managedObjectModel: harmonyModel) + + self.shouldAddStoresAsynchronously = true } } diff --git a/Delta/Launch/LaunchViewController.swift b/Delta/Launch/LaunchViewController.swift index b58274a..d6aa49d 100644 --- a/Delta/Launch/LaunchViewController.swift +++ b/Delta/Launch/LaunchViewController.swift @@ -9,7 +9,7 @@ import UIKit import Roxas -class LaunchViewController: UIViewController +class LaunchViewController: RSTLaunchViewController { @IBOutlet private var gameViewContainerView: UIView! private var gameViewController: GameViewController! @@ -37,40 +37,6 @@ class LaunchViewController: UIViewController NotificationCenter.default.addObserver(self, selector: #selector(LaunchViewController.deepLinkControllerLaunchGame(with:)), name: .deepLinkControllerLaunchGame, object: nil) } - override func viewDidAppear(_ animated: Bool) - { - super.viewDidAppear(animated) - - if !self.presentedGameViewController - { - self.presentedGameViewController = true - - func showGameViewController() - { - self.view.bringSubviewToFront(self.gameViewContainerView) - - self.setNeedsStatusBarAppearanceUpdate() - self.setNeedsUpdateOfHomeIndicatorAutoHidden() - } - - if let game = self.applicationLaunchDeepLinkGame - { - self.gameViewController.game = game - - UIView.transition(with: self.view, duration: 0.3, options: [.transitionCrossDissolve], animations: { - showGameViewController() - }, completion: nil) - } - else - { - self.gameViewController.performSegue(withIdentifier: "showInitialGamesViewController", sender: nil) - self.transitionCoordinator?.animate(alongsideTransition: nil, completion: { (context) in - showGameViewController() - }) - } - } - } - override func prepare(for segue: UIStoryboardSegue, sender: Any?) { guard segue.identifier == "embedGameViewController" else { return } @@ -79,6 +45,59 @@ class LaunchViewController: UIViewController } } +extension LaunchViewController +{ + override var launchConditions: [RSTLaunchCondition] { + let isDatabaseManagerStarted = RSTLaunchCondition(condition: { DatabaseManager.shared.isStarted }) { (completionHandler) in + DatabaseManager.shared.start(completionHandler: completionHandler) + } + + return [isDatabaseManagerStarted, isDatabaseManagerStarted] + } + + override func handleLaunchError(_ error: Error) + { + let alertController = UIAlertController(title: NSLocalizedString("Unable to Launch Delta", comment: ""), message: error.localizedDescription, preferredStyle: .alert) + alertController.addAction(UIAlertAction(title: NSLocalizedString("Retry", comment: ""), style: .default, handler: { (action) in + self.handleLaunchConditions() + })) + self.present(alertController, animated: true, completion: nil) + } + + override func finishLaunching() + { + super.finishLaunching() + + guard !self.presentedGameViewController else { return } + + self.presentedGameViewController = true + + func showGameViewController() + { + self.view.bringSubviewToFront(self.gameViewContainerView) + + self.setNeedsStatusBarAppearanceUpdate() + self.setNeedsUpdateOfHomeIndicatorAutoHidden() + } + + if let game = self.applicationLaunchDeepLinkGame + { + self.gameViewController.game = game + + UIView.transition(with: self.view, duration: 0.3, options: [.transitionCrossDissolve], animations: { + showGameViewController() + }, completion: nil) + } + else + { + self.gameViewController.performSegue(withIdentifier: "showInitialGamesViewController", sender: nil) + self.transitionCoordinator?.animate(alongsideTransition: nil, completion: { (context) in + showGameViewController() + }) + } + } +} + private extension LaunchViewController { @objc func deepLinkControllerLaunchGame(with notification: Notification)