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 _isSignupMode = false;
|
||||||
bool _usePasswordMode = 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
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
@@ -39,11 +45,25 @@ class _HomePageState extends State<HomePage> with TickerProviderStateMixin {
|
|||||||
duration: const Duration(milliseconds: 400),
|
duration: const Duration(milliseconds: 400),
|
||||||
vsync: this,
|
vsync: this,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
_permissionBloc = PermissionBloc();
|
||||||
|
_fileBrowserBloc = FileBrowserBloc(getIt<FileService>());
|
||||||
|
_uploadBloc = UploadBloc(getIt<FileRepository>());
|
||||||
|
_organizationBloc = OrganizationBloc(
|
||||||
|
_permissionBloc,
|
||||||
|
_fileBrowserBloc,
|
||||||
|
_uploadBloc,
|
||||||
|
getIt<OrgApi>(),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void dispose() {
|
void dispose() {
|
||||||
_animationController.dispose();
|
_animationController.dispose();
|
||||||
|
_organizationBloc.close();
|
||||||
|
_uploadBloc.close();
|
||||||
|
_fileBrowserBloc.close();
|
||||||
|
_permissionBloc.close();
|
||||||
super.dispose();
|
super.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -258,13 +278,13 @@ class _HomePageState extends State<HomePage> with TickerProviderStateMixin {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return BlocProvider(
|
return MultiBlocProvider(
|
||||||
create: (context) => OrganizationBloc(
|
providers: [
|
||||||
PermissionBloc(),
|
BlocProvider<PermissionBloc>.value(value: _permissionBloc),
|
||||||
FileBrowserBloc(getIt<FileService>()),
|
BlocProvider<FileBrowserBloc>.value(value: _fileBrowserBloc),
|
||||||
UploadBloc(getIt<FileRepository>()),
|
BlocProvider<UploadBloc>.value(value: _uploadBloc),
|
||||||
getIt<OrgApi>(),
|
BlocProvider<OrganizationBloc>.value(value: _organizationBloc),
|
||||||
),
|
],
|
||||||
child: Scaffold(
|
child: Scaffold(
|
||||||
backgroundColor: AppTheme.primaryBackground,
|
backgroundColor: AppTheme.primaryBackground,
|
||||||
body: Stack(
|
body: Stack(
|
||||||
@@ -307,15 +327,15 @@ class _HomePageState extends State<HomePage> with TickerProviderStateMixin {
|
|||||||
OrganizationState
|
OrganizationState
|
||||||
>(
|
>(
|
||||||
listener: (context, state) {
|
listener: (context, state) {
|
||||||
if (state is OrganizationLoaded &&
|
if (state is OrganizationLoaded) {
|
||||||
state.selectedOrg != null) {
|
final orgId = state.selectedOrg?.id ?? '';
|
||||||
// Reload file browser when org changes
|
// Reload file browser when org changes (or when falling back to personal workspace)
|
||||||
context.read<FileBrowserBloc>().add(
|
context.read<FileBrowserBloc>().add(
|
||||||
LoadDirectory(
|
LoadDirectory(
|
||||||
orgId: state.selectedOrg!.id,
|
orgId: orgId,
|
||||||
path: '/',
|
path: '/',
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
child:
|
child:
|
||||||
@@ -326,16 +346,20 @@ class _HomePageState extends State<HomePage> with TickerProviderStateMixin {
|
|||||||
builder: (context, orgState) {
|
builder: (context, orgState) {
|
||||||
if (orgState
|
if (orgState
|
||||||
is OrganizationInitial) {
|
is OrganizationInitial) {
|
||||||
WidgetsBinding.instance
|
WidgetsBinding.instance
|
||||||
.addPostFrameCallback((_) {
|
.addPostFrameCallback((_) {
|
||||||
context
|
// Kick off org fetch and immediately show personal workspace
|
||||||
.read<
|
// while org data loads.
|
||||||
OrganizationBloc
|
context.read<OrganizationBloc>().add(
|
||||||
>()
|
LoadOrganizations(),
|
||||||
.add(
|
);
|
||||||
LoadOrganizations(),
|
context.read<FileBrowserBloc>().add(
|
||||||
);
|
const LoadDirectory(
|
||||||
});
|
orgId: '',
|
||||||
|
path: '/',
|
||||||
|
),
|
||||||
|
);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
return Column(
|
return Column(
|
||||||
children: [
|
children: [
|
||||||
|
|||||||
Reference in New Issue
Block a user