From 71bcae8eccc6245822423175f57f2e68af1d17ff Mon Sep 17 00:00:00 2001 From: Nikolas Rimikis Date: Tue, 27 Jun 2023 18:54:48 +0200 Subject: [PATCH] settings: make option immutable --- packages/settings/lib/src/options/option.dart | 4 +++- packages/settings/lib/src/options/select_option.dart | 3 +-- packages/settings/lib/src/options/toggle_option.dart | 4 +--- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/packages/settings/lib/src/options/option.dart b/packages/settings/lib/src/options/option.dart index 14023de1..63be2b00 100644 --- a/packages/settings/lib/src/options/option.dart +++ b/packages/settings/lib/src/options/option.dart @@ -2,12 +2,14 @@ part of '../../settings.dart'; class OptionDisableException implements Exception {} +@immutable abstract class Option { Option({ required this.storage, required this.key, required this.label, required this.defaultValue, + required this.stream, this.category, final BehaviorSubject? enabled, }) : enabled = enabled ?? BehaviorSubject.seeded(true); @@ -18,8 +20,8 @@ abstract class Option { final T defaultValue; final OptionsCategory? category; final BehaviorSubject enabled; + final BehaviorSubject stream; - late BehaviorSubject stream; T get value { if (hasValue) { return stream.value; diff --git a/packages/settings/lib/src/options/select_option.dart b/packages/settings/lib/src/options/select_option.dart index 99d07a63..126307d9 100644 --- a/packages/settings/lib/src/options/select_option.dart +++ b/packages/settings/lib/src/options/select_option.dart @@ -9,8 +9,7 @@ class SelectOption extends Option { required this.values, super.category, super.enabled, - }) { - stream = BehaviorSubject(); + }) : super(stream: BehaviorSubject()) { unawaited( values.first.then((final vs) async { final valueStr = storage.getString(key); diff --git a/packages/settings/lib/src/options/toggle_option.dart b/packages/settings/lib/src/options/toggle_option.dart index c1c6fa6e..1e47d2b8 100644 --- a/packages/settings/lib/src/options/toggle_option.dart +++ b/packages/settings/lib/src/options/toggle_option.dart @@ -8,9 +8,7 @@ class ToggleOption extends Option { 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) {