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")
}
}
@@ -18,6 +18,7 @@ enum HomeScreenCoordinatorAction {
case presentRoom(roomIdentifier: String)
case presentRoomDetails(roomIdentifier: String)
case presentReportRoom(roomIdentifier: String)
case presentDeclineAndBlock(userID: String, roomID: String)
case roomLeft(roomIdentifier: String)
case presentSettingsScreen
case presentFeedbackScreen
@@ -81,6 +82,8 @@ final class HomeScreenCoordinator: CoordinatorProtocol {
actionsSubject.send(.logoutWithoutConfirmation)
case .logout:
actionsSubject.send(.logout)
case .presentDeclineAndBlock(let userID, let roomID):
actionsSubject.send(.presentDeclineAndBlock(userID: userID, roomID: roomID))
}
}
.store(in: &cancellables)
@@ -13,6 +13,7 @@ enum HomeScreenViewModelAction: Equatable {
case presentRoom(roomIdentifier: String)
case presentRoomDetails(roomIdentifier: String)
case presentReportRoom(roomIdentifier: String)
case presentDeclineAndBlock(userID: String, roomID: String)
case roomLeft(roomIdentifier: String)
case presentSecureBackupSettings
case presentRecoveryKeyScreen
@@ -426,11 +426,24 @@ class HomeScreenViewModel: HomeScreenViewModelType, HomeScreenViewModelProtocol
let title = room.isDirect ? L10n.screenInvitesDeclineDirectChatTitle : L10n.screenInvitesDeclineChatTitle
let message = room.isDirect ? L10n.screenInvitesDeclineDirectChatMessage(roomPlaceholder) : L10n.screenInvitesDeclineChatMessage(roomPlaceholder)
state.bindings.alertInfo = .init(id: UUID(),
title: title,
message: message,
primaryButton: .init(title: L10n.actionDecline, role: .destructive) { Task { await self.declineInvite(roomID: room.id) } },
secondaryButton: .init(title: L10n.actionCancel, role: .cancel, action: nil))
if appSettings.reportInviteEnabled, let userID = room.inviter?.id {
state.bindings.alertInfo = .init(id: UUID(),
title: title,
message: message,
primaryButton: .init(title: L10n.actionCancel, role: .cancel, action: nil),
secondaryButton: .init(title: L10n.actionDeclineAndBlock, role: .destructive) { [weak self] in self?.declineAndBlockInvite(userID: userID, roomID: roomID) },
verticalButtons: [.init(title: L10n.actionDecline) { [weak self] in Task { await self?.declineInvite(roomID: room.id) } }])
} else {
state.bindings.alertInfo = .init(id: UUID(),
title: title,
message: message,
primaryButton: .init(title: L10n.actionCancel, role: .cancel, action: nil),
secondaryButton: .init(title: L10n.actionDecline, role: .destructive) { [weak self] in Task { await self?.declineInvite(roomID: room.id) } })
}
}
private func declineAndBlockInvite(userID: String, roomID: String) {
actionsSubject.send(.presentDeclineAndBlock(userID: userID, roomID: roomID))
}
private func declineInvite(roomID: String) async {
@@ -20,6 +20,7 @@ struct JoinRoomScreenCoordinatorParameters {
enum JoinRoomScreenCoordinatorAction {
case joined
case cancelled
case presentDeclineAndBlock(userID: String)
}
final class JoinRoomScreenCoordinator: CoordinatorProtocol {
@@ -51,6 +52,8 @@ final class JoinRoomScreenCoordinator: CoordinatorProtocol {
actionsSubject.send(.joined)
case .dismiss:
actionsSubject.send(.cancelled)
case .presentDeclineAndBlock(let userID):
actionsSubject.send(.presentDeclineAndBlock(userID: userID))
}
}
.store(in: &cancellables)
@@ -7,9 +7,10 @@
import Foundation
enum JoinRoomScreenViewModelAction {
enum JoinRoomScreenViewModelAction: Equatable {
case joined
case dismiss
case presentDeclineAndBlock(userID: String)
}
enum JoinRoomScreenMode: Equatable {
@@ -323,11 +323,15 @@ class JoinRoomScreenViewModel: JoinRoomScreenViewModelType, JoinRoomScreenViewMo
}
private func showDeclineAndBlockConfirmationAlert(userID: String) {
state.bindings.alertInfo = .init(id: .declineInviteAndBlock,
title: L10n.screenJoinRoomDeclineAndBlockAlertTitle,
message: L10n.screenJoinRoomDeclineAndBlockAlertMessage(userID),
primaryButton: .init(title: L10n.actionCancel, role: .cancel, action: nil),
secondaryButton: .init(title: L10n.screenJoinRoomDeclineAndBlockAlertConfirmation, role: .destructive) { Task { await self.declineAndBlock(userID: userID) } })
if appSettings.reportInviteEnabled {
actionsSubject.send(.presentDeclineAndBlock(userID: userID))
} else {
state.bindings.alertInfo = .init(id: .declineInviteAndBlock,
title: L10n.screenJoinRoomDeclineAndBlockAlertTitle,
message: L10n.screenJoinRoomDeclineAndBlockAlertMessage(userID),
primaryButton: .init(title: L10n.actionCancel, role: .cancel, action: nil),
secondaryButton: .init(title: L10n.screenJoinRoomDeclineAndBlockAlertConfirmation, role: .destructive) { Task { await self.declineAndBlock(userID: userID) } })
}
}
private func declineAndBlock(userID: String) async {
@@ -45,6 +45,7 @@ protocol DeveloperOptionsProtocol: AnyObject {
var elementCallBaseURLOverride: URL? { get set }
var knockingEnabled: Bool { get set }
var reportRoomEnabled: Bool { get set }
var reportInviteEnabled: Bool { get set }
}
extension AppSettings: DeveloperOptionsProtocol { }
@@ -68,6 +68,10 @@ struct DeveloperOptionsScreen: View {
Text("Report rooms")
Text("Report API might not work properly")
}
Toggle(isOn: $context.reportInviteEnabled) {
Text("Report invites")
Text("Report API might not work properly")
}
}
Section {