row_bloc.dart 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import 'package:flowy_sdk/log.dart';
  2. import 'package:flowy_sdk/protobuf/flowy-grid-data-model/grid.pb.dart';
  3. import 'package:flutter_bloc/flutter_bloc.dart';
  4. import 'package:freezed_annotation/freezed_annotation.dart';
  5. import 'dart:async';
  6. import 'grid_service.dart';
  7. import 'row_listener.dart';
  8. import 'row_service.dart';
  9. part 'row_bloc.freezed.dart';
  10. class RowBloc extends Bloc<RowEvent, RowState> {
  11. final RowService rowService;
  12. final RowListener listener;
  13. RowBloc({required this.rowService, required this.listener}) : super(RowState.initial(rowService.rowData)) {
  14. on<RowEvent>(
  15. (event, emit) async {
  16. await event.map(
  17. initial: (_InitialRow value) async {
  18. _startRowListening();
  19. await _loadCellDatas(emit);
  20. },
  21. createRow: (_CreateRow value) {
  22. rowService.createRow();
  23. },
  24. activeRow: (_ActiveRow value) {
  25. emit(state.copyWith(active: true));
  26. },
  27. disactiveRow: (_DisactiveRow value) {
  28. emit(state.copyWith(active: false));
  29. },
  30. );
  31. },
  32. );
  33. }
  34. @override
  35. Future<void> close() async {
  36. await listener.close();
  37. return super.close();
  38. }
  39. Future<void> _startRowListening() async {
  40. listener.updateRowNotifier.addPublishListener((result) {
  41. result.fold((row) {
  42. //
  43. }, (err) => null);
  44. });
  45. listener.updateCellNotifier.addPublishListener((result) {
  46. result.fold((repeatedCvell) {
  47. //
  48. Log.info("$repeatedCvell");
  49. }, (r) => null);
  50. });
  51. listener.start();
  52. }
  53. Future<void> _loadCellDatas(Emitter<RowState> emit) async {
  54. final result = await rowService.getRow();
  55. result.fold(
  56. (row) {
  57. emit(state.copyWith(
  58. cellDatas: makeGridCellDatas(row),
  59. rowHeight: row.height.toDouble(),
  60. ));
  61. },
  62. (e) => Log.error(e),
  63. );
  64. }
  65. List<GridCellData> makeGridCellDatas(Row row) {
  66. return rowService.rowData.fields.map((field) {
  67. final cell = row.cellByFieldId[field.id];
  68. final rowData = rowService.rowData;
  69. return GridCellData(
  70. rowId: row.id,
  71. gridId: rowData.gridId,
  72. blockId: rowData.blockId,
  73. cell: cell,
  74. field: field,
  75. );
  76. }).toList();
  77. }
  78. }
  79. @freezed
  80. abstract class RowEvent with _$RowEvent {
  81. const factory RowEvent.initial() = _InitialRow;
  82. const factory RowEvent.createRow() = _CreateRow;
  83. const factory RowEvent.activeRow() = _ActiveRow;
  84. const factory RowEvent.disactiveRow() = _DisactiveRow;
  85. }
  86. @freezed
  87. abstract class RowState with _$RowState {
  88. const factory RowState({
  89. required String rowId,
  90. required double rowHeight,
  91. required List<GridCellData> cellDatas,
  92. required bool active,
  93. }) = _RowState;
  94. factory RowState.initial(GridRowData data) => RowState(
  95. rowId: data.rowId,
  96. active: false,
  97. rowHeight: data.height,
  98. cellDatas: [],
  99. );
  100. }