Browse Source

Merge pull request #428 from Leptopoda/cleanup/option_builder

neon, settings: improve nullability of OptionBuilder
pull/421/head
Nikolas Rimikis 2 years ago committed by GitHub
parent
commit
3e5f570502
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      packages/neon/neon/lib/src/app.dart
  2. 29
      packages/neon/neon/lib/src/utils/sort_box_builder.dart
  3. 4
      packages/settings/lib/src/options/option.dart
  4. 3
      packages/settings/lib/src/options/select_option.dart
  5. 4
      packages/settings/lib/src/options/toggle_option.dart
  6. 30
      packages/settings/lib/src/widgets/checkbox_settings_tile.dart
  7. 25
      packages/settings/lib/src/widgets/option_builder.dart

8
packages/neon/neon/lib/src/app.dart

@ -254,10 +254,6 @@ class _NeonAppState extends State<NeonApp> with WidgetsBindingObserver, tray.Tra
builder: (final context, final themeKeepOriginalAccentColor) => StreamBuilder<Account?>(
stream: _accountsBloc.activeAccount,
builder: (final context, final activeAccountSnapshot) {
if (themeMode == null || themeOLEDAsDark == null) {
return Container();
}
FlutterNativeSplash.remove();
return ResultBuilder<Capabilities?>.behaviorSubject(
stream: activeAccountSnapshot.hasData
@ -280,12 +276,12 @@ class _NeonAppState extends State<NeonApp> with WidgetsBindingObserver, tray.Tra
theme: getThemeFromNextcloudTheme(
nextcloudTheme,
Brightness.light,
keepOriginalAccentColor: nextcloudTheme == null || (themeKeepOriginalAccentColor ?? false),
keepOriginalAccentColor: nextcloudTheme == null || themeKeepOriginalAccentColor,
),
darkTheme: getThemeFromNextcloudTheme(
nextcloudTheme,
Brightness.dark,
keepOriginalAccentColor: nextcloudTheme == null || (themeKeepOriginalAccentColor ?? false),
keepOriginalAccentColor: nextcloudTheme == null || themeKeepOriginalAccentColor,
oledAsDark: themeOLEDAsDark,
),
routerConfig: _routerDelegate,

29
packages/neon/neon/lib/src/utils/sort_box_builder.dart

@ -17,16 +17,21 @@ class SortBoxBuilder<T extends Enum, R> extends StatelessWidget {
final Widget Function(BuildContext, List<R>?) builder;
@override
Widget build(final BuildContext context) => OptionBuilder<T>(
option: sortPropertyOption,
builder: (final context, final property) => OptionBuilder<SortBoxOrder>(
option: sortBoxOrderOption,
builder: (final context, final order) => property == null || order == null
? Container()
: builder(
context,
input == null ? null : sortBox.sort(input!, Box(property, order)),
),
),
);
Widget build(final BuildContext context) {
if (input == null || (input?.isEmpty ?? false)) {
return builder(context, null);
}
return OptionBuilder<T>(
option: sortPropertyOption,
builder: (final context, final property) => OptionBuilder<SortBoxOrder>(
option: sortBoxOrderOption,
builder: (final context, final order) {
final box = Box(property, order);
return builder(context, sortBox.sort(input!, box));
},
),
);
}
}

4
packages/settings/lib/src/options/option.dart

@ -2,12 +2,14 @@ part of '../../settings.dart';
class OptionDisableException implements Exception {}
@immutable
abstract class Option<T> {
Option({
required this.storage,
required this.key,
required this.label,
required this.defaultValue,
required this.stream,
this.category,
final BehaviorSubject<bool>? enabled,
}) : enabled = enabled ?? BehaviorSubject<bool>.seeded(true);
@ -18,8 +20,8 @@ abstract class Option<T> {
final T defaultValue;
final OptionsCategory? category;
final BehaviorSubject<bool> enabled;
final BehaviorSubject<T> stream;
late BehaviorSubject<T> stream;
T get value {
if (hasValue) {
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,
super.category,
super.enabled,
}) {
stream = BehaviorSubject();
}) : super(stream: BehaviorSubject()) {
unawaited(
values.first.then((final vs) async {
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,
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) {

30
packages/settings/lib/src/widgets/checkbox_settings_tile.dart

@ -9,21 +9,19 @@ class CheckBoxSettingsTile extends InputSettingsTile<ToggleOption> {
@override
Widget build(final BuildContext context) => OptionBuilder<bool>(
option: option,
builder: (final context, final value) => value == null
? Container()
: StreamBuilder<bool>(
stream: option.enabled,
builder: (final context, final enabledSnapshot) => !enabledSnapshot.hasData
? Container()
: CheckboxListTile(
title: Text(option.label(context)),
value: value,
onChanged: enabledSnapshot.requireData
? (final value) async {
await option.set(value!);
}
: null,
),
),
builder: (final context, final value) => StreamBuilder<bool>(
stream: option.enabled,
builder: (final context, final enabledSnapshot) => !enabledSnapshot.hasData
? Container()
: CheckboxListTile(
title: Text(option.label(context)),
value: value,
onChanged: enabledSnapshot.requireData
? (final value) async {
await option.set(value!);
}
: null,
),
),
);
}

25
packages/settings/lib/src/widgets/option_builder.dart

@ -1,22 +1,23 @@
part of '../../settings.dart';
class OptionBuilder<T> extends StatelessWidget {
const OptionBuilder({
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 Widget Function(BuildContext context, T? data) builder;
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) => StreamBuilder<T?>(
stream: option.stream,
initialData: option.defaultValue,
builder: (final context, final valueSnapshot) => builder(
context,
valueSnapshot.data,
),
);
Widget build(final BuildContext context, final T currentSummary) => builder(context, currentSummary);
}

Loading…
Cancel
Save