row_bloc.dart 3.0 KB

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