Make the remote settings hook usable within the app extensions. (#4342)

This commit is contained in:
Doug
2025-07-22 17:09:53 +01:00
committed by GitHub
parent 82c659f3da
commit 5e165be8c3
20 changed files with 245 additions and 139 deletions
@@ -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 a default value, remotely overridden and reset back
/// to the default, 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
/// remote value persisted between app launches.
struct RemotePreference<T: Equatable> {
private let defaultValue: T
private let subject: CurrentValueSubject<T, Never>
var publisher: CurrentValuePublisher<T, Never> { subject.asCurrentValuePublisher() }
var isRemotelyConfigured: Bool { subject.value != defaultValue }
init(_ defaultValue: T) {
self.defaultValue = defaultValue
subject = .init(defaultValue)
}
func applyRemoteValue(_ value: T) {
subject.send(value)
}
func reset() {
if isRemotelyConfigured {
subject.send(defaultValue)
}
}
}