board_bloc.dart 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. import 'dart:async';
  2. import 'dart:collection';
  3. import 'package:app_flowy/plugins/grid/application/block/block_cache.dart';
  4. import 'package:app_flowy/plugins/grid/application/field/field_controller.dart';
  5. import 'package:app_flowy/plugins/grid/application/row/row_cache.dart';
  6. import 'package:app_flowy/plugins/grid/application/row/row_service.dart';
  7. import 'package:appflowy_board/appflowy_board.dart';
  8. import 'package:dartz/dartz.dart';
  9. import 'package:equatable/equatable.dart';
  10. import 'package:flowy_sdk/log.dart';
  11. import 'package:flowy_sdk/protobuf/flowy-error/errors.pb.dart';
  12. import 'package:flowy_sdk/protobuf/flowy-folder/view.pb.dart';
  13. import 'package:flowy_sdk/protobuf/flowy-grid/protobuf.dart';
  14. import 'package:flutter_bloc/flutter_bloc.dart';
  15. import 'package:freezed_annotation/freezed_annotation.dart';
  16. import 'board_data_controller.dart';
  17. import 'group_controller.dart';
  18. part 'board_bloc.freezed.dart';
  19. class BoardBloc extends Bloc<BoardEvent, BoardState> {
  20. final BoardDataController _gridDataController;
  21. late final AppFlowyBoardController boardController;
  22. final MoveRowFFIService _rowService;
  23. LinkedHashMap<String, GroupController> groupControllers = LinkedHashMap();
  24. GridFieldController get fieldController =>
  25. _gridDataController.fieldController;
  26. String get gridId => _gridDataController.gridId;
  27. BoardBloc({required ViewPB view})
  28. : _rowService = MoveRowFFIService(gridId: view.id),
  29. _gridDataController = BoardDataController(view: view),
  30. super(BoardState.initial(view.id)) {
  31. boardController = AppFlowyBoardController(
  32. onMoveGroup: (
  33. fromGroupId,
  34. fromIndex,
  35. toGroupId,
  36. toIndex,
  37. ) {
  38. _moveGroup(fromGroupId, toGroupId);
  39. },
  40. onMoveGroupItem: (
  41. groupId,
  42. fromIndex,
  43. toIndex,
  44. ) {
  45. final fromRow = groupControllers[groupId]?.rowAtIndex(fromIndex);
  46. final toRow = groupControllers[groupId]?.rowAtIndex(toIndex);
  47. _moveRow(fromRow, groupId, toRow);
  48. },
  49. onMoveGroupItemToGroup: (
  50. fromGroupId,
  51. fromIndex,
  52. toGroupId,
  53. toIndex,
  54. ) {
  55. final fromRow = groupControllers[fromGroupId]?.rowAtIndex(fromIndex);
  56. final toRow = groupControllers[toGroupId]?.rowAtIndex(toIndex);
  57. _moveRow(fromRow, toGroupId, toRow);
  58. },
  59. );
  60. on<BoardEvent>(
  61. (event, emit) async {
  62. await event.when(
  63. initial: () async {
  64. _startListening();
  65. await _loadGrid(emit);
  66. },
  67. createBottomRow: (groupId) async {
  68. final startRowId = groupControllers[groupId]?.lastRow()?.id;
  69. final result = await _gridDataController.createBoardCard(
  70. groupId,
  71. startRowId: startRowId,
  72. );
  73. result.fold(
  74. (_) {},
  75. (err) => Log.error(err),
  76. );
  77. },
  78. createHeaderRow: (String groupId) async {
  79. final result = await _gridDataController.createBoardCard(groupId);
  80. result.fold(
  81. (_) {},
  82. (err) => Log.error(err),
  83. );
  84. },
  85. didCreateRow: (String groupId, RowPB row, int? index) {
  86. emit(state.copyWith(
  87. editingRow: Some(BoardEditingRow(
  88. columnId: groupId,
  89. row: row,
  90. index: index,
  91. )),
  92. ));
  93. },
  94. endEditRow: (rowId) {
  95. state.editingRow.fold(() => null, (editingRow) {
  96. assert(editingRow.row.id == rowId);
  97. emit(state.copyWith(editingRow: none()));
  98. });
  99. },
  100. didReceiveGridUpdate: (GridPB grid) {
  101. emit(state.copyWith(grid: Some(grid)));
  102. },
  103. didReceiveError: (FlowyError error) {
  104. emit(state.copyWith(noneOrError: some(error)));
  105. },
  106. didReceiveGroups: (List<GroupPB> groups) {
  107. emit(
  108. state.copyWith(
  109. groupIds: groups.map((group) => group.groupId).toList(),
  110. ),
  111. );
  112. },
  113. );
  114. },
  115. );
  116. }
  117. void _moveRow(RowPB? fromRow, String columnId, RowPB? toRow) {
  118. if (fromRow != null) {
  119. _rowService
  120. .moveGroupRow(
  121. fromRowId: fromRow.id,
  122. toGroupId: columnId,
  123. toRowId: toRow?.id,
  124. )
  125. .then((result) {
  126. result.fold((l) => null, (r) => add(BoardEvent.didReceiveError(r)));
  127. });
  128. }
  129. }
  130. void _moveGroup(String fromColumnId, String toColumnId) {
  131. _rowService
  132. .moveGroup(
  133. fromGroupId: fromColumnId,
  134. toGroupId: toColumnId,
  135. )
  136. .then((result) {
  137. result.fold((l) => null, (r) => add(BoardEvent.didReceiveError(r)));
  138. });
  139. }
  140. @override
  141. Future<void> close() async {
  142. await _gridDataController.dispose();
  143. for (final controller in groupControllers.values) {
  144. controller.dispose();
  145. }
  146. return super.close();
  147. }
  148. void initializeGroups(List<GroupPB> groups) {
  149. for (var controller in groupControllers.values) {
  150. controller.dispose();
  151. }
  152. groupControllers.clear();
  153. boardController.clear();
  154. //
  155. List<AppFlowyGroupData> columns = groups
  156. .where((group) => fieldController.getField(group.fieldId) != null)
  157. .map((group) {
  158. return AppFlowyGroupData(
  159. id: group.groupId,
  160. name: group.desc,
  161. items: _buildRows(group),
  162. customData: BoardCustomData(
  163. group: group,
  164. fieldContext: fieldController.getField(group.fieldId)!,
  165. ),
  166. );
  167. }).toList();
  168. boardController.addGroups(columns);
  169. for (final group in groups) {
  170. final delegate = GroupControllerDelegateImpl(
  171. controller: boardController,
  172. fieldController: fieldController,
  173. onNewColumnItem: (groupId, row, index) {
  174. add(BoardEvent.didCreateRow(groupId, row, index));
  175. },
  176. );
  177. final controller = GroupController(
  178. gridId: state.gridId,
  179. group: group,
  180. delegate: delegate,
  181. );
  182. controller.startListening();
  183. groupControllers[controller.group.groupId] = (controller);
  184. }
  185. }
  186. GridRowCache? getRowCache(String blockId) {
  187. final GridBlockCache? blockCache = _gridDataController.blocks[blockId];
  188. return blockCache?.rowCache;
  189. }
  190. void _startListening() {
  191. _gridDataController.addListener(
  192. onGridChanged: (grid) {
  193. if (!isClosed) {
  194. add(BoardEvent.didReceiveGridUpdate(grid));
  195. }
  196. },
  197. didLoadGroups: (groups) {
  198. if (isClosed) return;
  199. initializeGroups(groups);
  200. add(BoardEvent.didReceiveGroups(groups));
  201. },
  202. onDeletedGroup: (groupIds) {
  203. if (isClosed) return;
  204. //
  205. },
  206. onInsertedGroup: (insertedGroups) {
  207. if (isClosed) return;
  208. //
  209. },
  210. onUpdatedGroup: (updatedGroups) {
  211. if (isClosed) return;
  212. for (final group in updatedGroups) {
  213. final columnController =
  214. boardController.getGroupController(group.groupId);
  215. columnController?.updateGroupName(group.desc);
  216. }
  217. },
  218. onError: (err) {
  219. Log.error(err);
  220. },
  221. onResetGroups: (groups) {
  222. if (isClosed) return;
  223. initializeGroups(groups);
  224. add(BoardEvent.didReceiveGroups(groups));
  225. },
  226. );
  227. }
  228. List<AppFlowyGroupItem> _buildRows(GroupPB group) {
  229. final items = group.rows.map((row) {
  230. final fieldContext = fieldController.getField(group.fieldId);
  231. return BoardColumnItem(row: row, fieldContext: fieldContext!);
  232. }).toList();
  233. return <AppFlowyGroupItem>[...items];
  234. }
  235. Future<void> _loadGrid(Emitter<BoardState> emit) async {
  236. final result = await _gridDataController.loadData();
  237. result.fold(
  238. (grid) => emit(
  239. state.copyWith(loadingState: GridLoadingState.finish(left(unit))),
  240. ),
  241. (err) => emit(
  242. state.copyWith(loadingState: GridLoadingState.finish(right(err))),
  243. ),
  244. );
  245. }
  246. }
  247. @freezed
  248. class BoardEvent with _$BoardEvent {
  249. const factory BoardEvent.initial() = _InitialBoard;
  250. const factory BoardEvent.createBottomRow(String groupId) = _CreateBottomRow;
  251. const factory BoardEvent.createHeaderRow(String groupId) = _CreateHeaderRow;
  252. const factory BoardEvent.didCreateRow(
  253. String groupId,
  254. RowPB row,
  255. int? index,
  256. ) = _DidCreateRow;
  257. const factory BoardEvent.endEditRow(String rowId) = _EndEditRow;
  258. const factory BoardEvent.didReceiveError(FlowyError error) = _DidReceiveError;
  259. const factory BoardEvent.didReceiveGridUpdate(
  260. GridPB grid,
  261. ) = _DidReceiveGridUpdate;
  262. const factory BoardEvent.didReceiveGroups(List<GroupPB> groups) =
  263. _DidReceiveGroups;
  264. }
  265. @freezed
  266. class BoardState with _$BoardState {
  267. const factory BoardState({
  268. required String gridId,
  269. required Option<GridPB> grid,
  270. required List<String> groupIds,
  271. required Option<BoardEditingRow> editingRow,
  272. required GridLoadingState loadingState,
  273. required Option<FlowyError> noneOrError,
  274. }) = _BoardState;
  275. factory BoardState.initial(String gridId) => BoardState(
  276. grid: none(),
  277. gridId: gridId,
  278. groupIds: [],
  279. editingRow: none(),
  280. noneOrError: none(),
  281. loadingState: const _Loading(),
  282. );
  283. }
  284. @freezed
  285. class GridLoadingState with _$GridLoadingState {
  286. const factory GridLoadingState.loading() = _Loading;
  287. const factory GridLoadingState.finish(
  288. Either<Unit, FlowyError> successOrFail) = _Finish;
  289. }
  290. class GridFieldEquatable extends Equatable {
  291. final UnmodifiableListView<FieldPB> _fields;
  292. const GridFieldEquatable(
  293. UnmodifiableListView<FieldPB> fields,
  294. ) : _fields = fields;
  295. @override
  296. List<Object?> get props {
  297. if (_fields.isEmpty) {
  298. return [];
  299. }
  300. return [
  301. _fields.length,
  302. _fields
  303. .map((field) => field.width)
  304. .reduce((value, element) => value + element),
  305. ];
  306. }
  307. UnmodifiableListView<FieldPB> get value => UnmodifiableListView(_fields);
  308. }
  309. class BoardColumnItem extends AppFlowyGroupItem {
  310. final RowPB row;
  311. final GridFieldContext fieldContext;
  312. BoardColumnItem({
  313. required this.row,
  314. required this.fieldContext,
  315. });
  316. @override
  317. String get id => row.id;
  318. }
  319. class GroupControllerDelegateImpl extends GroupControllerDelegate {
  320. final GridFieldController fieldController;
  321. final AppFlowyBoardController controller;
  322. final void Function(String, RowPB, int?) onNewColumnItem;
  323. GroupControllerDelegateImpl({
  324. required this.controller,
  325. required this.fieldController,
  326. required this.onNewColumnItem,
  327. });
  328. @override
  329. void insertRow(GroupPB group, RowPB row, int? index) {
  330. final fieldContext = fieldController.getField(group.fieldId);
  331. if (fieldContext == null) {
  332. Log.warn("FieldContext should not be null");
  333. return;
  334. }
  335. if (index != null) {
  336. final item = BoardColumnItem(row: row, fieldContext: fieldContext);
  337. controller.insertGroupItem(group.groupId, index, item);
  338. } else {
  339. final item = BoardColumnItem(row: row, fieldContext: fieldContext);
  340. controller.addGroupItem(group.groupId, item);
  341. }
  342. }
  343. @override
  344. void removeRow(GroupPB group, String rowId) {
  345. controller.removeGroupItem(group.groupId, rowId);
  346. }
  347. @override
  348. void updateRow(GroupPB group, RowPB row) {
  349. final fieldContext = fieldController.getField(group.fieldId);
  350. if (fieldContext == null) {
  351. Log.warn("FieldContext should not be null");
  352. return;
  353. }
  354. controller.updateGroupItem(
  355. group.groupId,
  356. BoardColumnItem(row: row, fieldContext: fieldContext),
  357. );
  358. }
  359. @override
  360. void addNewRow(GroupPB group, RowPB row, int? index) {
  361. final fieldContext = fieldController.getField(group.fieldId);
  362. if (fieldContext == null) {
  363. Log.warn("FieldContext should not be null");
  364. return;
  365. }
  366. final item = BoardColumnItem(row: row, fieldContext: fieldContext);
  367. if (index != null) {
  368. controller.insertGroupItem(group.groupId, index, item);
  369. } else {
  370. controller.addGroupItem(group.groupId, item);
  371. }
  372. onNewColumnItem(group.groupId, row, index);
  373. }
  374. }
  375. class BoardEditingRow {
  376. String columnId;
  377. RowPB row;
  378. int? index;
  379. BoardEditingRow({
  380. required this.columnId,
  381. required this.row,
  382. required this.index,
  383. });
  384. }
  385. class BoardCustomData {
  386. final GroupPB group;
  387. final GridFieldContext fieldContext;
  388. BoardCustomData({
  389. required this.group,
  390. required this.fieldContext,
  391. });
  392. CheckboxGroup? asCheckboxGroup() {
  393. if (fieldType != FieldType.Checkbox) return null;
  394. return CheckboxGroup(group);
  395. }
  396. FieldType get fieldType => fieldContext.fieldType;
  397. }
  398. class CheckboxGroup {
  399. final GroupPB group;
  400. CheckboxGroup(this.group);
  401. // Hardcode value: "Yes" that equal to the value defined in Rust
  402. // pub const CHECK: &str = "Yes";
  403. bool get isCheck => group.groupId == "Yes";
  404. }