Nikolas Rimikis
1 year ago
28 changed files with 399 additions and 389 deletions
@ -1,51 +1,67 @@ |
|||||||
import 'dart:async'; |
import 'dart:async'; |
||||||
|
|
||||||
|
import 'package:collection/collection.dart'; |
||||||
import 'package:neon/src/settings/models/option.dart'; |
import 'package:neon/src/settings/models/option.dart'; |
||||||
import 'package:neon/src/settings/widgets/label_builder.dart'; |
import 'package:neon/src/settings/widgets/label_builder.dart'; |
||||||
import 'package:rxdart/rxdart.dart'; |
|
||||||
|
|
||||||
class SelectOption<T> extends Option<T> { |
class SelectOption<T> extends Option<T> { |
||||||
|
/// Creates a SelectOption |
||||||
SelectOption({ |
SelectOption({ |
||||||
required super.storage, |
required super.storage, |
||||||
required super.key, |
required super.key, |
||||||
required super.label, |
required super.label, |
||||||
required super.defaultValue, |
required super.defaultValue, |
||||||
required this.values, |
required final Map<T, LabelBuilder> values, |
||||||
super.category, |
super.category, |
||||||
super.enabled, |
super.enabled, |
||||||
}) : super(stream: BehaviorSubject()) { |
}) : _values = values, |
||||||
unawaited( |
super(initialValue: _fromString(values, storage.getString(key))); |
||||||
values.first.then((final vs) async { |
|
||||||
final valueStr = storage.getString(key); |
|
||||||
T? initialValue; |
|
||||||
|
|
||||||
if (valueStr != null) { |
|
||||||
initialValue = _fromString(vs, valueStr); |
|
||||||
} |
|
||||||
stream.add(initialValue ?? defaultValue); |
|
||||||
}), |
|
||||||
); |
|
||||||
} |
|
||||||
|
|
||||||
T? _fromString(final Map<T, LabelBuilder> vs, final String? valueStr) { |
/// Creates a SelectOption depending on the State of another [Option]. |
||||||
final v = vs.keys.where((final e) => e.toString() == valueStr); |
SelectOption.depend({ |
||||||
if (v.length == 1) { |
required super.storage, |
||||||
return v.first; |
required super.key, |
||||||
} |
required super.label, |
||||||
|
required super.defaultValue, |
||||||
|
required final Map<T, LabelBuilder> values, |
||||||
|
required super.enabled, |
||||||
|
super.category, |
||||||
|
}) : _values = values, |
||||||
|
super.depend(initialValue: _fromString(values, storage.getString(key))); |
||||||
|
|
||||||
|
static T? _fromString<T>(final Map<T, LabelBuilder> vs, final String? valueStr) { |
||||||
|
if (valueStr == null) { |
||||||
return null; |
return null; |
||||||
} |
} |
||||||
|
|
||||||
final BehaviorSubject<Map<T, LabelBuilder>> values; |
return vs.keys.firstWhereOrNull((final e) => e.toString() == valueStr); |
||||||
|
} |
||||||
|
|
||||||
|
Map<T, LabelBuilder> _values; |
||||||
|
|
||||||
@override |
@override |
||||||
Future set(final T value) { |
set value(final T value) { |
||||||
stream.add(value); |
unawaited(storage.setString(key, value.toString())); |
||||||
return storage.setString(key, value.toString()); |
super.value = value; |
||||||
|
} |
||||||
|
|
||||||
|
/// 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 |
@override |
||||||
String? serialize() => value?.toString(); |
String? serialize() => value?.toString(); |
||||||
|
|
||||||
@override |
@override |
||||||
Future<T?> deserialize(final dynamic data) async => _fromString(await values.first, data as String?); |
T? deserialize(final dynamic data) => _fromString(_values, data as String?); |
||||||
} |
} |
||||||
|
@ -1,25 +1,39 @@ |
|||||||
|
import 'dart:async'; |
||||||
|
|
||||||
import 'package:neon/src/settings/models/option.dart'; |
import 'package:neon/src/settings/models/option.dart'; |
||||||
import 'package:rxdart/rxdart.dart'; |
|
||||||
|
|
||||||
class ToggleOption extends Option<bool> { |
class ToggleOption extends Option<bool> { |
||||||
|
/// Creates a ToggleOption |
||||||
ToggleOption({ |
ToggleOption({ |
||||||
required super.storage, |
required super.storage, |
||||||
required super.key, |
required super.key, |
||||||
required super.label, |
required super.label, |
||||||
required super.defaultValue, |
required final bool defaultValue, |
||||||
super.category, |
super.category, |
||||||
super.enabled, |
super.enabled, |
||||||
}) : super(stream: BehaviorSubject.seeded(storage.getBool(key) ?? defaultValue)); |
}) : super(defaultValue: storage.getBool(key) ?? 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) ?? defaultValue, |
||||||
|
); |
||||||
|
|
||||||
@override |
@override |
||||||
Future set(final bool value) { |
set value(final bool value) { |
||||||
stream.add(value); |
unawaited(storage.setBool(key, value)); |
||||||
return storage.setBool(key, value); |
super.value = value; |
||||||
} |
} |
||||||
|
|
||||||
@override |
@override |
||||||
bool serialize() => value; |
bool serialize() => value; |
||||||
|
|
||||||
@override |
@override |
||||||
Future<bool?> deserialize(final dynamic data) async => data as bool; |
bool deserialize(final dynamic data) => data as bool; |
||||||
} |
} |
||||||
|
@ -1,24 +0,0 @@ |
|||||||
import 'package:flutter/widgets.dart'; |
|
||||||
import 'package:neon/src/settings/models/option.dart'; |
|
||||||
|
|
||||||
typedef OptionBuilderFunction<T> = Widget Function(BuildContext context, T snapshot); |
|
||||||
|
|
||||||
class OptionBuilder<T> extends StreamBuilderBase<T, T> { |
|
||||||
OptionBuilder({ |
|
||||||
required this.option, |
|
||||||
required this.builder, |
|
||||||
super.key, |
|
||||||
}) : super(stream: option.stream); |
|
||||||
|
|
||||||
final Option<T> option; |
|
||||||
final OptionBuilderFunction<T> builder; |
|
||||||
|
|
||||||
@override |
|
||||||
T afterData(final T current, final T data) => data; |
|
||||||
|
|
||||||
@override |
|
||||||
T initial() => option.defaultValue; |
|
||||||
|
|
||||||
@override |
|
||||||
Widget build(final BuildContext context, final T currentSummary) => builder(context, currentSummary); |
|
||||||
} |
|
Loading…
Reference in new issue