Refactor HomePage to use MultiBlocProvider for better state management and lifecycle handling
This commit is contained in:
@@ -32,6 +32,12 @@ class _HomePageState extends State<HomePage> 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<HomePage> with TickerProviderStateMixin {
|
||||
duration: const Duration(milliseconds: 400),
|
||||
vsync: this,
|
||||
);
|
||||
|
||||
_permissionBloc = PermissionBloc();
|
||||
_fileBrowserBloc = FileBrowserBloc(getIt<FileService>());
|
||||
_uploadBloc = UploadBloc(getIt<FileRepository>());
|
||||
_organizationBloc = OrganizationBloc(
|
||||
_permissionBloc,
|
||||
_fileBrowserBloc,
|
||||
_uploadBloc,
|
||||
getIt<OrgApi>(),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_animationController.dispose();
|
||||
_organizationBloc.close();
|
||||
_uploadBloc.close();
|
||||
_fileBrowserBloc.close();
|
||||
_permissionBloc.close();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@@ -258,13 +278,13 @@ class _HomePageState extends State<HomePage> with TickerProviderStateMixin {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocProvider(
|
||||
create: (context) => OrganizationBloc(
|
||||
PermissionBloc(),
|
||||
FileBrowserBloc(getIt<FileService>()),
|
||||
UploadBloc(getIt<FileRepository>()),
|
||||
getIt<OrgApi>(),
|
||||
),
|
||||
return MultiBlocProvider(
|
||||
providers: [
|
||||
BlocProvider<PermissionBloc>.value(value: _permissionBloc),
|
||||
BlocProvider<FileBrowserBloc>.value(value: _fileBrowserBloc),
|
||||
BlocProvider<UploadBloc>.value(value: _uploadBloc),
|
||||
BlocProvider<OrganizationBloc>.value(value: _organizationBloc),
|
||||
],
|
||||
child: Scaffold(
|
||||
backgroundColor: AppTheme.primaryBackground,
|
||||
body: Stack(
|
||||
@@ -307,15 +327,15 @@ class _HomePageState extends State<HomePage> 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<FileBrowserBloc>().add(
|
||||
LoadDirectory(
|
||||
orgId: state.selectedOrg!.id,
|
||||
path: '/',
|
||||
),
|
||||
);
|
||||
LoadDirectory(
|
||||
orgId: orgId,
|
||||
path: '/',
|
||||
),
|
||||
);
|
||||
}
|
||||
},
|
||||
child:
|
||||
@@ -326,16 +346,20 @@ class _HomePageState extends State<HomePage> 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<OrganizationBloc>().add(
|
||||
LoadOrganizations(),
|
||||
);
|
||||
context.read<FileBrowserBloc>().add(
|
||||
const LoadDirectory(
|
||||
orgId: '',
|
||||
path: '/',
|
||||
),
|
||||
);
|
||||
});
|
||||
}
|
||||
return Column(
|
||||
children: [
|
||||
|
||||
Reference in New Issue
Block a user