flutter-freaccess-hub/lib/shared/components/molecules/menu/presentation/blocs/menu_bloc.dart

63 lines
1.7 KiB
Dart

import 'dart:async';
import 'dart:developer';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:hub/flutter_flow/nav/nav.dart';
import 'package:hub/shared/components/molecules/locals/index.dart';
import 'package:hub/shared/components/molecules/menu/data/repositories/menu_repository_impl.dart';
import 'package:hub/shared/components/molecules/menu/index.dart';
import 'package:hub/shared/components/molecules/modules/index.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
class MenuEvent {}
class MenuState {
final List<MenuItem?> menuEntries;
final bool isGrid;
MenuState({this.menuEntries = const [], this.isGrid = false});
MenuState copyWith({List<MenuItem?>? menuEntries, bool? isGrid}) {
return MenuState(
menuEntries: menuEntries ?? this.menuEntries,
isGrid: isGrid ?? this.isGrid,
);
}
}
class MenuBloc extends Bloc<MenuEvent, MenuState> {
final MenuView style;
final EnumMenuItem item;
final List<MenuEntry> entries;
late StreamSubscription<void> _completer;
MenuBloc({required this.style, required this.item, required this.entries}) : super(MenuState()) {
on<MenuEvent>(_onMenuEvent);
_completer = LicenseRemoteDataSourceImpl().streamCompleterController.listen((_) {
add(MenuEvent());
});
}
Future<void> _onMenuEvent(MenuEvent event, Emitter<MenuState> emit) async {
await LicenseRemoteDataSourceImpl().waitForSaveCompletion();
final List<MenuItem?> NewEntries = await MenuRepositoryImpl().generateMenuEntries(entries, item);
emit(state.copyWith(menuEntries: NewEntries));
}
@override
Future<void> close() {
_completer.cancel();
return super.close();
}
}