board_bloc.dart 14 KB

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