Add support for account deactivation when not using OIDC. (#3295)

This commit is contained in:
Doug
2024-09-17 20:08:35 +01:00
committed by GitHub
parent 8660a22b3e
commit 5625e78fc9
41 changed files with 822 additions and 35 deletions
@@ -0,0 +1,57 @@
//
// Copyright 2022-2024 New Vector Ltd.
//
// SPDX-License-Identifier: AGPL-3.0-only
// Please see LICENSE in the repository root for full details.
//
import Combine
import SwiftUI
struct DeactivateAccountScreenCoordinatorParameters {
let clientProxy: ClientProxyProtocol
let userIndicatorController: UserIndicatorControllerProtocol
}
enum DeactivateAccountScreenCoordinatorAction {
case cancel
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 .cancel:
actionsSubject.send(.cancel)
case .accountDeactivated:
actionsSubject.send(.accountDeactivated)
}
}
.store(in: &cancellables)
}
func toPresentable() -> AnyView {
AnyView(DeactivateAccountScreen(context: viewModel.context))
}
}
@@ -0,0 +1,54 @@
//
// Copyright 2022-2024 New Vector Ltd.
//
// SPDX-License-Identifier: AGPL-3.0-only
// Please see LICENSE in the repository root for full details.
//
import Foundation
enum DeactivateAccountScreenViewModelAction {
case cancel
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
case cancel
}
@@ -0,0 +1,84 @@
//
// Copyright 2022-2024 New Vector Ltd.
//
// SPDX-License-Identifier: AGPL-3.0-only
// Please see LICENSE in the repository root for full details.
//
import Combine
import SwiftUI
typealias DeactivateAccountScreenViewModelType = StateStoreViewModel<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 .cancel:
actionsSubject.send(.cancel)
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, action: {
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
// Please see LICENSE 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,111 @@
//
// Copyright 2022-2024 New Vector Ltd.
//
// SPDX-License-Identifier: AGPL-3.0-only
// Please see LICENSE in the repository root for full details.
//
import Compound
import SwiftUI
struct DeactivateAccountScreen: View {
@ObservedObject 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)
.toolbar { toolbar }
.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 var toolbar: some ToolbarContent {
ToolbarItem(placement: .cancellationAction) {
Button(L10n.actionCancel) {
context.send(viewAction: .cancel)
}
}
}
}
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)
}
}
}
@@ -36,6 +36,7 @@ struct EncryptionResetPasswordScreen: View {
}
.buttonStyle(.compound(.primary))
}
.background()
.backgroundStyle(.compound.bgCanvasDefault)
.interactiveDismissDisabled()
.onAppear { textFieldFocus = true }
@@ -49,6 +50,7 @@ struct EncryptionResetPasswordScreen: View {
.font(.compound.bodySMSemibold)
SecureField(L10n.screenResetEncryptionPasswordPlaceholder, text: $context.password)
.tint(.compound.iconAccentTertiary)
.frame(maxWidth: .infinity)
.padding()
.background(Color.compound.bgSubtleSecondaryLevel0)
@@ -196,6 +196,7 @@ struct SecureBackupRecoveryKeyScreen: View {
.font(.compound.bodySMSemibold)
SecureField(L10n.screenRecoveryKeyConfirmKeyPlaceholder, text: $context.confirmationRecoveryKey)
.tint(.compound.iconAccentTertiary)
.frame(maxWidth: .infinity)
.padding()
.background(Color.compound.bgSubtleSecondaryLevel0)
@@ -27,6 +27,7 @@ enum SettingsScreenCoordinatorAction {
case notifications
case advancedSettings
case developerOptions
case deactivateAccount
}
final class SettingsScreenCoordinator: CoordinatorProtocol {
@@ -75,6 +76,8 @@ final class SettingsScreenCoordinator: CoordinatorProtocol {
actionsSubject.send(.developerOptions)
case .logout:
actionsSubject.send(.logout)
case .deactivateAccount:
actionsSubject.send(.deactivateAccount)
}
}
.store(in: &cancellables)
@@ -22,6 +22,7 @@ enum SettingsScreenViewModelAction: Equatable {
case advancedSettings
case developerOptions
case logout
case deactivateAccount
}
enum SettingsScreenSecuritySectionMode {
@@ -34,6 +35,7 @@ struct SettingsScreenViewState: BindableState {
var userID: String
var accountProfileURL: URL?
var accountSessionsListURL: URL?
var showAccountDeactivation: Bool
var userAvatarURL: URL?
var userDisplayName: String?
var showDeveloperOptions: Bool
@@ -42,6 +44,12 @@ struct SettingsScreenViewState: BindableState {
var showSecuritySectionBadge = false
var showBlockedUsers = false
var bindings = SettingsScreenViewStateBindings()
}
struct SettingsScreenViewStateBindings {
var isPresentingAccountDeactivationConfirmation = false
}
enum SettingsScreenViewAction {
@@ -59,4 +67,5 @@ enum SettingsScreenViewAction {
case developerOptions
case advancedSettings
case logout
case deactivateAccount
}
@@ -20,6 +20,7 @@ class SettingsScreenViewModel: SettingsScreenViewModelType, SettingsScreenViewMo
init(userSession: UserSessionProtocol) {
super.init(initialViewState: .init(deviceID: userSession.clientProxy.deviceID,
userID: userSession.clientProxy.userID,
showAccountDeactivation: userSession.clientProxy.canDeactivateAccount,
showDeveloperOptions: AppSettings.isDevelopmentBuild),
mediaProvider: userSession.mediaProvider)
@@ -105,6 +106,8 @@ class SettingsScreenViewModel: SettingsScreenViewModelType, SettingsScreenViewMo
state.showDeveloperOptions = true
case .developerOptions:
actionsSubject.send(.developerOptions)
case .deactivateAccount:
actionsSubject.send(.deactivateAccount)
}
}
}
@@ -29,6 +29,8 @@ struct SettingsScreen: View {
}
generalSection
signOutSection
}
.compoundList()
.navigationTitle(L10n.commonSettings)
@@ -167,7 +169,11 @@ struct SettingsScreen: View {
})
.accessibilityIdentifier(A11yIdentifiers.settingsScreen.developerOptions)
}
}
}
private var signOutSection: some View {
Section {
ListRow(label: .action(title: L10n.screenSignoutPreferenceItem,
icon: \.signOut,
role: .destructive),
@@ -175,6 +181,14 @@ struct SettingsScreen: View {
context.send(viewAction: .logout)
})
.accessibilityIdentifier(A11yIdentifiers.settingsScreen.logout)
if context.viewState.showAccountDeactivation {
ListRow(label: .action(title: L10n.actionDeactivateAccount,
icon: \.warning,
role: .destructive),
kind: .button {
context.send(viewAction: .deactivateAccount)
})
}
} footer: {
VStack(spacing: 0) {
versionText