mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-02 21:33:26 +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:
@@ -18,6 +18,7 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg
|
||||
private let stateMachine: AppCoordinatorStateMachine
|
||||
private let navigationRootCoordinator: NavigationRootCoordinator
|
||||
private let userSessionStore: UserSessionStoreProtocol
|
||||
private let targetConfiguration: Target.Configuration
|
||||
private let appMediator: AppMediator
|
||||
private let appSettings: AppSettings
|
||||
private let appDelegate: AppDelegate
|
||||
@@ -74,9 +75,9 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg
|
||||
|
||||
let appSettings = appHooks.appSettingsHook.configure(AppSettings())
|
||||
|
||||
Target.mainApp.configure(logLevel: appSettings.logLevel,
|
||||
traceLogPacks: appSettings.traceLogPacks,
|
||||
sentryURL: appSettings.bugReportSentryRustURL)
|
||||
targetConfiguration = Target.mainApp.configure(logLevel: appSettings.logLevel,
|
||||
traceLogPacks: appSettings.traceLogPacks,
|
||||
sentryURL: appSettings.bugReportSentryRustURL)
|
||||
|
||||
let appName = InfoPlistReader.main.bundleDisplayName
|
||||
let appVersion = InfoPlistReader.main.bundleShortVersionString
|
||||
@@ -372,7 +373,7 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg
|
||||
private static func setupServiceLocator(appSettings: AppSettings, appHooks: AppHooks) {
|
||||
ServiceLocator.shared.register(userIndicatorController: UserIndicatorController())
|
||||
ServiceLocator.shared.register(appSettings: appSettings)
|
||||
ServiceLocator.shared.register(bugReportService: BugReportService(rageshakeURL: appSettings.bugReportRageshakeURL,
|
||||
ServiceLocator.shared.register(bugReportService: BugReportService(rageshakeURLPublisher: appSettings.bugReportRageshakeURL.publisher,
|
||||
applicationID: appSettings.bugReportApplicationID,
|
||||
sdkGitSHA: sdkGitSha(),
|
||||
maxUploadSize: appSettings.bugReportMaxUploadSize,
|
||||
@@ -403,7 +404,7 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg
|
||||
}
|
||||
}
|
||||
|
||||
// This could be removed once the adotpion of 25.06.x is widespread.
|
||||
// This could be removed once the adoption of 25.06.x is widespread.
|
||||
private func performSettingsToAccountDataMigration(userSession: UserSessionProtocol) {
|
||||
guard let userDefaults = UserDefaults(suiteName: InfoPlistReader.main.appGroupIdentifier) else {
|
||||
return
|
||||
|
||||
+2
-3
@@ -65,7 +65,6 @@ final class AppSettings {
|
||||
}
|
||||
|
||||
private static var suiteName: String = InfoPlistReader.main.appGroupIdentifier
|
||||
private static var remoteSuiteName = "\(InfoPlistReader.main.appGroupIdentifier).remote"
|
||||
|
||||
/// UserDefaults to be used on reads and writes.
|
||||
private static var store: UserDefaults! = UserDefaults(suiteName: suiteName)
|
||||
@@ -252,8 +251,8 @@ final class AppSettings {
|
||||
var pusherProfileTag: String?
|
||||
|
||||
// MARK: - Bug report
|
||||
|
||||
let bugReportRageshakeURL: URL? = Secrets.rageshakeURL.map { URL(string: $0)! } // swiftlint:disable:this force_unwrapping
|
||||
|
||||
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
|
||||
@@ -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 { }
|
||||
@@ -8,51 +8,42 @@
|
||||
import Foundation
|
||||
import MatrixRustSDK
|
||||
|
||||
@MainActor
|
||||
enum Target: String {
|
||||
case mainApp = "elementx"
|
||||
case nse
|
||||
case shareExtension = "shareextension"
|
||||
case tests
|
||||
|
||||
private static var isConfigured = false
|
||||
|
||||
func configure(logLevel: LogLevel, traceLogPacks: Set<TraceLogPack>, sentryURL: URL?) {
|
||||
guard !Self.isConfigured else {
|
||||
return
|
||||
var useLightweightTokioRuntime: Bool {
|
||||
switch self {
|
||||
case .mainApp: false
|
||||
case .nse: true
|
||||
case .shareExtension: true
|
||||
case .tests: false
|
||||
}
|
||||
}
|
||||
|
||||
var logFilePrefix: String? {
|
||||
switch self {
|
||||
case .mainApp: nil
|
||||
default: rawValue
|
||||
}
|
||||
}
|
||||
|
||||
/// Configures the target with logging and an appropriate runtime.
|
||||
///
|
||||
/// Returns a `Configuration` which should be stored to
|
||||
/// a) detect whether the platform is already configured.
|
||||
/// b) reconfigure the platform if necessary.
|
||||
func configure(logLevel: LogLevel, traceLogPacks: Set<TraceLogPack>, sentryURL: URL?) -> Configuration {
|
||||
let tracingConfiguration = Tracing.buildConfiguration(logLevel: logLevel,
|
||||
traceLogPacks: traceLogPacks,
|
||||
currentTarget: rawValue,
|
||||
filePrefix: logFilePrefix,
|
||||
sentryURL: sentryURL)
|
||||
|
||||
do {
|
||||
switch self {
|
||||
case .mainApp:
|
||||
let tracingConfiguration = Tracing.buildConfiguration(logLevel: logLevel,
|
||||
traceLogPacks: traceLogPacks,
|
||||
currentTarget: rawValue,
|
||||
filePrefix: nil,
|
||||
sentryURL: sentryURL)
|
||||
try initPlatform(config: tracingConfiguration, useLightweightTokioRuntime: false)
|
||||
case .nse:
|
||||
let tracingConfiguration = Tracing.buildConfiguration(logLevel: logLevel,
|
||||
traceLogPacks: traceLogPacks,
|
||||
currentTarget: rawValue,
|
||||
filePrefix: rawValue,
|
||||
sentryURL: sentryURL)
|
||||
try initPlatform(config: tracingConfiguration, useLightweightTokioRuntime: true)
|
||||
case .shareExtension:
|
||||
let tracingConfiguration = Tracing.buildConfiguration(logLevel: logLevel,
|
||||
traceLogPacks: traceLogPacks,
|
||||
currentTarget: rawValue,
|
||||
filePrefix: rawValue,
|
||||
sentryURL: sentryURL)
|
||||
try initPlatform(config: tracingConfiguration, useLightweightTokioRuntime: true)
|
||||
case .tests:
|
||||
let tracingConfiguration = Tracing.buildConfiguration(logLevel: logLevel,
|
||||
traceLogPacks: traceLogPacks,
|
||||
currentTarget: rawValue,
|
||||
filePrefix: rawValue,
|
||||
sentryURL: sentryURL)
|
||||
try initPlatform(config: tracingConfiguration, useLightweightTokioRuntime: false)
|
||||
}
|
||||
try initPlatform(config: tracingConfiguration, useLightweightTokioRuntime: useLightweightTokioRuntime)
|
||||
} catch {
|
||||
fatalError("Failed configuring target \(self) with error: \(error)")
|
||||
}
|
||||
@@ -63,6 +54,14 @@ enum Target: String {
|
||||
|
||||
MXLog.configure(currentTarget: rawValue)
|
||||
|
||||
Self.isConfigured = true
|
||||
return Configuration(tracingConfiguration: tracingConfiguration)
|
||||
}
|
||||
|
||||
/// Represents the configuration that was applied by ``configure(logLevel:traceLogPacks:sentryURL:)``.
|
||||
struct Configuration {
|
||||
/// The configuration applied when calling ``configure(logLevel:traceLogPacks:sentryURL:)``.
|
||||
///
|
||||
/// **Note:** This is immutable and won't be updated to reflect further changes.
|
||||
let tracingConfiguration: TracingConfiguration
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user