mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-03 16:10:20 +00:00
Refactor Rageshake URL and Target configuration. (#4337)
* Move the rageshakeURL configuration from the BugReportService to AppSettings. * Refactor Target to return a handle when configuring. This helps with reconfiguration and removes the @MainActor constraint.
This commit is contained in:
@@ -0,0 +1,380 @@
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
#if canImport(EmbeddedElementCall)
|
||||
import EmbeddedElementCall
|
||||
#endif
|
||||
|
||||
import Foundation
|
||||
import SwiftUI
|
||||
|
||||
// Common settings between app and NSE
|
||||
protocol CommonSettingsProtocol {
|
||||
var logLevel: LogLevel { get }
|
||||
var traceLogPacks: Set<TraceLogPack> { get }
|
||||
var enableOnlySignedDeviceIsolationMode: Bool { get }
|
||||
var enableKeyShareOnInvite: Bool { get }
|
||||
var hideQuietNotificationAlerts: Bool { get }
|
||||
var threadsEnabled: Bool { get }
|
||||
}
|
||||
|
||||
/// Store Element specific app settings.
|
||||
final class AppSettings {
|
||||
private enum UserDefaultsKeys: String {
|
||||
case lastVersionLaunched
|
||||
case seenInvites
|
||||
case appLockNumberOfPINAttempts
|
||||
case appLockNumberOfBiometricAttempts
|
||||
case timelineStyle
|
||||
|
||||
case analyticsConsentState
|
||||
case hasRunNotificationPermissionsOnboarding
|
||||
case hasRunIdentityConfirmationOnboarding
|
||||
|
||||
case frequentlyUsedSystemEmojis
|
||||
|
||||
case enableNotifications
|
||||
case enableInAppNotifications
|
||||
case pusherProfileTag
|
||||
case logLevel
|
||||
case traceLogPacks
|
||||
case viewSourceEnabled
|
||||
case optimizeMediaUploads
|
||||
case appAppearance
|
||||
case sharePresence
|
||||
|
||||
case elementCallBaseURLOverride
|
||||
|
||||
// Feature flags
|
||||
case publicSearchEnabled
|
||||
case fuzzyRoomListSearchEnabled
|
||||
case enableOnlySignedDeviceIsolationMode
|
||||
case enableKeyShareOnInvite
|
||||
case knockingEnabled
|
||||
case threadsEnabled
|
||||
case developerOptionsEnabled
|
||||
case sharePosEnabled
|
||||
|
||||
// Doug's tweaks 🔧
|
||||
case hideUnreadMessagesBadge
|
||||
case hideQuietNotificationAlerts
|
||||
}
|
||||
|
||||
private static var suiteName: String = InfoPlistReader.main.appGroupIdentifier
|
||||
|
||||
/// UserDefaults to be used on reads and writes.
|
||||
private static var store: UserDefaults! = UserDefaults(suiteName: suiteName)
|
||||
|
||||
#if IS_MAIN_APP
|
||||
|
||||
static func resetAllSettings() {
|
||||
MXLog.warning("Resetting the AppSettings.")
|
||||
store.removePersistentDomain(forName: suiteName)
|
||||
}
|
||||
|
||||
static func resetSessionSpecificSettings() {
|
||||
MXLog.warning("Resetting the user session specific AppSettings.")
|
||||
store.removeObject(forKey: UserDefaultsKeys.hasRunIdentityConfirmationOnboarding.rawValue)
|
||||
}
|
||||
|
||||
static func configureWithSuiteName(_ name: String) {
|
||||
suiteName = name
|
||||
|
||||
guard let userDefaults = UserDefaults(suiteName: name) else {
|
||||
fatalError("Fail to load shared UserDefaults")
|
||||
}
|
||||
|
||||
store = userDefaults
|
||||
}
|
||||
|
||||
// MARK: - Hooks
|
||||
|
||||
// swiftlint:disable:next function_parameter_count
|
||||
func override(accountProviders: [String],
|
||||
allowOtherAccountProviders: Bool,
|
||||
pushGatewayBaseURL: URL,
|
||||
oidcRedirectURL: URL,
|
||||
websiteURL: URL,
|
||||
logoURL: URL,
|
||||
copyrightURL: URL,
|
||||
acceptableUseURL: URL,
|
||||
privacyURL: URL,
|
||||
encryptionURL: URL,
|
||||
deviceVerificationURL: URL,
|
||||
chatBackupDetailsURL: URL,
|
||||
identityPinningViolationDetailsURL: URL,
|
||||
elementWebHosts: [String],
|
||||
accountProvisioningHost: String,
|
||||
bugReportApplicationID: String,
|
||||
analyticsTermsURL: URL?,
|
||||
mapTilerConfiguration: MapTilerConfiguration) {
|
||||
self.accountProviders = accountProviders
|
||||
self.allowOtherAccountProviders = allowOtherAccountProviders
|
||||
self.pushGatewayBaseURL = pushGatewayBaseURL
|
||||
self.oidcRedirectURL = oidcRedirectURL
|
||||
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.elementWebHosts = elementWebHosts
|
||||
self.accountProvisioningHost = accountProvisioningHost
|
||||
self.bugReportApplicationID = bugReportApplicationID
|
||||
self.analyticsTermsURL = analyticsTermsURL
|
||||
self.mapTilerConfiguration = mapTilerConfiguration
|
||||
}
|
||||
|
||||
// MARK: - Application
|
||||
|
||||
/// Whether or not the app is a development build that isn't in production.
|
||||
static var isDevelopmentBuild: Bool = {
|
||||
#if DEBUG
|
||||
true
|
||||
#else
|
||||
let apps = ["io.element.elementx.nightly", "io.element.elementx.pr"]
|
||||
return apps.contains(InfoPlistReader.main.baseBundleIdentifier)
|
||||
#endif
|
||||
}()
|
||||
|
||||
/// 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(key: UserDefaultsKeys.lastVersionLaunched, storageType: .userDefaults(store))
|
||||
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(key: UserDefaultsKeys.seenInvites, defaultValue: [], storageType: .userDefaults(store))
|
||||
var seenInvites: Set<String>
|
||||
|
||||
/// 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
|
||||
|
||||
/// 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"
|
||||
/// 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(key: UserDefaultsKeys.appAppearance, defaultValue: .system, storageType: .userDefaults(store))
|
||||
var appAppearance: AppAppearance
|
||||
|
||||
// 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(key: UserDefaultsKeys.appLockNumberOfPINAttempts, defaultValue: 0, storageType: .userDefaults(store))
|
||||
var appLockNumberOfPINAttempts: Int
|
||||
|
||||
// MARK: - Authentication
|
||||
|
||||
/// Any pre-defined static client registrations for OIDC issuers.
|
||||
let oidcStaticRegistrations: [URL: String] = ["https://id.thirdroom.io/realms/thirdroom": "elementx"]
|
||||
/// The redirect URL used for OIDC. This no longer uses universal links so we don't need the bundle ID to avoid conflicts between Element X, Nightly and PR builds.
|
||||
private(set) var oidcRedirectURL: URL = "https://element.io/oidc/login"
|
||||
|
||||
private(set) lazy var oidcConfiguration = OIDCConfigurationProxy(clientName: InfoPlistReader.main.bundleDisplayName,
|
||||
redirectURI: oidcRedirectURL,
|
||||
clientURI: websiteURL,
|
||||
logoURI: logoURL,
|
||||
tosURI: acceptableUseURL,
|
||||
policyURI: privacyURL,
|
||||
staticRegistrations: oidcStaticRegistrations.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(key: UserDefaultsKeys.enableNotifications, defaultValue: true, storageType: .userDefaults(store))
|
||||
var enableNotifications
|
||||
|
||||
@UserPreference(key: UserDefaultsKeys.enableInAppNotifications, defaultValue: true, storageType: .userDefaults(store))
|
||||
var enableInAppNotifications
|
||||
|
||||
/// Tag describing which set of device specific rules a pusher executes.
|
||||
@UserPreference(key: UserDefaultsKeys.pusherProfileTag, storageType: .userDefaults(store))
|
||||
var pusherProfileTag: String?
|
||||
|
||||
// MARK: - Bug report
|
||||
|
||||
let bugReportRageshakeURL: ConfigurableSetting<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"
|
||||
/// The maximum size of the upload request. Default value is just below CloudFlare's max request size.
|
||||
let bugReportMaxUploadSize = 50 * 1024 * 1024
|
||||
|
||||
// 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(key: UserDefaultsKeys.analyticsConsentState, defaultValue: AnalyticsConsentState.unknown, storageType: .userDefaults(store))
|
||||
var analyticsConsentState
|
||||
|
||||
@UserPreference(key: UserDefaultsKeys.hasRunNotificationPermissionsOnboarding, defaultValue: false, storageType: .userDefaults(store))
|
||||
var hasRunNotificationPermissionsOnboarding
|
||||
|
||||
@UserPreference(key: UserDefaultsKeys.hasRunIdentityConfirmationOnboarding, defaultValue: false, storageType: .userDefaults(store))
|
||||
var hasRunIdentityConfirmationOnboarding
|
||||
|
||||
@UserPreference(key: UserDefaultsKeys.frequentlyUsedSystemEmojis, defaultValue: [FrequentlyUsedEmoji](), storageType: .userDefaults(store))
|
||||
var frequentlyUsedSystemEmojis
|
||||
|
||||
// MARK: - Home Screen
|
||||
|
||||
@UserPreference(key: UserDefaultsKeys.hideUnreadMessagesBadge, defaultValue: false, storageType: .userDefaults(store))
|
||||
var hideUnreadMessagesBadge
|
||||
|
||||
// MARK: - Room Screen
|
||||
|
||||
@UserPreference(key: UserDefaultsKeys.viewSourceEnabled, defaultValue: isDevelopmentBuild, storageType: .userDefaults(store))
|
||||
var viewSourceEnabled
|
||||
|
||||
@UserPreference(key: UserDefaultsKeys.optimizeMediaUploads, defaultValue: true, storageType: .userDefaults(store))
|
||||
var optimizeMediaUploads
|
||||
|
||||
/// 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
|
||||
|
||||
// swiftlint:disable:next force_unwrapping
|
||||
let elementCallBaseURL: URL = EmbeddedElementCall.appURL!
|
||||
|
||||
// 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(key: UserDefaultsKeys.elementCallBaseURLOverride, defaultValue: nil, storageType: .userDefaults(store))
|
||||
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
|
||||
|
||||
// maptiler base url
|
||||
private(set) var mapTilerConfiguration = MapTilerConfiguration(baseURL: "https://api.maptiler.com/maps",
|
||||
apiKey: Secrets.mapLibreAPIKey,
|
||||
lightStyleID: "9bc819c8-e627-474a-a348-ec144fe3d810",
|
||||
darkStyleID: "dea61faf-292b-4774-9660-58fcef89a7f3")
|
||||
|
||||
// MARK: - Presence
|
||||
|
||||
@UserPreference(key: UserDefaultsKeys.sharePresence, defaultValue: true, storageType: .userDefaults(store))
|
||||
var sharePresence
|
||||
|
||||
// MARK: - Feature Flags
|
||||
|
||||
@UserPreference(key: UserDefaultsKeys.publicSearchEnabled, defaultValue: false, storageType: .userDefaults(store))
|
||||
var publicSearchEnabled
|
||||
|
||||
@UserPreference(key: UserDefaultsKeys.fuzzyRoomListSearchEnabled, defaultValue: false, storageType: .userDefaults(store))
|
||||
var fuzzyRoomListSearchEnabled
|
||||
|
||||
@UserPreference(key: UserDefaultsKeys.knockingEnabled, defaultValue: false, storageType: .userDefaults(store))
|
||||
var knockingEnabled
|
||||
|
||||
@UserPreference(key: UserDefaultsKeys.developerOptionsEnabled, defaultValue: isDevelopmentBuild, storageType: .userDefaults(store))
|
||||
var developerOptionsEnabled
|
||||
|
||||
@UserPreference(key: UserDefaultsKeys.sharePosEnabled, defaultValue: false, storageType: .userDefaults(store))
|
||||
var sharePosEnabled
|
||||
|
||||
#endif
|
||||
|
||||
// MARK: - Shared
|
||||
|
||||
@UserPreference(key: UserDefaultsKeys.logLevel, defaultValue: LogLevel.info, storageType: .userDefaults(store))
|
||||
var logLevel
|
||||
|
||||
@UserPreference(key: UserDefaultsKeys.traceLogPacks, defaultValue: [], storageType: .userDefaults(store))
|
||||
var traceLogPacks: Set<TraceLogPack>
|
||||
|
||||
/// 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(key: UserDefaultsKeys.enableOnlySignedDeviceIsolationMode, defaultValue: false, storageType: .userDefaults(store))
|
||||
var enableOnlySignedDeviceIsolationMode
|
||||
|
||||
/// Configuration to enable encrypted history sharing on invite, and accepting keys from inviters.
|
||||
@UserPreference(key: UserDefaultsKeys.enableKeyShareOnInvite, defaultValue: false, storageType: .userDefaults(store))
|
||||
var enableKeyShareOnInvite
|
||||
|
||||
@UserPreference(key: UserDefaultsKeys.hideQuietNotificationAlerts, defaultValue: false, storageType: .userDefaults(store))
|
||||
var hideQuietNotificationAlerts
|
||||
|
||||
@UserPreference(key: UserDefaultsKeys.threadsEnabled, defaultValue: false, storageType: .userDefaults(store))
|
||||
var threadsEnabled
|
||||
}
|
||||
|
||||
extension AppSettings: CommonSettingsProtocol { }
|
||||
@@ -0,0 +1,35 @@
|
||||
//
|
||||
// Copyright 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 Combine
|
||||
|
||||
/// A setting that can be instantiated with an initial value, overridden and reset back again,
|
||||
/// automatically publishing the changes for downstream subscribers to react to.
|
||||
///
|
||||
/// Unlike ``UserPreference``, this type of setting isn't settable by the user, nor is the
|
||||
/// override persisted between app launches.
|
||||
struct ConfigurableSetting<T: Equatable> {
|
||||
private let initialValue: T
|
||||
private let subject: CurrentValueSubject<T, Never>
|
||||
var publisher: CurrentValuePublisher<T, Never> { subject.asCurrentValuePublisher() }
|
||||
var isOverridden: Bool { subject.value != initialValue }
|
||||
|
||||
init(_ initialValue: T) {
|
||||
self.initialValue = initialValue
|
||||
subject = .init(initialValue)
|
||||
}
|
||||
|
||||
func override(_ value: T) {
|
||||
subject.send(value)
|
||||
}
|
||||
|
||||
func reset() {
|
||||
if isOverridden {
|
||||
subject.send(initialValue)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,231 @@
|
||||
//
|
||||
// Copyright 2023, 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 Foundation
|
||||
|
||||
/// A property wrapper that allows storing data in a keyed storage while also exposing a Combine publisher
|
||||
/// to listen for value changes. The publisher does not skip consecutive duplicates, as there is no
|
||||
/// `Equatable` enforcement at this level.
|
||||
@propertyWrapper
|
||||
final class UserPreference<T: Codable> {
|
||||
static var remotePrefix: String {
|
||||
"remote-"
|
||||
}
|
||||
|
||||
enum Mode {
|
||||
case localOverRemote
|
||||
case remoteOverLocal
|
||||
}
|
||||
|
||||
private let key: String
|
||||
private var remoteKey: String { "\(Self.remotePrefix)\(key)" }
|
||||
private var keyedStorage: any KeyedStorage<T>
|
||||
private let defaultValue: T
|
||||
private let subject: PassthroughSubject<T, Never> = .init()
|
||||
private let mode: Mode
|
||||
|
||||
// This can be used to check if is still possible for the user to change the value or not
|
||||
// Can only be accessed by using `_preferenceName.isLockedToRemote`
|
||||
var isLockedToRemote: Bool {
|
||||
mode == .remoteOverLocal && remoteValue != nil
|
||||
}
|
||||
|
||||
/// Initializes the property wrapper with a static default value.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - key: The key used to store and retrieve the value.
|
||||
/// - defaultValue: The default value to use if no stored value exists or if `forceDefault` is `true`.
|
||||
/// - keyedStorage: The storage instance where the value is saved.
|
||||
/// - forceDefault: A publisher that determines whether the default value should always be used. Defaults to publish `false`. Useful in the context of remote settings that need to override the local value.
|
||||
init(key: String,
|
||||
defaultValue: T,
|
||||
keyedStorage: any KeyedStorage<T>,
|
||||
mode: Mode) {
|
||||
self.key = key
|
||||
self.defaultValue = defaultValue
|
||||
self.keyedStorage = keyedStorage
|
||||
self.mode = mode
|
||||
}
|
||||
|
||||
// The wrapped value is supposed to be the one updated by the user so it can only control the local value
|
||||
var wrappedValue: T {
|
||||
get {
|
||||
switch mode {
|
||||
case .localOverRemote:
|
||||
return keyedStorage[key] ?? keyedStorage[remoteKey] ?? defaultValue
|
||||
case .remoteOverLocal:
|
||||
return keyedStorage[remoteKey] ?? keyedStorage[key] ?? defaultValue
|
||||
}
|
||||
}
|
||||
set {
|
||||
keyedStorage[key] = newValue
|
||||
subject.send(wrappedValue)
|
||||
}
|
||||
}
|
||||
|
||||
// This is supposed to be the value that is set by the remote settings
|
||||
// So it can only be accessed by doing `AppSettings._preferenceName.remoteValue`
|
||||
var remoteValue: T? {
|
||||
get {
|
||||
keyedStorage[remoteKey]
|
||||
} set {
|
||||
keyedStorage[remoteKey] = newValue
|
||||
if mode == .remoteOverLocal || keyedStorage[key] == nil {
|
||||
subject.send(wrappedValue)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var projectedValue: AnyPublisher<T, Never> {
|
||||
subject
|
||||
.prepend(wrappedValue)
|
||||
.eraseToAnyPublisher()
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - UserPreference convenience initializers
|
||||
|
||||
extension UserPreference {
|
||||
enum StorageType {
|
||||
case userDefaults(UserDefaults)
|
||||
case volatile
|
||||
}
|
||||
|
||||
convenience init(key: String, defaultValue: T, storageType: StorageType, mode: Mode = .localOverRemote) {
|
||||
let storage: any KeyedStorage<T>
|
||||
|
||||
switch storageType {
|
||||
case .userDefaults(let userDefaults):
|
||||
storage = UserDefaultsStorage(userDefaults: userDefaults)
|
||||
case .volatile:
|
||||
storage = [String: T]()
|
||||
}
|
||||
|
||||
self.init(key: key, defaultValue: defaultValue, keyedStorage: storage, mode: mode)
|
||||
}
|
||||
|
||||
convenience init<R: RawRepresentable>(key: R, defaultValue: T, storageType: StorageType, mode: Mode = .localOverRemote) where R.RawValue == String {
|
||||
self.init(key: key.rawValue, defaultValue: defaultValue, storageType: storageType, mode: mode)
|
||||
}
|
||||
|
||||
convenience init(key: String, storageType: StorageType, mode: Mode = .localOverRemote) where T: ExpressibleByNilLiteral {
|
||||
self.init(key: key, defaultValue: nil, storageType: storageType, mode: mode)
|
||||
}
|
||||
|
||||
convenience init<R: RawRepresentable>(key: R, storageType: StorageType, mode: Mode = .localOverRemote) where R: RawRepresentable, R.RawValue == String, T: ExpressibleByNilLiteral {
|
||||
self.init(key: key.rawValue, storageType: storageType, mode: mode)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Storage
|
||||
|
||||
protocol KeyedStorage<Value> {
|
||||
associatedtype Value: Codable
|
||||
|
||||
subscript(key: String) -> Value? { get set }
|
||||
}
|
||||
|
||||
/// An implementation of KeyedStorage on the UserDefaults.
|
||||
///
|
||||
/// When used with a `Value` that conforms to `PlistRepresentable` the Codable encode/decode
|
||||
/// phase is skipped, and values are stored natively in the plist.
|
||||
final class UserDefaultsStorage<Value: Codable>: KeyedStorage {
|
||||
private let userDefaults: UserDefaults
|
||||
|
||||
init(userDefaults: UserDefaults) {
|
||||
self.userDefaults = userDefaults
|
||||
}
|
||||
|
||||
subscript(key: String) -> Value? {
|
||||
get {
|
||||
let value: Value?
|
||||
if Value.self is PlistRepresentable.Type {
|
||||
value = decodePlistRepresentableValue(for: key)
|
||||
} else {
|
||||
value = decodeValue(for: key)
|
||||
}
|
||||
|
||||
return value
|
||||
}
|
||||
set {
|
||||
if Value.self is PlistRepresentable.Type {
|
||||
encodePlistRepresentable(value: newValue, for: key)
|
||||
} else {
|
||||
encode(value: newValue, for: key)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func decodeValue(for key: String) -> Value? {
|
||||
userDefaults
|
||||
.data(forKey: key)
|
||||
.flatMap {
|
||||
try? JSONDecoder().decode(Value.self, from: $0)
|
||||
}
|
||||
}
|
||||
|
||||
private func decodePlistRepresentableValue(for key: String) -> Value? {
|
||||
userDefaults.object(forKey: key) as? Value
|
||||
}
|
||||
|
||||
private func encode(value: Value?, for key: String) {
|
||||
// Detects correctly double optionals like this: String?? = .some(nil)
|
||||
if value.isNil {
|
||||
userDefaults.removeObject(forKey: key)
|
||||
} else {
|
||||
let encodedValue = try? JSONEncoder().encode(value)
|
||||
userDefaults.setValue(encodedValue, forKey: key)
|
||||
}
|
||||
}
|
||||
|
||||
private func encodePlistRepresentable(value: Value?, for key: String) {
|
||||
// Detects correctly double optionals like this: String?? = .some(nil)
|
||||
if value.isNil {
|
||||
userDefaults.removeObject(forKey: key)
|
||||
} else {
|
||||
userDefaults.set(value, forKey: key)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private protocol Nullable {
|
||||
var isNil: Bool { get }
|
||||
}
|
||||
|
||||
extension Optional: Nullable {
|
||||
var isNil: Bool {
|
||||
switch self {
|
||||
case .none:
|
||||
return true
|
||||
case .some(let nullable as Nullable):
|
||||
return nullable.isNil
|
||||
case .some:
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension Dictionary: KeyedStorage where Key == String, Value: Codable { }
|
||||
|
||||
// MARK: - PlistRepresentable
|
||||
|
||||
/// A protocol to mark types as being plist compliant.
|
||||
/// UserDefaultsStorage uses this protocol to avoid to encode/decode with Codable plist compliant values.
|
||||
protocol PlistRepresentable { }
|
||||
|
||||
extension Bool: PlistRepresentable { }
|
||||
extension String: PlistRepresentable { }
|
||||
extension Int: PlistRepresentable { }
|
||||
extension Float: PlistRepresentable { }
|
||||
extension Double: PlistRepresentable { }
|
||||
extension Date: PlistRepresentable { }
|
||||
extension Data: PlistRepresentable { }
|
||||
|
||||
extension Array: PlistRepresentable where Element: PlistRepresentable { }
|
||||
extension Dictionary: PlistRepresentable where Key == String, Value: PlistRepresentable { }
|
||||
extension Optional: PlistRepresentable where Wrapped: PlistRepresentable { }
|
||||
Reference in New Issue
Block a user