board_data_controller.dart 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import 'dart:collection';
  2. import 'package:app_flowy/plugins/grid/application/view/grid_view_cache.dart';
  3. import 'package:app_flowy/plugins/grid/application/field/field_controller.dart';
  4. import 'package:app_flowy/plugins/grid/application/grid_service.dart';
  5. import 'package:app_flowy/plugins/grid/application/row/row_cache.dart';
  6. import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
  7. import 'package:appflowy_backend/protobuf/flowy-folder/view.pb.dart';
  8. import 'dart:async';
  9. import 'package:dartz/dartz.dart';
  10. import 'package:appflowy_backend/protobuf/flowy-database/protobuf.dart';
  11. import 'board_listener.dart';
  12. typedef OnFieldsChanged = void Function(UnmodifiableListView<FieldInfo>);
  13. typedef OnGridChanged = void Function(DatabasePB);
  14. typedef DidLoadGroups = void Function(List<GroupPB>);
  15. typedef OnUpdatedGroup = void Function(List<GroupPB>);
  16. typedef OnDeletedGroup = void Function(List<String>);
  17. typedef OnInsertedGroup = void Function(InsertedGroupPB);
  18. typedef OnResetGroups = void Function(List<GroupPB>);
  19. typedef OnRowsChanged = void Function(
  20. List<RowInfo>,
  21. RowsChangedReason,
  22. );
  23. typedef OnError = void Function(FlowyError);
  24. class BoardDataController {
  25. final String viewId;
  26. final DatabaseFFIService _databaseFFIService;
  27. final GridFieldController fieldController;
  28. final BoardListener _listener;
  29. late DatabaseViewCache _viewCache;
  30. OnFieldsChanged? _onFieldsChanged;
  31. OnGridChanged? _onGridChanged;
  32. DidLoadGroups? _didLoadGroup;
  33. OnRowsChanged? _onRowsChanged;
  34. OnError? _onError;
  35. List<RowInfo> get rowInfos => _viewCache.rowInfos;
  36. GridRowCache get rowCache => _viewCache.rowCache;
  37. BoardDataController({required ViewPB view})
  38. : viewId = view.id,
  39. _listener = BoardListener(view.id),
  40. _databaseFFIService = DatabaseFFIService(viewId: view.id),
  41. fieldController = GridFieldController(databaseId: view.id) {
  42. //
  43. _viewCache = DatabaseViewCache(
  44. databaseId: view.id,
  45. fieldController: fieldController,
  46. );
  47. _viewCache.addListener(onRowsChanged: (reason) {
  48. _onRowsChanged?.call(rowInfos, reason);
  49. });
  50. }
  51. void addListener({
  52. required OnGridChanged onGridChanged,
  53. OnFieldsChanged? onFieldsChanged,
  54. required DidLoadGroups didLoadGroups,
  55. OnRowsChanged? onRowsChanged,
  56. required OnUpdatedGroup onUpdatedGroup,
  57. required OnDeletedGroup onDeletedGroup,
  58. required OnInsertedGroup onInsertedGroup,
  59. required OnResetGroups onResetGroups,
  60. required OnError? onError,
  61. }) {
  62. _onGridChanged = onGridChanged;
  63. _onFieldsChanged = onFieldsChanged;
  64. _didLoadGroup = didLoadGroups;
  65. _onRowsChanged = onRowsChanged;
  66. _onError = onError;
  67. fieldController.addListener(onFields: (fields) {
  68. _onFieldsChanged?.call(UnmodifiableListView(fields));
  69. });
  70. _listener.start(
  71. onBoardChanged: (result) {
  72. result.fold(
  73. (changeset) {
  74. if (changeset.updateGroups.isNotEmpty) {
  75. onUpdatedGroup.call(changeset.updateGroups);
  76. }
  77. if (changeset.deletedGroups.isNotEmpty) {
  78. onDeletedGroup.call(changeset.deletedGroups);
  79. }
  80. for (final insertedGroup in changeset.insertedGroups) {
  81. onInsertedGroup.call(insertedGroup);
  82. }
  83. },
  84. (e) => _onError?.call(e),
  85. );
  86. },
  87. onGroupByNewField: (result) {
  88. result.fold(
  89. (groups) => onResetGroups(groups),
  90. (e) => _onError?.call(e),
  91. );
  92. },
  93. );
  94. }
  95. Future<Either<Unit, FlowyError>> openGrid() async {
  96. final result = await _databaseFFIService.openGrid();
  97. return result.fold(
  98. (grid) async {
  99. _onGridChanged?.call(grid);
  100. return fieldController.loadFields(fieldIds: grid.fields).then((result) {
  101. return result.fold(
  102. (l) => Future(() async {
  103. await _loadGroups();
  104. _viewCache.rowCache.initializeRows(grid.rows);
  105. return left(l);
  106. }),
  107. (err) => right(err),
  108. );
  109. });
  110. },
  111. (err) => right(err),
  112. );
  113. }
  114. Future<Either<RowPB, FlowyError>> createBoardCard(String groupId,
  115. {String? startRowId}) {
  116. return _databaseFFIService.createBoardCard(groupId, startRowId);
  117. }
  118. Future<void> dispose() async {
  119. await _viewCache.dispose();
  120. await _databaseFFIService.closeGrid();
  121. await fieldController.dispose();
  122. }
  123. Future<void> _loadGroups() async {
  124. final result = await _databaseFFIService.loadGroups();
  125. return Future(
  126. () => result.fold(
  127. (groups) {
  128. _didLoadGroup?.call(groups.items);
  129. },
  130. (err) => _onError?.call(err),
  131. ),
  132. );
  133. }
  134. }