#1899, #1900, #1901 - Implement chat backup setting screens

This commit is contained in:
Stefan Ceriu
2023-10-19 15:34:10 +03:00
committed by GitHub
parent 15b5b44422
commit c8b9d95122
70 changed files with 2293 additions and 48 deletions
@@ -0,0 +1,62 @@
//
// Copyright 2022 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Combine
import SwiftUI
struct SecureBackupKeyBackupScreenCoordinatorParameters {
let secureBackupController: SecureBackupControllerProtocol
weak var userIndicatorController: UserIndicatorControllerProtocol?
}
enum SecureBackupKeyBackupScreenCoordinatorAction {
case done
}
final class SecureBackupKeyBackupScreenCoordinator: CoordinatorProtocol {
private let parameters: SecureBackupKeyBackupScreenCoordinatorParameters
private var viewModel: SecureBackupKeyBackupScreenViewModelProtocol
private let actionsSubject: PassthroughSubject<SecureBackupKeyBackupScreenCoordinatorAction, Never> = .init()
private var cancellables = Set<AnyCancellable>()
var actions: AnyPublisher<SecureBackupKeyBackupScreenCoordinatorAction, Never> {
actionsSubject.eraseToAnyPublisher()
}
init(parameters: SecureBackupKeyBackupScreenCoordinatorParameters) {
self.parameters = parameters
viewModel = SecureBackupKeyBackupScreenViewModel(secureBackupController: parameters.secureBackupController,
userIndicatorController: parameters.userIndicatorController)
}
func start() {
viewModel.actions.sink { [weak self] action in
MXLog.info("Coordinator: received view model action: \(action)")
guard let self else { return }
switch action {
case .done:
self.actionsSubject.send(.done)
}
}
.store(in: &cancellables)
}
func toPresentable() -> AnyView {
AnyView(SecureBackupKeyBackupScreen(context: viewModel.context))
}
}
@@ -0,0 +1,41 @@
//
// Copyright 2022 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
enum SecureBackupKeyBackupScreenViewModelAction {
case done
}
enum SecureBackupKeyBackupScreenViewMode {
case disableBackup
}
struct SecureBackupKeyBackupScreenViewState: BindableState {
let mode: SecureBackupKeyBackupScreenViewMode
var bindings = SecureBackupKeyBackupScreenViewStateBindings()
}
struct SecureBackupKeyBackupScreenViewStateBindings {
var alertInfo: AlertInfo<UUID>?
}
var alertInfo: AlertInfo<UUID>?
enum SecureBackupKeyBackupScreenViewAction {
case cancel
case toggleBackup
}
@@ -0,0 +1,107 @@
//
// Copyright 2022 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Combine
import SwiftUI
typealias SecureBackupKeyBackupScreenViewModelType = StateStoreViewModel<SecureBackupKeyBackupScreenViewState, SecureBackupKeyBackupScreenViewAction>
class SecureBackupKeyBackupScreenViewModel: SecureBackupKeyBackupScreenViewModelType, SecureBackupKeyBackupScreenViewModelProtocol {
private let secureBackupController: SecureBackupControllerProtocol
private var actionsSubject: PassthroughSubject<SecureBackupKeyBackupScreenViewModelAction, Never> = .init()
var actions: AnyPublisher<SecureBackupKeyBackupScreenViewModelAction, Never> {
actionsSubject.eraseToAnyPublisher()
}
init(secureBackupController: SecureBackupControllerProtocol, userIndicatorController: UserIndicatorControllerProtocol?) {
self.secureBackupController = secureBackupController
super.init(initialViewState: .init(mode: secureBackupController.keyBackupState.value.viewMode))
secureBackupController.keyBackupState
.receive(on: DispatchQueue.main)
.sink { [weak userIndicatorController] state in
let loadingIndicatorIdentifier = "SecureBackupKeyBackupScreenLoading"
switch state {
case .disabling, .enabling, .unknown:
userIndicatorController?.submitIndicator(.init(id: loadingIndicatorIdentifier, type: .modal, title: L10n.commonLoading, persistent: true))
default:
userIndicatorController?.retractIndicatorWithId(loadingIndicatorIdentifier)
}
}
.store(in: &cancellables)
}
// MARK: - Public
override func process(viewAction: SecureBackupKeyBackupScreenViewAction) {
MXLog.info("View model: received view action: \(viewAction)")
switch viewAction {
case .cancel:
actionsSubject.send(.done)
case .toggleBackup:
guard secureBackupController.keyBackupState.value == .enabled else {
fatalError("Tried disabling backup when not enabled")
}
state.bindings.alertInfo = .init(id: .init(),
title: L10n.screenKeyBackupDisableConfirmationTitle,
message: L10n.screenKeyBackupDisableConfirmationDescription,
primaryButton: .init(title: L10n.screenKeyBackupDisableConfirmationActionTurnOff, role: .destructive) { [weak self] in
self?.disableBackup()
},
secondaryButton: .init(title: L10n.actionCancel, role: .cancel, action: nil))
}
}
// MARK: - Private
private func enableBackup() {
Task {
switch await secureBackupController.enableBackup() {
case .success:
actionsSubject.send(.done)
case .failure(let error):
MXLog.error("Failed enabling key backup with error: \(error)")
state.bindings.alertInfo = .init(id: .init())
}
}
}
private func disableBackup() {
Task {
switch await secureBackupController.disableBackup() {
case .success:
actionsSubject.send(.done)
case .failure(let error):
MXLog.error("Failed disabling key backup with error: \(error)")
state.bindings.alertInfo = .init(id: .init())
}
}
}
}
extension SecureBackupKeyBackupState {
var viewMode: SecureBackupKeyBackupScreenViewMode {
guard self == .enabled else {
fatalError("Invalid key backup state")
}
return .disableBackup
}
}
@@ -0,0 +1,23 @@
//
// Copyright 2022 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Combine
@MainActor
protocol SecureBackupKeyBackupScreenViewModelProtocol {
var actions: AnyPublisher<SecureBackupKeyBackupScreenViewModelAction, Never> { get }
var context: SecureBackupKeyBackupScreenViewModelType.Context { get }
}
@@ -0,0 +1,117 @@
//
// Copyright 2022 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Combine
import Compound
import SwiftUI
struct SecureBackupKeyBackupScreen: View {
@ObservedObject var context: SecureBackupKeyBackupScreenViewModel.Context
@ScaledMetric private var iconSize = 70
var body: some View {
mainContent
.padding()
.interactiveDismissDisabled()
.toolbar { toolbar }
.background(Color.compound.bgCanvasDefault.ignoresSafeArea())
.alert(item: $context.alertInfo)
}
@ViewBuilder
private var mainContent: some View {
switch context.viewState.mode {
case .disableBackup:
disableBackupSection
}
}
private var disableBackupSection: some View {
VStack(spacing: 16) {
HeroImage(image: Image(asset: Asset.Images.secureBackupOff))
Text(L10n.screenKeyBackupDisableTitle)
.foregroundColor(.compound.textPrimary)
.font(.compound.headingMDBold)
.multilineTextAlignment(.center)
Text(L10n.screenKeyBackupDisableDescription)
.foregroundColor(.compound.textSecondary)
.font(.compound.bodyMD)
.multilineTextAlignment(.center)
VStack(alignment: .leading) {
Label {
Text(L10n.screenKeyBackupDisableDescriptionPoint1)
.foregroundColor(.compound.textSecondary)
.font(.compound.bodyMD)
} icon: {
CompoundIcon(\.close)
.foregroundColor(.compound.iconCriticalPrimary)
}
Label {
Text(L10n.screenKeyBackupDisableDescriptionPoint2(InfoPlistReader.main.bundleDisplayName))
.foregroundColor(.compound.textSecondary)
.font(.compound.bodyMD)
} icon: {
CompoundIcon(\.close)
.foregroundColor(.compound.iconCriticalPrimary)
}
}
Spacer()
Button(role: .destructive) {
context.send(viewAction: .toggleBackup)
} label: {
Text(L10n.screenChatBackupKeyBackupActionDisable)
}
.buttonStyle(.compound(.primary))
}
}
@ToolbarContentBuilder
private var toolbar: some ToolbarContent {
ToolbarItem(placement: .cancellationAction) {
Button(L10n.actionCancel) {
context.send(viewAction: .cancel)
}
}
}
}
// MARK: - Previews
struct SecureBackupKeyBackupScreen_Previews: PreviewProvider, TestablePreview {
static let setupViewModel = viewModel(keyBackupState: .enabled)
static var previews: some View {
NavigationStack {
SecureBackupKeyBackupScreen(context: setupViewModel.context)
}
.previewDisplayName("Set up")
}
static func viewModel(keyBackupState: SecureBackupKeyBackupState) -> SecureBackupKeyBackupScreenViewModelType {
let backupController = SecureBackupControllerMock()
backupController.underlyingKeyBackupState = CurrentValueSubject<SecureBackupKeyBackupState, Never>(keyBackupState).asCurrentValuePublisher()
return SecureBackupKeyBackupScreenViewModel(secureBackupController: backupController,
userIndicatorController: nil)
}
}