From bb33ad12412e4ebcb7fc7e6db740fdd282be225d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leon=20B=C3=B6sche?= Date: Fri, 9 Jan 2026 22:11:14 +0100 Subject: [PATCH] Refactor HomePage to use MultiBlocProvider for better state management and lifecycle handling --- b0esche_cloud/lib/pages/home_page.dart | 74 +++++++++++++++++--------- 1 file changed, 49 insertions(+), 25 deletions(-) diff --git a/b0esche_cloud/lib/pages/home_page.dart b/b0esche_cloud/lib/pages/home_page.dart index e4245f0..772f84b 100644 --- a/b0esche_cloud/lib/pages/home_page.dart +++ b/b0esche_cloud/lib/pages/home_page.dart @@ -32,6 +32,12 @@ class _HomePageState extends State with TickerProviderStateMixin { bool _isSignupMode = false; bool _usePasswordMode = false; + // Shared blocs for the page lifecycle + late final PermissionBloc _permissionBloc; + late final FileBrowserBloc _fileBrowserBloc; + late final UploadBloc _uploadBloc; + late final OrganizationBloc _organizationBloc; + @override void initState() { super.initState(); @@ -39,11 +45,25 @@ class _HomePageState extends State with TickerProviderStateMixin { duration: const Duration(milliseconds: 400), vsync: this, ); + + _permissionBloc = PermissionBloc(); + _fileBrowserBloc = FileBrowserBloc(getIt()); + _uploadBloc = UploadBloc(getIt()); + _organizationBloc = OrganizationBloc( + _permissionBloc, + _fileBrowserBloc, + _uploadBloc, + getIt(), + ); } @override void dispose() { _animationController.dispose(); + _organizationBloc.close(); + _uploadBloc.close(); + _fileBrowserBloc.close(); + _permissionBloc.close(); super.dispose(); } @@ -258,13 +278,13 @@ class _HomePageState extends State with TickerProviderStateMixin { @override Widget build(BuildContext context) { - return BlocProvider( - create: (context) => OrganizationBloc( - PermissionBloc(), - FileBrowserBloc(getIt()), - UploadBloc(getIt()), - getIt(), - ), + return MultiBlocProvider( + providers: [ + BlocProvider.value(value: _permissionBloc), + BlocProvider.value(value: _fileBrowserBloc), + BlocProvider.value(value: _uploadBloc), + BlocProvider.value(value: _organizationBloc), + ], child: Scaffold( backgroundColor: AppTheme.primaryBackground, body: Stack( @@ -307,15 +327,15 @@ class _HomePageState extends State with TickerProviderStateMixin { OrganizationState >( listener: (context, state) { - if (state is OrganizationLoaded && - state.selectedOrg != null) { - // Reload file browser when org changes + if (state is OrganizationLoaded) { + final orgId = state.selectedOrg?.id ?? ''; + // Reload file browser when org changes (or when falling back to personal workspace) context.read().add( - LoadDirectory( - orgId: state.selectedOrg!.id, - path: '/', - ), - ); + LoadDirectory( + orgId: orgId, + path: '/', + ), + ); } }, child: @@ -326,16 +346,20 @@ class _HomePageState extends State with TickerProviderStateMixin { builder: (context, orgState) { if (orgState is OrganizationInitial) { - WidgetsBinding.instance - .addPostFrameCallback((_) { - context - .read< - OrganizationBloc - >() - .add( - LoadOrganizations(), - ); - }); + WidgetsBinding.instance + .addPostFrameCallback((_) { + // Kick off org fetch and immediately show personal workspace + // while org data loads. + context.read().add( + LoadOrganizations(), + ); + context.read().add( + const LoadDirectory( + orgId: '', + path: '/', + ), + ); + }); } return Column( children: [