From de01354e78322ae5de36146e66966f3a9d80a53d Mon Sep 17 00:00:00 2001 From: Nikolas Rimikis Date: Thu, 22 Jun 2023 14:52:36 +0200 Subject: [PATCH] neon: add make AppsBlocStates supply the active AppImplementation --- packages/neon/neon/lib/src/blocs/apps.dart | 25 +++++++++++-------- packages/neon/neon/lib/src/pages/home.dart | 10 ++++---- .../neon/neon/lib/src/widgets/app_bar.dart | 10 ++++---- .../neon/neon/lib/src/widgets/drawer.dart | 2 +- 4 files changed, 25 insertions(+), 22 deletions(-) diff --git a/packages/neon/neon/lib/src/blocs/apps.dart b/packages/neon/neon/lib/src/blocs/apps.dart index 90acfe60..730a9945 100644 --- a/packages/neon/neon/lib/src/blocs/apps.dart +++ b/packages/neon/neon/lib/src/blocs/apps.dart @@ -13,7 +13,7 @@ abstract class AppsBlocStates { BehaviorSubject> get notificationsAppImplementation; - BehaviorSubject get activeAppID; + BehaviorSubject get activeApp; BehaviorSubject get openNotifications; @@ -47,8 +47,8 @@ class AppsBloc extends InteractiveBloc implements AppsBlocEvents, AppsBlocStates initialApp = result.requireData.first.id; } } - if (!activeAppID.hasValue) { - await setActiveApp(initialApp!); + if (!activeApp.hasValue && initialApp != null) { + await setActiveApp(initialApp); } }), ); @@ -132,7 +132,7 @@ class AppsBloc extends InteractiveBloc implements AppsBlocEvents, AppsBlocStates unawaited(apps.close()); unawaited(appImplementations.close()); unawaited(notificationsAppImplementation.close()); - unawaited(activeAppID.close()); + unawaited(activeApp.close()); unawaited(openNotifications.close()); unawaited(appVersions.close()); @@ -144,7 +144,7 @@ class AppsBloc extends InteractiveBloc implements AppsBlocEvents, AppsBlocStates } @override - BehaviorSubject activeAppID = BehaviorSubject(); + BehaviorSubject activeApp = BehaviorSubject(); @override BehaviorSubject>>> appImplementations = @@ -176,14 +176,17 @@ class AppsBloc extends InteractiveBloc implements AppsBlocEvents, AppsBlocStates @override Future setActiveApp(final String appID) async { + if (appID == 'notifications') { + openNotifications.add(null); + return; + } + final apps = await appImplementations.firstWhere((final a) => a.hasData); - if (apps.requireData.tryFind(appID) != null) { - // TODO: make activeAppID distinct - if (activeAppID.valueOrNull != appID) { - activeAppID.add(appID); + final app = apps.requireData.tryFind(appID); + if (app != null) { + if (activeApp.valueOrNull?.id != appID) { + activeApp.add(app); } - } else if (appID == 'notifications') { - openNotifications.add(null); } else { throw Exception('App $appID not found'); } diff --git a/packages/neon/neon/lib/src/pages/home.dart b/packages/neon/neon/lib/src/pages/home.dart index 42c16476..47f518bb 100644 --- a/packages/neon/neon/lib/src/pages/home.dart +++ b/packages/neon/neon/lib/src/pages/home.dart @@ -100,9 +100,9 @@ class _HomePageState extends State { @override Widget build(final BuildContext context) => ResultBuilder>.behaviorSubject( stream: _appsBloc.appImplementations, - builder: (final context, final appImplementations) => StreamBuilder( - stream: _appsBloc.activeAppID, - builder: (final context, final activeAppIDSnapshot) => OptionBuilder( + builder: (final context, final appImplementations) => StreamBuilder( + stream: _appsBloc.activeApp, + builder: (final context, final activeAppSnapshot) => OptionBuilder( option: _globalOptions.navigationMode, builder: (final context, final navigationMode) { final drawerAlwaysVisible = navigationMode == NavigationMode.drawerAlwaysVisible; @@ -124,9 +124,9 @@ class _HomePageState extends State { ), ), ] else ...[ - if (activeAppIDSnapshot.hasData) ...[ + if (activeAppSnapshot.hasData) ...[ Expanded( - child: appImplementations.requireData.find(activeAppIDSnapshot.requireData).page, + child: activeAppSnapshot.requireData.page, ), ], ], diff --git a/packages/neon/neon/lib/src/widgets/app_bar.dart b/packages/neon/neon/lib/src/widgets/app_bar.dart index 2114335e..eeb8bfe3 100644 --- a/packages/neon/neon/lib/src/widgets/app_bar.dart +++ b/packages/neon/neon/lib/src/widgets/app_bar.dart @@ -23,18 +23,18 @@ class NeonAppBar extends StatelessWidget implements PreferredSizeWidget { return ResultBuilder>.behaviorSubject( stream: appsBloc.appImplementations, - builder: (final context, final appImplementations) => StreamBuilder( - stream: appsBloc.activeAppID, - builder: (final context, final activeAppIDSnapshot) => AppBar( + builder: (final context, final appImplementations) => StreamBuilder( + stream: appsBloc.activeApp, + builder: (final context, final activeAppSnapshot) => AppBar( title: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Row( children: [ - if (appImplementations.hasData && activeAppIDSnapshot.hasData) ...[ + if (activeAppSnapshot.hasData) ...[ Flexible( child: Text( - appImplementations.requireData.find(activeAppIDSnapshot.data!).name(context), + activeAppSnapshot.requireData.name(context), ), ), ], diff --git a/packages/neon/neon/lib/src/widgets/drawer.dart b/packages/neon/neon/lib/src/widgets/drawer.dart index eff0fcdd..2cf034b4 100644 --- a/packages/neon/neon/lib/src/widgets/drawer.dart +++ b/packages/neon/neon/lib/src/widgets/drawer.dart @@ -61,7 +61,7 @@ class __NeonDrawerState extends State<_NeonDrawer> with SingleTickerProviderStat _appsBloc = _accountsBloc.activeAppsBloc; _apps = widget.apps.toList(); - _activeApp = _apps.indexWhere((final app) => app.id == _appsBloc.activeAppID.valueOrNull); + _activeApp = _apps.indexWhere((final app) => app.id == _appsBloc.activeApp.valueOrNull?.id); _tabController = TabController( vsync: this,