row_bloc.dart 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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_data_controller.dart';
  9. import 'row_service.dart';
  10. part 'row_bloc.freezed.dart';
  11. class RowBloc extends Bloc<RowEvent, RowState> {
  12. final RowService _rowService;
  13. final GridRowDataController _dataController;
  14. RowBloc({
  15. required GridRowInfo rowInfo,
  16. required GridRowDataController dataController,
  17. }) : _rowService = RowService(
  18. gridId: rowInfo.gridId,
  19. blockId: rowInfo.blockId,
  20. rowId: rowInfo.id,
  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();
  32. },
  33. didReceiveCells: (_DidReceiveCells value) async {
  34. final fields = value.gridCellMap.values
  35. .map((e) => GridCellEquatable(e.field))
  36. .toList();
  37. final snapshots = UnmodifiableListView(fields);
  38. emit(state.copyWith(
  39. gridCellMap: value.gridCellMap,
  40. snapshots: snapshots,
  41. changeReason: value.reason,
  42. ));
  43. },
  44. );
  45. },
  46. );
  47. }
  48. @override
  49. Future<void> close() async {
  50. _dataController.dispose();
  51. return super.close();
  52. }
  53. Future<void> _startListening() async {
  54. _dataController.addListener(
  55. onRowChanged: (cells, reason) {
  56. if (!isClosed) {
  57. add(RowEvent.didReceiveCells(cells, reason));
  58. }
  59. },
  60. );
  61. }
  62. }
  63. @freezed
  64. class RowEvent with _$RowEvent {
  65. const factory RowEvent.initial() = _InitialRow;
  66. const factory RowEvent.createRow() = _CreateRow;
  67. const factory RowEvent.didReceiveCells(
  68. GridCellMap gridCellMap, GridRowChangeReason reason) = _DidReceiveCells;
  69. }
  70. @freezed
  71. class RowState with _$RowState {
  72. const factory RowState({
  73. required GridRowInfo rowInfo,
  74. required GridCellMap gridCellMap,
  75. required UnmodifiableListView<GridCellEquatable> snapshots,
  76. GridRowChangeReason? changeReason,
  77. }) = _RowState;
  78. factory RowState.initial(GridRowInfo rowInfo, GridCellMap cellDataMap) =>
  79. RowState(
  80. rowInfo: rowInfo,
  81. gridCellMap: cellDataMap,
  82. snapshots: UnmodifiableListView(
  83. cellDataMap.values.map((e) => GridCellEquatable(e.field)).toList()),
  84. );
  85. }
  86. class GridCellEquatable extends Equatable {
  87. final GridFieldPB _field;
  88. const GridCellEquatable(GridFieldPB field) : _field = field;
  89. @override
  90. List<Object?> get props => [
  91. _field.id,
  92. _field.fieldType,
  93. _field.visibility,
  94. _field.width,
  95. ];
  96. }