row_cache.dart 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. import 'dart:collection';
  2. import 'package:app_flowy/plugins/grid/application/cell/cell_service/cell_service.dart';
  3. import 'package:app_flowy/plugins/grid/application/field/field_controller.dart';
  4. import 'package:flowy_sdk/dispatch/dispatch.dart';
  5. import 'package:flowy_sdk/log.dart';
  6. import 'package:flowy_sdk/protobuf/flowy-grid/block_entities.pb.dart';
  7. import 'package:flowy_sdk/protobuf/flowy-grid/row_entities.pb.dart';
  8. import 'package:flutter/foundation.dart';
  9. import 'package:freezed_annotation/freezed_annotation.dart';
  10. import 'row_list.dart';
  11. part 'row_cache.freezed.dart';
  12. typedef RowUpdateCallback = void Function();
  13. abstract class IGridRowFieldNotifier {
  14. UnmodifiableListView<FieldInfo> get fields;
  15. void onRowFieldsChanged(VoidCallback callback);
  16. void onRowFieldChanged(void Function(FieldInfo) callback);
  17. void onRowDispose();
  18. }
  19. /// Cache the rows in memory
  20. /// Insert / delete / update row
  21. ///
  22. /// Read https://appflowy.gitbook.io/docs/essential-documentation/contribute-to-appflowy/architecture/frontend/grid for more information.
  23. class GridRowCache {
  24. final String gridId;
  25. final BlockPB block;
  26. /// _rows containers the current block's rows
  27. /// Use List to reverse the order of the GridRow.
  28. final RowList _rowList = RowList();
  29. final GridCellCache _cellCache;
  30. final IGridRowFieldNotifier _fieldNotifier;
  31. final _RowChangesetNotifier _rowChangeReasonNotifier;
  32. UnmodifiableListView<RowInfo> get visibleRows {
  33. var visibleRows = [..._rowList.rows];
  34. return UnmodifiableListView(visibleRows);
  35. }
  36. GridCellCache get cellCache => _cellCache;
  37. GridRowCache({
  38. required this.gridId,
  39. required this.block,
  40. required IGridRowFieldNotifier notifier,
  41. }) : _cellCache = GridCellCache(gridId: gridId),
  42. _rowChangeReasonNotifier = _RowChangesetNotifier(),
  43. _fieldNotifier = notifier {
  44. //
  45. notifier.onRowFieldsChanged(() => _rowChangeReasonNotifier
  46. .receive(const RowsChangedReason.fieldDidChange()));
  47. notifier.onRowFieldChanged(
  48. (field) => _cellCache.removeCellWithFieldId(field.id));
  49. for (final row in block.rows) {
  50. final rowInfo = buildGridRow(row);
  51. _rowList.add(rowInfo);
  52. }
  53. }
  54. Future<void> dispose() async {
  55. _fieldNotifier.onRowDispose();
  56. _rowChangeReasonNotifier.dispose();
  57. await _cellCache.dispose();
  58. }
  59. void applyChangesets(GridBlockChangesetPB changeset) {
  60. _deleteRows(changeset.deletedRows);
  61. _insertRows(changeset.insertedRows);
  62. _updateRows(changeset.updatedRows);
  63. _hideRows(changeset.invisibleRows);
  64. _showRows(changeset.visibleRows);
  65. }
  66. void _deleteRows(List<String> deletedRowIds) {
  67. for (final rowId in deletedRowIds) {
  68. final deletedRow = _rowList.remove(rowId);
  69. if (deletedRow != null) {
  70. _rowChangeReasonNotifier.receive(RowsChangedReason.delete(deletedRow));
  71. }
  72. }
  73. }
  74. void _insertRows(List<InsertedRowPB> insertRows) {
  75. for (final insertedRow in insertRows) {
  76. final insertedIndex =
  77. _rowList.insert(insertedRow.index, buildGridRow(insertedRow.row));
  78. if (insertedIndex != null) {
  79. _rowChangeReasonNotifier
  80. .receive(RowsChangedReason.insert(insertedIndex));
  81. }
  82. }
  83. }
  84. void _updateRows(List<RowPB> updatedRows) {
  85. if (updatedRows.isEmpty) return;
  86. final updatedIndexs =
  87. _rowList.updateRows(updatedRows, (rowPB) => buildGridRow(rowPB));
  88. if (updatedIndexs.isNotEmpty) {
  89. _rowChangeReasonNotifier.receive(RowsChangedReason.update(updatedIndexs));
  90. }
  91. }
  92. void _hideRows(List<String> invisibleRows) {
  93. for (final rowId in invisibleRows) {
  94. final deletedRow = _rowList.remove(rowId);
  95. if (deletedRow != null) {
  96. _rowChangeReasonNotifier.receive(RowsChangedReason.delete(deletedRow));
  97. }
  98. }
  99. }
  100. void _showRows(List<InsertedRowPB> visibleRows) {
  101. for (final insertedRow in visibleRows) {
  102. final insertedIndex =
  103. _rowList.insert(insertedRow.index, buildGridRow(insertedRow.row));
  104. if (insertedIndex != null) {
  105. _rowChangeReasonNotifier
  106. .receive(RowsChangedReason.insert(insertedIndex));
  107. }
  108. }
  109. }
  110. void onRowsChanged(void Function(RowsChangedReason) onRowChanged) {
  111. _rowChangeReasonNotifier.addListener(() {
  112. onRowChanged(_rowChangeReasonNotifier.reason);
  113. });
  114. }
  115. RowUpdateCallback addListener({
  116. required String rowId,
  117. void Function(GridCellMap, RowsChangedReason)? onCellUpdated,
  118. bool Function()? listenWhen,
  119. }) {
  120. listenerHandler() async {
  121. if (listenWhen != null && listenWhen() == false) {
  122. return;
  123. }
  124. notifyUpdate() {
  125. if (onCellUpdated != null) {
  126. final rowInfo = _rowList.get(rowId);
  127. if (rowInfo != null) {
  128. final GridCellMap cellDataMap =
  129. _makeGridCells(rowId, rowInfo.rowPB);
  130. onCellUpdated(cellDataMap, _rowChangeReasonNotifier.reason);
  131. }
  132. }
  133. }
  134. _rowChangeReasonNotifier.reason.whenOrNull(
  135. update: (indexs) {
  136. if (indexs[rowId] != null) notifyUpdate();
  137. },
  138. fieldDidChange: () => notifyUpdate(),
  139. );
  140. }
  141. _rowChangeReasonNotifier.addListener(listenerHandler);
  142. return listenerHandler;
  143. }
  144. void removeRowListener(VoidCallback callback) {
  145. _rowChangeReasonNotifier.removeListener(callback);
  146. }
  147. GridCellMap loadGridCells(String rowId) {
  148. final RowPB? data = _rowList.get(rowId)?.rowPB;
  149. if (data == null) {
  150. _loadRow(rowId);
  151. }
  152. return _makeGridCells(rowId, data);
  153. }
  154. Future<void> _loadRow(String rowId) async {
  155. final payload = RowIdPB.create()
  156. ..gridId = gridId
  157. ..blockId = block.id
  158. ..rowId = rowId;
  159. final result = await GridEventGetRow(payload).send();
  160. result.fold(
  161. (optionRow) => _refreshRow(optionRow),
  162. (err) => Log.error(err),
  163. );
  164. }
  165. GridCellMap _makeGridCells(String rowId, RowPB? row) {
  166. // ignore: prefer_collection_literals
  167. var cellDataMap = GridCellMap();
  168. for (final field in _fieldNotifier.fields) {
  169. if (field.visibility) {
  170. cellDataMap[field.id] = GridCellIdentifier(
  171. rowId: rowId,
  172. gridId: gridId,
  173. fieldInfo: field,
  174. );
  175. }
  176. }
  177. return cellDataMap;
  178. }
  179. void _refreshRow(OptionalRowPB optionRow) {
  180. if (!optionRow.hasRow()) {
  181. return;
  182. }
  183. final updatedRow = optionRow.row;
  184. updatedRow.freeze();
  185. final rowInfo = _rowList.get(updatedRow.id);
  186. final rowIndex = _rowList.indexOfRow(updatedRow.id);
  187. if (rowInfo != null && rowIndex != null) {
  188. final updatedRowInfo = rowInfo.copyWith(rowPB: updatedRow);
  189. _rowList.remove(updatedRow.id);
  190. _rowList.insert(rowIndex, updatedRowInfo);
  191. final UpdatedIndexMap updatedIndexs = UpdatedIndexMap();
  192. updatedIndexs[rowInfo.rowPB.id] = UpdatedIndex(
  193. index: rowIndex,
  194. rowId: updatedRowInfo.rowPB.id,
  195. );
  196. _rowChangeReasonNotifier.receive(RowsChangedReason.update(updatedIndexs));
  197. }
  198. }
  199. RowInfo buildGridRow(RowPB rowPB) {
  200. return RowInfo(
  201. gridId: gridId,
  202. fields: _fieldNotifier.fields,
  203. rowPB: rowPB,
  204. );
  205. }
  206. }
  207. class _RowChangesetNotifier extends ChangeNotifier {
  208. RowsChangedReason reason = const InitialListState();
  209. _RowChangesetNotifier();
  210. void receive(RowsChangedReason newReason) {
  211. reason = newReason;
  212. reason.map(
  213. insert: (_) => notifyListeners(),
  214. delete: (_) => notifyListeners(),
  215. update: (_) => notifyListeners(),
  216. fieldDidChange: (_) => notifyListeners(),
  217. initial: (_) {},
  218. );
  219. }
  220. }
  221. @unfreezed
  222. class RowInfo with _$RowInfo {
  223. factory RowInfo({
  224. required String gridId,
  225. required UnmodifiableListView<FieldInfo> fields,
  226. required RowPB rowPB,
  227. }) = _RowInfo;
  228. }
  229. typedef InsertedIndexs = List<InsertedIndex>;
  230. typedef DeletedIndexs = List<DeletedIndex>;
  231. // key: id of the row
  232. // value: UpdatedIndex
  233. typedef UpdatedIndexMap = LinkedHashMap<String, UpdatedIndex>;
  234. @freezed
  235. class RowsChangedReason with _$RowsChangedReason {
  236. const factory RowsChangedReason.insert(InsertedIndex item) = _Insert;
  237. const factory RowsChangedReason.delete(DeletedIndex item) = _Delete;
  238. const factory RowsChangedReason.update(UpdatedIndexMap indexs) = _Update;
  239. const factory RowsChangedReason.fieldDidChange() = _FieldDidChange;
  240. const factory RowsChangedReason.initial() = InitialListState;
  241. }
  242. class InsertedIndex {
  243. final int index;
  244. final String rowId;
  245. InsertedIndex({
  246. required this.index,
  247. required this.rowId,
  248. });
  249. }
  250. class DeletedIndex {
  251. final int index;
  252. final RowInfo rowInfo;
  253. DeletedIndex({
  254. required this.index,
  255. required this.rowInfo,
  256. });
  257. }
  258. class UpdatedIndex {
  259. final int index;
  260. final String rowId;
  261. UpdatedIndex({
  262. required this.index,
  263. required this.rowId,
  264. });
  265. }