row_bloc.dart 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import 'package:flowy_sdk/log.dart';
  2. import 'package:flutter_bloc/flutter_bloc.dart';
  3. import 'package:freezed_annotation/freezed_annotation.dart';
  4. import 'dart:async';
  5. import 'data.dart';
  6. import 'row_listener.dart';
  7. import 'row_service.dart';
  8. part 'row_bloc.freezed.dart';
  9. class RowBloc extends Bloc<RowEvent, RowState> {
  10. final RowService service;
  11. final RowListener listener;
  12. RowBloc({required this.service, required this.listener}) : super(RowState.initial(service.rowData)) {
  13. on<RowEvent>(
  14. (event, emit) async {
  15. await event.map(
  16. initial: (_InitialRow value) async {
  17. _startRowListening();
  18. },
  19. createRow: (_CreateRow value) {
  20. service.createRow();
  21. },
  22. activeRow: (_ActiveRow value) {
  23. emit(state.copyWith(active: true));
  24. },
  25. disactiveRow: (_DisactiveRow value) {
  26. emit(state.copyWith(active: false));
  27. },
  28. );
  29. },
  30. );
  31. }
  32. @override
  33. Future<void> close() async {
  34. await listener.close();
  35. return super.close();
  36. }
  37. Future<void> _startRowListening() async {
  38. listener.updateRowNotifier.addPublishListener((result) {
  39. result.fold((row) {
  40. //
  41. }, (err) => null);
  42. });
  43. listener.updateCellNotifier.addPublishListener((result) {
  44. result.fold((repeatedCvell) {
  45. //
  46. Log.info("$repeatedCvell");
  47. }, (r) => null);
  48. });
  49. listener.start();
  50. }
  51. }
  52. @freezed
  53. abstract class RowEvent with _$RowEvent {
  54. const factory RowEvent.initial() = _InitialRow;
  55. const factory RowEvent.createRow() = _CreateRow;
  56. const factory RowEvent.activeRow() = _ActiveRow;
  57. const factory RowEvent.disactiveRow() = _DisactiveRow;
  58. }
  59. @freezed
  60. abstract class RowState with _$RowState {
  61. const factory RowState({
  62. required GridRowData data,
  63. required bool active,
  64. }) = _RowState;
  65. factory RowState.initial(GridRowData data) => RowState(data: data, active: false);
  66. }