forked from zhiphe/Potatso-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSyncManager.swift
106 lines (88 loc) · 3.11 KB
/
SyncManager.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//
// SyncManager.swift
// Potatso
//
// Created by LEI on 8/2/16.
// Copyright © 2016 TouchingApp. All rights reserved.
//
import Foundation
import CloudKit
public enum SyncServiceType: String {
case None
}
public protocol SyncServiceProtocol {
func setup(completion: ((Error?) -> Void)?)
func sync(manually: Bool, completion: ((Error?) -> Void)?)
func stop()
}
public class SyncManager {
static let shared = SyncManager()
public static let syncServiceChangedNotification = "syncServiceChangedNotification"
private var services: [SyncServiceType: SyncServiceProtocol] = [:]
private static let serviceTypeKey = "serviceTypeKey"
private(set) var syncing = false
var currentSyncServiceType: SyncServiceType {
get {
if let raw = UserDefaults.standard.object(forKey: SyncManager.serviceTypeKey) as? String, let type = SyncServiceType(rawValue: raw) {
return type
}
return .None
}
set(new) {
guard currentSyncServiceType != new else {
return
}
getCurrentSyncService()?.stop()
UserDefaults.standard.set(new.rawValue, forKey: SyncManager.serviceTypeKey)
UserDefaults.standard.synchronize()
NotificationCenter.default.post(name: NSNotification.Name(rawValue: SyncManager.syncServiceChangedNotification), object: nil)
}
}
init() {
}
func getCurrentSyncService() -> SyncServiceProtocol? {
return getSyncService(forType: currentSyncServiceType)
}
func getSyncService(forType type: SyncServiceType) -> SyncServiceProtocol? {
if let service = services[type] {
return service
}
let s: SyncServiceProtocol
switch type {
default:
return nil
}
services[type] = s
return s
}
func showSyncVC(inVC vc:UIViewController? = nil) {
guard let currentVC = vc ?? UIApplication.shared.keyWindow?.rootViewController else {
return
}
let syncVC = SyncVC()
currentVC.show(syncVC, sender: self)
}
}
extension SyncManager {
func setupNewService(type: SyncServiceType, completion: ((Error?) -> Void)?) {
if let service = getSyncService(forType: type) {
service.setup(completion: completion)
} else {
completion?(nil)
}
}
func setup(completion: ((Error?) -> Void)?) {
getCurrentSyncService()?.setup(completion: completion)
}
func sync(manually: Bool = false, completion: ((Error?) -> Void)? = nil) {
if let service = getCurrentSyncService() {
syncing = true
NotificationCenter.default.post(name: NSNotification.Name(rawValue: SyncManager.syncServiceChangedNotification), object: nil)
service.sync(manually: manually) { [weak self] error in
self?.syncing = false
NotificationCenter.default.post(name: NSNotification.Name(rawValue: SyncManager.syncServiceChangedNotification), object: nil)
completion?(error)
}
}
}
}