Skip to content

Commit

Permalink
Merge pull request #7078 from vector-im/andy/large_backups
Browse files Browse the repository at this point in the history
Import progress for room keys
  • Loading branch information
Anderas authored Nov 15, 2022
2 parents 9dc6ee0 + 20668a5 commit 62980be
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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),
Expand All @@ -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 -

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
4 changes: 2 additions & 2 deletions Riot/Modules/Settings/Security/SecurityViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions changelog.d/pr-7078.change
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CryptoV2: Import progress for room keys

0 comments on commit 62980be

Please sign in to comment.