Browse Source

neon, settings: improve nullability of OptionBuilder

pull/428/head
Nikolas Rimikis 2 years ago
parent
commit
47e0d0de1f
No known key found for this signature in database
GPG Key ID: 85ED1DE9786A4FF2
  1. 8
      packages/neon/neon/lib/src/app.dart
  2. 10
      packages/neon/neon/lib/src/utils/sort_box_builder.dart
  3. 30
      packages/settings/lib/src/widgets/checkbox_settings_tile.dart
  4. 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?>( builder: (final context, final themeKeepOriginalAccentColor) => StreamBuilder<Account?>(
stream: _accountsBloc.activeAccount, stream: _accountsBloc.activeAccount,
builder: (final context, final activeAccountSnapshot) { builder: (final context, final activeAccountSnapshot) {
if (themeMode == null || themeOLEDAsDark == null) {
return Container();
}
FlutterNativeSplash.remove(); FlutterNativeSplash.remove();
return ResultBuilder<Capabilities?>.behaviorSubject( return ResultBuilder<Capabilities?>.behaviorSubject(
stream: activeAccountSnapshot.hasData stream: activeAccountSnapshot.hasData
@ -280,12 +276,12 @@ class _NeonAppState extends State<NeonApp> with WidgetsBindingObserver, tray.Tra
theme: getThemeFromNextcloudTheme( theme: getThemeFromNextcloudTheme(
nextcloudTheme, nextcloudTheme,
Brightness.light, Brightness.light,
keepOriginalAccentColor: nextcloudTheme == null || (themeKeepOriginalAccentColor ?? false), keepOriginalAccentColor: nextcloudTheme == null || themeKeepOriginalAccentColor,
), ),
darkTheme: getThemeFromNextcloudTheme( darkTheme: getThemeFromNextcloudTheme(
nextcloudTheme, nextcloudTheme,
Brightness.dark, Brightness.dark,
keepOriginalAccentColor: nextcloudTheme == null || (themeKeepOriginalAccentColor ?? false), keepOriginalAccentColor: nextcloudTheme == null || themeKeepOriginalAccentColor,
oledAsDark: themeOLEDAsDark, oledAsDark: themeOLEDAsDark,
), ),
routerConfig: _routerDelegate, routerConfig: _routerDelegate,

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

@ -21,12 +21,10 @@ class SortBoxBuilder<T extends Enum, R> extends StatelessWidget {
option: sortPropertyOption, option: sortPropertyOption,
builder: (final context, final property) => OptionBuilder<SortBoxOrder>( builder: (final context, final property) => OptionBuilder<SortBoxOrder>(
option: sortBoxOrderOption, option: sortBoxOrderOption,
builder: (final context, final order) => property == null || order == null builder: (final context, final order) => builder(
? Container() context,
: builder( input == null ? null : sortBox.sort(input!, Box(property, order)),
context, ),
input == null ? null : sortBox.sort(input!, Box(property, order)),
),
), ),
); );
} }

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

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

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

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

Loading…
Cancel
Save