feat: decline and block screen

fix

improvement

fix conflict

fix tests

tests
This commit is contained in:
Mauro Romito
2025-04-03 17:36:34 +02:00
committed by Mauro
parent 2ee5c1b403
commit d92fecdc7e
32 changed files with 597 additions and 13 deletions
@@ -0,0 +1,60 @@
//
// 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.
//
// periphery:ignore:all - this is just a reportInvite remove this comment once generating the final file
import Combine
import SwiftUI
struct DeclineAndBlockScreenCoordinatorParameters {
let userID: String
let roomID: String
let clientProxy: ClientProxyProtocol
let userIndicatorController: UserIndicatorControllerProtocol
}
enum DeclineAndBlockScreenCoordinatorAction {
case dismiss(hasDeclined: Bool)
}
final class DeclineAndBlockScreenCoordinator: CoordinatorProtocol {
private let parameters: DeclineAndBlockScreenCoordinatorParameters
private let viewModel: DeclineAndBlockScreenViewModelProtocol
private var cancellables = Set<AnyCancellable>()
private let actionsSubject: PassthroughSubject<DeclineAndBlockScreenCoordinatorAction, Never> = .init()
var actionsPublisher: AnyPublisher<DeclineAndBlockScreenCoordinatorAction, Never> {
actionsSubject.eraseToAnyPublisher()
}
init(parameters: DeclineAndBlockScreenCoordinatorParameters) {
self.parameters = parameters
viewModel = DeclineAndBlockScreenViewModel(userID: parameters.userID,
roomID: parameters.roomID,
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 .dismiss(let hasDeclined):
actionsSubject.send(.dismiss(hasDeclined: hasDeclined))
}
}
.store(in: &cancellables)
}
func toPresentable() -> AnyView {
AnyView(DeclineAndBlockScreen(context: viewModel.context))
}
}
@@ -0,0 +1,31 @@
//
// 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 DeclineAndBlockScreenViewModelAction {
case dismiss(hasDeclined: Bool)
}
struct DeclineAndBlockScreenViewState: BindableState {
var bindings = DeclineAndBlockScreenViewStateBindings()
var isDeclineDisabled: Bool {
!bindings.shouldBlockUser && !bindings.shouldReport
}
}
struct DeclineAndBlockScreenViewStateBindings {
var shouldBlockUser = true
var shouldReport = false
var reportReason = ""
}
enum DeclineAndBlockScreenViewAction {
case decline
case dismiss
}
@@ -0,0 +1,99 @@
//
// 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 DeclineAndBlockScreenViewModelType = StateStoreViewModel<DeclineAndBlockScreenViewState, DeclineAndBlockScreenViewAction>
class DeclineAndBlockScreenViewModel: DeclineAndBlockScreenViewModelType, DeclineAndBlockScreenViewModelProtocol {
let userID: String
let roomID: String
let clientProxy: ClientProxyProtocol
let userIndicatorController: UserIndicatorControllerProtocol
private let actionsSubject: PassthroughSubject<DeclineAndBlockScreenViewModelAction, Never> = .init()
var actionsPublisher: AnyPublisher<DeclineAndBlockScreenViewModelAction, Never> {
actionsSubject.eraseToAnyPublisher()
}
init(userID: String,
roomID: String,
clientProxy: ClientProxyProtocol,
userIndicatorController: UserIndicatorControllerProtocol) {
self.userID = userID
self.roomID = roomID
self.clientProxy = clientProxy
self.userIndicatorController = userIndicatorController
super.init(initialViewState: DeclineAndBlockScreenViewState())
}
// MARK: - Public
override func process(viewAction: DeclineAndBlockScreenViewAction) {
MXLog.info("View model: received view action: \(viewAction)")
switch viewAction {
case .dismiss:
actionsSubject.send(.dismiss(hasDeclined: false))
case .decline:
Task { await decline() }
}
}
private func decline() async {
showLoadingIndicator()
guard case let .invited(roomProxy) = await clientProxy.roomForIdentifier(roomID) else {
MXLog.error("DeclineAndBlockScreenViewModel: Unable to find an invited room for identifier \(roomID)")
hideLoadingIndicator()
showError()
return
}
let result = await roomProxy.rejectInvitation()
hideLoadingIndicator()
// TODO: Check with design how to handle the error cases and what message to present for success and error
switch result {
case .success:
if state.bindings.shouldReport {
Task {
await clientProxy.reportRoomForIdentifier(roomID, reason: state.bindings.reportReason.isBlank ? nil : state.bindings.reportReason)
}
}
if state.bindings.shouldBlockUser {
Task {
await clientProxy.ignoreUser(userID)
}
}
actionsSubject.send(.dismiss(hasDeclined: true))
case .failure:
userIndicatorController.submitIndicator(.init(title: L10n.errorUnknown))
}
}
private static let loadingIndicator = "\(DeclineAndBlockScreenViewModel.self).loadingIndicator"
private func showLoadingIndicator() {
userIndicatorController.submitIndicator(.init(id: Self.loadingIndicator,
type: .modal(progress: .indeterminate,
interactiveDismissDisabled: true,
allowsInteraction: false),
title: L10n.commonLoading,
persistent: true))
}
private func hideLoadingIndicator() {
userIndicatorController.retractIndicatorWithId(Self.loadingIndicator)
}
private func showError() {
userIndicatorController.submitIndicator(.init(title: L10n.errorUnknown))
}
}
@@ -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 DeclineAndBlockScreenViewModelProtocol {
var actionsPublisher: AnyPublisher<DeclineAndBlockScreenViewModelAction, Never> { get }
var context: DeclineAndBlockScreenViewModelType.Context { get }
}
@@ -0,0 +1,89 @@
//
// 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 DeclineAndBlockScreen: View {
@ObservedObject var context: DeclineAndBlockScreenViewModel.Context
var body: some View {
Form {
blockUserSection
reportSection
if context.shouldReport {
reportReasonSection
}
}
.compoundList()
.navigationTitle(L10n.screenDeclineAndBlockTitle)
.navigationBarTitleDisplayMode(.inline)
.toolbar { toolbar }
.animation(.elementDefault, value: context.shouldReport)
}
private var blockUserSection: some View {
Section {
ListRow(label: .plain(title: L10n.screenDeclineAndBlockBlockUserOptionTitle),
kind: .toggle($context.shouldBlockUser))
}
}
private var reportSection: some View {
Section {
ListRow(label: .plain(title: L10n.actionReportRoom),
kind: .toggle($context.shouldReport))
}
}
private var reportReasonSection: some View {
Section {
ListRow(label: .plain(title: ""),
kind: .textField(text: $context.reportReason, axis: .vertical))
.lineLimit(4, reservesSpace: true)
}
}
@ToolbarContentBuilder
private var toolbar: some ToolbarContent {
ToolbarItem(placement: .cancellationAction) {
Button(L10n.actionCancel) {
context.send(viewAction: .dismiss)
}
}
ToolbarItem(placement: .confirmationAction) {
Button(L10n.actionDecline) {
context.send(viewAction: .decline)
}
.disabled(context.viewState.isDeclineDisabled)
}
}
}
// MARK: - Previews
struct DeclineAndBlockScreen_Previews: PreviewProvider, TestablePreview {
static let viewModel = DeclineAndBlockScreenViewModel(userID: "@alice:matrix.org",
roomID: "!room:matrix.org",
clientProxy: ClientProxyMock(.init()),
userIndicatorController: UserIndicatorControllerMock())
static var previews: some View {
NavigationStack {
DeclineAndBlockScreen(context: viewModel.context)
}
.previewDisplayName("Default")
NavigationStack {
DeclineAndBlockScreen(context: viewModel.context)
.onAppear {
viewModel.context.shouldReport = true
}
}
.previewDisplayName("Report room selected")
}
}