fix: bugs

This commit is contained in:
appflowy 2022-04-24 17:03:00 +08:00
parent af5f42d296
commit 6d385877a9
8 changed files with 28 additions and 74 deletions

View File

@ -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-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/date_type_option.pb.dart';
import 'package:flowy_sdk/protobuf/flowy-grid/number_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:flowy_sdk/protobuf/flowy-user-data-model/user_profile.pb.dart';
import 'package:get_it/get_it.dart'; import 'package:get_it/get_it.dart';
@ -169,13 +168,13 @@ void _resolveGridDeps(GetIt getIt) {
), ),
); );
getIt.registerFactoryParam<TextCellBloc, GridCellContext, void>( getIt.registerFactoryParam<TextCellBloc, GridDefaultCellContext, void>(
(context, _) => TextCellBloc( (context, _) => TextCellBloc(
cellContext: context, cellContext: context,
), ),
); );
getIt.registerFactoryParam<SelectionCellBloc, GridCellContext<SelectOptionContext>, void>( getIt.registerFactoryParam<SelectionCellBloc, GridSelectOptionCellContext, void>(
(context, _) => SelectionCellBloc( (context, _) => SelectionCellBloc(
cellContext: context, cellContext: context,
), ),

View File

@ -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:flowy_sdk/protobuf/flowy-grid/selection_type_option.pb.dart';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:freezed_annotation/freezed_annotation.dart'; import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:app_flowy/workspace/application/grid/cell/cell_listener.dart'; import 'package:app_flowy/workspace/application/grid/cell/cell_listener.dart';
part 'cell_service.freezed.dart'; part 'cell_service.freezed.dart';
@ -21,19 +20,22 @@ typedef GridSelectOptionCellContext = GridCellContext<SelectOptionContext>;
class GridCellContext<T> { class GridCellContext<T> {
final GridCell gridCell; final GridCell gridCell;
final GridCellCache cellCache; final GridCellCache cellCache;
late GridCellCacheKey _cacheKey; final GridCellCacheKey _cacheKey;
final GridCellDataLoader<T> cellDataLoader; final GridCellDataLoader<T> cellDataLoader;
final CellListener _cellListener; final CellListener _cellListener;
final CellService _cellService = CellService(); final CellService _cellService = CellService();
final ValueNotifier<dynamic> _cellDataNotifier = ValueNotifier(null); late final ValueNotifier<T?> _cellDataNotifier;
Timer? _delayOperation; Timer? _delayOperation;
GridCellContext({ GridCellContext({
required this.gridCell, required this.gridCell,
required this.cellCache, required this.cellCache,
required this.cellDataLoader, 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) { _cellListener.updateCellNotifier?.addPublishListener((result) {
result.fold( result.fold(
(notification) => _loadData(), (notification) => _loadData(),
@ -43,15 +45,8 @@ class GridCellContext<T> {
_cellListener.start(); _cellListener.start();
_cacheKey = GridCellCacheKey(
objectId: "$hashCode",
fieldId: gridCell.field.id,
);
if (cellDataLoader.reloadOnFieldChanged) { if (cellDataLoader.reloadOnFieldChanged) {
cellCache.addListener(cacheKey, () { cellCache.addListener(cacheKey, () => reloadCellData());
reloadCellData();
});
} }
} }
@ -82,7 +77,9 @@ class GridCellContext<T> {
} }
void saveCellData(String data) { 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() { void reloadCellData() {
@ -265,42 +262,6 @@ class CellService {
} }
} }
// class CellCache {
// final CellService _cellService;
// final HashMap<String, Cell> _cellDataMap = HashMap();
// CellCache() : _cellService = CellService();
// Future<Option<Cell>> 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 @freezed
class GridCell with _$GridCell { class GridCell with _$GridCell {
const factory GridCell({ const factory GridCell({

View File

@ -7,7 +7,7 @@ import 'package:app_flowy/workspace/application/grid/cell/cell_service.dart';
part 'selection_cell_bloc.freezed.dart'; part 'selection_cell_bloc.freezed.dart';
class SelectionCellBloc extends Bloc<SelectionCellEvent, SelectionCellState> { class SelectionCellBloc extends Bloc<SelectionCellEvent, SelectionCellState> {
final GridCellContext<SelectOptionContext> cellContext; final GridSelectOptionCellContext cellContext;
SelectionCellBloc({ SelectionCellBloc({
required this.cellContext, required this.cellContext,
@ -63,7 +63,7 @@ class SelectionCellState with _$SelectionCellState {
required List<SelectOption> selectedOptions, required List<SelectOption> selectedOptions,
}) = _SelectionCellState; }) = _SelectionCellState;
factory SelectionCellState.initial(GridCellContext<SelectOptionContext> context) { factory SelectionCellState.initial(GridSelectOptionCellContext context) {
final data = context.getCellData(); final data = context.getCellData();
return SelectionCellState( return SelectionCellState(

View File

@ -10,7 +10,7 @@ part 'selection_editor_bloc.freezed.dart';
class SelectOptionEditorBloc extends Bloc<SelectOptionEditorEvent, SelectOptionEditorState> { class SelectOptionEditorBloc extends Bloc<SelectOptionEditorEvent, SelectOptionEditorState> {
final SelectOptionService _selectOptionService; final SelectOptionService _selectOptionService;
final GridCellContext<SelectOptionContext> cellContext; final GridSelectOptionCellContext cellContext;
SelectOptionEditorBloc({ SelectOptionEditorBloc({
required this.cellContext, required this.cellContext,
@ -111,7 +111,7 @@ class SelectOptionEditorState with _$SelectOptionEditorState {
required List<SelectOption> selectedOptions, required List<SelectOption> selectedOptions,
}) = _SelectOptionEditorState; }) = _SelectOptionEditorState;
factory SelectOptionEditorState.initial(GridCellContext<SelectOptionContext> context) { factory SelectOptionEditorState.initial(GridSelectOptionCellContext context) {
final data = context.getCellData(); final data = context.getCellData();
return SelectOptionEditorState( return SelectOptionEditorState(
options: data?.options ?? [], options: data?.options ?? [],

View File

@ -7,10 +7,10 @@ import 'cell_service.dart';
part 'text_cell_bloc.freezed.dart'; part 'text_cell_bloc.freezed.dart';
class TextCellBloc extends Bloc<TextCellEvent, TextCellState> { class TextCellBloc extends Bloc<TextCellEvent, TextCellState> {
final GridCellContext cellContext; final GridDefaultCellContext cellContext;
TextCellBloc({ TextCellBloc({
required this.cellContext, required this.cellContext,
}) : super(TextCellState.initial(cellContext.gridCell)) { }) : super(TextCellState.initial(cellContext)) {
on<TextCellEvent>( on<TextCellEvent>(
(event, emit) async { (event, emit) async {
await event.map( await event.map(
@ -22,14 +22,10 @@ class TextCellBloc extends Bloc<TextCellEvent, TextCellState> {
emit(state.copyWith(content: value.text)); emit(state.copyWith(content: value.text));
}, },
didReceiveCellData: (_DidReceiveCellData value) { didReceiveCellData: (_DidReceiveCellData value) {
emit(state.copyWith( emit(state.copyWith(content: value.cellData.cell?.content ?? ""));
cellData: value.cellData,
content: value.cellData.cell?.content ?? "",
));
}, },
didReceiveCellUpdate: (_DidReceiveCellUpdate value) { didReceiveCellUpdate: (_DidReceiveCellUpdate value) {
emit(state.copyWith( emit(state.copyWith(
cellData: state.cellData.copyWith(cell: value.cell),
content: value.cell.content, content: value.cell.content,
)); ));
}, },
@ -65,11 +61,9 @@ class TextCellEvent with _$TextCellEvent {
class TextCellState with _$TextCellState { class TextCellState with _$TextCellState {
const factory TextCellState({ const factory TextCellState({
required String content, required String content,
required GridCell cellData,
}) = _TextCellState; }) = _TextCellState;
factory TextCellState.initial(GridCell cellData) => TextCellState( factory TextCellState.initial(GridDefaultCellContext context) => TextCellState(
content: cellData.cell?.content ?? "", content: context.getCellData()?.content ?? "",
cellData: cellData,
); );
} }

View File

@ -21,13 +21,13 @@ GridCellWidget buildGridCellWidget(GridCell gridCell, GridCellCache cellCache, {
case FieldType.DateTime: case FieldType.DateTime:
return DateCell(cellContext: cellContext, key: key); return DateCell(cellContext: cellContext, key: key);
case FieldType.MultiSelect: case FieldType.MultiSelect:
return MultiSelectCell(cellContext: cellContext as GridCellContext<SelectOptionContext>, style: style, key: key); return MultiSelectCell(cellContext: cellContext as GridSelectOptionCellContext, style: style, key: key);
case FieldType.Number: case FieldType.Number:
return NumberCell(cellContext: cellContext, key: key); return NumberCell(cellContext: cellContext, key: key);
case FieldType.RichText: case FieldType.RichText:
return GridTextCell(cellContext: cellContext, style: style, key: key); return GridTextCell(cellContext: cellContext, style: style, key: key);
case FieldType.SingleSelect: case FieldType.SingleSelect:
return SingleSelectCell(cellContext: cellContext as GridCellContext<SelectOptionContext>, style: style, key: key); return SingleSelectCell(cellContext: cellContext as GridSelectOptionCellContext, style: style, key: key);
default: default:
throw UnimplementedError; throw UnimplementedError;
} }
@ -46,7 +46,7 @@ GridCellContext makeCellContext(GridCell gridCell, GridCellCache cellCache) {
); );
case FieldType.MultiSelect: case FieldType.MultiSelect:
case FieldType.SingleSelect: case FieldType.SingleSelect:
return GridCellContext<SelectOptionContext>( return GridSelectOptionCellContext(
gridCell: gridCell, gridCell: gridCell,
cellCache: cellCache, cellCache: cellCache,
cellDataLoader: SelectOptionCellDataLoader(gridCell: gridCell), cellDataLoader: SelectOptionCellDataLoader(gridCell: gridCell),

View File

@ -19,7 +19,7 @@ class SelectOptionCellStyle extends GridCellStyle {
} }
class SingleSelectCell extends GridCellWidget { class SingleSelectCell extends GridCellWidget {
final GridCellContext<SelectOptionContext> cellContext; final GridSelectOptionCellContext cellContext;
late final SelectOptionCellStyle? cellStyle; late final SelectOptionCellStyle? cellStyle;
SingleSelectCell({ SingleSelectCell({
@ -88,7 +88,7 @@ class _SingleSelectCellState extends State<SingleSelectCell> {
//---------------------------------------------------------------- //----------------------------------------------------------------
class MultiSelectCell extends GridCellWidget { class MultiSelectCell extends GridCellWidget {
final GridCellContext<SelectOptionContext> cellContext; final GridSelectOptionCellContext cellContext;
late final SelectOptionCellStyle? cellStyle; late final SelectOptionCellStyle? cellStyle;
MultiSelectCell({ MultiSelectCell({

View File

@ -25,7 +25,7 @@ import 'text_field.dart';
const double _editorPannelWidth = 300; const double _editorPannelWidth = 300;
class SelectOptionCellEditor extends StatelessWidget with FlowyOverlayDelegate { class SelectOptionCellEditor extends StatelessWidget with FlowyOverlayDelegate {
final GridCellContext<SelectOptionContext> cellContext; final GridSelectOptionCellContext cellContext;
final VoidCallback onDismissed; final VoidCallback onDismissed;
const SelectOptionCellEditor({ const SelectOptionCellEditor({
@ -60,7 +60,7 @@ class SelectOptionCellEditor extends StatelessWidget with FlowyOverlayDelegate {
static void show( static void show(
BuildContext context, BuildContext context,
GridCellContext<SelectOptionContext> cellContext, GridSelectOptionCellContext cellContext,
VoidCallback onDismissed, VoidCallback onDismissed,
) { ) {
SelectOptionCellEditor.remove(context); SelectOptionCellEditor.remove(context);