-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCloudSyncStatusView.swift
More file actions
109 lines (93 loc) · 3.46 KB
/
CloudSyncStatusView.swift
File metadata and controls
109 lines (93 loc) · 3.46 KB
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
107
108
import Foundation
import SwiftUI
import CoreData
class CloudKitSyncMonitor: ObservableObject {
@Published var syncStatus: SyncStatus = .unknown
// Keep track of active sync events to know when we are truly done
private var activeEventIdentifiers: Set<UUID> = []
enum SyncStatus: String {
case unknown = "Desconocido"
case syncing = "Sincronizando"
case synced = "Sincronizado"
case error = "Error"
}
init() {
setupNotificationObservers()
}
private func setupNotificationObservers() {
NotificationCenter.default.addObserver(
forName: NSPersistentCloudKitContainer.eventChangedNotification,
object: nil,
queue: .main) { [weak self] notification in
self?.handleCloudKitNotification(notification)
}
}
private func handleCloudKitNotification(_ notification: Notification) {
guard let cloudEvent = notification.userInfo?[NSPersistentCloudKitContainer.eventNotificationUserInfoKey] as? NSPersistentCloudKitContainer.Event else {
return
}
if cloudEvent.endDate == nil {
// Event started
activeEventIdentifiers.insert(cloudEvent.identifier)
syncStatus = .syncing
} else {
// Event ended
activeEventIdentifiers.remove(cloudEvent.identifier)
if let error = cloudEvent.error {
// If any event errors, we show error state
print("CloudKit Sync Error: \(error.localizedDescription)")
syncStatus = .error
} else if activeEventIdentifiers.isEmpty {
// Only switch to synced if no other events are active
syncStatus = .synced
}
}
}
}
struct CloudSyncStatusView: View {
@ObservedObject var syncMonitor: CloudKitSyncMonitor
var body: some View {
VStack(alignment: .leading) {
Text("iCloud sync")
.font(.headline)
.foregroundColor(.secondary)
HStack {
VStack(alignment: .leading, spacing: 10) {
HStack {
Text(statusMessage)
.font(.body)
.foregroundColor(.primary)
Spacer()
}
HStack {
Image(systemName: "icloud")
Text(syncMonitor.syncStatus == .synced ? "Synced with iCloud" : "Syncing with iCloud...")
}
.font(.caption)
.foregroundColor(.secondary)
}
Spacer()
}
.padding()
.background(Color(.systemBackground))
.cornerRadius(10)
.shadow(radius: 1)
}
.padding(.vertical, 5)
}
var statusMessage: String {
switch syncMonitor.syncStatus {
case .unknown:
return "Unknown synchronization status."
case .syncing:
return "Synchronization with iCloud may take a few minutes to initially upload your data."
case .synced:
return "Your data is synchronized with iCloud."
case .error:
return "Unfortunately, iCloud is not the most reliable service, so problems can occur. We apologize for any inconvenience this may cause."
}
}
}
#Preview {
CloudSyncStatusView(syncMonitor: .init())
}