Skip to content

Commit

Permalink
Merge branch 'develop' into mauroromito/show_rr_sheet
Browse files Browse the repository at this point in the history
  • Loading branch information
Velin92 authored Nov 21, 2023
2 parents 951b901 + bec6cda commit bf0ba79
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class HomeScreenViewModel: HomeScreenViewModelType, HomeScreenViewModelProtocol
.sink { [weak self] recoveryKeyState in
guard let self, appSettings.chatBackupEnabled else { return }

let requiresSecureBackupSetup = recoveryKeyState == .unknown || recoveryKeyState == .disabled || recoveryKeyState == .incomplete
let requiresSecureBackupSetup = recoveryKeyState == .disabled || recoveryKeyState == .incomplete

state.showUserMenuBadge = requiresSecureBackupSetup
state.showSettingsMenuOptionBadge = requiresSecureBackupSetup
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct SecureBackupScreen: View {

var body: some View {
Form {
if context.viewState.keyBackupState != .enabled {
if context.viewState.keyBackupState == .disabled {
keyBackupSection
} else {
keyBackupSection
Expand Down Expand Up @@ -77,10 +77,12 @@ struct SecureBackupScreen: View {
ListRow(label: .plain(title: L10n.screenChatBackupKeyBackupActionDisable, role: .destructive), kind: .navigationLink {
context.send(viewAction: .keyBackup)
})
case .disabled, .enabling, .unknown:
case .disabled, .enabling:
ListRow(label: .plain(title: L10n.screenChatBackupKeyBackupActionEnable), kind: .navigationLink {
context.send(viewAction: .keyBackup)
})
case .unknown:
EmptyView()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ class SecureBackupController: SecureBackupControllerProtocol {
private var backupStateListenerTaskHandle: TaskHandle?
private var recoveryStateListenerTaskHandle: TaskHandle?

/// Used to dedupe remote backup state requests
@CancellableTask private var remoteBackupStateTask: Task<Void, Error>?

var recoveryKeyState: CurrentValuePublisher<SecureBackupRecoveryKeyState, Never> {
recoveryKeyStateSubject.asCurrentValuePublisher()
}
Expand All @@ -39,11 +42,18 @@ class SecureBackupController: SecureBackupControllerProtocol {
self.encryption = encryption

backupStateListenerTaskHandle = encryption.backupStateListener(listener: SecureBackupControllerBackupStateListener { [weak self] state in
self?.keyBackupStateSubject.send(state.keyBackupState)
guard let self else { return }

keyBackupStateSubject.send(state.keyBackupState)

if case .unknown = state {
updateBackupStateFromRemote()
}
})

recoveryStateListenerTaskHandle = encryption.recoveryStateListener(listener: SecureBackupRecoveryStateListener { [weak self] state in
guard let self else { return }

switch state {
case .unknown:
recoveryKeyStateSubject.send(.unknown)
Expand All @@ -55,6 +65,8 @@ class SecureBackupController: SecureBackupControllerProtocol {
recoveryKeyStateSubject.send(.incomplete)
}
})

updateBackupStateFromRemote()
}

func enable() async -> Result<Void, SecureBackupControllerError> {
Expand Down Expand Up @@ -140,6 +152,30 @@ class SecureBackupController: SecureBackupControllerProtocol {
return .failure(.failedUploadingForBackup)
}
}

// MARK: - Private

private func updateBackupStateFromRemote(retry: Bool = true) {
remoteBackupStateTask = Task {
do {
let backupExists = try await self.encryption.backupExistsOnServer()

if Task.isCancelled {
return
}

if !backupExists {
keyBackupStateSubject.send(.disabled)
}
} catch {
MXLog.error("Failed retrieving remote backup state with error: \(error)")

if retry {
updateBackupStateFromRemote(retry: false)
}
}
}
}
}

private final class SecureBackupControllerBackupStateListener: BackupStateListener {
Expand Down Expand Up @@ -182,7 +218,7 @@ extension BackupState {
var keyBackupState: SecureBackupKeyBackupState {
switch self {
case .unknown:
return .disabled
return .unknown
case .creating:
return .enabling
case .enabling:
Expand Down

0 comments on commit bf0ba79

Please sign in to comment.