Files
element-x-ios/ElementX/Sources/Screens/SecureBackup/SecureBackupLogoutConfirmationScreen/View/SecureBackupLogoutConfirmationScreen.swift
T
Mauro 2d486ece33 Use iOS 26 Observation for Unit and Preview tests. (#5797)
* use observation in unit and preview tests instead of the async stream

* use a parsing approach that is independent of the sourcery annotation

* Fix observation deferred fulfillments not receiving the initial value

Observation's observe only emits transitions, not the value present at subscription time. Drop the leading initial value from the affected transitionValues sequences and assert it directly with #expect instead.

* improved docs
2026-06-30 14:01:08 +02:00

176 lines
6.3 KiB
Swift

//
// Copyright 2025 Element Creations Ltd.
// Copyright 2022-2025 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 Compound
import SwiftUI
struct SecureBackupLogoutConfirmationScreen: View {
@Bindable var context: SecureBackupLogoutConfirmationScreenViewModel.Context
var body: some View {
FullscreenDialog {
VStack(spacing: 16) {
BigIcon(icon: \.keyOffSolid)
content
}
.padding()
} bottomContent: {
footer.padding()
}
.toolbar { toolbar }
.background()
.backgroundStyle(.compound.bgCanvasDefault)
.alert(item: $context.alertInfo)
}
@ViewBuilder
private var content: some View {
Text(title)
.foregroundColor(.compound.textPrimary)
.font(.compound.headingMDBold)
.multilineTextAlignment(.center)
Text(subtitle)
.foregroundColor(.compound.textSecondary)
.font(.compound.bodyMD)
.multilineTextAlignment(.center)
if case let .waitingToStart(hasStalled) = context.viewState.mode {
Spacer()
ProgressView()
if hasStalled {
Text(L10n.commonPleaseCheckInternetConnection)
.font(.compound.bodySM)
.foregroundColor(.compound.textPrimary)
}
} else if case let .backupOngoing(progress) = context.viewState.mode {
Spacer()
ProgressView(value: progress)
}
}
private var footer: some View {
VStack(spacing: 16.0) {
if case .saveRecoveryKey = context.viewState.mode {
Button {
context.send(viewAction: .settings)
} label: {
Text(L10n.commonSettings)
}
.buttonStyle(.compound(.primary))
}
Button(role: .destructive) {
context.send(viewAction: .logout)
} label: {
Text(L10n.actionSignout)
}
.buttonStyle(.compound(.primary))
}
}
@ToolbarContentBuilder
private var toolbar: some ToolbarContent {
ToolbarItem(placement: .cancellationAction) {
Button(L10n.actionCancel) {
context.send(viewAction: .cancel)
}
}
}
private var title: String {
switch context.viewState.mode {
case .saveRecoveryKey:
return L10n.screenSignoutSaveRecoveryKeyTitle
case .waitingToStart, .backupOngoing:
return L10n.screenSignoutKeyBackupOngoingTitle
case .offline:
return L10n.screenSignoutKeyBackupOfflineTitle
}
}
private var subtitle: String {
switch context.viewState.mode {
case .saveRecoveryKey:
return L10n.screenSignoutSaveRecoveryKeySubtitle
case .waitingToStart, .backupOngoing:
return L10n.screenSignoutKeyBackupOngoingSubtitle
case .offline:
return L10n.screenSignoutKeyBackupOfflineSubtitle
}
}
}
// MARK: - Previews
@available(iOS 26.0, *)
struct SecureBackupLogoutConfirmationScreen_Previews: PreviewProvider, TestablePreview {
static let viewModel = makeViewModel(mode: .saveRecoveryKey)
static let waitingViewModel = makeViewModel(mode: .waitingToStart(hasStalled: false))
static let ongoingViewModel = makeViewModel(mode: .backupOngoing(progress: 0.5))
static let offlineViewModel = makeViewModel(mode: .offline)
static var previews: some View {
ElementNavigationStack {
SecureBackupLogoutConfirmationScreen(context: viewModel.context)
}
.previewDisplayName("Confirmation")
ElementNavigationStack {
SecureBackupLogoutConfirmationScreen(context: waitingViewModel.context)
}
.previewDisplayName("Waiting")
.snapshotPreferences(expect: waitingViewModel.context.observe(\.viewState.mode).map { $0 == .waitingToStart(hasStalled: false) })
ElementNavigationStack {
SecureBackupLogoutConfirmationScreen(context: ongoingViewModel.context)
}
.previewDisplayName("Ongoing")
.snapshotPreferences(expect: ongoingViewModel.context.observe(\.viewState.mode).map { $0 == .backupOngoing(progress: 0.5) })
// Uses the same view model as Waiting but with a different expectation.
ElementNavigationStack {
SecureBackupLogoutConfirmationScreen(context: waitingViewModel.context)
}
.previewDisplayName("Stalled")
.snapshotPreferences(expect: waitingViewModel.context.observe(\.viewState.mode).map { $0 == .waitingToStart(hasStalled: true) })
ElementNavigationStack {
SecureBackupLogoutConfirmationScreen(context: offlineViewModel.context)
}
.previewDisplayName("Offline")
.snapshotPreferences(expect: offlineViewModel.context.observe(\.viewState.mode).map { $0 == .offline })
}
static func makeViewModel(mode: SecureBackupLogoutConfirmationScreenViewMode) -> SecureBackupLogoutConfirmationScreenViewModel {
let secureBackupController = SecureBackupControllerMock()
secureBackupController.keyBackupState = CurrentValueSubject<SecureBackupKeyBackupState, Never>(.enabled).asCurrentValuePublisher()
secureBackupController.waitForKeyBackupUploadUploadStateSubjectClosure = { uploadStateSubject in
if case .backupOngoing = mode {
uploadStateSubject.send(.uploading(uploadedKeyCount: 50, totalKeyCount: 100))
}
return .success(())
}
let reachability: HomeserverReachability = mode == .offline ? .unreachable : .reachable
let viewModel = SecureBackupLogoutConfirmationScreenViewModel(secureBackupController: secureBackupController,
homeserverReachabilityPublisher: .init(reachability))
if mode != .saveRecoveryKey {
viewModel.context.send(viewAction: .logout)
}
return viewModel
}
}