row_bloc.dart 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import 'dart:collection';
  2. import 'package:app_flowy/plugins/grid/application/cell/cell_service/cell_service.dart';
  3. import 'package:app_flowy/plugins/grid/application/field/field_controller.dart';
  4. import 'package:equatable/equatable.dart';
  5. import 'package:flutter_bloc/flutter_bloc.dart';
  6. import 'package:freezed_annotation/freezed_annotation.dart';
  7. import 'dart:async';
  8. import 'row_cache.dart';
  9. import 'row_data_controller.dart';
  10. import 'row_service.dart';
  11. part 'row_bloc.freezed.dart';
  12. class RowBloc extends Bloc<RowEvent, RowState> {
  13. final RowFFIService _rowService;
  14. final GridRowDataController _dataController;
  15. RowBloc({
  16. required RowInfo rowInfo,
  17. required GridRowDataController dataController,
  18. }) : _rowService = RowFFIService(
  19. gridId: rowInfo.gridId,
  20. blockId: rowInfo.rowPB.blockId,
  21. ),
  22. _dataController = dataController,
  23. super(RowState.initial(rowInfo, dataController.loadData())) {
  24. on<RowEvent>(
  25. (event, emit) async {
  26. await event.map(
  27. initial: (_InitialRow value) async {
  28. await _startListening();
  29. },
  30. createRow: (_CreateRow value) {
  31. _rowService.createRow(rowInfo.rowPB.id);
  32. },
  33. didReceiveCells: (_DidReceiveCells value) async {
  34. final cells = value.gridCellMap.values
  35. .map((e) => GridCellEquatable(e.fieldContext))
  36. .toList();
  37. emit(state.copyWith(
  38. gridCellMap: value.gridCellMap,
  39. cells: UnmodifiableListView(cells),
  40. changeReason: value.reason,
  41. ));
  42. },
  43. );
  44. },
  45. );
  46. }
  47. @override
  48. Future<void> close() async {
  49. _dataController.dispose();
  50. return super.close();
  51. }
  52. Future<void> _startListening() async {
  53. _dataController.addListener(
  54. onRowChanged: (cells, reason) {
  55. if (!isClosed) {
  56. add(RowEvent.didReceiveCells(cells, reason));
  57. }
  58. },
  59. );
  60. }
  61. }
  62. @freezed
  63. class RowEvent with _$RowEvent {
  64. const factory RowEvent.initial() = _InitialRow;
  65. const factory RowEvent.createRow() = _CreateRow;
  66. const factory RowEvent.didReceiveCells(
  67. GridCellMap gridCellMap, RowsChangedReason reason) = _DidReceiveCells;
  68. }
  69. @freezed
  70. class RowState with _$RowState {
  71. const factory RowState({
  72. required RowInfo rowInfo,
  73. required GridCellMap gridCellMap,
  74. required UnmodifiableListView<GridCellEquatable> cells,
  75. RowsChangedReason? changeReason,
  76. }) = _RowState;
  77. factory RowState.initial(RowInfo rowInfo, GridCellMap cellDataMap) =>
  78. RowState(
  79. rowInfo: rowInfo,
  80. gridCellMap: cellDataMap,
  81. cells: UnmodifiableListView(
  82. cellDataMap.values
  83. .map((e) => GridCellEquatable(e.fieldContext))
  84. .toList(),
  85. ),
  86. );
  87. }
  88. class GridCellEquatable extends Equatable {
  89. final GridFieldContext _fieldContext;
  90. const GridCellEquatable(GridFieldContext field) : _fieldContext = field;
  91. @override
  92. List<Object?> get props => [
  93. _fieldContext.id,
  94. _fieldContext.fieldType,
  95. _fieldContext.visibility,
  96. _fieldContext.width,
  97. ];
  98. }