mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-05 11:58:32 +00:00
UserPreference now supports closure based default
and you can also force the defaults to be always used through a subscription
This commit is contained in:
@@ -8,28 +8,59 @@
|
||||
import Combine
|
||||
import Foundation
|
||||
|
||||
/// Property wrapper that allows to store data into a keyed storage.
|
||||
/// It also exposes a Combine publisher for listening to value changes.
|
||||
/// The publisher isn't supposed to skip consecutive duplicates if any,
|
||||
/// there is no concept of Equatable at this level.
|
||||
/// 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.
|
||||
///
|
||||
/// - Note: This wrapper allows enforcing a default value through the `forceDefault` closure.
|
||||
@propertyWrapper
|
||||
final class UserPreference<T: Codable> {
|
||||
private let key: String
|
||||
private var keyedStorage: any KeyedStorage<T>
|
||||
private let defaultValue: T
|
||||
private let defaultValue: () -> T
|
||||
private let subject: PassthroughSubject<T, Never> = .init()
|
||||
private var cancellable: AnyCancellable?
|
||||
|
||||
init(key: String, defaultValue: T, keyedStorage: any KeyedStorage<T>) {
|
||||
/// A publisher that determines whether the default value is always being enforced.
|
||||
let forceDefault: CurrentValuePublisher<Bool, Never>
|
||||
|
||||
/// Initializes the property wrapper.
|
||||
///
|
||||
/// - 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 MDM settings.
|
||||
init(key: String,
|
||||
defaultValue: @autoclosure @escaping () -> T,
|
||||
keyedStorage: any KeyedStorage<T>,
|
||||
forceDefault: CurrentValuePublisher<Bool, Never> = .init(.init(false))) {
|
||||
self.key = key
|
||||
self.defaultValue = defaultValue
|
||||
self.keyedStorage = keyedStorage
|
||||
self.forceDefault = forceDefault
|
||||
|
||||
cancellable = forceDefault
|
||||
.sink { [weak self] value in
|
||||
guard value else {
|
||||
return
|
||||
}
|
||||
// If we are now forcing the default value, we need to update the subject with the default value.
|
||||
self?.subject.send(defaultValue())
|
||||
}
|
||||
}
|
||||
|
||||
var wrappedValue: T {
|
||||
get {
|
||||
keyedStorage[key] ?? defaultValue
|
||||
guard !forceDefault.value else {
|
||||
return defaultValue()
|
||||
}
|
||||
return keyedStorage[key] ?? defaultValue()
|
||||
}
|
||||
set {
|
||||
guard !forceDefault.value else {
|
||||
return
|
||||
}
|
||||
keyedStorage[key] = newValue
|
||||
subject.send(wrappedValue)
|
||||
}
|
||||
|
||||
@@ -333,7 +333,9 @@ struct JoinRoomScreen_Previews: PreviewProvider, TestablePreview {
|
||||
}
|
||||
|
||||
@ViewBuilder
|
||||
static func makePreview(viewModel: JoinRoomScreenViewModel, mode: JoinRoomScreenMode) -> some View {
|
||||
static func makePreview(viewModel: JoinRoomScreenViewModel,
|
||||
mode: JoinRoomScreenMode,
|
||||
customPreviewName: String? = nil) -> some View {
|
||||
if mode == .forbidden {
|
||||
NavigationStack {
|
||||
JoinRoomScreen(context: viewModel.context)
|
||||
@@ -344,7 +346,7 @@ struct JoinRoomScreen_Previews: PreviewProvider, TestablePreview {
|
||||
.onAppear {
|
||||
forbiddenViewModel.context.send(viewAction: .join)
|
||||
}
|
||||
.previewDisplayName(mode.previewDisplayName)
|
||||
.previewDisplayName(customPreviewName ?? mode.previewDisplayName)
|
||||
} else {
|
||||
NavigationStack {
|
||||
JoinRoomScreen(context: viewModel.context)
|
||||
@@ -352,7 +354,7 @@ struct JoinRoomScreen_Previews: PreviewProvider, TestablePreview {
|
||||
.snapshotPreferences(expect: viewModel.context.$viewState.map { state in
|
||||
state.roomDetails != nil
|
||||
})
|
||||
.previewDisplayName(mode.previewDisplayName)
|
||||
.previewDisplayName(customPreviewName ?? mode.previewDisplayName)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+9
@@ -32,6 +32,15 @@ struct AdvancedSettingsScreen: View {
|
||||
.onChange(of: context.optimizeMediaUploads) {
|
||||
context.send(viewAction: .optimizeMediaUploadsChanged)
|
||||
}
|
||||
|
||||
// TODO: Waiting for designs and copies
|
||||
ListRow(label: .plain(title: "hide avatars",
|
||||
description: ""),
|
||||
kind: .toggle($context.hideInviteAvatars))
|
||||
|
||||
ListRow(label: .plain(title: "hide media",
|
||||
description: ""),
|
||||
kind: .toggle($context.hideTimelineMedia))
|
||||
}
|
||||
}
|
||||
.compoundList()
|
||||
|
||||
Reference in New Issue
Block a user