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.
This commit is contained in:
Doug
2025-05-30 12:24:56 +01:00
committed by GitHub
parent 565ca3ab0b
commit 458c6aaa57
54 changed files with 238 additions and 294 deletions
@@ -8,7 +8,7 @@
import Combine
import SwiftUI
typealias AnalyticsSettingsScreenViewModelType = StateStoreViewModel<AnalyticsSettingsScreenViewState, AnalyticsSettingsScreenViewAction>
typealias AnalyticsSettingsScreenViewModelType = StateStoreViewModelV2<AnalyticsSettingsScreenViewState, AnalyticsSettingsScreenViewAction>
class AnalyticsSettingsScreenViewModel: AnalyticsSettingsScreenViewModelType, AnalyticsSettingsScreenViewModelProtocol {
private let analytics: AnalyticsService
@@ -9,7 +9,7 @@ import Compound
import SwiftUI
struct AnalyticsSettingsScreen: View {
@ObservedObject var context: AnalyticsSettingsScreenViewModel.Context
@Bindable var context: AnalyticsSettingsScreenViewModel.Context
var body: some View {
Form {
@@ -0,0 +1,54 @@
//
// 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
struct DeactivateAccountScreenCoordinatorParameters {
let clientProxy: ClientProxyProtocol
let userIndicatorController: UserIndicatorControllerProtocol
}
enum DeactivateAccountScreenCoordinatorAction {
case accountDeactivated
}
final class DeactivateAccountScreenCoordinator: CoordinatorProtocol {
private let parameters: DeactivateAccountScreenCoordinatorParameters
private let viewModel: DeactivateAccountScreenViewModelProtocol
private var cancellables = Set<AnyCancellable>()
private let actionsSubject: PassthroughSubject<DeactivateAccountScreenCoordinatorAction, Never> = .init()
var actionsPublisher: AnyPublisher<DeactivateAccountScreenCoordinatorAction, Never> {
actionsSubject.eraseToAnyPublisher()
}
init(parameters: DeactivateAccountScreenCoordinatorParameters) {
self.parameters = parameters
viewModel = DeactivateAccountScreenViewModel(clientProxy: parameters.clientProxy,
userIndicatorController: parameters.userIndicatorController)
}
func start() {
viewModel.actionsPublisher.sink { [weak self] action in
MXLog.info("Coordinator: received view model action: \(action)")
guard let self else { return }
switch action {
case .accountDeactivated:
actionsSubject.send(.accountDeactivated)
}
}
.store(in: &cancellables)
}
func toPresentable() -> AnyView {
AnyView(DeactivateAccountScreen(context: viewModel.context))
}
}
@@ -0,0 +1,52 @@
//
// 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 Foundation
enum DeactivateAccountScreenViewModelAction {
case accountDeactivated
}
struct DeactivateAccountScreenViewState: BindableState {
let info: AttributedString
let infoPoint1: AttributedString
let infoPoint2 = AttributedString(L10n.screenDeactivateAccountListItem2)
let infoPoint3 = AttributedString(L10n.screenDeactivateAccountListItem3)
let infoPoint4 = AttributedString(L10n.screenDeactivateAccountListItem4)
var bindings = DeactivateAccountScreenViewStateBindings()
init() {
let boldPlaceholder = "{bold}"
var attributedString = AttributedString(L10n.screenDeactivateAccountDescription(boldPlaceholder))
var boldString = AttributedString(L10n.screenDeactivateAccountDescriptionBoldPart)
boldString.bold()
attributedString.replace(boldPlaceholder, with: boldString)
info = attributedString
attributedString = AttributedString(L10n.screenDeactivateAccountListItem1(boldPlaceholder))
boldString = AttributedString(L10n.screenDeactivateAccountListItem1BoldPart)
boldString.bold()
attributedString.replace(boldPlaceholder, with: boldString)
infoPoint1 = attributedString
}
}
struct DeactivateAccountScreenViewStateBindings {
var password = ""
var eraseData = false
var alertInfo: AlertInfo<DeactivateAccountScreenAlert>?
}
enum DeactivateAccountScreenAlert {
case confirmation
case deactivationFailed
}
enum DeactivateAccountScreenViewAction {
case deactivate
}
@@ -0,0 +1,82 @@
//
// 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 DeactivateAccountScreenViewModelType = StateStoreViewModelV2<DeactivateAccountScreenViewState, DeactivateAccountScreenViewAction>
class DeactivateAccountScreenViewModel: DeactivateAccountScreenViewModelType, DeactivateAccountScreenViewModelProtocol {
private let clientProxy: ClientProxyProtocol
private let userIndicatorController: UserIndicatorControllerProtocol
private let actionsSubject: PassthroughSubject<DeactivateAccountScreenViewModelAction, Never> = .init()
var actionsPublisher: AnyPublisher<DeactivateAccountScreenViewModelAction, Never> {
actionsSubject.eraseToAnyPublisher()
}
init(clientProxy: ClientProxyProtocol, userIndicatorController: UserIndicatorControllerProtocol) {
self.clientProxy = clientProxy
self.userIndicatorController = userIndicatorController
super.init(initialViewState: DeactivateAccountScreenViewState())
}
override func process(viewAction: DeactivateAccountScreenViewAction) {
MXLog.info("View model: received view action: \(viewAction)")
switch viewAction {
case .deactivate:
showDeactivationConfirmation()
}
}
// MARK: - Private
private let deactivatingIndicatorID = "\(DeactivateAccountScreenViewModel.self)-Deactivating"
func showDeactivationConfirmation() {
state.bindings.alertInfo = .init(id: .confirmation,
title: L10n.screenDeactivateAccountTitle,
message: L10n.screenDeactivateAccountConfirmationDialogContent,
primaryButton: .init(title: L10n.actionDeactivate) {
Task { await self.deactivateAccount() }
},
secondaryButton: .init(title: L10n.actionCancel, role: .cancel, action: nil))
}
func deactivateAccount() async {
userIndicatorController.submitIndicator(UserIndicator(id: deactivatingIndicatorID,
type: .modal(progress: .indeterminate, interactiveDismissDisabled: true, allowsInteraction: false),
title: L10n.commonPleaseWait,
persistent: true))
MXLog.warning("Deactivating account.")
switch await clientProxy.deactivateAccount(password: nil, eraseData: state.bindings.eraseData) {
case .success:
MXLog.info("Account deactivated (no password needed).")
actionsSubject.send(.accountDeactivated)
return
case .failure:
MXLog.info("Request failed, including password.")
}
switch await clientProxy.deactivateAccount(password: state.bindings.password, eraseData: state.bindings.eraseData) {
case .success:
MXLog.info("Account deactivated.")
actionsSubject.send(.accountDeactivated)
return
case .failure(let failure):
MXLog.info("Deactivation failed \(failure).")
state.bindings.alertInfo = .init(id: .deactivationFailed,
title: L10n.errorUnknown,
message: String(describing: failure))
userIndicatorController.retractIndicatorWithId(deactivatingIndicatorID)
}
}
}
@@ -0,0 +1,14 @@
//
// 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
@MainActor
protocol DeactivateAccountScreenViewModelProtocol {
var actionsPublisher: AnyPublisher<DeactivateAccountScreenViewModelAction, Never> { get }
var context: DeactivateAccountScreenViewModelType.Context { get }
}
@@ -0,0 +1,102 @@
//
// 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 Compound
import SwiftUI
struct DeactivateAccountScreen: View {
@Bindable var context: DeactivateAccountScreenViewModel.Context
var body: some View {
Form {
infoSection
eraseDataSection
passwordSection
}
.compoundList()
.safeAreaInset(edge: .bottom) {
Button(L10n.actionDeactivateAccount, role: .destructive) {
context.send(viewAction: .deactivate)
}
.buttonStyle(.compound(.primary))
.disabled(context.password.isEmpty)
.padding(16)
.background(Color.compound.bgSubtleSecondaryLevel0.ignoresSafeArea())
}
.navigationTitle(L10n.screenDeactivateAccountTitle)
.navigationBarTitleDisplayMode(.inline)
.alert(item: $context.alertInfo)
}
private var infoSection: some View {
ListRow(kind: .custom {
VStack(alignment: .leading, spacing: 16) {
Text(context.viewState.info)
VStack(alignment: .leading, spacing: 8) {
InfoItem(title: context.viewState.infoPoint1)
InfoItem(title: context.viewState.infoPoint2)
InfoItem(title: context.viewState.infoPoint3)
InfoItem(title: context.viewState.infoPoint4, isSuccess: true)
}
}
.foregroundColor(.compound.textSecondary)
.font(.compound.bodyMD)
.listRowBackground(Color.clear)
})
}
private var eraseDataSection: some View {
Section {
ListRow(label: .plain(title: L10n.screenDeactivateAccountDeleteAllMessages),
kind: .toggle($context.eraseData))
} footer: {
Text(L10n.screenDeactivateAccountDeleteAllMessagesNotice)
.compoundListSectionFooter()
}
}
private var passwordSection: some View {
Section {
ListRow(label: .plain(title: L10n.commonPassword),
kind: .secureField(text: $context.password))
.submitLabel(.done)
} header: {
Text(L10n.actionConfirmPassword)
.compoundListSectionHeader()
}
}
}
private struct InfoItem: View {
let title: AttributedString
var isSuccess = false
var body: some View {
Label {
Text(title).padding(.vertical, 1)
} icon: {
CompoundIcon(isSuccess ? \.check : \.close,
size: .small,
relativeTo: .compound.bodyMD)
.foregroundStyle(isSuccess ? .compound.iconSuccessPrimary : .compound.iconCriticalPrimary)
}
.labelStyle(.custom(spacing: 8, alignment: .top))
}
}
// MARK: - Previews
struct DeactivateAccountScreen_Previews: PreviewProvider, TestablePreview {
static let viewModel = DeactivateAccountScreenViewModel(clientProxy: ClientProxyMock(.init()),
userIndicatorController: UserIndicatorControllerMock())
static var previews: some View {
NavigationStack {
DeactivateAccountScreen(context: viewModel.context)
}
}
}
@@ -8,7 +8,7 @@
import Combine
import SwiftUI
typealias LegalInformationScreenViewModelType = StateStoreViewModel<LegalInformationScreenViewState, LegalInformationScreenViewAction>
typealias LegalInformationScreenViewModelType = StateStoreViewModelV2<LegalInformationScreenViewState, LegalInformationScreenViewAction>
class LegalInformationScreenViewModel: LegalInformationScreenViewModelType, LegalInformationScreenViewModelProtocol {
init(appSettings: AppSettings) {
@@ -9,7 +9,7 @@ import Compound
import SwiftUI
struct LegalInformationScreen: View {
@ObservedObject var context: LegalInformationScreenViewModel.Context
let context: LegalInformationScreenViewModel.Context
@Environment(\.openURL) private var openURL
var body: some View {
@@ -8,7 +8,7 @@
import Combine
import SwiftUI
typealias NotificationSettingsEditScreenViewModelType = StateStoreViewModel<NotificationSettingsEditScreenViewState, NotificationSettingsEditScreenViewAction>
typealias NotificationSettingsEditScreenViewModelType = StateStoreViewModelV2<NotificationSettingsEditScreenViewState, NotificationSettingsEditScreenViewAction>
class NotificationSettingsEditScreenViewModel: NotificationSettingsEditScreenViewModelType, NotificationSettingsEditScreenViewModelProtocol {
private var actionsSubject: PassthroughSubject<NotificationSettingsEditScreenViewModelAction, Never> = .init()
@@ -9,7 +9,7 @@ import Compound
import SwiftUI
struct NotificationSettingsEditScreen: View {
@ObservedObject var context: NotificationSettingsEditScreenViewModel.Context
@Bindable var context: NotificationSettingsEditScreenViewModel.Context
var body: some View {
Form {
@@ -8,7 +8,7 @@
import Combine
import SwiftUI
typealias NotificationSettingsScreenViewModelType = StateStoreViewModel<NotificationSettingsScreenViewState, NotificationSettingsScreenViewAction>
typealias NotificationSettingsScreenViewModelType = StateStoreViewModelV2<NotificationSettingsScreenViewState, NotificationSettingsScreenViewAction>
class NotificationSettingsScreenViewModel: NotificationSettingsScreenViewModelType, NotificationSettingsScreenViewModelProtocol {
private var actionsSubject: PassthroughSubject<NotificationSettingsScreenViewModelAction, Never> = .init()
@@ -9,7 +9,7 @@ import Compound
import SwiftUI
struct NotificationSettingsScreen: View {
@ObservedObject var context: NotificationSettingsScreenViewModel.Context
@Bindable var context: NotificationSettingsScreenViewModel.Context
var body: some View {
Form {
@@ -254,14 +254,10 @@ struct NotificationSettingsScreen_Previews: PreviewProvider, TestablePreview {
static var previews: some View {
NotificationSettingsScreen(context: viewModel.context)
.snapshotPreferences(expect: viewModel.context.$viewState.map { state in
state.settings != nil
})
.snapshotPreferences(expect: viewModel.context.observe(\.viewState.settings).map { $0 != nil }.eraseToStream())
NotificationSettingsScreen(context: viewModelConfigurationMismatch.context)
.snapshotPreferences(expect: viewModelConfigurationMismatch.context.$viewState.map { state in
state.settings != nil
})
.snapshotPreferences(expect: viewModelConfigurationMismatch.context.observe(\.viewState.settings).map { $0 != nil }.eraseToStream())
.previewDisplayName("Configuration mismatch")
}
}
@@ -8,7 +8,7 @@
import Combine
import SwiftUI
typealias UserDetailsEditScreenViewModelType = StateStoreViewModel<UserDetailsEditScreenViewState, UserDetailsEditScreenViewAction>
typealias UserDetailsEditScreenViewModelType = StateStoreViewModelV2<UserDetailsEditScreenViewState, UserDetailsEditScreenViewAction>
class UserDetailsEditScreenViewModel: UserDetailsEditScreenViewModelType, UserDetailsEditScreenViewModelProtocol {
private let actionsSubject: PassthroughSubject<UserDetailsEditScreenViewModelAction, Never> = .init()
@@ -9,7 +9,7 @@ import Compound
import SwiftUI
struct UserDetailsEditScreen: View {
@ObservedObject var context: UserDetailsEditScreenViewModel.Context
@Bindable var context: UserDetailsEditScreenViewModel.Context
@FocusState private var focus: Bool
var body: some View {