mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-01 18:23:03 +00:00
22d14ff8e4
* create DependenciesProtocol and replace ServiceLocator registration with it replace mocking calls for ServiceLocator to Dependencies replace ServiceLocator with instance level calls replace ServiceLocator configs with TestDependencies config in Tests replace servicelocator usage in analytics service replace remaining ServiceLocator calls in unit tests that depended on the UnitTestAppCoordinator configuration with per suite dependencies instances fix swiftlint warning remove remaining references to ServiceLocator make dependencies properties consistent in tests delete Dependencies and DependenciesProtocol fix whitespace modifications updates from PR feedback a pass over converting AppSettings and AnalyticsService test instance properties to per test instantiation fix a missed PR feedback Sync up after rebase create DependenciesProtocol and replace ServiceLocator registration with it replace mocking calls for ServiceLocator to Dependencies replace ServiceLocator with instance level calls replace ServiceLocator configs with TestDependencies config in Tests replace servicelocator usage in analytics service replace remaining ServiceLocator calls in unit tests that depended on the UnitTestAppCoordinator configuration with per suite dependencies instances fix swiftlint warning remove remaining references to ServiceLocator make dependencies properties consistent in tests delete Dependencies and DependenciesProtocol * AppSettings is instance level mockable now * replace mocked and preview usages of reset + AppSettings global init with AppSettings(store: UserDefaultsMock()) * AppSettings.UserDefaultsKeys is now a RawRep struct, making it extensible * update tests with new AppSettings so it builds * update remaining AppSettings test/mock inits * AppSettings.UserDefaultsKey conformed to ExpressibleByStringLiteral * addressed issues found in first pass ai review * update AppSettings instantiations after rebase * fix accessibility tests coordinator after rebase * Rename to VolatileUserDefaults * AppSettings init uses mandatory parameter * Revert UserDefaultsKey to enum * Add TrackedUserDefaults to be able to reset the defaults object * Added volatile static func to AppSettings * Use .volatile() static func instead of full init * review fixups * Resolve build issues after rebase * updated enterprise module * remove the static reset settings call from the preview tests --------- Co-authored-by: Mauro Romito <mauro.romito@element.io>
412 lines
23 KiB
Swift
412 lines
23 KiB
Swift
//
|
|
// Copyright 2025 Element Creations Ltd.
|
|
// Copyright 2022-2025 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 SecurityAndPrivacyScreen: View {
|
|
@ObservedObject var context: SecurityAndPrivacyScreenViewModel.Context
|
|
|
|
var body: some View {
|
|
Form {
|
|
if context.viewState.canEditJoinRule {
|
|
roomAccessSection
|
|
}
|
|
|
|
if context.desiredSettings.accessType.isAddressRequired, context.viewState.canEditAddress {
|
|
visibilitySection
|
|
if let canonicalAlias = context.viewState.canonicalAlias {
|
|
addressSection(canonicalAlias: canonicalAlias)
|
|
} else {
|
|
addAddressSection
|
|
}
|
|
}
|
|
|
|
if !context.viewState.isSpace {
|
|
if context.viewState.canEnableEncryption {
|
|
encryptionSection
|
|
}
|
|
if context.viewState.canEditHistoryVisibility {
|
|
historySection
|
|
}
|
|
}
|
|
}
|
|
.compoundList()
|
|
.navigationBarTitleDisplayMode(.inline)
|
|
.navigationTitle(L10n.screenSecurityAndPrivacyTitle)
|
|
.navigationBarBackButtonHidden(!context.viewState.isSaveDisabled)
|
|
.toolbar { toolbar }
|
|
.alert(item: $context.alertInfo)
|
|
}
|
|
|
|
private var roomAccessSection: some View {
|
|
Section {
|
|
ListRow(label: .default(title: L10n.screenSecurityAndPrivacyRoomAccessAnyoneOptionTitle,
|
|
description: L10n.screenSecurityAndPrivacyRoomAccessAnyoneOptionDescription,
|
|
icon: \.public),
|
|
kind: .selection(isSelected: context.desiredSettings.accessType == .anyone) { context.desiredSettings.accessType = .anyone })
|
|
|
|
if context.viewState.isSpaceMembersOptionAvailable {
|
|
ListRow(label: .default(title: L10n.screenSecurityAndPrivacyRoomAccessSpaceMembersOptionTitle,
|
|
description: context.viewState.spaceMembersDescription,
|
|
icon: \.space),
|
|
kind: .selection(isSelected: context.desiredSettings.accessType.isSpaceMembers) {
|
|
context.send(viewAction: .selectedSpaceMembersAccess)
|
|
})
|
|
.disabled(!context.viewState.isSpaceMembersOptionSelectable)
|
|
}
|
|
|
|
if context.viewState.currentSettings.accessType == .askToJoin {
|
|
askToJoinOption
|
|
}
|
|
|
|
// Ask to join and askToJoin with space members can coexist if the initial setting is ask to join but the space members option is available otherwise it's either one or the other.
|
|
if context.viewState.isAskToJoinWithSpaceMembersOptionAvailable {
|
|
askToJoinWithSpaceMembersOption
|
|
} else if context.viewState.isKnockingEnabled, context.viewState.currentSettings.accessType != .askToJoin {
|
|
askToJoinOption
|
|
}
|
|
|
|
ListRow(label: .default(title: L10n.screenSecurityAndPrivacyRoomAccessInviteOnlyOptionTitle,
|
|
description: L10n.screenSecurityAndPrivacyRoomAccessInviteOnlyOptionDescription,
|
|
icon: \.lock),
|
|
kind: .selection(isSelected: context.desiredSettings.accessType == .inviteOnly) { context.desiredSettings.accessType = .inviteOnly })
|
|
} header: {
|
|
Text(L10n.screenSecurityAndPrivacyRoomAccessSectionHeader)
|
|
.compoundListSectionHeader()
|
|
} footer: {
|
|
if context.viewState.shouldShowAccessSectionFooter {
|
|
Text(context.viewState.strings.accessSectionFooterString)
|
|
.compoundListSectionFooter()
|
|
.environment(\.openURL, OpenURLAction { _ in
|
|
context.send(viewAction: .manageSpaces)
|
|
return .handled
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
private var askToJoinOption: some View {
|
|
ListRow(label: .default(title: L10n.screenSecurityAndPrivacyAskToJoinOptionTitle,
|
|
description: L10n.screenSecurityAndPrivacyAskToJoinOptionDescription,
|
|
icon: \.userAdd),
|
|
kind: .selection(isSelected: context.desiredSettings.accessType == .askToJoin) { context.desiredSettings.accessType = .askToJoin })
|
|
.disabled(!context.viewState.isKnockingEnabled)
|
|
}
|
|
|
|
private var askToJoinWithSpaceMembersOption: some View {
|
|
ListRow(label: .default(title: L10n.screenSecurityAndPrivacyAskToJoinOptionTitle,
|
|
description: context.viewState.askToJoinWithSpaceMembersDescription,
|
|
icon: \.userAdd),
|
|
kind: .selection(isSelected: context.desiredSettings.accessType.isAskToJoinWithSpaceMembers) { context.send(viewAction: .selectedAskToJoinWithSpaceMembersAccess) })
|
|
.disabled(!context.viewState.isAskToJoinWithSpaceMembersOptionSelectable)
|
|
}
|
|
|
|
@ViewBuilder
|
|
private var encryptionSection: some View {
|
|
let binding = Binding<Bool>(get: {
|
|
context.desiredSettings.isEncryptionEnabled
|
|
}, set: { newValue in
|
|
context.send(viewAction: .tryUpdatingEncryption(newValue))
|
|
})
|
|
|
|
Section {
|
|
ListRow(label: .plain(title: L10n.screenSecurityAndPrivacyEncryptionToggleTitle),
|
|
kind: .toggle(binding))
|
|
// We don't allow editing the encryption state if the current setting on the server is `enabled`
|
|
.disabled(context.viewState.currentSettings.isEncryptionEnabled)
|
|
} header: {
|
|
Text(L10n.screenSecurityAndPrivacyEncryptionSectionHeader)
|
|
.compoundListSectionHeader()
|
|
} footer: {
|
|
Text(L10n.screenSecurityAndPrivacyEncryptionSectionFooter)
|
|
.compoundListSectionFooter()
|
|
}
|
|
}
|
|
|
|
private var historySection: some View {
|
|
Section {
|
|
ForEach(context.viewState.availableVisibilityOptions, id: \.self) { option in
|
|
switch option {
|
|
case .invited:
|
|
ListRow(label: .plain(title: L10n.screenSecurityAndPrivacyRoomHistorySinceInviteOptionTitle),
|
|
kind: .selection(isSelected: context.desiredSettings.historyVisibility == .invited) { context.desiredSettings.historyVisibility = .invited })
|
|
case .shared:
|
|
ListRow(label: .plain(title: L10n.screenSecurityAndPrivacyRoomHistorySinceSelectingOptionTitle),
|
|
kind: .selection(isSelected: context.desiredSettings.historyVisibility == .shared) { context.desiredSettings.historyVisibility = .shared })
|
|
case .worldReadable:
|
|
ListRow(label: .plain(title: L10n.screenSecurityAndPrivacyRoomHistoryAnyoneOptionTitle),
|
|
kind: .selection(isSelected: context.desiredSettings.historyVisibility == .worldReadable) { context.desiredSettings.historyVisibility = .worldReadable })
|
|
}
|
|
}
|
|
} header: {
|
|
Text(L10n.screenSecurityAndPrivacyRoomHistorySectionHeader)
|
|
.compoundListSectionHeader()
|
|
} footer: {
|
|
Text(context.viewState.strings.historySectionFooterString)
|
|
.compoundListSectionFooter()
|
|
}
|
|
}
|
|
|
|
private var visibilitySection: some View {
|
|
Section {
|
|
EmptyView()
|
|
} header: {
|
|
Text(L10n.screenSecurityAndPrivacyRoomVisibilitySectionHeader)
|
|
.compoundListSectionHeader()
|
|
} footer: {
|
|
Text(L10n.screenSecurityAndPrivacyRoomVisibilitySectionFooter)
|
|
.compoundListSectionFooter()
|
|
}
|
|
}
|
|
|
|
private func addressSection(canonicalAlias: String) -> some View {
|
|
Section {
|
|
ListRow(label: .plain(title: canonicalAlias),
|
|
kind: .navigationLink { context.send(viewAction: .editAddress) })
|
|
roomDirectoryVisibilityRow
|
|
} header: {
|
|
Text(L10n.screenSecurityAndPrivacyRoomAddressSectionHeader)
|
|
.compoundListSectionHeader()
|
|
} footer: {
|
|
Text(L10n.screenSecurityAndPrivacyRoomAddressSectionFooter)
|
|
.compoundListSectionFooter()
|
|
}
|
|
}
|
|
|
|
private var addAddressSection: some View {
|
|
Section {
|
|
ListRow(kind: .custom {
|
|
Button {
|
|
context.send(viewAction: .editAddress)
|
|
} label: {
|
|
Text(L10n.screenSecurityAndPrivacyAddRoomAddressAction)
|
|
.foregroundColor(.compound.textActionAccent)
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
|
}
|
|
.padding(ListRowPadding.insets)
|
|
})
|
|
}
|
|
}
|
|
|
|
@ViewBuilder
|
|
private var roomDirectoryVisibilityRow: some View {
|
|
let binding = Binding<Bool>(get: {
|
|
context.desiredSettings.isVisibileInRoomDirectory ?? false
|
|
}, set: { newValue in
|
|
context.desiredSettings.isVisibileInRoomDirectory = newValue
|
|
})
|
|
ListRow(label: .plain(title: L10n.screenSecurityAndPrivacyRoomDirectoryVisibilityToggleTitle,
|
|
description: L10n.screenSecurityAndPrivacyRoomDirectoryVisibilityToggleDescription),
|
|
details: context.desiredSettings.isVisibileInRoomDirectory == nil ? .isWaiting(true) : nil,
|
|
kind: context.desiredSettings.isVisibileInRoomDirectory == nil ? .label : .toggle(binding))
|
|
}
|
|
|
|
@ToolbarContentBuilder
|
|
private var toolbar: some ToolbarContent {
|
|
ToolbarItem(placement: .cancellationAction) {
|
|
if !context.viewState.isSaveDisabled {
|
|
Button(L10n.actionCancel) {
|
|
context.send(viewAction: .cancel)
|
|
}
|
|
}
|
|
}
|
|
|
|
ToolbarItem(placement: .confirmationAction) {
|
|
Button(L10n.actionSave) {
|
|
context.send(viewAction: .save)
|
|
}
|
|
.disabled(context.viewState.isSaveDisabled)
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: - Previews
|
|
|
|
struct SecurityAndPrivacyScreen_Previews: PreviewProvider, TestablePreview {
|
|
static let inviteOnlyViewModel = SecurityAndPrivacyScreenViewModel(roomProxy: JoinedRoomProxyMock(.init(members: .allMembersAsCreator,
|
|
joinRule: .invite)),
|
|
clientProxy: ClientProxyMock(.init()),
|
|
userIndicatorController: UserIndicatorControllerMock(),
|
|
appSettings: .volatile())
|
|
|
|
static let publicViewModel = SecurityAndPrivacyScreenViewModel(roomProxy: JoinedRoomProxyMock(.init(isEncrypted: false,
|
|
canonicalAlias: "#room:matrix.org",
|
|
members: .allMembersAsCreator,
|
|
joinRule: .public,
|
|
isVisibleInPublicDirectory: true)),
|
|
clientProxy: ClientProxyMock(.init(userIDServerName: "matrix.org")),
|
|
userIndicatorController: UserIndicatorControllerMock(),
|
|
appSettings: .volatile())
|
|
|
|
static let publicNoAddressViewModel = SecurityAndPrivacyScreenViewModel(roomProxy: JoinedRoomProxyMock(.init(isEncrypted: false,
|
|
members: .allMembersAsCreator,
|
|
joinRule: .public)),
|
|
clientProxy: ClientProxyMock(.init(userIDServerName: "matrix.org")),
|
|
userIndicatorController: UserIndicatorControllerMock(),
|
|
appSettings: .volatile())
|
|
|
|
static let singleSpaceMembersViewModel = {
|
|
let appSettings = AppSettings.volatile()
|
|
|
|
let space = [SpaceServiceRoom].mockSingleRoom[0]
|
|
|
|
return SecurityAndPrivacyScreenViewModel(roomProxy: JoinedRoomProxyMock(.init(isEncrypted: false,
|
|
canonicalAlias: "#room:matrix.org",
|
|
members: .allMembersAsCreator,
|
|
joinRule: .restricted(rules: [.roomMembership(roomID: space.id)]),
|
|
isVisibleInPublicDirectory: true)),
|
|
clientProxy: ClientProxyMock(.init(userIDServerName: "matrix.org",
|
|
spaceServiceConfiguration: .init(joinedParentSpaces: [space]))),
|
|
userIndicatorController: UserIndicatorControllerMock(),
|
|
appSettings: appSettings)
|
|
}()
|
|
|
|
static let multipleSpacesMembersViewModel = {
|
|
let appSettings = AppSettings.volatile()
|
|
|
|
let spaces = [SpaceServiceRoom].mockJoinedSpaces
|
|
|
|
return SecurityAndPrivacyScreenViewModel(roomProxy: JoinedRoomProxyMock(.init(isEncrypted: false,
|
|
canonicalAlias: "#room:matrix.org",
|
|
members: .allMembersAsCreator,
|
|
joinRule: .restricted(rules: spaces.map { .roomMembership(roomID: $0.id) }),
|
|
isVisibleInPublicDirectory: true)),
|
|
clientProxy: ClientProxyMock(.init(userIDServerName: "matrix.org",
|
|
spaceServiceConfiguration: .init(joinedParentSpaces: spaces))),
|
|
userIndicatorController: UserIndicatorControllerMock(),
|
|
appSettings: appSettings)
|
|
}()
|
|
|
|
static let askToJoinViewModel = {
|
|
let appSettings = AppSettings.volatile()
|
|
appSettings.knockingEnabled = true
|
|
|
|
return SecurityAndPrivacyScreenViewModel(roomProxy: JoinedRoomProxyMock(.init(isEncrypted: false,
|
|
canonicalAlias: "#room:matrix.org",
|
|
members: .allMembersAsCreator,
|
|
joinRule: .knock,
|
|
isVisibleInPublicDirectory: true)),
|
|
clientProxy: ClientProxyMock(.init(userIDServerName: "matrix.org")),
|
|
userIndicatorController: UserIndicatorControllerMock(),
|
|
appSettings: appSettings)
|
|
}()
|
|
|
|
static let singleAskToJoinSpaceMembersViewModel = {
|
|
let appSettings = AppSettings.volatile()
|
|
appSettings.knockingEnabled = true
|
|
|
|
let space = [SpaceServiceRoom].mockSingleRoom[0]
|
|
|
|
return SecurityAndPrivacyScreenViewModel(roomProxy: JoinedRoomProxyMock(.init(isEncrypted: false,
|
|
canonicalAlias: "#room:matrix.org",
|
|
members: .allMembersAsCreator,
|
|
joinRule: .knockRestricted(rules: [.roomMembership(roomID: space.id)]),
|
|
isVisibleInPublicDirectory: true)),
|
|
clientProxy: ClientProxyMock(.init(userIDServerName: "matrix.org",
|
|
spaceServiceConfiguration: .init(joinedParentSpaces: [space]))),
|
|
userIndicatorController: UserIndicatorControllerMock(),
|
|
appSettings: appSettings)
|
|
}()
|
|
|
|
static let multipleAskToJoinSpacesMembersViewModel = {
|
|
let appSettings = AppSettings.volatile()
|
|
appSettings.knockingEnabled = true
|
|
|
|
let spaces = [SpaceServiceRoom].mockJoinedSpaces
|
|
|
|
return SecurityAndPrivacyScreenViewModel(roomProxy: JoinedRoomProxyMock(.init(isEncrypted: false,
|
|
canonicalAlias: "#room:matrix.org",
|
|
members: .allMembersAsCreator,
|
|
joinRule: .knockRestricted(rules: spaces.map { .roomMembership(roomID: $0.id) }),
|
|
isVisibleInPublicDirectory: true)),
|
|
clientProxy: ClientProxyMock(.init(userIDServerName: "matrix.org",
|
|
spaceServiceConfiguration: .init(joinedParentSpaces: spaces))),
|
|
userIndicatorController: UserIndicatorControllerMock(),
|
|
appSettings: appSettings)
|
|
}()
|
|
|
|
static let publicSpaceViewModel = SecurityAndPrivacyScreenViewModel(roomProxy: JoinedRoomProxyMock(.init(isSpace: true,
|
|
isEncrypted: false,
|
|
canonicalAlias: "#space:matrix.org",
|
|
members: .allMembersAsCreator,
|
|
joinRule: .public,
|
|
isVisibleInPublicDirectory: true)),
|
|
clientProxy: ClientProxyMock(.init(userIDServerName: "matrix.org")),
|
|
userIndicatorController: UserIndicatorControllerMock(),
|
|
appSettings: .volatile())
|
|
|
|
static var previews: some View {
|
|
ElementNavigationStack {
|
|
SecurityAndPrivacyScreen(context: inviteOnlyViewModel.context)
|
|
}
|
|
.previewDisplayName("Private invite only room")
|
|
|
|
ElementNavigationStack {
|
|
SecurityAndPrivacyScreen(context: publicViewModel.context)
|
|
}
|
|
.snapshotPreferences(expect: publicViewModel.context.$viewState.map { state in
|
|
state.currentSettings.isVisibileInRoomDirectory == true
|
|
})
|
|
.previewDisplayName("Public room")
|
|
|
|
ElementNavigationStack {
|
|
SecurityAndPrivacyScreen(context: publicNoAddressViewModel.context)
|
|
}
|
|
.previewDisplayName("Public room without address")
|
|
|
|
ElementNavigationStack {
|
|
SecurityAndPrivacyScreen(context: singleSpaceMembersViewModel.context)
|
|
}
|
|
.snapshotPreferences(expect: singleSpaceMembersViewModel.context.$viewState.map { state in
|
|
state.currentSettings.isVisibileInRoomDirectory == true
|
|
})
|
|
.previewDisplayName("Space members")
|
|
|
|
ElementNavigationStack {
|
|
SecurityAndPrivacyScreen(context: multipleSpacesMembersViewModel.context)
|
|
}
|
|
.snapshotPreferences(expect: multipleSpacesMembersViewModel.context.$viewState.map { state in
|
|
state.currentSettings.isVisibileInRoomDirectory == true
|
|
})
|
|
.previewDisplayName("Multiple Spaces members")
|
|
|
|
ElementNavigationStack {
|
|
SecurityAndPrivacyScreen(context: askToJoinViewModel.context)
|
|
}
|
|
.snapshotPreferences(expect: askToJoinViewModel.context.$viewState.map { state in
|
|
state.currentSettings.isVisibileInRoomDirectory == true
|
|
})
|
|
.previewDisplayName("Ask to join room")
|
|
|
|
ElementNavigationStack {
|
|
SecurityAndPrivacyScreen(context: singleAskToJoinSpaceMembersViewModel.context)
|
|
}
|
|
.snapshotPreferences(expect: singleAskToJoinSpaceMembersViewModel.context.$viewState.map { state in
|
|
state.currentSettings.isVisibileInRoomDirectory == true
|
|
})
|
|
.previewDisplayName("Ask to join with single space members room")
|
|
|
|
ElementNavigationStack {
|
|
SecurityAndPrivacyScreen(context: multipleAskToJoinSpacesMembersViewModel.context)
|
|
}
|
|
.snapshotPreferences(expect: multipleAskToJoinSpacesMembersViewModel.context.$viewState.map { state in
|
|
state.currentSettings.isVisibileInRoomDirectory == true
|
|
})
|
|
.previewDisplayName("Ask to join with multiple spaces members room")
|
|
|
|
ElementNavigationStack {
|
|
SecurityAndPrivacyScreen(context: publicSpaceViewModel.context)
|
|
}
|
|
.snapshotPreferences(expect: publicSpaceViewModel.context.$viewState.map { state in
|
|
state.currentSettings.isVisibileInRoomDirectory == true
|
|
})
|
|
.previewDisplayName("Public space")
|
|
}
|
|
}
|