Files
letro-ios/ElementX/Sources/Screens/SecureBackup/SecureBackupLogoutConfirmationScreen/SecureBackupLogoutConfirmationScreenViewModel.swift
Doug b4174aed22 Adopt StateStoreViewModelV2 in the remaining settings screens. (#4158)
* Move the AuthenticationStartScreen into the Authentication directory.

* Commit the updated Sentry license.

No idea why they dropped the 2024 🤷‍♂️

* Use StateStoreViewModelV2 in BugReportScreen.

* Use StateStoreViewModelV2 in UserDetailsEditScreen.

* Use StateStoreViewModelV2 in NotificationSettingsScreen.

* Use StateStoreViewModelV2 in NotificationSettingsEditScreen.

* Use StateStoreViewModelV2 in LegalInformationScreen.

* Use StateStoreViewModelV2 in LogViewerScreen.

* Use StateStoreViewModelV2 in AnalyticsSettingsScreen.

* Rename AdvancedSettingsScreen directory.

* Use StateStoreViewModelV2 in EncryptionResetScreen.

* Use StateStoreViewModelV2 in EncryptionResetPasswordScreen.

* Use StateStoreViewModelV2 in SecureBackup…Screens.

* Use StateStoreViewModelV2 in LoginScreen.

Seems this one was ignored waiting on the fulfillment transitionValues implementation.

* Use StateStoreViewModelV2 in DeactivateAccountScreen.

* Move DeactivateAccountScreen into the Settings directory.
2025-05-30 12:24:56 +01:00

117 lines
4.8 KiB
Swift

//
// Copyright 2022-2024 New Vector Ltd.
//
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
// Please see LICENSE files in the repository root for full details.
//
import Combine
import SwiftUI
typealias SecureBackupLogoutConfirmationScreenViewModelType = StateStoreViewModelV2<SecureBackupLogoutConfirmationScreenViewState, SecureBackupLogoutConfirmationScreenViewAction>
class SecureBackupLogoutConfirmationScreenViewModel: SecureBackupLogoutConfirmationScreenViewModelType, SecureBackupLogoutConfirmationScreenViewModelProtocol {
private let secureBackupController: SecureBackupControllerProtocol
private let appMediator: AppMediatorProtocol
private let backupUploadStateSubject: CurrentValueSubject<SecureBackupSteadyState, Never> = .init(.waiting)
// periphery:ignore - auto cancels when reassigned
@CancellableTask
private var keyUploadWaitingTask: Task<Void, Never>?
@CancellableTask
private var keyUploadStalledTask: Task<Void, Error>?
private var actionsSubject: PassthroughSubject<SecureBackupLogoutConfirmationScreenViewModelAction, Never> = .init()
var actions: AnyPublisher<SecureBackupLogoutConfirmationScreenViewModelAction, Never> {
actionsSubject.eraseToAnyPublisher()
}
init(secureBackupController: SecureBackupControllerProtocol, appMediator: AppMediatorProtocol) {
self.secureBackupController = secureBackupController
self.appMediator = appMediator
super.init(initialViewState: .init(mode: .saveRecoveryKey))
backupUploadStateSubject.combineLatest(appMediator.networkMonitor.reachabilityPublisher)
.receive(on: DispatchQueue.main)
.sink { [weak self] backupState, reachability in
guard let self, state.mode != .saveRecoveryKey else { return }
updateMode(backupState: backupState, reachability: reachability)
}
.store(in: &cancellables)
}
// MARK: - Public
override func process(viewAction: SecureBackupLogoutConfirmationScreenViewAction) {
MXLog.info("View model: received view action: \(viewAction)")
switch viewAction {
case .cancel:
keyUploadWaitingTask = nil
actionsSubject.send(.cancel)
case .settings:
actionsSubject.send(.settings)
case .logout:
attemptLogout()
}
}
// MARK: - Private
private func attemptLogout() {
if case .saveRecoveryKey = state.mode {
updateMode(backupState: backupUploadStateSubject.value, reachability: appMediator.networkMonitor.reachabilityPublisher.value)
keyUploadWaitingTask = Task {
var result = await secureBackupController.waitForKeyBackupUpload(uploadStateSubject: backupUploadStateSubject)
guard !Task.isCancelled else { return }
if case .failure = result {
// Retry the upload first, conditions might have changed.
result = await secureBackupController.waitForKeyBackupUpload(uploadStateSubject: backupUploadStateSubject)
}
guard !Task.isCancelled else { return }
guard case .success = result else {
MXLog.error("Aborting logout due to failure waiting for backup upload.")
state.bindings.alertInfo = .init(id: .backupUploadFailed)
return
}
actionsSubject.send(.logout)
}
} else {
actionsSubject.send(.logout)
}
}
private func updateMode(backupState: SecureBackupSteadyState, reachability: NetworkMonitorReachability) {
switch (backupState, reachability) {
case (.waiting, .reachable):
state.mode = .waitingToStart(hasStalled: false)
showAsStalledAfterTimeout()
case (.uploading(let uploadedKeyCount, let totalKeyCount), .reachable):
state.mode = .backupOngoing(progress: Double(uploadedKeyCount) / Double(totalKeyCount))
case (.error, .reachable):
break // Nothing to do here, it will be handled with the result.
case (.done, .reachable):
state.mode = .backupOngoing(progress: 1.0)
case (_, .unreachable):
state.mode = .offline
}
}
/// If we stay in the waiting state for more than 2-seconds we ask the user to check their connection.
private func showAsStalledAfterTimeout() {
keyUploadStalledTask = Task { [weak self] in
try await Task.sleep(for: .seconds(2))
guard let self, case .waitingToStart(hasStalled: false) = state.mode else { return }
state.mode = .waitingToStart(hasStalled: true)
}
}
}