mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-04 00:49:44 +00:00
Add RoomChangePermissionsScreen (#2513)
This commit is contained in:
@@ -162,6 +162,7 @@
|
||||
"common_room" = "Room";
|
||||
"common_room_name" = "Room name";
|
||||
"common_room_name_placeholder" = "e.g. your project name";
|
||||
"common_saving" = "Saving";
|
||||
"common_screen_lock" = "Screen lock";
|
||||
"common_search_for_someone" = "Search for someone";
|
||||
"common_search_results" = "Search results";
|
||||
|
||||
@@ -378,6 +378,8 @@ internal enum L10n {
|
||||
internal static var commonRoomName: String { return L10n.tr("Localizable", "common_room_name") }
|
||||
/// e.g. your project name
|
||||
internal static var commonRoomNamePlaceholder: String { return L10n.tr("Localizable", "common_room_name_placeholder") }
|
||||
/// Saving
|
||||
internal static var commonSaving: String { return L10n.tr("Localizable", "common_saving") }
|
||||
/// Screen lock
|
||||
internal static var commonScreenLock: String { return L10n.tr("Localizable", "common_screen_lock") }
|
||||
/// Search for someone
|
||||
|
||||
@@ -2723,69 +2723,6 @@ class RoomProxyMock: RoomProxyProtocol {
|
||||
return uploadAvatarMediaReturnValue
|
||||
}
|
||||
}
|
||||
//MARK: - canUserRedactOther
|
||||
|
||||
var canUserRedactOtherUserIDCallsCount = 0
|
||||
var canUserRedactOtherUserIDCalled: Bool {
|
||||
return canUserRedactOtherUserIDCallsCount > 0
|
||||
}
|
||||
var canUserRedactOtherUserIDReceivedUserID: String?
|
||||
var canUserRedactOtherUserIDReceivedInvocations: [String] = []
|
||||
var canUserRedactOtherUserIDReturnValue: Result<Bool, RoomProxyError>!
|
||||
var canUserRedactOtherUserIDClosure: ((String) async -> Result<Bool, RoomProxyError>)?
|
||||
|
||||
func canUserRedactOther(userID: String) async -> Result<Bool, RoomProxyError> {
|
||||
canUserRedactOtherUserIDCallsCount += 1
|
||||
canUserRedactOtherUserIDReceivedUserID = userID
|
||||
canUserRedactOtherUserIDReceivedInvocations.append(userID)
|
||||
if let canUserRedactOtherUserIDClosure = canUserRedactOtherUserIDClosure {
|
||||
return await canUserRedactOtherUserIDClosure(userID)
|
||||
} else {
|
||||
return canUserRedactOtherUserIDReturnValue
|
||||
}
|
||||
}
|
||||
//MARK: - canUserRedactOwn
|
||||
|
||||
var canUserRedactOwnUserIDCallsCount = 0
|
||||
var canUserRedactOwnUserIDCalled: Bool {
|
||||
return canUserRedactOwnUserIDCallsCount > 0
|
||||
}
|
||||
var canUserRedactOwnUserIDReceivedUserID: String?
|
||||
var canUserRedactOwnUserIDReceivedInvocations: [String] = []
|
||||
var canUserRedactOwnUserIDReturnValue: Result<Bool, RoomProxyError>!
|
||||
var canUserRedactOwnUserIDClosure: ((String) async -> Result<Bool, RoomProxyError>)?
|
||||
|
||||
func canUserRedactOwn(userID: String) async -> Result<Bool, RoomProxyError> {
|
||||
canUserRedactOwnUserIDCallsCount += 1
|
||||
canUserRedactOwnUserIDReceivedUserID = userID
|
||||
canUserRedactOwnUserIDReceivedInvocations.append(userID)
|
||||
if let canUserRedactOwnUserIDClosure = canUserRedactOwnUserIDClosure {
|
||||
return await canUserRedactOwnUserIDClosure(userID)
|
||||
} else {
|
||||
return canUserRedactOwnUserIDReturnValue
|
||||
}
|
||||
}
|
||||
//MARK: - canUserTriggerRoomNotification
|
||||
|
||||
var canUserTriggerRoomNotificationUserIDCallsCount = 0
|
||||
var canUserTriggerRoomNotificationUserIDCalled: Bool {
|
||||
return canUserTriggerRoomNotificationUserIDCallsCount > 0
|
||||
}
|
||||
var canUserTriggerRoomNotificationUserIDReceivedUserID: String?
|
||||
var canUserTriggerRoomNotificationUserIDReceivedInvocations: [String] = []
|
||||
var canUserTriggerRoomNotificationUserIDReturnValue: Result<Bool, RoomProxyError>!
|
||||
var canUserTriggerRoomNotificationUserIDClosure: ((String) async -> Result<Bool, RoomProxyError>)?
|
||||
|
||||
func canUserTriggerRoomNotification(userID: String) async -> Result<Bool, RoomProxyError> {
|
||||
canUserTriggerRoomNotificationUserIDCallsCount += 1
|
||||
canUserTriggerRoomNotificationUserIDReceivedUserID = userID
|
||||
canUserTriggerRoomNotificationUserIDReceivedInvocations.append(userID)
|
||||
if let canUserTriggerRoomNotificationUserIDClosure = canUserTriggerRoomNotificationUserIDClosure {
|
||||
return await canUserTriggerRoomNotificationUserIDClosure(userID)
|
||||
} else {
|
||||
return canUserTriggerRoomNotificationUserIDReturnValue
|
||||
}
|
||||
}
|
||||
//MARK: - markAsRead
|
||||
|
||||
var markAsReadReceiptTypeCallsCount = 0
|
||||
@@ -2871,6 +2808,107 @@ class RoomProxyMock: RoomProxyProtocol {
|
||||
return flagAsFavouriteReturnValue
|
||||
}
|
||||
}
|
||||
//MARK: - currentPowerLevelChanges
|
||||
|
||||
var currentPowerLevelChangesCallsCount = 0
|
||||
var currentPowerLevelChangesCalled: Bool {
|
||||
return currentPowerLevelChangesCallsCount > 0
|
||||
}
|
||||
var currentPowerLevelChangesReturnValue: Result<RoomPowerLevelChanges, RoomProxyError>!
|
||||
var currentPowerLevelChangesClosure: (() async -> Result<RoomPowerLevelChanges, RoomProxyError>)?
|
||||
|
||||
func currentPowerLevelChanges() async -> Result<RoomPowerLevelChanges, RoomProxyError> {
|
||||
currentPowerLevelChangesCallsCount += 1
|
||||
if let currentPowerLevelChangesClosure = currentPowerLevelChangesClosure {
|
||||
return await currentPowerLevelChangesClosure()
|
||||
} else {
|
||||
return currentPowerLevelChangesReturnValue
|
||||
}
|
||||
}
|
||||
//MARK: - applyPowerLevelChanges
|
||||
|
||||
var applyPowerLevelChangesCallsCount = 0
|
||||
var applyPowerLevelChangesCalled: Bool {
|
||||
return applyPowerLevelChangesCallsCount > 0
|
||||
}
|
||||
var applyPowerLevelChangesReceivedChanges: RoomPowerLevelChanges?
|
||||
var applyPowerLevelChangesReceivedInvocations: [RoomPowerLevelChanges] = []
|
||||
var applyPowerLevelChangesReturnValue: Result<Void, RoomProxyError>!
|
||||
var applyPowerLevelChangesClosure: ((RoomPowerLevelChanges) async -> Result<Void, RoomProxyError>)?
|
||||
|
||||
func applyPowerLevelChanges(_ changes: RoomPowerLevelChanges) async -> Result<Void, RoomProxyError> {
|
||||
applyPowerLevelChangesCallsCount += 1
|
||||
applyPowerLevelChangesReceivedChanges = changes
|
||||
applyPowerLevelChangesReceivedInvocations.append(changes)
|
||||
if let applyPowerLevelChangesClosure = applyPowerLevelChangesClosure {
|
||||
return await applyPowerLevelChangesClosure(changes)
|
||||
} else {
|
||||
return applyPowerLevelChangesReturnValue
|
||||
}
|
||||
}
|
||||
//MARK: - canUserRedactOther
|
||||
|
||||
var canUserRedactOtherUserIDCallsCount = 0
|
||||
var canUserRedactOtherUserIDCalled: Bool {
|
||||
return canUserRedactOtherUserIDCallsCount > 0
|
||||
}
|
||||
var canUserRedactOtherUserIDReceivedUserID: String?
|
||||
var canUserRedactOtherUserIDReceivedInvocations: [String] = []
|
||||
var canUserRedactOtherUserIDReturnValue: Result<Bool, RoomProxyError>!
|
||||
var canUserRedactOtherUserIDClosure: ((String) async -> Result<Bool, RoomProxyError>)?
|
||||
|
||||
func canUserRedactOther(userID: String) async -> Result<Bool, RoomProxyError> {
|
||||
canUserRedactOtherUserIDCallsCount += 1
|
||||
canUserRedactOtherUserIDReceivedUserID = userID
|
||||
canUserRedactOtherUserIDReceivedInvocations.append(userID)
|
||||
if let canUserRedactOtherUserIDClosure = canUserRedactOtherUserIDClosure {
|
||||
return await canUserRedactOtherUserIDClosure(userID)
|
||||
} else {
|
||||
return canUserRedactOtherUserIDReturnValue
|
||||
}
|
||||
}
|
||||
//MARK: - canUserRedactOwn
|
||||
|
||||
var canUserRedactOwnUserIDCallsCount = 0
|
||||
var canUserRedactOwnUserIDCalled: Bool {
|
||||
return canUserRedactOwnUserIDCallsCount > 0
|
||||
}
|
||||
var canUserRedactOwnUserIDReceivedUserID: String?
|
||||
var canUserRedactOwnUserIDReceivedInvocations: [String] = []
|
||||
var canUserRedactOwnUserIDReturnValue: Result<Bool, RoomProxyError>!
|
||||
var canUserRedactOwnUserIDClosure: ((String) async -> Result<Bool, RoomProxyError>)?
|
||||
|
||||
func canUserRedactOwn(userID: String) async -> Result<Bool, RoomProxyError> {
|
||||
canUserRedactOwnUserIDCallsCount += 1
|
||||
canUserRedactOwnUserIDReceivedUserID = userID
|
||||
canUserRedactOwnUserIDReceivedInvocations.append(userID)
|
||||
if let canUserRedactOwnUserIDClosure = canUserRedactOwnUserIDClosure {
|
||||
return await canUserRedactOwnUserIDClosure(userID)
|
||||
} else {
|
||||
return canUserRedactOwnUserIDReturnValue
|
||||
}
|
||||
}
|
||||
//MARK: - canUserTriggerRoomNotification
|
||||
|
||||
var canUserTriggerRoomNotificationUserIDCallsCount = 0
|
||||
var canUserTriggerRoomNotificationUserIDCalled: Bool {
|
||||
return canUserTriggerRoomNotificationUserIDCallsCount > 0
|
||||
}
|
||||
var canUserTriggerRoomNotificationUserIDReceivedUserID: String?
|
||||
var canUserTriggerRoomNotificationUserIDReceivedInvocations: [String] = []
|
||||
var canUserTriggerRoomNotificationUserIDReturnValue: Result<Bool, RoomProxyError>!
|
||||
var canUserTriggerRoomNotificationUserIDClosure: ((String) async -> Result<Bool, RoomProxyError>)?
|
||||
|
||||
func canUserTriggerRoomNotification(userID: String) async -> Result<Bool, RoomProxyError> {
|
||||
canUserTriggerRoomNotificationUserIDCallsCount += 1
|
||||
canUserTriggerRoomNotificationUserIDReceivedUserID = userID
|
||||
canUserTriggerRoomNotificationUserIDReceivedInvocations.append(userID)
|
||||
if let canUserTriggerRoomNotificationUserIDClosure = canUserTriggerRoomNotificationUserIDClosure {
|
||||
return await canUserTriggerRoomNotificationUserIDClosure(userID)
|
||||
} else {
|
||||
return canUserTriggerRoomNotificationUserIDReturnValue
|
||||
}
|
||||
}
|
||||
//MARK: - kickUser
|
||||
|
||||
var kickUserCallsCount = 0
|
||||
|
||||
@@ -81,15 +81,18 @@ extension RoomProxyMock {
|
||||
setNameClosure = { _ in .success(()) }
|
||||
setTopicClosure = { _ in .success(()) }
|
||||
getMemberUserIDReturnValue = .success(configuration.memberForID)
|
||||
canUserRedactOtherUserIDReturnValue = .success(false)
|
||||
canUserTriggerRoomNotificationUserIDReturnValue = .success(configuration.canUserTriggerRoomNotification)
|
||||
canUserJoinCallUserIDReturnValue = .success(configuration.canUserJoinCall)
|
||||
|
||||
flagAsUnreadReturnValue = .success(())
|
||||
markAsReadReceiptTypeReturnValue = .success(())
|
||||
underlyingIsFavourite = false
|
||||
flagAsFavouriteReturnValue = .success(())
|
||||
|
||||
currentPowerLevelChangesReturnValue = .success(.init())
|
||||
applyPowerLevelChangesReturnValue = .success(())
|
||||
canUserRedactOtherUserIDReturnValue = .success(false)
|
||||
canUserTriggerRoomNotificationUserIDReturnValue = .success(configuration.canUserTriggerRoomNotification)
|
||||
canUserJoinCallUserIDReturnValue = .success(configuration.canUserJoinCall)
|
||||
|
||||
kickUserReturnValue = .success(())
|
||||
banUserReturnValue = .success(())
|
||||
unbanUserReturnValue = .success(())
|
||||
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
// periphery:ignore:all - this is just a roomChangePermissions remove this comment once generating the final file
|
||||
|
||||
import Combine
|
||||
import SwiftUI
|
||||
|
||||
struct RoomChangePermissionsScreenCoordinatorParameters {
|
||||
let permissions: RoomPermissions
|
||||
let permissionsGroup: RoomRolesAndPermissionsScreenPermissionsGroup
|
||||
let roomProxy: RoomProxyProtocol
|
||||
let userIndicatorController: UserIndicatorControllerProtocol
|
||||
}
|
||||
|
||||
enum RoomChangePermissionsScreenCoordinatorAction {
|
||||
case cancel
|
||||
}
|
||||
|
||||
final class RoomChangePermissionsScreenCoordinator: CoordinatorProtocol {
|
||||
private let parameters: RoomChangePermissionsScreenCoordinatorParameters
|
||||
private var viewModel: RoomChangePermissionsScreenViewModelProtocol
|
||||
private let actionsSubject: PassthroughSubject<RoomChangePermissionsScreenCoordinatorAction, Never> = .init()
|
||||
private var cancellables = Set<AnyCancellable>()
|
||||
|
||||
var actions: AnyPublisher<RoomChangePermissionsScreenCoordinatorAction, Never> {
|
||||
actionsSubject.eraseToAnyPublisher()
|
||||
}
|
||||
|
||||
init(parameters: RoomChangePermissionsScreenCoordinatorParameters) {
|
||||
self.parameters = parameters
|
||||
|
||||
viewModel = RoomChangePermissionsScreenViewModel(currentPermissions: parameters.permissions,
|
||||
group: parameters.permissionsGroup,
|
||||
roomProxy: parameters.roomProxy,
|
||||
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 .cancel:
|
||||
actionsSubject.send(.cancel)
|
||||
}
|
||||
}
|
||||
.store(in: &cancellables)
|
||||
}
|
||||
|
||||
func toPresentable() -> AnyView {
|
||||
AnyView(RoomChangePermissionsScreen(context: viewModel.context))
|
||||
}
|
||||
}
|
||||
+105
@@ -0,0 +1,105 @@
|
||||
//
|
||||
// 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
|
||||
import MatrixRustSDK
|
||||
|
||||
enum RoomChangePermissionsScreenViewModelAction {
|
||||
case cancel
|
||||
}
|
||||
|
||||
struct RoomChangePermissionsScreenViewState: BindableState {
|
||||
/// The screen's title.
|
||||
let title: String
|
||||
/// The current permissions that are set on the room.
|
||||
var currentPermissions: RoomPermissions
|
||||
|
||||
var bindings: RoomChangePermissionsScreenViewStateBindings
|
||||
|
||||
/// Whether or not there are and changes to be saved.
|
||||
var hasChanges: Bool {
|
||||
bindings.settings.contains { currentPermissions[keyPath: $0.keyPath] ?? RoomPermissions.defaultValue(for: $0.keyPath) != $0.value }
|
||||
}
|
||||
}
|
||||
|
||||
struct RoomChangePermissionsScreenViewStateBindings: BindableState {
|
||||
/// All of the settings shown for this screen.
|
||||
var settings: [RoomPermissionsSetting]
|
||||
/// Information about the currently displayed alert.
|
||||
var alertInfo: AlertInfo<RoomChangePermissionsScreenAlertType>?
|
||||
}
|
||||
|
||||
enum RoomChangePermissionsScreenAlertType {
|
||||
/// The generic error message.
|
||||
case generic
|
||||
}
|
||||
|
||||
enum RoomChangePermissionsScreenViewAction {
|
||||
/// Save the permissions.
|
||||
case save
|
||||
}
|
||||
|
||||
extension RoomChangePermissionsScreenViewState {
|
||||
/// Creates a view state for a particular group of permissions.
|
||||
/// - Parameters:
|
||||
/// - currentPermissions: The current permissions for the room.
|
||||
/// - group: The group of permissions that should be shown in the screen.
|
||||
init(currentPermissions: RoomPermissions, group: RoomRolesAndPermissionsScreenPermissionsGroup) {
|
||||
switch group {
|
||||
case .roomDetails:
|
||||
let settings = [
|
||||
RoomPermissionsSetting(keyPath: \.roomName,
|
||||
value: currentPermissions.roomName ?? RoomPermissions.defaultValue(for: \.roomName),
|
||||
title: L10n.screenRoomChangePermissionsRoomName),
|
||||
RoomPermissionsSetting(keyPath: \.roomAvatar,
|
||||
value: currentPermissions.roomAvatar ?? RoomPermissions.defaultValue(for: \.roomAvatar),
|
||||
title: L10n.screenRoomChangePermissionsRoomAvatar),
|
||||
RoomPermissionsSetting(keyPath: \.roomTopic,
|
||||
value: currentPermissions.roomTopic ?? RoomPermissions.defaultValue(for: \.roomTopic),
|
||||
title: L10n.screenRoomChangePermissionsRoomTopic)
|
||||
]
|
||||
|
||||
self.init(title: L10n.screenRoomChangePermissionsRoomDetails, currentPermissions: currentPermissions, bindings: .init(settings: settings))
|
||||
|
||||
case .messagesAndContent:
|
||||
let settings = [
|
||||
RoomPermissionsSetting(keyPath: \.eventsDefault,
|
||||
value: currentPermissions.eventsDefault ?? RoomPermissions.defaultValue(for: \.eventsDefault),
|
||||
title: L10n.screenRoomChangePermissionsSendMessages),
|
||||
RoomPermissionsSetting(keyPath: \.redact,
|
||||
value: currentPermissions.redact ?? RoomPermissions.defaultValue(for: \.redact),
|
||||
title: L10n.screenRoomChangePermissionsDeleteMessages)
|
||||
]
|
||||
|
||||
self.init(title: L10n.screenRoomChangePermissionsMessagesAndContent, currentPermissions: currentPermissions, bindings: .init(settings: settings))
|
||||
|
||||
case .memberModeration:
|
||||
let settings = [
|
||||
RoomPermissionsSetting(keyPath: \.invite,
|
||||
value: currentPermissions.invite ?? RoomPermissions.defaultValue(for: \.invite),
|
||||
title: L10n.screenRoomChangePermissionsInvitePeople),
|
||||
RoomPermissionsSetting(keyPath: \.kick,
|
||||
value: currentPermissions.kick ?? RoomPermissions.defaultValue(for: \.kick),
|
||||
title: L10n.screenRoomChangePermissionsRemovePeople),
|
||||
RoomPermissionsSetting(keyPath: \.ban,
|
||||
value: currentPermissions.ban ?? RoomPermissions.defaultValue(for: \.ban),
|
||||
title: L10n.screenRoomChangePermissionsBanPeople)
|
||||
]
|
||||
|
||||
self.init(title: L10n.screenRoomChangePermissionsMemberModeration, currentPermissions: currentPermissions, bindings: .init(settings: settings))
|
||||
}
|
||||
}
|
||||
}
|
||||
+101
@@ -0,0 +1,101 @@
|
||||
//
|
||||
// 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 RoomChangePermissionsScreenViewModelType = StateStoreViewModel<RoomChangePermissionsScreenViewState, RoomChangePermissionsScreenViewAction>
|
||||
|
||||
class RoomChangePermissionsScreenViewModel: RoomChangePermissionsScreenViewModelType, RoomChangePermissionsScreenViewModelProtocol {
|
||||
let roomProxy: RoomProxyProtocol
|
||||
let userIndicatorController: UserIndicatorControllerProtocol
|
||||
private var actionsSubject: PassthroughSubject<RoomChangePermissionsScreenViewModelAction, Never> = .init()
|
||||
|
||||
var actions: AnyPublisher<RoomChangePermissionsScreenViewModelAction, Never> {
|
||||
actionsSubject.eraseToAnyPublisher()
|
||||
}
|
||||
|
||||
init(currentPermissions: RoomPermissions,
|
||||
group: RoomRolesAndPermissionsScreenPermissionsGroup,
|
||||
roomProxy: RoomProxyProtocol,
|
||||
userIndicatorController: UserIndicatorControllerProtocol) {
|
||||
self.roomProxy = roomProxy
|
||||
self.userIndicatorController = userIndicatorController
|
||||
super.init(initialViewState: .init(currentPermissions: currentPermissions, group: group))
|
||||
}
|
||||
|
||||
// MARK: - Public
|
||||
|
||||
override func process(viewAction: RoomChangePermissionsScreenViewAction) {
|
||||
MXLog.info("View model: received view action: \(viewAction)")
|
||||
|
||||
switch viewAction {
|
||||
case .save:
|
||||
Task { await save() }
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Private
|
||||
|
||||
private func save() async {
|
||||
guard state.hasChanges else {
|
||||
MXLog.warning("Nothing to save.")
|
||||
return
|
||||
}
|
||||
|
||||
showLoadingIndicator()
|
||||
|
||||
defer {
|
||||
hideLoadingIndicator()
|
||||
}
|
||||
|
||||
var updatedPermissions = RoomPermissions()
|
||||
for setting in state.bindings.settings {
|
||||
updatedPermissions[keyPath: setting.keyPath] = setting.value
|
||||
}
|
||||
|
||||
switch await roomProxy.applyPowerLevelChanges(updatedPermissions.makePowerLevelChanges()) {
|
||||
case .success:
|
||||
MXLog.info("Success")
|
||||
case .failure:
|
||||
context.alertInfo = AlertInfo(id: .generic)
|
||||
return
|
||||
}
|
||||
|
||||
switch await roomProxy.currentPowerLevelChanges() {
|
||||
case .success(let powerLevelChanges):
|
||||
state.currentPermissions = .init(powerLevelChanges: powerLevelChanges)
|
||||
case .failure:
|
||||
context.alertInfo = AlertInfo(id: .generic)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Loading indicator
|
||||
|
||||
private static let indicatorID = "SavingRoomPermissions"
|
||||
|
||||
private func showLoadingIndicator() {
|
||||
userIndicatorController.submitIndicator(UserIndicator(id: Self.indicatorID,
|
||||
type: .modal(progress: .indeterminate, interactiveDismissDisabled: true, allowsInteraction: false),
|
||||
title: L10n.commonSaving,
|
||||
persistent: true))
|
||||
}
|
||||
|
||||
private func hideLoadingIndicator() {
|
||||
userIndicatorController.retractIndicatorWithId(Self.indicatorID)
|
||||
}
|
||||
}
|
||||
+23
@@ -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 RoomChangePermissionsScreenViewModelProtocol {
|
||||
var actions: AnyPublisher<RoomChangePermissionsScreenViewModelAction, Never> { get }
|
||||
var context: RoomChangePermissionsScreenViewModelType.Context { get }
|
||||
}
|
||||
+83
@@ -0,0 +1,83 @@
|
||||
//
|
||||
// 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 Compound
|
||||
import SwiftUI
|
||||
|
||||
struct RoomChangePermissionsScreen: View {
|
||||
@ObservedObject var context: RoomChangePermissionsScreenViewModel.Context
|
||||
|
||||
var body: some View {
|
||||
Form {
|
||||
ForEach($context.settings) { $setting in
|
||||
Section {
|
||||
ListRow(label: .plain(title: setting.title),
|
||||
kind: .inlinePicker(selection: $setting.value, items: setting.allValues))
|
||||
} header: {
|
||||
Text(setting.title)
|
||||
.compoundListSectionHeader()
|
||||
}
|
||||
}
|
||||
}
|
||||
.compoundList()
|
||||
.navigationTitle(context.viewState.title)
|
||||
.navigationBarTitleDisplayMode(.inline)
|
||||
.toolbar { toolbar }
|
||||
.alert(item: $context.alertInfo)
|
||||
}
|
||||
|
||||
@ToolbarContentBuilder
|
||||
var toolbar: some ToolbarContent {
|
||||
ToolbarItem(placement: .confirmationAction) {
|
||||
Button(L10n.actionSave) {
|
||||
context.send(viewAction: .save)
|
||||
}
|
||||
.disabled(!context.viewState.hasChanges)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Previews
|
||||
|
||||
struct RoomChangePermissionsScreen_Previews: PreviewProvider, TestablePreview {
|
||||
static let detailsViewModel = makeViewModel(group: .roomDetails)
|
||||
static let messagesViewModel = makeViewModel(group: .messagesAndContent)
|
||||
static let membersViewModel = makeViewModel(group: .memberModeration)
|
||||
|
||||
static var previews: some View {
|
||||
NavigationStack {
|
||||
RoomChangePermissionsScreen(context: detailsViewModel.context)
|
||||
}
|
||||
.previewDisplayName("Room details")
|
||||
|
||||
NavigationStack {
|
||||
RoomChangePermissionsScreen(context: messagesViewModel.context)
|
||||
}
|
||||
.previewDisplayName("Messages and Content")
|
||||
|
||||
NavigationStack {
|
||||
RoomChangePermissionsScreen(context: membersViewModel.context)
|
||||
}
|
||||
.previewDisplayName("Member moderation")
|
||||
}
|
||||
|
||||
static func makeViewModel(group: RoomRolesAndPermissionsScreenPermissionsGroup) -> RoomChangePermissionsScreenViewModel {
|
||||
RoomChangePermissionsScreenViewModel(currentPermissions: .default,
|
||||
group: group,
|
||||
roomProxy: RoomProxyMock(with: .init()),
|
||||
userIndicatorController: UserIndicatorControllerMock())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
//
|
||||
// Copyright 2024 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
|
||||
import MatrixRustSDK
|
||||
|
||||
struct RoomPermissionsSetting: Identifiable {
|
||||
var id: KeyPath<RoomPermissions, RoomMemberDetails.Role?> { keyPath }
|
||||
|
||||
let keyPath: WritableKeyPath<RoomPermissions, RoomMemberDetails.Role?>
|
||||
var value: RoomMemberDetails.Role
|
||||
let title: String
|
||||
|
||||
var allValues: [(title: String, tag: RoomMemberDetails.Role)] {
|
||||
[
|
||||
(title: L10n.screenRoomChangePermissionsAdministrators, tag: .administrator),
|
||||
(title: L10n.screenRoomChangePermissionsModerators, tag: .moderator),
|
||||
(title: L10n.screenRoomChangePermissionsEveryone, tag: .user)
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
struct RoomPermissions {
|
||||
/// The level required to ban a user.
|
||||
var ban: RoomMemberDetails.Role?
|
||||
/// The level required to invite a user.
|
||||
var invite: RoomMemberDetails.Role?
|
||||
/// The level required to kick a user.
|
||||
var kick: RoomMemberDetails.Role?
|
||||
/// The level required to redact an event.
|
||||
var redact: RoomMemberDetails.Role?
|
||||
/// The default level required to send message events.
|
||||
var eventsDefault: RoomMemberDetails.Role?
|
||||
/// The default level required to send state events.
|
||||
var stateDefault: RoomMemberDetails.Role?
|
||||
/// The default power level for every user in the room.
|
||||
var usersDefault: RoomMemberDetails.Role?
|
||||
/// The level required to change the room's name.
|
||||
var roomName: RoomMemberDetails.Role?
|
||||
/// The level required to change the room's avatar.
|
||||
var roomAvatar: RoomMemberDetails.Role?
|
||||
/// The level required to change the room's topic.
|
||||
var roomTopic: RoomMemberDetails.Role?
|
||||
}
|
||||
|
||||
extension RoomPermissions {
|
||||
/// Returns the default value for a particular permission.
|
||||
static func defaultValue(for keyPath: KeyPath<RoomPermissions, RoomMemberDetails.Role?>) -> RoomMemberDetails.Role {
|
||||
switch keyPath {
|
||||
case \.ban: .moderator
|
||||
case \.invite: .user
|
||||
case \.kick: .moderator
|
||||
case \.redact: .moderator
|
||||
case \.eventsDefault: .user
|
||||
case \.stateDefault: .moderator
|
||||
case \.usersDefault: .user
|
||||
case \.roomName: .moderator
|
||||
case \.roomAvatar: .moderator
|
||||
case \.roomTopic: .moderator
|
||||
default: fatalError("Unexpected key path: \(keyPath)")
|
||||
}
|
||||
}
|
||||
|
||||
/// Constructs a set of permissions using the default values.
|
||||
static var `default`: RoomPermissions {
|
||||
RoomPermissions(ban: defaultValue(for: \.ban),
|
||||
invite: defaultValue(for: \.invite),
|
||||
kick: defaultValue(for: \.kick),
|
||||
redact: defaultValue(for: \.redact),
|
||||
eventsDefault: defaultValue(for: \.eventsDefault),
|
||||
stateDefault: defaultValue(for: \.stateDefault),
|
||||
usersDefault: defaultValue(for: \.usersDefault),
|
||||
roomName: defaultValue(for: \.roomName),
|
||||
roomAvatar: defaultValue(for: \.roomAvatar),
|
||||
roomTopic: defaultValue(for: \.roomTopic))
|
||||
}
|
||||
|
||||
init(powerLevelChanges: RoomPowerLevelChanges) {
|
||||
ban = powerLevelChanges.ban.map(RoomMemberDetails.Role.init)
|
||||
invite = powerLevelChanges.invite.map(RoomMemberDetails.Role.init)
|
||||
kick = powerLevelChanges.kick.map(RoomMemberDetails.Role.init)
|
||||
redact = powerLevelChanges.redact.map(RoomMemberDetails.Role.init)
|
||||
eventsDefault = powerLevelChanges.eventsDefault.map(RoomMemberDetails.Role.init)
|
||||
stateDefault = powerLevelChanges.stateDefault.map(RoomMemberDetails.Role.init)
|
||||
usersDefault = powerLevelChanges.usersDefault.map(RoomMemberDetails.Role.init)
|
||||
roomName = powerLevelChanges.roomName.map(RoomMemberDetails.Role.init)
|
||||
roomAvatar = powerLevelChanges.roomAvatar.map(RoomMemberDetails.Role.init)
|
||||
roomTopic = powerLevelChanges.roomTopic.map(RoomMemberDetails.Role.init)
|
||||
}
|
||||
|
||||
func makePowerLevelChanges() -> RoomPowerLevelChanges {
|
||||
RoomPowerLevelChanges(ban: ban?.rustPowerLevel,
|
||||
invite: invite?.rustPowerLevel,
|
||||
kick: kick?.rustPowerLevel,
|
||||
redact: redact?.rustPowerLevel,
|
||||
eventsDefault: eventsDefault?.rustPowerLevel,
|
||||
stateDefault: stateDefault?.rustPowerLevel,
|
||||
usersDefault: usersDefault?.rustPowerLevel,
|
||||
roomName: roomName?.rustPowerLevel,
|
||||
roomAvatar: roomAvatar?.rustPowerLevel,
|
||||
roomTopic: roomTopic?.rustPowerLevel)
|
||||
}
|
||||
}
|
||||
|
||||
extension RoomMemberDetails.Role {
|
||||
init(rustPowerLevel: Int64) {
|
||||
self.init(suggestedRoleForPowerLevel(powerLevel: rustPowerLevel))
|
||||
}
|
||||
|
||||
var rustRole: RoomMemberRole {
|
||||
switch self {
|
||||
case .administrator:
|
||||
.administrator
|
||||
case .moderator:
|
||||
.moderator
|
||||
case .user:
|
||||
.user
|
||||
}
|
||||
}
|
||||
|
||||
var rustPowerLevel: Int64 {
|
||||
suggestedPowerLevelForRole(role: rustRole)
|
||||
}
|
||||
}
|
||||
@@ -342,33 +342,6 @@ class RoomProxy: RoomProxyProtocol {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func canUserRedactOther(userID: String) async -> Result<Bool, RoomProxyError> {
|
||||
do {
|
||||
return try await .success(room.canUserRedactOther(userId: userID))
|
||||
} catch {
|
||||
MXLog.error("Failed checking if the user can redact others with error: \(error)")
|
||||
return .failure(.failedCheckingPermission)
|
||||
}
|
||||
}
|
||||
|
||||
func canUserRedactOwn(userID: String) async -> Result<Bool, RoomProxyError> {
|
||||
do {
|
||||
return try await .success(room.canUserRedactOwn(userId: userID))
|
||||
} catch {
|
||||
MXLog.error("Failed checking if the user can redact self with error: \(error)")
|
||||
return .failure(.failedCheckingPermission)
|
||||
}
|
||||
}
|
||||
|
||||
func canUserTriggerRoomNotification(userID: String) async -> Result<Bool, RoomProxyError> {
|
||||
do {
|
||||
return try await .success(room.canUserTriggerRoomNotification(userId: userID))
|
||||
} catch {
|
||||
MXLog.error("Failed checking if the user can trigger room notification with error: \(error)")
|
||||
return .failure(.failedCheckingPermission)
|
||||
}
|
||||
}
|
||||
|
||||
func markAsRead(receiptType: ReceiptType) async -> Result<Void, RoomProxyError> {
|
||||
do {
|
||||
@@ -416,6 +389,53 @@ class RoomProxy: RoomProxyProtocol {
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Power Levels
|
||||
|
||||
func currentPowerLevelChanges() async -> Result<RoomPowerLevelChanges, RoomProxyError> {
|
||||
do {
|
||||
return try await .success(room.buildPowerLevelChangesFromCurrent())
|
||||
} catch {
|
||||
MXLog.error("Failed building the current power level settings: \(error)")
|
||||
return .failure(.failedCheckingPermission)
|
||||
}
|
||||
}
|
||||
|
||||
func applyPowerLevelChanges(_ changes: RoomPowerLevelChanges) async -> Result<Void, RoomProxyError> {
|
||||
do {
|
||||
return try await .success(room.applyPowerLevelChanges(changes: changes))
|
||||
} catch {
|
||||
MXLog.error("Failed applying the power level changes: \(error)")
|
||||
return .failure(.failedSettingPermission)
|
||||
}
|
||||
}
|
||||
|
||||
func canUserRedactOther(userID: String) async -> Result<Bool, RoomProxyError> {
|
||||
do {
|
||||
return try await .success(room.canUserRedactOther(userId: userID))
|
||||
} catch {
|
||||
MXLog.error("Failed checking if the user can redact others with error: \(error)")
|
||||
return .failure(.failedCheckingPermission)
|
||||
}
|
||||
}
|
||||
|
||||
func canUserRedactOwn(userID: String) async -> Result<Bool, RoomProxyError> {
|
||||
do {
|
||||
return try await .success(room.canUserRedactOwn(userId: userID))
|
||||
} catch {
|
||||
MXLog.error("Failed checking if the user can redact self with error: \(error)")
|
||||
return .failure(.failedCheckingPermission)
|
||||
}
|
||||
}
|
||||
|
||||
func canUserTriggerRoomNotification(userID: String) async -> Result<Bool, RoomProxyError> {
|
||||
do {
|
||||
return try await .success(room.canUserTriggerRoomNotification(userId: userID))
|
||||
} catch {
|
||||
MXLog.error("Failed checking if the user can trigger room notification with error: \(error)")
|
||||
return .failure(.failedCheckingPermission)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Moderation
|
||||
|
||||
func kickUser(_ userID: String) async -> Result<Void, RoomProxyError> {
|
||||
|
||||
@@ -32,6 +32,7 @@ enum RoomProxyError: Error, Equatable {
|
||||
case failedRemovingAvatar
|
||||
case failedUploadingAvatar
|
||||
case failedCheckingPermission
|
||||
case failedSettingPermission
|
||||
case failedFlaggingAsUnread
|
||||
case failedMarkingAsRead
|
||||
case failedSendingTypingNotice
|
||||
@@ -103,12 +104,6 @@ protocol RoomProxyProtocol {
|
||||
func removeAvatar() async -> Result<Void, RoomProxyError>
|
||||
|
||||
func uploadAvatar(media: MediaInfo) async -> Result<Void, RoomProxyError>
|
||||
|
||||
func canUserRedactOther(userID: String) async -> Result<Bool, RoomProxyError>
|
||||
|
||||
func canUserRedactOwn(userID: String) async -> Result<Bool, RoomProxyError>
|
||||
|
||||
func canUserTriggerRoomNotification(userID: String) async -> Result<Bool, RoomProxyError>
|
||||
|
||||
func markAsRead(receiptType: ReceiptType) async -> Result<Void, RoomProxyError>
|
||||
|
||||
@@ -121,6 +116,14 @@ protocol RoomProxyProtocol {
|
||||
|
||||
func flagAsFavourite(_ isFavourite: Bool) async -> Result<Void, RoomProxyError>
|
||||
|
||||
// MARK: - Power Levels
|
||||
|
||||
func currentPowerLevelChanges() async -> Result<RoomPowerLevelChanges, RoomProxyError>
|
||||
func applyPowerLevelChanges(_ changes: RoomPowerLevelChanges) async -> Result<Void, RoomProxyError>
|
||||
func canUserRedactOther(userID: String) async -> Result<Bool, RoomProxyError>
|
||||
func canUserRedactOwn(userID: String) async -> Result<Bool, RoomProxyError>
|
||||
func canUserTriggerRoomNotification(userID: String) async -> Result<Bool, RoomProxyError>
|
||||
|
||||
// MARK: - Moderation
|
||||
|
||||
func kickUser(_ userID: String) async -> Result<Void, RoomProxyError>
|
||||
|
||||
Reference in New Issue
Block a user