From fa7097587f81e9fee72c3b21f9628c358b631819 Mon Sep 17 00:00:00 2001 From: appflowy Date: Fri, 22 Apr 2022 20:39:49 +0800 Subject: [PATCH] chore: reuse widget while inserting row --- .../application/grid/grid_service.dart | 6 ++-- .../application/grid/row/row_bloc.dart | 21 ++++------- .../application/grid/row/row_detail_bloc.dart | 10 +++--- .../application/grid/row/row_service.dart | 35 ++++++++++--------- .../plugins/grid/src/grid_page.dart | 2 +- .../grid/src/widgets/cell/cell_builder.dart | 2 +- .../grid/src/widgets/row/grid_row.dart | 31 ++++++++-------- 7 files changed, 48 insertions(+), 59 deletions(-) diff --git a/frontend/app_flowy/lib/workspace/application/grid/grid_service.dart b/frontend/app_flowy/lib/workspace/application/grid/grid_service.dart index aee2a82be4..5a3cda7b41 100644 --- a/frontend/app_flowy/lib/workspace/application/grid/grid_service.dart +++ b/frontend/app_flowy/lib/workspace/application/grid/grid_service.dart @@ -180,14 +180,14 @@ class GridRowDataDelegateAdaptor extends GridRowDataDelegate { } @override - CellDataMap buildCellDataMap(Row rowData) { + CellDataMap buildCellDataMap(String rowId, Row? rowData) { var map = CellDataMap.new(); for (final field in fields) { if (field.visibility) { map[field.id] = GridCell( - rowId: rowData.id, + rowId: rowId, gridId: _cache.gridId, - cell: rowData.cellByFieldId[field.id], + cell: rowData?.cellByFieldId[field.id], field: field, ); } diff --git a/frontend/app_flowy/lib/workspace/application/grid/row/row_bloc.dart b/frontend/app_flowy/lib/workspace/application/grid/row/row_bloc.dart index b49485c16b..213dda768f 100644 --- a/frontend/app_flowy/lib/workspace/application/grid/row/row_bloc.dart +++ b/frontend/app_flowy/lib/workspace/application/grid/row/row_bloc.dart @@ -17,19 +17,18 @@ class RowBloc extends Bloc { required GridRowCache rowCache, }) : _rowService = RowService(gridId: rowData.gridId, rowId: rowData.rowId), _rowCache = rowCache, - super(RowState.initial(rowData)) { + super(RowState.initial(rowData, rowCache.loadCellData(rowData.rowId))) { on( (event, emit) async { await event.map( initial: (_InitialRow value) async { await _startListening(); - await _loadRow(emit); }, createRow: (_CreateRow value) { _rowService.createRow(); }, didReceiveCellDatas: (_DidReceiveCellDatas value) async { - emit(state.copyWith(cellDataMap: Some(value.cellData))); + emit(state.copyWith(cellDataMap: value.cellData)); }, ); }, @@ -41,6 +40,7 @@ class RowBloc extends Bloc { if (_rowListenFn != null) { _rowCache.removeRowListener(_rowListenFn!); } + return super.close(); } @@ -51,15 +51,6 @@ class RowBloc extends Bloc { listenWhen: () => !isClosed, ); } - - Future _loadRow(Emitter emit) async { - final data = _rowCache.loadCellData(state.rowData.rowId); - data.foldRight(null, (cellDatas, _) { - if (!isClosed) { - add(RowEvent.didReceiveCellDatas(cellDatas)); - } - }); - } } @freezed @@ -73,11 +64,11 @@ class RowEvent with _$RowEvent { class RowState with _$RowState { const factory RowState({ required GridRow rowData, - required Option cellDataMap, + required CellDataMap cellDataMap, }) = _RowState; - factory RowState.initial(GridRow rowData) => RowState( + factory RowState.initial(GridRow rowData, CellDataMap cellDataMap) => RowState( rowData: rowData, - cellDataMap: none(), + cellDataMap: cellDataMap, ); } diff --git a/frontend/app_flowy/lib/workspace/application/grid/row/row_detail_bloc.dart b/frontend/app_flowy/lib/workspace/application/grid/row/row_detail_bloc.dart index 2390dc40a0..c9ff9df407 100644 --- a/frontend/app_flowy/lib/workspace/application/grid/row/row_detail_bloc.dart +++ b/frontend/app_flowy/lib/workspace/application/grid/row/row_detail_bloc.dart @@ -47,12 +47,10 @@ class RowDetailBloc extends Bloc { } Future _loadCellData() async { - final data = _rowCache.loadCellData(rowData.rowId); - data.foldRight(null, (cellDataMap, _) { - if (!isClosed) { - add(RowDetailEvent.didReceiveCellDatas(cellDataMap.values.toList())); - } - }); + final cellDataMap = _rowCache.loadCellData(rowData.rowId); + if (!isClosed) { + add(RowDetailEvent.didReceiveCellDatas(cellDataMap.values.toList())); + } } } diff --git a/frontend/app_flowy/lib/workspace/application/grid/row/row_service.dart b/frontend/app_flowy/lib/workspace/application/grid/row/row_service.dart index f524e03d0c..dbaf25188d 100644 --- a/frontend/app_flowy/lib/workspace/application/grid/row/row_service.dart +++ b/frontend/app_flowy/lib/workspace/application/grid/row/row_service.dart @@ -18,7 +18,7 @@ typedef CellDataMap = LinkedHashMap; abstract class GridRowDataDelegate { UnmodifiableListView get fields; GridRow buildGridRow(RowOrder rowOrder); - CellDataMap buildCellDataMap(Row rowData); + CellDataMap buildCellDataMap(String rowId, Row? rowData); void onFieldChanged(FieldDidUpdateCallback callback); } @@ -92,7 +92,7 @@ class GridRowCache { notify() { final row = _rowNotifier.rowDataWithId(rowId); if (row != null) { - final cellDataMap = _dataDelegate.buildCellDataMap(row); + final cellDataMap = _dataDelegate.buildCellDataMap(rowId, row); onUpdated(cellDataMap); } } @@ -115,23 +115,22 @@ class GridRowCache { _rowNotifier.removeListener(callback); } - Option loadCellData(String rowId) { + CellDataMap loadCellData(String rowId) { final Row? data = _rowNotifier.rowDataWithId(rowId); - if (data != null) { - return Some(_dataDelegate.buildCellDataMap(data)); + if (data == null) { + final payload = RowIdentifierPayload.create() + ..gridId = gridId + ..rowId = rowId; + + GridEventGetRow(payload).send().then((result) { + result.fold( + (rowData) => _rowNotifier.rowData = rowData, + (err) => Log.error(err), + ); + }); } - final payload = RowIdentifierPayload.create() - ..gridId = gridId - ..rowId = rowId; - - GridEventGetRow(payload).send().then((result) { - result.fold( - (rowData) => _rowNotifier.rowData = rowData, - (err) => Log.error(err), - ); - }); - return none(); + return _dataDelegate.buildCellDataMap(rowId, data); } void updateWithBlock(List blocks) { @@ -331,6 +330,10 @@ class GridCell with _$GridCell { required Field field, Cell? cell, }) = _GridCell; + + ValueKey key() { + return ValueKey(rowId + (cell?.fieldId ?? "")); + } } typedef InsertedIndexs = List; diff --git a/frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/grid_page.dart b/frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/grid_page.dart index 6e01d56b66..56c78195bc 100755 --- a/frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/grid_page.dart +++ b/frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/grid_page.dart @@ -213,7 +213,7 @@ class _GridRowsState extends State<_GridRows> { key: _key, initialItemCount: context.read().state.rows.length, itemBuilder: (BuildContext context, int index, Animation animation) { - final rowData = context.read().state.rows[index]; + final GridRow rowData = context.read().state.rows[index]; return _renderRow(context, rowData, animation); }, ); diff --git a/frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/cell/cell_builder.dart b/frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/cell/cell_builder.dart index 4b52cae908..df57efaff6 100755 --- a/frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/cell/cell_builder.dart +++ b/frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/cell/cell_builder.dart @@ -9,7 +9,7 @@ import 'selection_cell/selection_cell.dart'; import 'text_cell.dart'; GridCellWidget buildGridCell(GridCell cellData, {GridCellStyle? style}) { - final key = ValueKey(cellData.field.id + cellData.rowId); + final key = cellData.key(); switch (cellData.field.fieldType) { case FieldType.Checkbox: return CheckboxCell(cellData: cellData, key: key); diff --git a/frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/row/grid_row.dart b/frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/row/grid_row.dart index 1201120f37..327f29ba74 100755 --- a/frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/row/grid_row.dart +++ b/frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/row/grid_row.dart @@ -164,24 +164,21 @@ class _RowCells extends StatelessWidget { ); } - List _makeCells(Option data) { - return data.fold( - () => [], - (cellDataMap) => cellDataMap.values.map( - (cellData) { - Widget? expander; - if (cellData.field.isPrimary) { - expander = _CellExpander(onExpand: onExpand); - } + List _makeCells(CellDataMap cellDataMap) { + return cellDataMap.values.map( + (cellData) { + Widget? expander; + if (cellData.field.isPrimary) { + expander = _CellExpander(onExpand: onExpand); + } - return CellContainer( - width: cellData.field.width.toDouble(), - child: buildGridCell(cellData), - expander: expander, - ); - }, - ).toList(), - ); + return CellContainer( + width: cellData.field.width.toDouble(), + child: buildGridCell(cellData), + expander: expander, + ); + }, + ).toList(); } }