From 6d385877a94c648c7c2145fa248bac9476e495c1 Mon Sep 17 00:00:00 2001 From: appflowy Date: Sun, 24 Apr 2022 17:03:00 +0800 Subject: [PATCH] fix: bugs --- .../app_flowy/lib/startup/deps_resolver.dart | 5 +- .../application/grid/cell/cell_service.dart | 59 ++++--------------- .../grid/cell/selection_cell_bloc.dart | 4 +- .../grid/cell/selection_editor_bloc.dart | 4 +- .../application/grid/cell/text_cell_bloc.dart | 16 ++--- .../grid/src/widgets/cell/cell_builder.dart | 6 +- .../cell/selection_cell/selection_cell.dart | 4 +- .../cell/selection_cell/selection_editor.dart | 4 +- 8 files changed, 28 insertions(+), 74 deletions(-) diff --git a/frontend/app_flowy/lib/startup/deps_resolver.dart b/frontend/app_flowy/lib/startup/deps_resolver.dart index e61096831c..5896d559a3 100644 --- a/frontend/app_flowy/lib/startup/deps_resolver.dart +++ b/frontend/app_flowy/lib/startup/deps_resolver.dart @@ -18,7 +18,6 @@ import 'package:flowy_sdk/protobuf/flowy-folder-data-model/app.pb.dart'; import 'package:flowy_sdk/protobuf/flowy-folder-data-model/view.pb.dart'; import 'package:flowy_sdk/protobuf/flowy-grid/date_type_option.pb.dart'; import 'package:flowy_sdk/protobuf/flowy-grid/number_type_option.pb.dart'; -import 'package:flowy_sdk/protobuf/flowy-grid/selection_type_option.pb.dart'; import 'package:flowy_sdk/protobuf/flowy-user-data-model/user_profile.pb.dart'; import 'package:get_it/get_it.dart'; @@ -169,13 +168,13 @@ void _resolveGridDeps(GetIt getIt) { ), ); - getIt.registerFactoryParam( + getIt.registerFactoryParam( (context, _) => TextCellBloc( cellContext: context, ), ); - getIt.registerFactoryParam, void>( + getIt.registerFactoryParam( (context, _) => SelectionCellBloc( cellContext: context, ), diff --git a/frontend/app_flowy/lib/workspace/application/grid/cell/cell_service.dart b/frontend/app_flowy/lib/workspace/application/grid/cell/cell_service.dart index b4d67e41ac..bcc3c2726b 100644 --- a/frontend/app_flowy/lib/workspace/application/grid/cell/cell_service.dart +++ b/frontend/app_flowy/lib/workspace/application/grid/cell/cell_service.dart @@ -10,7 +10,6 @@ import 'package:flowy_sdk/protobuf/flowy-grid/cell_entities.pb.dart'; import 'package:flowy_sdk/protobuf/flowy-grid/selection_type_option.pb.dart'; import 'package:flutter/foundation.dart'; import 'package:freezed_annotation/freezed_annotation.dart'; - import 'package:app_flowy/workspace/application/grid/cell/cell_listener.dart'; part 'cell_service.freezed.dart'; @@ -21,19 +20,22 @@ typedef GridSelectOptionCellContext = GridCellContext; class GridCellContext { final GridCell gridCell; final GridCellCache cellCache; - late GridCellCacheKey _cacheKey; + final GridCellCacheKey _cacheKey; final GridCellDataLoader cellDataLoader; final CellListener _cellListener; final CellService _cellService = CellService(); - final ValueNotifier _cellDataNotifier = ValueNotifier(null); + late final ValueNotifier _cellDataNotifier; Timer? _delayOperation; GridCellContext({ required this.gridCell, required this.cellCache, required this.cellDataLoader, - }) : _cellListener = CellListener(rowId: gridCell.rowId, fieldId: gridCell.field.id) { + }) : _cellListener = CellListener(rowId: gridCell.rowId, fieldId: gridCell.field.id), + _cacheKey = GridCellCacheKey(objectId: gridCell.rowId, fieldId: gridCell.field.id) { + _cellDataNotifier = ValueNotifier(cellCache.get(cacheKey)); + _cellListener.updateCellNotifier?.addPublishListener((result) { result.fold( (notification) => _loadData(), @@ -43,15 +45,8 @@ class GridCellContext { _cellListener.start(); - _cacheKey = GridCellCacheKey( - objectId: "$hashCode", - fieldId: gridCell.field.id, - ); - if (cellDataLoader.reloadOnFieldChanged) { - cellCache.addListener(cacheKey, () { - reloadCellData(); - }); + cellCache.addListener(cacheKey, () => reloadCellData()); } } @@ -82,7 +77,9 @@ class GridCellContext { } void saveCellData(String data) { - _cellService.updateCell(gridId: gridId, fieldId: field.id, rowId: rowId, data: data); + _cellService.updateCell(gridId: gridId, fieldId: field.id, rowId: rowId, data: data).then((result) { + result.fold((l) => null, (err) => Log.error(err)); + }); } void reloadCellData() { @@ -265,42 +262,6 @@ class CellService { } } -// class CellCache { -// final CellService _cellService; -// final HashMap _cellDataMap = HashMap(); - -// CellCache() : _cellService = CellService(); - -// Future> getCellData(GridCell identifier) async { -// final cellId = _cellId(identifier); -// final Cell? data = _cellDataMap[cellId]; -// if (data != null) { -// return Future(() => Some(data)); -// } - -// final result = await _cellService.getCell( -// gridId: identifier.gridId, -// fieldId: identifier.field.id, -// rowId: identifier.rowId, -// ); - -// return result.fold( -// (cell) { -// _cellDataMap[_cellId(identifier)] = cell; -// return Some(cell); -// }, -// (err) { -// Log.error(err); -// return none(); -// }, -// ); -// } - -// String _cellId(GridCell identifier) { -// return "${identifier.rowId}/${identifier.field.id}"; -// } -// } - @freezed class GridCell with _$GridCell { const factory GridCell({ diff --git a/frontend/app_flowy/lib/workspace/application/grid/cell/selection_cell_bloc.dart b/frontend/app_flowy/lib/workspace/application/grid/cell/selection_cell_bloc.dart index 6c1d700d9d..f1ee45546f 100644 --- a/frontend/app_flowy/lib/workspace/application/grid/cell/selection_cell_bloc.dart +++ b/frontend/app_flowy/lib/workspace/application/grid/cell/selection_cell_bloc.dart @@ -7,7 +7,7 @@ import 'package:app_flowy/workspace/application/grid/cell/cell_service.dart'; part 'selection_cell_bloc.freezed.dart'; class SelectionCellBloc extends Bloc { - final GridCellContext cellContext; + final GridSelectOptionCellContext cellContext; SelectionCellBloc({ required this.cellContext, @@ -63,7 +63,7 @@ class SelectionCellState with _$SelectionCellState { required List selectedOptions, }) = _SelectionCellState; - factory SelectionCellState.initial(GridCellContext context) { + factory SelectionCellState.initial(GridSelectOptionCellContext context) { final data = context.getCellData(); return SelectionCellState( diff --git a/frontend/app_flowy/lib/workspace/application/grid/cell/selection_editor_bloc.dart b/frontend/app_flowy/lib/workspace/application/grid/cell/selection_editor_bloc.dart index bd3eb0961e..69fa8237ef 100644 --- a/frontend/app_flowy/lib/workspace/application/grid/cell/selection_editor_bloc.dart +++ b/frontend/app_flowy/lib/workspace/application/grid/cell/selection_editor_bloc.dart @@ -10,7 +10,7 @@ part 'selection_editor_bloc.freezed.dart'; class SelectOptionEditorBloc extends Bloc { final SelectOptionService _selectOptionService; - final GridCellContext cellContext; + final GridSelectOptionCellContext cellContext; SelectOptionEditorBloc({ required this.cellContext, @@ -111,7 +111,7 @@ class SelectOptionEditorState with _$SelectOptionEditorState { required List selectedOptions, }) = _SelectOptionEditorState; - factory SelectOptionEditorState.initial(GridCellContext context) { + factory SelectOptionEditorState.initial(GridSelectOptionCellContext context) { final data = context.getCellData(); return SelectOptionEditorState( options: data?.options ?? [], diff --git a/frontend/app_flowy/lib/workspace/application/grid/cell/text_cell_bloc.dart b/frontend/app_flowy/lib/workspace/application/grid/cell/text_cell_bloc.dart index 4aa8aef027..f564a69a76 100644 --- a/frontend/app_flowy/lib/workspace/application/grid/cell/text_cell_bloc.dart +++ b/frontend/app_flowy/lib/workspace/application/grid/cell/text_cell_bloc.dart @@ -7,10 +7,10 @@ import 'cell_service.dart'; part 'text_cell_bloc.freezed.dart'; class TextCellBloc extends Bloc { - final GridCellContext cellContext; + final GridDefaultCellContext cellContext; TextCellBloc({ required this.cellContext, - }) : super(TextCellState.initial(cellContext.gridCell)) { + }) : super(TextCellState.initial(cellContext)) { on( (event, emit) async { await event.map( @@ -22,14 +22,10 @@ class TextCellBloc extends Bloc { emit(state.copyWith(content: value.text)); }, didReceiveCellData: (_DidReceiveCellData value) { - emit(state.copyWith( - cellData: value.cellData, - content: value.cellData.cell?.content ?? "", - )); + emit(state.copyWith(content: value.cellData.cell?.content ?? "")); }, didReceiveCellUpdate: (_DidReceiveCellUpdate value) { emit(state.copyWith( - cellData: state.cellData.copyWith(cell: value.cell), content: value.cell.content, )); }, @@ -65,11 +61,9 @@ class TextCellEvent with _$TextCellEvent { class TextCellState with _$TextCellState { const factory TextCellState({ required String content, - required GridCell cellData, }) = _TextCellState; - factory TextCellState.initial(GridCell cellData) => TextCellState( - content: cellData.cell?.content ?? "", - cellData: cellData, + factory TextCellState.initial(GridDefaultCellContext context) => TextCellState( + content: context.getCellData()?.content ?? "", ); } 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 2b43569d26..6a60113f62 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 @@ -21,13 +21,13 @@ GridCellWidget buildGridCellWidget(GridCell gridCell, GridCellCache cellCache, { case FieldType.DateTime: return DateCell(cellContext: cellContext, key: key); case FieldType.MultiSelect: - return MultiSelectCell(cellContext: cellContext as GridCellContext, style: style, key: key); + return MultiSelectCell(cellContext: cellContext as GridSelectOptionCellContext, style: style, key: key); case FieldType.Number: return NumberCell(cellContext: cellContext, key: key); case FieldType.RichText: return GridTextCell(cellContext: cellContext, style: style, key: key); case FieldType.SingleSelect: - return SingleSelectCell(cellContext: cellContext as GridCellContext, style: style, key: key); + return SingleSelectCell(cellContext: cellContext as GridSelectOptionCellContext, style: style, key: key); default: throw UnimplementedError; } @@ -46,7 +46,7 @@ GridCellContext makeCellContext(GridCell gridCell, GridCellCache cellCache) { ); case FieldType.MultiSelect: case FieldType.SingleSelect: - return GridCellContext( + return GridSelectOptionCellContext( gridCell: gridCell, cellCache: cellCache, cellDataLoader: SelectOptionCellDataLoader(gridCell: gridCell), diff --git a/frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/cell/selection_cell/selection_cell.dart b/frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/cell/selection_cell/selection_cell.dart index 5d1019d32c..44ab0813d1 100644 --- a/frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/cell/selection_cell/selection_cell.dart +++ b/frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/cell/selection_cell/selection_cell.dart @@ -19,7 +19,7 @@ class SelectOptionCellStyle extends GridCellStyle { } class SingleSelectCell extends GridCellWidget { - final GridCellContext cellContext; + final GridSelectOptionCellContext cellContext; late final SelectOptionCellStyle? cellStyle; SingleSelectCell({ @@ -88,7 +88,7 @@ class _SingleSelectCellState extends State { //---------------------------------------------------------------- class MultiSelectCell extends GridCellWidget { - final GridCellContext cellContext; + final GridSelectOptionCellContext cellContext; late final SelectOptionCellStyle? cellStyle; MultiSelectCell({ diff --git a/frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/cell/selection_cell/selection_editor.dart b/frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/cell/selection_cell/selection_editor.dart index fcf263256d..0379bd55dd 100644 --- a/frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/cell/selection_cell/selection_editor.dart +++ b/frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/cell/selection_cell/selection_editor.dart @@ -25,7 +25,7 @@ import 'text_field.dart'; const double _editorPannelWidth = 300; class SelectOptionCellEditor extends StatelessWidget with FlowyOverlayDelegate { - final GridCellContext cellContext; + final GridSelectOptionCellContext cellContext; final VoidCallback onDismissed; const SelectOptionCellEditor({ @@ -60,7 +60,7 @@ class SelectOptionCellEditor extends StatelessWidget with FlowyOverlayDelegate { static void show( BuildContext context, - GridCellContext cellContext, + GridSelectOptionCellContext cellContext, VoidCallback onDismissed, ) { SelectOptionCellEditor.remove(context);