Browse Source

settings: make option immutable

pull/428/head
Nikolas Rimikis 1 year ago
parent
commit
71bcae8ecc
No known key found for this signature in database
GPG Key ID: 85ED1DE9786A4FF2
  1. 4
      packages/settings/lib/src/options/option.dart
  2. 3
      packages/settings/lib/src/options/select_option.dart
  3. 4
      packages/settings/lib/src/options/toggle_option.dart

4
packages/settings/lib/src/options/option.dart

@ -2,12 +2,14 @@ part of '../../settings.dart';
class OptionDisableException implements Exception {}
@immutable
abstract class Option<T> {
Option({
required this.storage,
required this.key,
required this.label,
required this.defaultValue,
required this.stream,
this.category,
final BehaviorSubject<bool>? enabled,
}) : enabled = enabled ?? BehaviorSubject<bool>.seeded(true);
@ -18,8 +20,8 @@ abstract class Option<T> {
final T defaultValue;
final OptionsCategory? category;
final BehaviorSubject<bool> enabled;
final BehaviorSubject<T> stream;
late BehaviorSubject<T> stream;
T get value {
if (hasValue) {
return stream.value;

3
packages/settings/lib/src/options/select_option.dart

@ -9,8 +9,7 @@ class SelectOption<T> extends Option<T> {
required this.values,
super.category,
super.enabled,
}) {
stream = BehaviorSubject();
}) : super(stream: BehaviorSubject()) {
unawaited(
values.first.then((final vs) async {
final valueStr = storage.getString(key);

4
packages/settings/lib/src/options/toggle_option.dart

@ -8,9 +8,7 @@ class ToggleOption extends Option<bool> {
required super.defaultValue,
super.category,
super.enabled,
}) {
stream = BehaviorSubject.seeded(storage.getBool(key) ?? defaultValue);
}
}) : super(stream: BehaviorSubject.seeded(storage.getBool(key) ?? defaultValue));
@override
Future set(final bool value) {

Loading…
Cancel
Save