72 lines
2.5 KiB
Dart
72 lines
2.5 KiB
Dart
import 'package:flutter/widgets.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
|
|
import '../breaker/bloc/breaker_bloc.dart';
|
|
import '../capabilities/bloc/capabilities_cubit.dart';
|
|
import '../chat/bloc/chat_bloc.dart';
|
|
import '../chat_list/bloc/chat_list_bloc.dart';
|
|
import '../children/child_selection_cubit.dart';
|
|
import '../nextcloud_capabilities/bloc/nextcloud_capabilities_cubit.dart';
|
|
import '../parent_letters/bloc/parent_letters_bloc.dart';
|
|
import '../timetable/primary/primary_timetable_scope.dart';
|
|
import 'bloc/account_bloc.dart';
|
|
import 'bloc/account_state.dart';
|
|
|
|
/// The blocs of one account. Keyed by the account id above, so a switch
|
|
/// recreates all of them from the new account's storage instead of resetting
|
|
/// them one by one.
|
|
class AccountScope extends StatelessWidget {
|
|
/// Called with a context below the account's blocs once the scope mounted
|
|
/// for a signed-in account.
|
|
final void Function(BuildContext scopeContext) onActive;
|
|
final Widget child;
|
|
|
|
const AccountScope({super.key, required this.onActive, required this.child});
|
|
|
|
@override
|
|
Widget build(BuildContext context) => MultiBlocProvider(
|
|
providers: [
|
|
BlocProvider<BreakerBloc>(create: (_) => BreakerBloc()),
|
|
BlocProvider<CapabilitiesCubit>(create: (_) => CapabilitiesCubit()),
|
|
BlocProvider<NextcloudCapabilitiesCubit>(
|
|
create: (_) => NextcloudCapabilitiesCubit(),
|
|
),
|
|
BlocProvider<ChatListBloc>(create: (_) => ChatListBloc()),
|
|
BlocProvider<ChatBloc>(
|
|
create: (ctx) => ChatBloc(chatListBloc: ctx.read<ChatListBloc>()),
|
|
),
|
|
BlocProvider<ChildSelectionCubit>(create: (_) => ChildSelectionCubit()),
|
|
BlocProvider<ParentLettersBloc>(create: (_) => ParentLettersBloc()),
|
|
],
|
|
child: PrimaryTimetableScope(
|
|
child: _Activation(onActive: onActive, child: child),
|
|
),
|
|
);
|
|
}
|
|
|
|
class _Activation extends StatefulWidget {
|
|
final void Function(BuildContext scopeContext) onActive;
|
|
final Widget child;
|
|
|
|
const _Activation({required this.onActive, required this.child});
|
|
|
|
@override
|
|
State<_Activation> createState() => _ActivationState();
|
|
}
|
|
|
|
class _ActivationState extends State<_Activation> {
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
if (context.read<AccountBloc>().state.status != AccountStatus.loggedIn) {
|
|
return;
|
|
}
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
if (mounted) widget.onActive(context);
|
|
});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) => widget.child;
|
|
}
|