Files
element-x-ios/ElementX/Sources/Application/Settings/AppSettings.swift
T
Mauro 426fdc70a1 Add a developer option and selection mode for timeline messages (#6102)
* Add a developer option and selection mode for timeline messages

First step of the multi-select messages feature (element-meta#3255, iOS
counterpart of element-x-android#6918). Everything is gated behind the new
`messageMultiSelectEnabled` developer option and nothing changes while it
is off.

- Adds a "Select" action to the timeline item menu for remote messages in
  the room, focused and thread timelines. Choosing it enters selection
  mode with that message selected.
- While selecting, every bubbled row shows a checkbox on its leading edge
  and tapping or long pressing a row toggles it. State events, redacted
  messages, live locations, unsupported events and local echoes cannot be
  selected and stay inert. Selected rows get the accent background.
- The navigation bar swaps to a Cancel button and an "N selected" title,
  the back button is hidden and the composer is removed until the
  selection ends (Cancel or deselecting the last item). This is the hook
  where the bulk action bar will land in the next PR.
- The selection is capped at 30 messages, showing a toast when the cap
  is hit, and is cleared if the developer option is switched off.
- The selection state lives in `TimelineViewModel` so room and thread
  timelines share the behaviour without extra plumbing.

Includes unit tests for the menu action gating and the selection state
transitions, plus RoomScreen and bubble styler previews for the new state.

* Harden the timeline selection mode after review

- Reconcile the selection with timeline updates: selected messages that
  become unselectable (e.g. redacted) are dropped, and the selection is
  cleared when the timeline is swapped for a focused or live one, so the
  count can never refer to items that aren't on screen.
- Keep the composer mounted but collapsed while selecting, instead of
  removing it from the hierarchy. This preserves the draft and composer
  mode and avoids the disappear side effects. The footer with the identity
  warnings stays visible.
- Give the long press modifier an `isEnabled` flag so the recogniser is
  disabled while selecting, rather than only skipping the action while
  still animating and buzzing.
- Gate the Mac context menu on the selection like the other gestures.
- VoiceOver: a selectable bubble becomes a button carrying the selected
  trait whose default action toggles it, non-selectable rows lose their
  message actions while selecting, and reactions, thread summaries and the
  checkbox are hidden from the accessibility tree in selection mode.

* Stop voice message recordings when entering selection mode

The composer is collapsed rather than removed while selecting, so an
in-progress recording would otherwise keep the microphone open out of
sight. Entering the selection now stops the recording, which moves the
composer to the preview state so the message is still there to play or
send once the selection ends. Playback of voice messages is unaffected.

* Use the translated strings for the message selection mode

The selection strings have been imported to Localazy, so the untranslated
placeholders are dropped in favour of `action_select`,
`screen_room_maximum_messages_selected` and the `screen_room_selection_count`
plural. Only these keys are taken from the download to keep the change
focused.

* Apply review suggestions to the message selection mode

Addresses the first review round on the selection mode:

- Undecryptable messages are no longer selectable, as none of the bulk
  actions can be taken on them from the single item menu either.
- Renames for clarity: the menu action is `selectMessages` (leaving room
  for a future `selectText`), the view action is `startSelection`, the
  state exposes `contains(_:)`, `limit` and `isAtLimit`, and the composer
  collapsing modifier is `collapsedInPlace(_:)`.
- `canSelectMessages` uses a switch expression, the selection state doc
  comment refers to a feature flag rather than Labs, the recording helper
  doc describes only what it does, and `toggleSelection()` moves below
  the subviews.

* Keep the accessibility labels of timeline items while selecting

The selection mode used to replace each item's accessibility body with a
plain combined element, dropping the sender name representation and the
gallery label. The per item labelling now always applies, and the selection
only changes the interaction on top of it: the message actions are offered
as before, or the item becomes a button carrying the selected trait whose
default action toggles it. Polls and galleries collapse into a single
element only while selecting, as their children are inert then.

* Keep the timeline types within the lint limits after rebasing

The redaction reason work on develop grew the menu action handler and the
timeline view model right up to SwiftLint's size limits, so the selection
additions tipped them over. The `selectMessages` case now shares the
`downloadMedia` break and the selection flag subscription lives in the
selection extension.

* Fix the snapshot regressions from the selection mode

- Drop `geometryGroup()` from the bubble row: it stopped the avatar
  placeholder initial from rendering, which showed up as a mismatch in
  every timeline item snapshot.
- Collapse the composer with opacity instead of clipping, as the clip also
  removed the composer background from the home indicator area while the
  composer was fully visible.
- Keep the timeline item menu preview on the default flag-off state so the
  existing menu snapshots stay valid.
- Record the new "Selecting" room screen and "Selection" bubble snapshots
  on the same iPhone SE simulator CI uses.
2026-09-09 13:36:57 +02:00

468 lines
20 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.
//
#if IS_MAIN_APP
import EmbeddedElementCall
#endif
import Combine
import Foundation
import Macros
import SwiftUI
/// Common settings between app and NSE
nonisolated protocol CommonSettingsProtocol: AnyObject, Sendable {
var lastNotificationBootTime: TimeInterval? { get set }
var selectedNotificationTone: NotificationTone? { get set }
var logLevel: LogLevel { get }
var traceLogPacks: Set<TraceLogPack> { get }
var bugReportRageshakeURL: RemotePreference<RageshakeConfiguration> { get }
var contentScannerURL: RemotePreference<URL?> { get }
var forceDisableE2EE: RemotePreference<Bool> { get }
var mapTilerConfiguration: RemotePreference<MapTilerConfiguration> { get }
var enableOnlySignedDeviceIsolationMode: Bool { get }
var threadsEnabled: Bool { get }
var hideQuietNotificationAlerts: Bool { get }
}
nonisolated enum AppBuildType {
case debug
case nightly
case release
static var current: AppBuildType {
#if DEBUG
return .debug
#else
if InfoPlistReader.main.isNightlyBuild {
.nightly
} else {
.release
}
#endif
}
}
/// Store Element specific app settings.
///
/// State is persisted in `UserDefaults`, which is thread-safe per Apple's documentation, hence `@unchecked`.
final nonisolated class AppSettings: @unchecked Sendable {
static let suiteName: String = InfoPlistReader.main.appGroupIdentifier
/// UserDefaults to be used on reads and writes.
private let store: UserDefaultsProtocol
static var appBuildType: AppBuildType {
AppBuildType.current
}
func resetAllSettings() {
MXLog.warning("Resetting the AppSettings.")
store.reset()
}
func resetSessionSpecificSettings() {
MXLog.warning("Resetting the user session specific AppSettings.")
resetHasRunIdentityConfirmationOnboarding()
}
// MARK: - Hooks
// swiftlint:disable:next function_parameter_count
func override(accountProviders: [String],
allowOtherAccountProviders: Bool,
hideBrandChrome: Bool,
pushGatewayBaseURL: URL,
oAuthRedirectURL: URL,
oAuthClientURIPath: String?,
websiteURL: URL,
logoURL: URL,
copyrightURL: URL,
acceptableUseURL: URL,
privacyURL: URL,
encryptionURL: URL,
deviceVerificationURL: URL,
chatBackupDetailsURL: URL,
identityPinningViolationDetailsURL: URL,
historySharingDetailsURL: URL,
elementWebHosts: [String],
accountProvisioningHost: String,
bugReportApplicationID: String,
analyticsTermsURL: URL?,
mapTilerConfiguration: MapTilerConfiguration) {
self.accountProviders = accountProviders
self.allowOtherAccountProviders = allowOtherAccountProviders
self.hideBrandChrome = hideBrandChrome
self.pushGatewayBaseURL = pushGatewayBaseURL
self.oAuthRedirectURL = oAuthRedirectURL
self.oAuthClientURIPath = oAuthClientURIPath
self.websiteURL = websiteURL
self.logoURL = logoURL
self.copyrightURL = copyrightURL
self.acceptableUseURL = acceptableUseURL
self.privacyURL = privacyURL
self.encryptionURL = encryptionURL
self.deviceVerificationURL = deviceVerificationURL
self.chatBackupDetailsURL = chatBackupDetailsURL
self.identityPinningViolationDetailsURL = identityPinningViolationDetailsURL
self.historySharingDetailsURL = historySharingDetailsURL
self.elementWebHosts = elementWebHosts
self.accountProvisioningHost = accountProvisioningHost
self.bugReportApplicationID = bugReportApplicationID
self.analyticsTermsURL = analyticsTermsURL
self.mapTilerConfiguration = RemotePreference(mapTilerConfiguration)
}
// MARK: - Application
/// The last known version of the app that was launched on this device, which is
/// used to detect when migrations should be run. When `nil` the app may have been
/// deleted between runs so should clear data in the shared container and keychain.
@UserPreference
var lastVersionLaunched: String?
/// The Set of room identifiers of invites that the user already saw in the invites list.
/// This Set is being used to implement badges for unread invites.
@UserPreference(defaultValue: Set<String>())
var seenInvites: Set<String>
/// Defaults to `true` for new users, and we use a migration to set it to `false` for existing users.
@UserPreference(defaultValue: true)
var hasSeenNewSoundBanner: Bool
/// The initial set of account providers shown to the user in the authentication flow.
///
/// Account provider is the friendly term for the server name. It should not contain an `https` prefix and should
/// match the last part of the user ID. For example `example.com` and not `https://matrix.example.com`.
private(set) var accountProviders = ["matrix.org"]
/// Whether or not the user is allowed to manually enter their own account provider or must select from one of `defaultAccountProviders`.
private(set) var allowOtherAccountProviders = true
/// Whether the components surrounding the app brand/logo should be hidden or not
private(set) var hideBrandChrome = false
/// The task identifier used for background app refresh. Also used in main target's the Info.plist
let backgroundAppRefreshTaskIdentifier = "io.element.elementx.background.refresh"
/// A URL where users can go read more about the app.
private(set) var websiteURL: URL = "https://element.io"
/// A URL that contains the app's logo that may be used when showing content in a web view.
private(set) var logoURL: URL = "https://element.io/mobile-icon.png"
/// A URL that contains that app's copyright notice.
private(set) var copyrightURL: URL = "https://element.io/copyright"
/// A URL that contains the app's Terms of use.
private(set) var acceptableUseURL: URL = "https://element.io/acceptable-use-policy-terms"
/// A URL that contains the app's Privacy Policy.
private(set) var privacyURL: URL = "https://element.io/privacy"
/// A URL where users can go read more about encryption in general.
private(set) var encryptionURL: URL = "https://element.io/help#encryption"
/// A URL where users can go read more about device verification..
private(set) var deviceVerificationURL: URL = "https://element.io/help#encryption-device-verification"
/// A URL where users can go read more about the chat backup.
private(set) var chatBackupDetailsURL: URL = "https://element.io/help#encryption5"
/// A URL where users can go read more about identity pinning violations
private(set) var identityPinningViolationDetailsURL: URL = "https://element.io/help#encryption18"
/// A URL describing how history sharing works
private(set) var historySharingDetailsURL: URL = "https://element.io/en/help#e2ee-history-sharing"
/// Any domains that Element web may be hosted on - used for handling links.
private(set) var elementWebHosts = ["app.element.io", "staging.element.io", "develop.element.io"]
/// The domain that account provisioning links will be hosted on - used for handling the links.
private(set) var accountProvisioningHost = "mobile.element.io"
/// The App Store URL for Element Pro, shown to the user when a homeserver requires that app.
/// **Note:** This property isn't overridable as it in unexpected for forks to come across the error (or to even have a "Pro" app).
let elementProAppStoreURL: URL = "https://apps.apple.com/app/element-pro-for-work/id6502951615"
@UserPreference(defaultValue: AppAppearance.system)
var appAppearance: AppAppearance
/// Tracks previous servers the user connected to for autocompletion purposes. Entries are made lowercase on write.
@UserPreference(key: "previousServers", defaultValue: [])
var previousServers: [String]
var defaultServer: String {
previousServers.first ?? accountProviders[0]
}
// MARK: - Security
/// The app must be locked with a PIN code as part of the authentication flow.
let appLockIsMandatory = false
/// The amount of time the app can remain in the background for without requesting the PIN/TouchID/FaceID.
let appLockGracePeriod: TimeInterval = 0
/// Any codes that the user isn't allowed to use for their PIN.
let appLockPINCodeBlockList = ["0000", "1234"]
/// The number of attempts the user has made to unlock the app with a PIN code (resets when unlocked).
@UserPreference(defaultValue: 0)
var appLockNumberOfPINAttempts: Int
// MARK: - Authentication
/// Any pre-defined static client registrations for OAuth issuers.
let oAuthStaticRegistrations: [URL: String] = ["https://id.thirdroom.io/realms/thirdroom": "elementx"]
/// The redirect URL used for OAuth. For the normal case we don't actually need the bundle ID as the web authentication session handles the redirect internally.
/// However in the case where MAS sends the user to an external app, we need to make sure that the system will open the correct variant of the app (e.g. Nightly).
private(set) nonisolated(unsafe) var oAuthRedirectURL: URL! = URL(string: "https://element.io/oauth/ios/\(InfoPlistReader.main.bundleIdentifier)")
/// A path that is appended to `websiteURL` to form the OAuth `clientURI`. MAS uses `clientURI` as the identifier for a specific app, allowing us to
/// distinguish the various clients we have for Android, iOS and Web from each other.
/// Intentionally a distinct property so it can be easily overridden without having to manipulate the website URL.
private(set) var oAuthClientURIPath: String? = "apps/ios"
var oAuthConfiguration: OAuthConfiguration {
OAuthConfiguration(clientName: InfoPlistReader.main.bundleDisplayName,
redirectURI: oAuthRedirectURL,
clientURI: oAuthClientURIPath.map { websiteURL.appending(path: $0) } ?? websiteURL,
logoURI: logoURL,
tosURI: acceptableUseURL,
policyURI: privacyURL,
staticRegistrations: oAuthStaticRegistrations.mapKeys { $0.absoluteString })
}
/// Whether or not the Create Account button is shown on the start screen.
///
/// **Note:** Setting this to false doesn't prevent someone from creating an account when the selected homeserver's MAS allows registration.
let showCreateAccountButton = true
// MARK: - Notifications
var pusherAppID: String {
#if DEBUG
InfoPlistReader.main.baseBundleIdentifier + ".ios.dev"
#else
InfoPlistReader.main.baseBundleIdentifier + ".ios.prod"
#endif
}
private(set) var pushGatewayBaseURL: URL = "https://matrix.org"
var pushGatewayNotifyEndpoint: URL {
pushGatewayBaseURL.appending(path: "_matrix/push/v1/notify")
}
@UserPreference(defaultValue: true)
var enableNotifications: Bool
@UserPreference(defaultValue: true)
var enableInAppNotifications: Bool
@UserPreference(defaultValue: false)
var hideQuietNotificationAlerts: Bool
/// Tag describing which set of device specific rules a pusher executes.
@UserPreference
var pusherProfileTag: String?
/// The device's last boot time as recorded by the NSE.
@UserPreference
var lastNotificationBootTime: TimeInterval?
/// The sound played when delivering noisy notifications. If nil, use the ElementX default
@UserPreference
var selectedNotificationTone: NotificationTone?
// MARK: - Logging
@UserPreference(defaultValue: LogLevel.info)
var logLevel: LogLevel
@UserPreference(defaultValue: Set<TraceLogPack>())
var traceLogPacks: Set<TraceLogPack>
// MARK: - Bug report
let bugReportRageshakeURL: RemotePreference<RageshakeConfiguration> = .init(Secrets.rageshakeURL.map { .url(URL(string: $0)!) } ?? .disabled) // swiftlint:disable:this force_unwrapping
let bugReportSentryURL: URL? = Secrets.sentryDSN.map { URL(string: $0)! } // swiftlint:disable:this force_unwrapping
let bugReportSentryRustURL: URL? = Secrets.sentryRustDSN.map { URL(string: $0)! } // swiftlint:disable:this force_unwrapping
/// The name allocated by the bug report server
private(set) var bugReportApplicationID = "element-x-ios"
// MARK: - Content scanner
/// The base URL of the content scanner server used to scan media before it is downloaded.
/// `nil` when content scanning is disabled.
let contentScannerURL: RemotePreference<URL?> = .init(nil)
// MARK: - Encryption
/// Whether the server forbids the use of E2EE: new rooms are created unencrypted and
/// enabling encryption on existing rooms is not offered.
let forceDisableE2EE: RemotePreference<Bool> = .init(false)
// MARK: - Analytics
/// The configuration to use for analytics. Set to `nil` to disable analytics.
let analyticsConfiguration: AnalyticsConfiguration? = AppSettings.makeAnalyticsConfiguration()
/// The URL to open with more information about analytics terms. When this is `nil` the "Learn more" link will be hidden.
private(set) var analyticsTermsURL: URL? = "https://element.io/cookie-policy"
/// Whether or not there the app is able ask for user consent to enable analytics or sentry reporting.
var canPromptForAnalytics: Bool {
analyticsConfiguration != nil || bugReportSentryURL != nil
}
private static func makeAnalyticsConfiguration() -> AnalyticsConfiguration? {
guard let host = Secrets.postHogHost, let apiKey = Secrets.postHogAPIKey else { return nil }
return AnalyticsConfiguration(host: host, apiKey: apiKey)
}
/// Whether the user has opted in to send analytics.
@UserPreference(defaultValue: AnalyticsConsentState.unknown)
var analyticsConsentState: AnalyticsConsentState
/// Whether a user session has ever been set up on this device. Deliberately not cleared on
/// logout: it stops the Classic app migration prompt from reappearing when an Element X
/// user is signed out unexpectedly (invalidated token, corrupted storage, etc).
@UserPreference(defaultValue: false)
var hasSignedInBefore: Bool
@UserPreference(defaultValue: false)
var hasRunNotificationPermissionsOnboarding: Bool
@UserPreference(defaultValue: false)
var hasRunIdentityConfirmationOnboarding: Bool
@UserPreference(defaultValue: false)
var hasRequestedLocationAlwaysLocationAuthorization: Bool
@UserPreference(defaultValue: [FrequentlyUsedEmoji]())
var frequentlyUsedSystemEmojis: [FrequentlyUsedEmoji]
// MARK: - Live Location
@UserPreference(key: "liveLocationSharingTimeoutDatesByRoomID", defaultValue: [String: LiveLocationSession]())
var liveLocationSharingSessionsByRoomID: [String: LiveLocationSession]
@UserPreference(defaultValue: 10)
var liveLocationMinimumDistanceUpdate: Int
@UserPreference(defaultValue: false)
var liveLocationDisclaimerDisplayed: Bool
// MARK: - Home Screen
@UserPreference(defaultValue: RoomListActivityVisibility.current)
var roomListActivityVisibility: RoomListActivityVisibility
@UserPreference(defaultValue: false)
var roomListNotificationCountEnabled: Bool
// MARK: - Room Screen
@UserPreference(defaultValue: AppBuildType.current == .debug)
var viewSourceEnabled: Bool
@UserPreference(defaultValue: true)
var optimizeMediaUploads: Bool
@UserPreference(defaultValue: AudioPlaybackSpeed.default)
var voiceMessagePlaybackSpeed: AudioPlaybackSpeed
/// Whether or not to show a warning on the media caption composer so the user knows
/// that captions might not be visible to users who are using other Matrix clients.
let shouldShowMediaCaptionWarning = true
// MARK: - Element Call
#if IS_MAIN_APP
// swiftlint:disable:next force_unwrapping
let elementCallBaseURL: URL = EmbeddedElementCall.appURL!
#endif
// These are publicly availble on https://call.element.io so we don't neeed to treat them as secrets
let elementCallPosthogAPIHost = "https://posthog-element-call.element.io"
let elementCallPosthogAPIKey = "phc_rXGHx9vDmyEvyRxPziYtdVIv0ahEv8A9uLWFcCi1WcU"
let elementCallPosthogSentryDSN = "https://3bd2f95ba5554d4497da7153b552ffb5@sentry.tools.element.io/41"
@UserPreference
var elementCallBaseURLOverride: URL?
// MARK: - Users
/// Whether to hide the display name and avatar of ignored users as these may contain objectionable content.
let hideIgnoredUserProfiles = true
// MARK: - Maps
/// The locally-bundled MapTiler configuration.
static let bundledMapTilerConfiguration = MapTilerConfiguration(baseURL: "https://api.maptiler.com/maps",
apiKey: Secrets.mapLibreAPIKey,
lightStyleID: "9bc819c8-e627-474a-a348-ec144fe3d810",
darkStyleID: "dea61faf-292b-4774-9660-58fcef89a7f3")
/// The MapTiler configuration used to build map URLs, which defaults to the bundled one.
private(set) var mapTilerConfiguration = RemotePreference(AppSettings.bundledMapTilerConfiguration)
// MARK: - Presence
@UserPreference(defaultValue: true)
var sharePresence: Bool
// MARK: - Feature Flags
/// Others
@UserPreference(defaultValue: false)
var fuzzyRoomListSearchEnabled: Bool
@UserPreference(defaultValue: false)
var lowPriorityFilterEnabled: Bool
@UserPreference(defaultValue: false)
var mentionsFilterEnabled: Bool
/// Configuration to enable only signed device isolation mode for crypto. In this mode only devices signed by their owner will be considered in e2ee rooms.
@UserPreference(defaultValue: false)
var enableOnlySignedDeviceIsolationMode: Bool
@UserPreference(defaultValue: false)
var knockingEnabled: Bool
@UserPreference(defaultValue: false)
var threadsEnabled: Bool
@UserPreference(defaultValue: false)
var messageMultiSelectEnabled: Bool
@UserPreference(defaultValue: ProcessInfo().isiOSAppOnMac)
var globalSearchEnabled: Bool
@UserPreference(defaultValue: false)
var focusEventOnNotificationTap: Bool
@UserPreference(defaultValue: false)
var linkPreviewsEnabled: Bool
/// Enables *sending* gallery messages (multiple media in a single message).
/// Received galleries are always rendered regardless of this flag.
@UserPreference(defaultValue: false)
var galleryEnabled: Bool
@UserPreference(defaultValue: false)
var jumpToReadMarkerEnabled: Bool
@UserPreference(defaultValue: false)
var linkNewDeviceEnabled: Bool
@UserPreference(defaultValue: false)
var automaticBackPaginationEnabled: Bool
@UserPreference(key: "clientPausingAndResumingEnabledV2", defaultValue: false, volatile: true)
var clientPausingAndResumingEnabled: Bool
@UserPreference(defaultValue: AppBuildType.current != .release)
var developerOptionsEnabled: Bool
init(store: UserDefaultsProtocol) {
self.store = store
}
static func volatile() -> AppSettings {
AppSettings(store: VolatileUserDefaults())
}
}
nonisolated extension AppSettings: CommonSettingsProtocol { }