Nikolas Rimikis
1 year ago
13 changed files with 200 additions and 178 deletions
@ -1,7 +1,6 @@
|
||||
export 'package:neon/src/models/label_builder.dart'; |
||||
export 'package:neon/src/settings/models/option.dart'; |
||||
export 'package:neon/src/settings/models/options_category.dart'; |
||||
export 'package:neon/src/settings/models/options_collection.dart'; |
||||
export 'package:neon/src/settings/models/select_option.dart'; |
||||
export 'package:neon/src/settings/models/storage.dart' show Storable; |
||||
export 'package:neon/src/settings/models/toggle_option.dart'; |
||||
export 'package:neon/src/settings/widgets/settings_list.dart'; |
||||
|
@ -1,99 +0,0 @@
|
||||
import 'dart:async'; |
||||
|
||||
import 'package:collection/collection.dart'; |
||||
import 'package:neon/src/models/label_builder.dart'; |
||||
import 'package:neon/src/settings/models/option.dart'; |
||||
|
||||
class SelectOption<T> extends Option<T> { |
||||
/// Creates a SelectOption |
||||
SelectOption({ |
||||
required super.storage, |
||||
required super.key, |
||||
required super.label, |
||||
required super.defaultValue, |
||||
required final Map<T, LabelBuilder> values, |
||||
|
||||
/// Force loading the stored value. |
||||
/// |
||||
/// This is needed when [values] is empty but the stored value should still be loaded. |
||||
/// This only works when [T] is of type String?. |
||||
final bool forceLoadValue = true, |
||||
super.category, |
||||
super.enabled, |
||||
}) : _values = values, |
||||
super(initialValue: loadValue(values, storage.getString(key.value), forceLoad: forceLoadValue)); |
||||
|
||||
/// Creates a SelectOption depending on the State of another [Option]. |
||||
SelectOption.depend({ |
||||
required super.storage, |
||||
required super.key, |
||||
required super.label, |
||||
required super.defaultValue, |
||||
required final Map<T, LabelBuilder> values, |
||||
required super.enabled, |
||||
|
||||
/// Force loading the stored value. |
||||
/// |
||||
/// This is needed when [values] is empty but the stored value should still be loaded. |
||||
/// This only works when [T] is of type String?. |
||||
final bool forceLoadValue = true, |
||||
super.category, |
||||
}) : _values = values, |
||||
super.depend(initialValue: loadValue(values, storage.getString(key.value), forceLoad: forceLoadValue)); |
||||
|
||||
static T? loadValue<T>(final Map<T, LabelBuilder> vs, final String? stored, {final bool forceLoad = true}) { |
||||
if (forceLoad && vs.isEmpty && stored is T) { |
||||
return stored as T; |
||||
} |
||||
|
||||
return _deserialize(vs, stored); |
||||
} |
||||
|
||||
@override |
||||
void reset() { |
||||
unawaited(storage.remove(key.value)); |
||||
|
||||
super.reset(); |
||||
} |
||||
|
||||
Map<T, LabelBuilder> _values; |
||||
|
||||
@override |
||||
set value(final T value) { |
||||
super.value = value; |
||||
|
||||
if (value != null) { |
||||
unawaited(storage.setString(key.value, serialize()!)); |
||||
} |
||||
} |
||||
|
||||
/// A collection of different values this can have. |
||||
/// |
||||
/// See: |
||||
/// * [value] for the currently selected one |
||||
Map<T, LabelBuilder> get values => _values; |
||||
|
||||
set values(final Map<T, LabelBuilder> newValues) { |
||||
if (_values == newValues) { |
||||
return; |
||||
} |
||||
_values = newValues; |
||||
notifyListeners(); |
||||
} |
||||
|
||||
@override |
||||
String? serialize() => _serialize(value); |
||||
|
||||
static String? _serialize<T>(final T value) => value?.toString(); |
||||
|
||||
@override |
||||
T? deserialize(final Object? data) => _deserialize(_values, data as String?); |
||||
|
||||
static T? _deserialize<T>(final Map<T, LabelBuilder> vs, final String? valueStr) { |
||||
if (valueStr == null) { |
||||
return null; |
||||
} |
||||
|
||||
return vs.keys.firstWhereOrNull((final e) => _serialize(e) == valueStr); |
||||
} |
||||
} |
@ -1,47 +0,0 @@
|
||||
import 'dart:async'; |
||||
|
||||
import 'package:neon/src/settings/models/option.dart'; |
||||
|
||||
class ToggleOption extends Option<bool> { |
||||
/// Creates a ToggleOption |
||||
ToggleOption({ |
||||
required super.storage, |
||||
required super.key, |
||||
required super.label, |
||||
required final bool defaultValue, |
||||
super.category, |
||||
super.enabled, |
||||
}) : super(defaultValue: storage.getBool(key.value) ?? defaultValue); |
||||
|
||||
/// Creates a ToggleOption depending on the State of another [Option]. |
||||
ToggleOption.depend({ |
||||
required super.storage, |
||||
required super.key, |
||||
required super.label, |
||||
required final bool defaultValue, |
||||
required super.enabled, |
||||
super.category, |
||||
}) : super.depend( |
||||
defaultValue: storage.getBool(key.value) ?? defaultValue, |
||||
); |
||||
|
||||
@override |
||||
void reset() { |
||||
unawaited(storage.remove(key.value)); |
||||
|
||||
super.reset(); |
||||
} |
||||
|
||||
@override |
||||
set value(final bool value) { |
||||
super.value = value; |
||||
|
||||
unawaited(storage.setBool(key.value, serialize())); |
||||
} |
||||
|
||||
@override |
||||
bool serialize() => value; |
||||
|
||||
@override |
||||
bool? deserialize(final Object? data) => data as bool?; |
||||
} |
@ -0,0 +1,20 @@
|
||||
import 'package:flutter/widgets.dart'; |
||||
import 'package:meta/meta.dart'; |
||||
import 'package:neon/settings.dart'; |
||||
import 'package:neon/src/settings/widgets/checkbox_settings_tile.dart'; |
||||
import 'package:neon/src/settings/widgets/dropdown_button_settings_tile.dart'; |
||||
import 'package:neon/src/settings/widgets/settings_tile.dart'; |
||||
|
||||
@internal |
||||
class OptionSettingsTile extends InputSettingsTile { |
||||
const OptionSettingsTile({ |
||||
required super.option, |
||||
super.key, |
||||
}); |
||||
|
||||
@override |
||||
Widget build(final BuildContext context) => switch (option) { |
||||
ToggleOption() => CheckBoxSettingsTile(option: option as ToggleOption), |
||||
SelectOption() => DropdownButtonSettingsTile(option: option as SelectOption), |
||||
}; |
||||
} |
Loading…
Reference in new issue