Skip to content

Commit

Permalink
Added last icloud sync time
Browse files Browse the repository at this point in the history
  • Loading branch information
joeldavidw committed Jun 11, 2024
1 parent 21ec90a commit 6185490
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Chronos/App/MainAppView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ struct MainAppView: View {
@Environment(\.scenePhase) private var scenePhase
@EnvironmentObject var loginStatus: LoginStatus

@AppStorage(StateEnum.LAST_ICLOUD_SYNC.rawValue) var lastIcloudSync: TimeInterval = 0

@Query var chronosCryptos: [ChronosCrypto]

let stateService = Container.shared.stateService()
Expand Down Expand Up @@ -45,5 +47,12 @@ struct MainAppView: View {
loginStatus.loggedIn = false
}
}
.onReceive(NotificationCenter.default.publisher(for: Notification.Name.NSManagedObjectContextDidSave), perform: { _ in
print(modelContext.hasChanges)
lastIcloudSync = Date().timeIntervalSince1970
})
.onReceive(NotificationCenter.default.publisher(for: Notification.Name.NSManagedObjectContextObjectsDidChange), perform: { _ in
lastIcloudSync = Date().timeIntervalSince1970
})
}
}
17 changes: 17 additions & 0 deletions Chronos/App/Tabs/Settings/SettingsTab.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@ struct SettingsTab: View {

@AppStorage(StateEnum.BIOMETRICS_AUTH_ENABLED.rawValue) var stateBiometricsAuth: Bool = false
@AppStorage(StateEnum.ICLOUD_BACKUP_ENABLED.rawValue) var isICloudEnabled: Bool = false
@AppStorage(StateEnum.LAST_ICLOUD_SYNC.rawValue) var lastIcloudSync: TimeInterval = 0

let secureEnclaveService = Container.shared.secureEnclaveService()
let swiftDataService = Container.shared.swiftDataService()
let stateService = Container.shared.stateService()

@State private var showLogoutConfirmation = false
@State private var lastSyncedText = "Syncing..."

let timer = Timer.publish(every: 1, tolerance: 1, on: .main, in: .common).autoconnect()
let formatter = RelativeDateTimeFormatter()

var body: some View {
NavigationStack {
Expand All @@ -20,6 +25,18 @@ struct SettingsTab: View {
Toggle(isOn: $isICloudEnabled, label: {
Text("iCloud Backup")
}).disabled(true)

LabeledContent {
Text(lastSyncedText)
} label: {
Text("Last Synced")
}
.onReceive(timer) { _ in
lastSyncedText = formatter.localizedString(for: Date(timeIntervalSince1970: lastIcloudSync), relativeTo: Date.now)
if lastSyncedText.starts(with: "in") {
lastSyncedText = "Syncing..."
}
}
}

Section(header: Text("Security")) {
Expand Down
2 changes: 2 additions & 0 deletions Chronos/Services/StateService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ enum StateEnum: String {
case ONBOARDING_COMPLETED

case LAST_BIOMETRICS_AUTH_ATTEMPT
case LAST_ICLOUD_SYNC
}

public class StateService {
Expand All @@ -20,6 +21,7 @@ public class StateService {
defaults.setValue(false, forKey: StateEnum.ONBOARDING_COMPLETED.rawValue)

defaults.setValue(0, forKey: StateEnum.LAST_BIOMETRICS_AUTH_ATTEMPT.rawValue)
defaults.setValue(0, forKey: StateEnum.LAST_ICLOUD_SYNC.rawValue)

masterKey.clear()
}
Expand Down

0 comments on commit 6185490

Please sign in to comment.