A framework for building convergent cross-platform Nextcloud clients using Flutter.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.2 KiB

2 years ago
part of '../../settings.dart';
class SelectOption<T> extends Option<T> {
SelectOption({
required super.storage,
required super.key,
required super.label,
required super.defaultValue,
required this.values,
super.category,
super.enabled,
}) {
stream = BehaviorSubject();
unawaited(
values.first.then((final vs) async {
final valueStr = storage.getString(key);
T? initialValue;
2 years ago
if (valueStr != null) {
initialValue = _fromString(vs, valueStr);
}
stream.add(initialValue ?? await defaultValue.first);
}),
);
2 years ago
}
T? _fromString(final Map<T, LabelBuilder> vs, final String? valueStr) {
final v = vs.keys.where((final e) => e.toString() == valueStr).toList();
if (v.length == 1) {
return v[0];
}
return null;
}
final BehaviorSubject<Map<T, LabelBuilder>> values;
@override
Future set(final T value) {
stream.add(value);
return storage.setString(key, value.toString());
}
@override
String? serialize() => value?.toString();
@override
Future<T?> deserialize(final dynamic data) async => _fromString(await values.first, data as String?);
}