diff --git a/Delta/Game Selection/GamesViewController.swift b/Delta/Game Selection/GamesViewController.swift index b89a6a5..7da5a72 100644 --- a/Delta/Game Selection/GamesViewController.swift +++ b/Delta/Game Selection/GamesViewController.swift @@ -48,6 +48,8 @@ class GamesViewController: UIViewController private var searchController: RSTSearchController? + private var syncingToastView: RSTToastView? + override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) { fatalError("initWithNibName: not implemented") } @@ -364,13 +366,65 @@ extension GamesViewController: ImportControllerDelegate } } } - +} + +//MARK: - Syncing - +/// Syncing +private extension GamesViewController +{ @IBAction func sync() { + // Show toast view in case sync started before this view controller existed. + self.showSyncingToastViewIfNeeded() + SyncManager.shared.sync() } + + func showSyncingToastViewIfNeeded() + { + guard SyncManager.shared.syncCoordinator.isSyncing && self.syncingToastView == nil else { return } + + let toastView = RSTToastView(text: NSLocalizedString("Syncing...", comment: ""), detailText: nil) + toastView.activityIndicatorView.startAnimating() + toastView.addTarget(self, action: #selector(GamesViewController.hideSyncingToastView), for: .touchUpInside) + toastView.show(in: self.view) + + self.syncingToastView = toastView + } + + func showSyncFinishedToastView(result: SyncResult) + { + let toastView: RSTToastView + + switch result + { + case .success: toastView = RSTToastView(text: NSLocalizedString("Sync Complete", comment: ""), detailText: nil) + case .failure(let error): toastView = RSTToastView(text: NSLocalizedString("Sync Failed", comment: ""), detailText: error.failureReason) + } + + toastView.addTarget(self, action: #selector(GamesViewController.presentSyncResultsViewController), for: .touchUpInside) + + toastView.show(in: self.view, duration: 2.0) + + self.syncingToastView = nil + } + + @objc func hideSyncingToastView() + { + self.syncingToastView = nil + } + + @objc func presentSyncResultsViewController() + { + guard let result = SyncManager.shared.previousSyncResult else { return } + + let navigationController = SyncResultViewController.make(result: result) + self.present(navigationController, animated: true, completion: nil) + } } +//MARK: - Notifications - +/// Notifications private extension GamesViewController { @objc func managedObjectContextDidChange(with notification: Notification) @@ -397,9 +451,7 @@ private extension GamesViewController @objc func syncingDidStart(_ notification: Notification) { DispatchQueue.main.async { - let toastView = RSTToastView(text: NSLocalizedString("Syncing...", comment: ""), detailText: nil) - toastView.activityIndicatorView.startAnimating() - toastView.show(in: self.view) + self.showSyncingToastViewIfNeeded() } } @@ -407,28 +459,9 @@ private extension GamesViewController { DispatchQueue.main.async { guard let result = notification.userInfo?[SyncCoordinator.syncResultKey] as? SyncResult else { return } - - let toastView: RSTToastView - - switch result - { - case .success: toastView = RSTToastView(text: NSLocalizedString("Sync Complete", comment: ""), detailText: nil) - case .failure(let error): toastView = RSTToastView(text: NSLocalizedString("Sync Failed", comment: ""), detailText: error.failureReason) - } - - toastView.addTarget(self, action: #selector(GamesViewController.presentSyncResultsViewController), for: .touchUpInside) - - toastView.show(in: self.view, duration: 2.0) + self.showSyncFinishedToastView(result: result) } } - - @objc func presentSyncResultsViewController() - { - guard let result = SyncManager.shared.previousSyncResult else { return } - - let navigationController = SyncResultViewController.make(result: result) - self.present(navigationController, animated: true, completion: nil) - } } //MARK: - UIPageViewController - diff --git a/Delta/Syncing/SyncManager.swift b/Delta/Syncing/SyncManager.swift index 077f0cd..08c4b2e 100644 --- a/Delta/Syncing/SyncManager.swift +++ b/Delta/Syncing/SyncManager.swift @@ -65,6 +65,8 @@ final class SyncManager DriveService.shared.clientID = "457607414709-5puj6lcv779gpu3ql43e6k3smjj40dmu.apps.googleusercontent.com" NotificationCenter.default.addObserver(self, selector: #selector(SyncManager.syncingDidFinish(_:)), name: SyncCoordinator.didFinishSyncingNotification, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(SyncManager.didEnterBackground(_:)), name: UIApplication.didEnterBackgroundNotification, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(SyncManager.willEnterForeground(_:)), name: UIApplication.willEnterForegroundNotification, object: nil) } } @@ -85,4 +87,14 @@ private extension SyncManager guard let result = notification.userInfo?[SyncCoordinator.syncResultKey] as? SyncResult else { return } self.previousSyncResult = result } + + @objc func didEnterBackground(_ notification: Notification) + { + self.sync() + } + + @objc func willEnterForeground(_ notification: Notification) + { + self.sync() + } } diff --git a/External/Harmony b/External/Harmony index cbb28ae..2cfca81 160000 --- a/External/Harmony +++ b/External/Harmony @@ -1 +1 @@ -Subproject commit cbb28ae105e60d5a7effabd6aacb65e4722413cd +Subproject commit 2cfca813e9e4d0ecbc824050ab0336cb9b7c6b37