Skip to content

Commit c2896e2

Browse files
committed
fix backup time checks and timestamps for vss
1 parent d9c00ec commit c2896e2

File tree

4 files changed

+60
-16
lines changed

4 files changed

+60
-16
lines changed

Bitkit/AppScene.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ struct AppScene: View {
349349
let rnTimestamp: UInt64? = await hasRNBackup ? (try? RNBackupClient.shared.getLatestBackupTimestamp()) : nil
350350

351351
// Get VSS backup timestamp
352-
let vssTimestamp = BackupService.shared.getLatestBackupTime()
352+
let vssTimestamp = await BackupService.shared.getLatestBackupTime()
353353

354354
// Determine which backup is more recent
355355
let shouldRestoreRN: Bool = {

Bitkit/Services/BackupService.swift

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -499,14 +499,53 @@ class BackupService {
499499
return statuses[category] ?? BackupItemStatus()
500500
}
501501

502-
func getLatestBackupTime() -> UInt64? {
503-
let statuses = getAllBackupStatuses()
504-
let syncedTimestamps = BackupCategory.allCases.compactMap { category -> UInt64? in
505-
let status = statuses[category] ?? BackupItemStatus()
506-
return status.synced > 0 ? status.synced : nil
502+
func getLatestBackupTime() async -> UInt64? {
503+
do {
504+
try await vssBackupClient.setup()
505+
506+
let timestamps = await withTaskGroup(of: UInt64?.self) { group in
507+
for category in BackupCategory.allCases where category != .lightningConnections {
508+
group.addTask {
509+
await self.getRemoteBackupTimestamp(category: category)
510+
}
511+
}
512+
513+
var results: [UInt64] = []
514+
for await timestamp in group {
515+
if let ts = timestamp, ts > 0 {
516+
results.append(ts)
517+
}
518+
}
519+
return results
520+
}
521+
522+
return timestamps.max()
523+
} catch {
524+
Logger.warn("Failed to get VSS backup timestamp: \(error)", context: "BackupService")
525+
return nil
507526
}
527+
}
528+
529+
private func getRemoteBackupTimestamp(category: BackupCategory) async -> UInt64? {
530+
do {
531+
guard let item = try await vssBackupClient.getObject(key: category.rawValue) else {
532+
return nil
533+
}
508534

509-
return syncedTimestamps.max()
535+
struct BackupWithCreatedAt: Codable {
536+
let createdAt: UInt64?
537+
}
538+
539+
let backup = try JSONDecoder().decode(BackupWithCreatedAt.self, from: item.value)
540+
guard let createdAtMillis = backup.createdAt, createdAtMillis > 0 else {
541+
return nil
542+
}
543+
// Convert from milliseconds to seconds (matching Android behavior)
544+
return createdAtMillis / 1000
545+
} catch {
546+
Logger.debug("Failed to get remote backup timestamp for \(category.rawValue): \(error)", context: "BackupService")
547+
return nil
548+
}
510549
}
511550

512551
func scheduleFullBackup() async {
@@ -576,7 +615,7 @@ class BackupService {
576615
let settingsDict = await SettingsViewModel.shared.getSettingsDictionary()
577616
let payload = SettingsBackupV1(
578617
version: 1,
579-
createdAt: UInt64(Date().timeIntervalSince1970),
618+
createdAt: UInt64(Date().timeIntervalSince1970 * 1000),
580619
settings: settingsDict
581620
)
582621
return try payload.encode()
@@ -592,7 +631,7 @@ class BackupService {
592631

593632
let payload = WidgetsBackupV1(
594633
version: 1,
595-
createdAt: UInt64(Date().timeIntervalSince1970),
634+
createdAt: UInt64(Date().timeIntervalSince1970 * 1000),
596635
widgets: androidWidgetsDict
597636
)
598637
let encoded = try payload.encode()
@@ -603,13 +642,13 @@ class BackupService {
603642
let transfers = try TransferStorage.shared.getAll()
604643
let payload = WalletBackupV1(
605644
version: 1,
606-
createdAt: UInt64(Date().timeIntervalSince1970),
645+
createdAt: UInt64(Date().timeIntervalSince1970 * 1000),
607646
transfers: transfers
608647
)
609648
return try JSONEncoder().encode(payload)
610649

611650
case .metadata:
612-
let currentTime = UInt64(Date().timeIntervalSince1970)
651+
let currentTime = UInt64(Date().timeIntervalSince1970 * 1000)
613652
let cache = await SettingsViewModel.shared.getAppCacheData()
614653

615654
let preActivityMetadata = try await CoreService.shared.activity.getAllPreActivityMetadata()
@@ -629,7 +668,7 @@ class BackupService {
629668

630669
let payload = BlocktankBackupV1(
631670
version: 1,
632-
createdAt: UInt64(Date().timeIntervalSince1970),
671+
createdAt: UInt64(Date().timeIntervalSince1970 * 1000),
633672
orders: orders,
634673
cjitEntries: cjitEntries,
635674
info: info
@@ -644,7 +683,7 @@ class BackupService {
644683

645684
let payload = ActivityBackupV1(
646685
version: 1,
647-
createdAt: UInt64(Date().timeIntervalSince1970),
686+
createdAt: UInt64(Date().timeIntervalSince1970 * 1000),
648687
activities: activities,
649688
activityTags: activityTags,
650689
closedChannels: closedChannels

Bitkit/Views/Backup/BackupMetadata.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ struct BackupMetadata: View {
4646
}
4747

4848
private func loadLastBackupTime() async {
49-
if let timestamp = BackupService.shared.getLatestBackupTime() {
49+
if let timestamp = await BackupService.shared.getLatestBackupTime() {
5050
let date = Date(timeIntervalSince1970: TimeInterval(timestamp))
5151
let formatter = DateFormatter()
5252
formatter.dateStyle = .medium

Bitkit/Views/Settings/AppStatusView.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ struct AppStatusView: View {
66
@EnvironmentObject private var wallet: WalletViewModel
77
@EnvironmentObject private var app: AppViewModel
88

9+
@State private var backupTimestamp: UInt64?
10+
911
var body: some View {
1012
VStack(alignment: .leading, spacing: 0) {
1113
NavigationBar(title: t("settings__status__title"))
@@ -30,10 +32,14 @@ struct AppStatusView: View {
3032
.onAppear {
3133
wallet.syncState()
3234
}
35+
.task {
36+
backupTimestamp = await BackupService.shared.getLatestBackupTime()
37+
}
3338
}
3439

3540
private func refreshAppStatus() async {
3641
wallet.syncState()
42+
backupTimestamp = await BackupService.shared.getLatestBackupTime()
3743

3844
if wallet.nodeLifecycleState == .running {
3945
do {
@@ -133,11 +139,10 @@ struct AppStatusView: View {
133139
}
134140

135141
private var backupStatusRow: some View {
136-
let timestamp = BackupService.shared.getLatestBackupTime()
137142
let description: String
138143
let status: HealthStatus
139144

140-
if let timestamp {
145+
if let timestamp = backupTimestamp {
141146
let date = Date(timeIntervalSince1970: TimeInterval(timestamp))
142147
let formatter = DateFormatter()
143148
formatter.dateStyle = .medium

0 commit comments

Comments
 (0)