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 {} class OptionDisableException implements Exception {}
@immutable
abstract class Option<T> { abstract class Option<T> {
Option({ Option({
required this.storage, required this.storage,
required this.key, required this.key,
required this.label, required this.label,
required this.defaultValue, required this.defaultValue,
required this.stream,
this.category, this.category,
final BehaviorSubject<bool>? enabled, final BehaviorSubject<bool>? enabled,
}) : enabled = enabled ?? BehaviorSubject<bool>.seeded(true); }) : enabled = enabled ?? BehaviorSubject<bool>.seeded(true);
@ -18,8 +20,8 @@ abstract class Option<T> {
final T defaultValue; final T defaultValue;
final OptionsCategory? category; final OptionsCategory? category;
final BehaviorSubject<bool> enabled; final BehaviorSubject<bool> enabled;
final BehaviorSubject<T> stream;
late BehaviorSubject<T> stream;
T get value { T get value {
if (hasValue) { if (hasValue) {
return stream.value; 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, required this.values,
super.category, super.category,
super.enabled, super.enabled,
}) { }) : super(stream: BehaviorSubject()) {
stream = BehaviorSubject();
unawaited( unawaited(
values.first.then((final vs) async { values.first.then((final vs) async {
final valueStr = storage.getString(key); 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, required super.defaultValue,
super.category, super.category,
super.enabled, super.enabled,
}) { }) : super(stream: BehaviorSubject.seeded(storage.getBool(key) ?? defaultValue));
stream = BehaviorSubject.seeded(storage.getBool(key) ?? defaultValue);
}
@override @override
Future set(final bool value) { Future set(final bool value) {

Loading…
Cancel
Save