Automatically syncs when entering/returning from background

This commit is contained in:
Riley Testut 2019-02-25 17:13:47 -08:00
parent 2c0709fa38
commit 3b05afd21e
3 changed files with 70 additions and 25 deletions

View File

@ -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,27 +459,8 @@ 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)
self.showSyncFinishedToastView(result: result)
}
toastView.addTarget(self, action: #selector(GamesViewController.presentSyncResultsViewController), for: .touchUpInside)
toastView.show(in: self.view, duration: 2.0)
}
}
@objc func presentSyncResultsViewController()
{
guard let result = SyncManager.shared.previousSyncResult else { return }
let navigationController = SyncResultViewController.make(result: result)
self.present(navigationController, animated: true, completion: nil)
}
}

View File

@ -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()
}
}

2
External/Harmony vendored

@ -1 +1 @@
Subproject commit cbb28ae105e60d5a7effabd6aacb65e4722413cd
Subproject commit 2cfca813e9e4d0ecbc824050ab0336cb9b7c6b37