row_bloc.dart 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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_service.dart';
  9. part 'row_bloc.freezed.dart';
  10. class RowBloc extends Bloc<RowEvent, RowState> {
  11. final RowService _rowService;
  12. final GridRowCache _rowCache;
  13. void Function()? _rowListenFn;
  14. RowBloc({
  15. required GridRowInfo rowInfo,
  16. required GridRowCache rowCache,
  17. }) : _rowService = RowService(
  18. gridId: rowInfo.gridId,
  19. blockId: rowInfo.blockId,
  20. rowId: rowInfo.id,
  21. ),
  22. _rowCache = rowCache,
  23. super(RowState.initial(rowInfo, rowCache.loadGridCells(rowInfo.id))) {
  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. didReceiveCellDatas: (_DidReceiveCellDatas 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. if (_rowListenFn != null) {
  51. _rowCache.removeRowListener(_rowListenFn!);
  52. }
  53. return super.close();
  54. }
  55. Future<void> _startListening() async {
  56. _rowListenFn = _rowCache.addListener(
  57. rowId: state.rowInfo.id,
  58. onCellUpdated: (cellDatas, reason) =>
  59. add(RowEvent.didReceiveCellDatas(cellDatas, reason)),
  60. listenWhen: () => !isClosed,
  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.didReceiveCellDatas(
  69. GridCellMap gridCellMap, GridRowChangeReason reason) =
  70. _DidReceiveCellDatas;
  71. }
  72. @freezed
  73. class RowState with _$RowState {
  74. const factory RowState({
  75. required GridRowInfo rowInfo,
  76. required GridCellMap gridCellMap,
  77. required UnmodifiableListView<GridCellEquatable> snapshots,
  78. GridRowChangeReason? changeReason,
  79. }) = _RowState;
  80. factory RowState.initial(GridRowInfo rowInfo, GridCellMap cellDataMap) =>
  81. RowState(
  82. rowInfo: rowInfo,
  83. gridCellMap: cellDataMap,
  84. snapshots: UnmodifiableListView(
  85. cellDataMap.values.map((e) => GridCellEquatable(e.field)).toList()),
  86. );
  87. }
  88. class GridCellEquatable extends Equatable {
  89. final GridFieldPB _field;
  90. const GridCellEquatable(GridFieldPB field) : _field = field;
  91. @override
  92. List<Object?> get props => [
  93. _field.id,
  94. _field.fieldType,
  95. _field.visibility,
  96. _field.width,
  97. ];
  98. }