board_bloc.dart 14 KB

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