Skip to content

Commit

Permalink
Update "Last backup" logic
Browse files Browse the repository at this point in the history
  • Loading branch information
kugel3 committed Jun 10, 2024
1 parent b051682 commit 97cbff1
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,10 @@ public enum SecurityProblem: Hashable, Sendable, Identifiable {
// MARK: - BackupStatus
public struct BackupStatus: Hashable, Codable, Sendable {
public let result: BackupResult
public let upToDate: Bool
public let isCurrent: Bool

public init(result: BackupResult, profile: Profile) {
self.result = result
self.upToDate = result.saveIdentifier == profile.saveIdentifier
self.isCurrent = result.saveIdentifier == profile.saveIdentifier
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,26 +65,11 @@ extension SecurityCenterClient {
problematic.unrecoverable.isEmpty ? nil : problematic.unrecoverable
}

switch backups.cloud?.result.result {
case .success:
if backups.cloud?.upToDate == true {
print("•• CLOUD SUCCESS")
} else {
print("•• CLOUD old but success")
}
case let .started(date):
print("•• CLOUD STARTED \(Date.now.timeIntervalSince(date))")
case .failure:
print("•• CLOUD FAILURE")
case .none:
print("•• CLOUD nil")
}

func hasProblem5() -> Bool {
if isCloudProfileSyncEnabled, let cloudBackup = backups.cloud {
cloudBackup.result.failed
} else {
false // FIXME: GK - is this what we want?
false
}
}

Expand All @@ -93,7 +78,7 @@ extension SecurityCenterClient {
}

func hasProblem7() -> Bool {
!isCloudProfileSyncEnabled && backups.manual?.upToDate == false
!isCloudProfileSyncEnabled && backups.manual?.isCurrent == false
}

func hasProblem9() async -> ProblematicAddresses? {
Expand All @@ -114,8 +99,6 @@ extension SecurityCenterClient {
if hasProblem7() { result.append(.problem7) }

problemsSubject.send(result)

print("•• -> \(result.map(\.number))")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ extension UserDefaults.Dependency {
let now = Date.now
let lastSuccess = result == .success ? now : backups[profile.id]?.lastSuccess
backups[profile.id] = .init(
backupDate: now,
date: now,
saveIdentifier: profile.saveIdentifier,
result: result,
lastSuccess: lastSuccess
Expand All @@ -192,7 +192,7 @@ extension UserDefaults.Dependency {
var backups: [ProfileID: BackupResult] = getLastManualBackups
let now = Date.now
backups[profile.id] = .init(
backupDate: now,
date: now,
saveIdentifier: profile.saveIdentifier,
result: .success,
lastSuccess: now
Expand Down Expand Up @@ -240,7 +240,7 @@ extension UserDefaults.Dependency {
public struct BackupResult: Hashable, Codable, Sendable {
private static let timeoutInterval: TimeInterval = 5 * 60

public let backupDate: Date
public let date: Date
public let saveIdentifier: String
public let result: Result
public let lastSuccess: Date?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,14 @@ public struct ConfigurationBackup: Sendable, FeatureReducer {

public var outdatedBackupPresent: Bool {
guard let lastCloudBackup, lastCloudBackup.result.succeeded else { return false }
return !cloudBackupsEnabled && !lastCloudBackup.upToDate
return !cloudBackupsEnabled && !lastCloudBackup.isCurrent
}

public var actionsRequired: [Item] {
guard let lastCloudBackup else { return [] }
if lastCloudBackup.upToDate, !lastCloudBackup.result.failed {
return []
if let lastCloudBackup, lastCloudBackup.isCurrent, !lastCloudBackup.result.failed {
[]
} else {
return Item.allCases
}
}

public var displayedLastBackup: Date? {
guard let lastCloudBackup else { return nil }
if lastCloudBackup.result.succeeded {
return lastCloudBackup.upToDate ? nil : lastCloudBackup.result.backupDate
} else {
return lastCloudBackup.result.lastSuccess
Item.allCases
}
}

Expand Down Expand Up @@ -218,7 +208,7 @@ public struct ConfigurationBackup: Sendable, FeatureReducer {
.run { send in
for try await lastBackup in await securityCenterClient.lastManualBackup() {
guard !Task.isCancelled else { return }
await send(.internal(.setLastManualBackup(lastBackup?.result.backupDate)))
await send(.internal(.setLastManualBackup(lastBackup?.result.date)))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
import ComposableArchitecture
import SwiftUI

extension ConfigurationBackup.State {
var lastCloudBackupString: String? {
if actionsRequired.isEmpty {
nil
} else {
L10n.ConfigurationBackup.Automated.lastBackup(
lastCloudBackup?.result.lastSuccess.map(RadixDateFormatter.string) ?? L10n.Common.none
)
}
}

var lastManualBackupString: String? {
guard let lastManualBackup else { return nil }
return L10n.ConfigurationBackup.Automated.lastBackup(RadixDateFormatter.string(from: lastManualBackup))
}
}

// MARK: - ConfigurationBackup.View
extension ConfigurationBackup {
@MainActor
Expand Down Expand Up @@ -30,7 +47,7 @@ extension ConfigurationBackup {
let backupsEnabled = viewStore.binding(get: \.cloudBackupsEnabled) { .view(.cloudBackupsToggled($0)) }
AutomatedBackupView(
backupsEnabled: backupsEnabled,
displayedLastBackup: viewStore.displayedLastBackup,
lastBackupString: viewStore.lastCloudBackupString,
actionsRequired: viewStore.actionsRequired,
outdatedBackupPresent: viewStore.outdatedBackupPresent,
deleteOutdatedAction: { store.send(.view(.deleteOutdatedTapped)) }
Expand All @@ -42,7 +59,7 @@ extension ConfigurationBackup {
.textStyle(.body1Header)
.padding(.bottom, .medium2)

ManualBackupView(lastBackedUp: viewStore.lastManualBackup) {
ManualBackupView(lastBackupString: viewStore.lastManualBackupString) {
store.send(.view(.exportTapped))
}
}
Expand Down Expand Up @@ -138,7 +155,7 @@ extension ConfigurationBackup {

struct AutomatedBackupView: SwiftUI.View {
@Binding var backupsEnabled: Bool
let displayedLastBackup: Date?
let lastBackupString: String?
let actionsRequired: [Item]
let outdatedBackupPresent: Bool
let deleteOutdatedAction: () -> Void
Expand All @@ -157,8 +174,8 @@ extension ConfigurationBackup {
.textStyle(.body1Header)
.foregroundStyle(.app.gray1)

if let lastBackedUpString {
Text(lastBackedUpString)
if let lastBackupString {
Text(lastBackupString)
.textStyle(.body2Regular)
.foregroundStyle(.app.gray2)
}
Expand Down Expand Up @@ -209,12 +226,6 @@ extension ConfigurationBackup {
}
}

private var lastBackedUpString: String? {
displayedLastBackup.map { date in
L10n.ConfigurationBackup.Automated.lastBackup(RadixDateFormatter.string(from: date))
}
}

struct ItemView: SwiftUI.View {
@SwiftUI.State private var expanded: Bool = false
let item: Item
Expand Down Expand Up @@ -275,7 +286,7 @@ extension ConfigurationBackup {
}

struct ManualBackupView: SwiftUI.View {
let lastBackedUp: Date?
let lastBackupString: String?
let exportAction: () -> Void

var body: some SwiftUI.View {
Expand All @@ -293,8 +304,8 @@ extension ConfigurationBackup {
.buttonStyle(.primaryRectangular(shouldExpand: true))
.padding(.horizontal, .large2)

if let lastBackedUpString {
Text(lastBackedUpString)
if let lastBackupString {
Text(lastBackupString)
.textStyle(.body2Regular)
.foregroundStyle(.app.gray2)
.padding(.horizontal, .medium2)
Expand All @@ -304,11 +315,6 @@ extension ConfigurationBackup {
}
}
}

private var lastBackedUpString: String? {
guard let lastBackedUp else { return nil }
return L10n.ConfigurationBackup.Automated.lastBackup(RadixDateFormatter.string(from: lastBackedUp))
}
}

struct WarningView: SwiftUI.View {
Expand Down

0 comments on commit 97cbff1

Please sign in to comment.