row_cache.dart 9.0 KB

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