diff --git a/Riot/Modules/Settings/Security/SecureBackup/SettingsSecureBackupTableViewSection.swift b/Riot/Modules/Settings/Security/SecureBackup/SettingsSecureBackupTableViewSection.swift index ffeed21214..58982bb2a9 100644 --- a/Riot/Modules/Settings/Security/SecureBackup/SettingsSecureBackupTableViewSection.swift +++ b/Riot/Modules/Settings/Security/SecureBackup/SettingsSecureBackupTableViewSection.swift @@ -140,8 +140,20 @@ private enum BackupRows { .info(text: infoText), .createSecureBackupAction ] - case .keyBackup(let keyBackupVersion, _, _), - .keyBackupNotTrusted(let keyBackupVersion, _): // Manage the key backup in the same way for the moment + case .keyBackup(let keyBackupVersion, _, let progress): + if let progress = progress { + backupRows = [ + .info(text: importProgressText(for: progress)), + .deleteKeyBackupAction(keyBackupVersion: keyBackupVersion) + ] + } else { + backupRows = [ + .info(text: VectorL10n.securitySettingsSecureBackupInfoValid), + .restoreFromKeyBackupAction(keyBackupVersion: keyBackupVersion, title: VectorL10n.securitySettingsSecureBackupRestore), + .deleteKeyBackupAction(keyBackupVersion: keyBackupVersion) + ] + } + case .keyBackupNotTrusted(let keyBackupVersion, _): backupRows = [ .info(text: VectorL10n.securitySettingsSecureBackupInfoValid), .restoreFromKeyBackupAction(keyBackupVersion: keyBackupVersion, title: VectorL10n.securitySettingsSecureBackupRestore), @@ -160,8 +172,22 @@ private enum BackupRows { .createKeyBackupAction, .resetSecureBackupAction ] - case .keyBackup(let keyBackupVersion, _, _), - .keyBackupNotTrusted(let keyBackupVersion, _): // Manage the key backup in the same way for the moment + case .keyBackup(let keyBackupVersion, _, let progress): + if let progress = progress { + backupRows = [ + .info(text: importProgressText(for: progress)), + .deleteKeyBackupAction(keyBackupVersion: keyBackupVersion), + .resetSecureBackupAction + ] + } else { + backupRows = [ + .info(text: VectorL10n.securitySettingsSecureBackupInfoValid), + .restoreFromKeyBackupAction(keyBackupVersion: keyBackupVersion, title: VectorL10n.securitySettingsSecureBackupRestore), + .deleteKeyBackupAction(keyBackupVersion: keyBackupVersion), + .resetSecureBackupAction + ] + } + case .keyBackupNotTrusted(let keyBackupVersion, _): backupRows = [ .info(text: VectorL10n.securitySettingsSecureBackupInfoValid), .restoreFromKeyBackupAction(keyBackupVersion: keyBackupVersion, title: VectorL10n.securitySettingsSecureBackupRestore), @@ -172,6 +198,11 @@ private enum BackupRows { } self.backupRows = backupRows } + + private func importProgressText(for progress: Progress) -> String { + let percentage = Int(round(progress.fractionCompleted * 100)) + return VectorL10n.keyBackupRecoverFromPrivateKeyInfo + " \(percentage)%" + } // MARK: - Cells - diff --git a/Riot/Modules/Settings/Security/SecureBackup/SettingsSecureBackupViewModel.swift b/Riot/Modules/Settings/Security/SecureBackup/SettingsSecureBackupViewModel.swift index 66b7208615..5039bef6f6 100644 --- a/Riot/Modules/Settings/Security/SecureBackup/SettingsSecureBackupViewModel.swift +++ b/Riot/Modules/Settings/Security/SecureBackup/SettingsSecureBackupViewModel.swift @@ -24,6 +24,7 @@ final class SettingsSecureBackupViewModel: SettingsSecureBackupViewModelType { // MARK: Private private let recoveryService: MXRecoveryService private let keyBackup: MXKeyBackup + private var progressUpdateTimer: Timer? init(recoveryService: MXRecoveryService, keyBackup: MXKeyBackup) { self.recoveryService = recoveryService @@ -106,17 +107,13 @@ final class SettingsSecureBackupViewModel: SettingsSecureBackupViewModelType { guard let keyBackupVersion = self.keyBackup.keyBackupVersion, let keyBackupVersionTrust = keyBackupVersionTrust else { return } - - // Get the backup progress before updating the state - self.keyBackup.backupProgress { [weak self] (progress) in - guard let self = self else { - return - } - - let keyBackupState: SettingsSecureBackupViewState.KeyBackupState = .keyBackup(keyBackupVersion, keyBackupVersionTrust, progress) - let viewState: SettingsSecureBackupViewState = self.recoveryService.hasRecovery() ? .secureBackup(keyBackupState) : .noSecureBackup(keyBackupState) - self.viewDelegate?.settingsSecureBackupViewModel(self, didUpdateViewState: viewState) - } + + let importProgress = keyBackup.importProgress + let keyBackupState: SettingsSecureBackupViewState.KeyBackupState = .keyBackup(keyBackupVersion, keyBackupVersionTrust, importProgress) + let viewState: SettingsSecureBackupViewState = self.recoveryService.hasRecovery() ? .secureBackup(keyBackupState) : .noSecureBackup(keyBackupState) + self.viewDelegate?.settingsSecureBackupViewModel(self, didUpdateViewState: viewState) + scheduleProgressUpdateIfNecessary(keyBackupVersionTrust: keyBackupVersionTrust, progress: importProgress) + default: break } @@ -130,6 +127,17 @@ final class SettingsSecureBackupViewModel: SettingsSecureBackupViewModelType { self.viewDelegate?.settingsSecureBackupViewModel(self, didUpdateViewState: viewState) } } + + private func scheduleProgressUpdateIfNecessary(keyBackupVersionTrust: MXKeyBackupVersionTrust, progress: Progress?) { + if progress != nil { + progressUpdateTimer = Timer.scheduledTimer(withTimeInterval: 1, repeats: false) { [weak self] _ in + self?.computeState(withBackupVersionTrust: keyBackupVersionTrust) + } + } else { + progressUpdateTimer?.invalidate() + progressUpdateTimer = nil + } + } private func deleteKeyBackupVersion(_ keyBackupVersion: MXKeyBackupVersion) { guard let keyBackupVersionVersion = keyBackupVersion.version else { diff --git a/Riot/Modules/Settings/Security/SecureBackup/SettingsSecureBackupViewState.swift b/Riot/Modules/Settings/Security/SecureBackup/SettingsSecureBackupViewState.swift index 1390f56ba7..96e698a423 100644 --- a/Riot/Modules/Settings/Security/SecureBackup/SettingsSecureBackupViewState.swift +++ b/Riot/Modules/Settings/Security/SecureBackup/SettingsSecureBackupViewState.swift @@ -35,7 +35,7 @@ enum SettingsSecureBackupViewState { /// - keyBackupNotTrusted: There is a backup on the homeserver but it is not trusted enum KeyBackupState { case noKeyBackup - case keyBackup(MXKeyBackupVersion, MXKeyBackupVersionTrust, Progress) + case keyBackup(MXKeyBackupVersion, MXKeyBackupVersionTrust, Progress?) case keyBackupNotTrusted(MXKeyBackupVersion, MXKeyBackupVersionTrust) } } diff --git a/Riot/Modules/Settings/Security/SecurityViewController.m b/Riot/Modules/Settings/Security/SecurityViewController.m index d3c7c6c357..cb72f38df1 100644 --- a/Riot/Modules/Settings/Security/SecurityViewController.m +++ b/Riot/Modules/Settings/Security/SecurityViewController.m @@ -1218,9 +1218,9 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N cell = [secureBackupSection cellForRowAtRow:rowTag]; } #ifdef CROSS_SIGNING_AND_BACKUP_DEV - else if (section == SECTION_KEYBACKUP) + else if (sectionTag == SECTION_KEYBACKUP) { - cell = [keyBackupSection cellForRowAtRow:row]; + cell = [keyBackupSection cellForRowAtRow:rowTag]; } #endif else if (sectionTag == SECTION_CROSSSIGNING) diff --git a/changelog.d/pr-7078.change b/changelog.d/pr-7078.change new file mode 100644 index 0000000000..5f2b053a5c --- /dev/null +++ b/changelog.d/pr-7078.change @@ -0,0 +1 @@ +CryptoV2: Import progress for room keys