chore: remove field listener in cell

This commit is contained in:
appflowy 2022-04-22 23:36:39 +08:00
parent b0f5af679c
commit 5af378c810
15 changed files with 180 additions and 110 deletions

View File

@ -168,34 +168,34 @@ void _resolveGridDeps(GetIt getIt) {
),
);
getIt.registerFactoryParam<TextCellBloc, GridCellDataContext, void>(
getIt.registerFactoryParam<TextCellBloc, GridCellContext, void>(
(context, _) => TextCellBloc(
cellDataContext: context,
cellContext: context,
),
);
getIt.registerFactoryParam<SelectionCellBloc, GridCellDataContext, void>(
getIt.registerFactoryParam<SelectionCellBloc, GridCellContext, void>(
(context, _) => SelectionCellBloc(
cellDataContext: context,
cellContext: context,
),
);
getIt.registerFactoryParam<NumberCellBloc, GridCellDataContext, void>(
getIt.registerFactoryParam<NumberCellBloc, GridCellContext, void>(
(context, _) => NumberCellBloc(
cellDataContext: context,
cellContext: context,
),
);
getIt.registerFactoryParam<DateCellBloc, GridCellDataContext, void>(
getIt.registerFactoryParam<DateCellBloc, GridCellContext, void>(
(context, _) => DateCellBloc(
cellDataContext: context,
cellContext: context,
),
);
getIt.registerFactoryParam<CheckboxCellBloc, GridCellDataContext, void>(
getIt.registerFactoryParam<CheckboxCellBloc, GridCellContext, void>(
(cellData, _) => CheckboxCellBloc(
service: CellService(),
cellDataContext: cellData,
cellContext: cellData,
),
);

View File

@ -11,10 +11,10 @@ import 'package:freezed_annotation/freezed_annotation.dart';
part 'cell_service.freezed.dart';
class GridCellDataContext {
class GridCellContext {
GridCell cellData;
GridCellCache cellCache;
GridCellDataContext({
GridCellContext({
required this.cellData,
required this.cellCache,
});
@ -30,15 +30,47 @@ class GridCellDataContext {
FieldType get fieldType => cellData.field.fieldType;
Field get field => cellData.field;
GridCellCacheKey get cacheKey => GridCellCacheKey(rowId: cellData.rowId, fieldId: cellData.field.id);
T? getCacheData<T>() {
return cellCache.get(cacheKey);
}
void setCacheData(dynamic data) {
cellCache.insert(GridCellCacheData(key: cacheKey, value: data));
}
void onFieldChanged(VoidCallback callback) {
cellCache.addListener(fieldId, rowId, callback);
}
void removeListener() {
cellCache.removeListener(fieldId, rowId);
}
}
// key: rowId
typedef CellDataMap = LinkedHashMap<String, GridCell>;
abstract class GridCellCacheData {
String get fieldId;
String get cacheKey;
dynamic get cacheData;
class GridCellCacheData {
GridCellCacheKey key;
dynamic value;
GridCellCacheData({
required this.key,
required this.value,
});
}
class GridCellCacheKey {
final String fieldId;
final String rowId;
GridCellCacheKey({
required this.fieldId,
required this.rowId,
});
String get cellId => "$rowId + $fieldId";
}
abstract class GridCellFieldDelegate {
@ -49,33 +81,56 @@ class GridCellCache {
final String gridId;
final GridCellFieldDelegate fieldDelegate;
/// fieldId: {rowId: callback}
final Map<String, Map<String, VoidCallback>> _cellListenerByFieldId = {};
/// fieldId: {cacheKey: cacheData}
final Map<String, Map<String, dynamic>> _cells = {};
final Map<String, Map<String, dynamic>> _cellCacheByFieldId = {};
GridCellCache({
required this.gridId,
required this.fieldDelegate,
}) {
fieldDelegate.onFieldChanged((fieldId) {
_cells.remove(fieldId);
_cellCacheByFieldId.remove(fieldId);
final map = _cellListenerByFieldId[fieldId];
if (map != null) {
for (final callback in map.values) {
callback();
}
}
});
}
void insert<T extends GridCellCacheData>(T cacheData) {
var map = _cells[cacheData.fieldId];
void addListener(String fieldId, String rowId, VoidCallback callback) {
var map = _cellListenerByFieldId[fieldId];
if (map == null) {
_cells[cacheData.fieldId] = {};
map = _cells[cacheData.fieldId];
_cellListenerByFieldId[fieldId] = {};
map = _cellListenerByFieldId[fieldId];
}
map![cacheData.cacheKey] = cacheData.cacheData;
map![rowId] = callback;
}
T? get<T>(String fieldId, String cacheKey) {
final map = _cells[fieldId];
void removeListener(String fieldId, String rowId) {
_cellListenerByFieldId[fieldId]?.remove(rowId);
}
void insert<T extends GridCellCacheData>(T item) {
var map = _cellCacheByFieldId[item.key.fieldId];
if (map == null) {
_cellCacheByFieldId[item.key.fieldId] = {};
map = _cellCacheByFieldId[item.key.fieldId];
}
map![item.key.cellId] = item.value;
}
T? get<T>(GridCellCacheKey key) {
final map = _cellCacheByFieldId[key.fieldId];
if (map == null) {
return null;
} else {
final object = map[cacheKey];
final object = map[key.cellId];
if (object is T) {
return object;
} else {

View File

@ -14,10 +14,10 @@ class CheckboxCellBloc extends Bloc<CheckboxCellEvent, CheckboxCellState> {
CheckboxCellBloc({
required CellService service,
required GridCellDataContext cellDataContext,
required GridCellContext cellContext,
}) : _service = service,
_cellListener = CellListener(rowId: cellDataContext.rowId, fieldId: cellDataContext.fieldId),
super(CheckboxCellState.initial(cellDataContext.cellData)) {
_cellListener = CellListener(rowId: cellContext.rowId, fieldId: cellContext.fieldId),
super(CheckboxCellState.initial(cellContext.cellData)) {
on<CheckboxCellEvent>(
(event, emit) async {
await event.map(

View File

@ -14,11 +14,11 @@ class DateCellBloc extends Bloc<DateCellEvent, DateCellState> {
final CellListener _cellListener;
final SingleFieldListener _fieldListener;
DateCellBloc({required GridCellDataContext cellDataContext})
DateCellBloc({required GridCellContext cellContext})
: _service = CellService(),
_cellListener = CellListener(rowId: cellDataContext.rowId, fieldId: cellDataContext.fieldId),
_fieldListener = SingleFieldListener(fieldId: cellDataContext.fieldId),
super(DateCellState.initial(cellDataContext.cellData)) {
_cellListener = CellListener(rowId: cellContext.rowId, fieldId: cellContext.fieldId),
_fieldListener = SingleFieldListener(fieldId: cellContext.fieldId),
super(DateCellState.initial(cellContext.cellData)) {
on<DateCellEvent>(
(event, emit) async {
event.map(

View File

@ -15,11 +15,11 @@ class NumberCellBloc extends Bloc<NumberCellEvent, NumberCellState> {
final SingleFieldListener _fieldListener;
NumberCellBloc({
required GridCellDataContext cellDataContext,
required GridCellContext cellContext,
}) : _service = CellService(),
_cellListener = CellListener(rowId: cellDataContext.rowId, fieldId: cellDataContext.fieldId),
_fieldListener = SingleFieldListener(fieldId: cellDataContext.fieldId),
super(NumberCellState.initial(cellDataContext.cellData)) {
_cellListener = CellListener(rowId: cellContext.rowId, fieldId: cellContext.fieldId),
_fieldListener = SingleFieldListener(fieldId: cellContext.fieldId),
super(NumberCellState.initial(cellContext.cellData)) {
on<NumberCellEvent>(
(event, emit) async {
await event.map(

View File

@ -1,12 +1,14 @@
import 'package:app_flowy/workspace/application/grid/cell/cell_listener.dart';
import 'package:app_flowy/workspace/application/grid/cell/cell_service.dart';
import 'package:app_flowy/workspace/application/grid/cell/select_option_service.dart';
import 'package:app_flowy/workspace/application/grid/field/field_listener.dart';
import 'dart:async';
import 'package:flowy_sdk/log.dart';
import 'package:flowy_sdk/protobuf/flowy-grid/selection_type_option.pb.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'dart:async';
import 'package:app_flowy/workspace/application/grid/cell/cell_listener.dart';
import 'package:app_flowy/workspace/application/grid/cell/cell_service.dart';
import 'package:app_flowy/workspace/application/grid/cell/select_option_service.dart';
import 'package:app_flowy/workspace/application/grid/field/field_listener.dart';
part 'selection_cell_bloc.freezed.dart';
@ -14,19 +16,21 @@ class SelectionCellBloc extends Bloc<SelectionCellEvent, SelectionCellState> {
final SelectOptionService _service;
final CellListener _cellListener;
final SingleFieldListener _fieldListener;
final GridCellContext _cellContext;
SelectionCellBloc({
required GridCellDataContext cellDataContext,
required GridCellContext cellContext,
}) : _service = SelectOptionService(),
_cellListener = CellListener(rowId: cellDataContext.rowId, fieldId: cellDataContext.fieldId),
_fieldListener = SingleFieldListener(fieldId: cellDataContext.fieldId),
super(SelectionCellState.initial(cellDataContext.cellData)) {
_cellContext = cellContext,
_cellListener = CellListener(rowId: cellContext.rowId, fieldId: cellContext.fieldId),
_fieldListener = SingleFieldListener(fieldId: cellContext.fieldId),
super(SelectionCellState.initial(cellContext.cellData)) {
on<SelectionCellEvent>(
(event, emit) async {
await event.map(
initial: (_InitialCell value) async {
_loadOptions();
_startListening();
_loadOptions();
},
didReceiveOptions: (_DidReceiveOptions value) {
emit(state.copyWith(options: value.options, selectedOptions: value.selectedOptions));
@ -40,26 +44,35 @@ class SelectionCellBloc extends Bloc<SelectionCellEvent, SelectionCellState> {
Future<void> close() async {
await _cellListener.stop();
await _fieldListener.stop();
_cellContext.removeListener();
return super.close();
}
void _loadOptions() async {
final result = await _service.getOpitonContext(
gridId: state.cellData.gridId,
fieldId: state.cellData.field.id,
rowId: state.cellData.rowId,
);
if (isClosed) {
return;
var selectOptionContext = _cellContext.getCacheData<SelectOptionContext>();
if (selectOptionContext == null) {
final result = await _service.getOpitonContext(
gridId: state.cellData.gridId,
fieldId: state.cellData.field.id,
rowId: state.cellData.rowId,
);
if (isClosed) {
return;
}
result.fold(
(newSelectOptionContext) {
_cellContext.setCacheData(newSelectOptionContext);
selectOptionContext = newSelectOptionContext;
},
(err) => Log.error(err),
);
}
result.fold(
(selectOptionContext) => add(SelectionCellEvent.didReceiveOptions(
selectOptionContext.options,
selectOptionContext.selectOptions,
)),
(err) => Log.error(err),
);
add(SelectionCellEvent.didReceiveOptions(
selectOptionContext!.options,
selectOptionContext!.selectOptions,
));
}
void _startListening() {
@ -71,13 +84,15 @@ class SelectionCellBloc extends Bloc<SelectionCellEvent, SelectionCellState> {
});
_cellListener.start();
_fieldListener.updateFieldNotifier?.addPublishListener((result) {
result.fold(
(field) => _loadOptions(),
(err) => Log.error(err),
);
});
_fieldListener.start();
_cellContext.onFieldChanged(() => _loadOptions());
// _fieldListener.updateFieldNotifier?.addPublishListener((result) {
// result.fold(
// (field) => _loadOptions(),
// (err) => Log.error(err),
// );
// });
// _fieldListener.start();
}
}

View File

@ -13,10 +13,10 @@ class TextCellBloc extends Bloc<TextCellEvent, TextCellState> {
final CellListener _cellListener;
TextCellBloc({
required GridCellDataContext cellDataContext,
required GridCellContext cellContext,
}) : _service = CellService(),
_cellListener = CellListener(rowId: cellDataContext.rowId, fieldId: cellDataContext.fieldId),
super(TextCellState.initial(cellDataContext.cellData)) {
_cellListener = CellListener(rowId: cellContext.rowId, fieldId: cellContext.fieldId),
super(TextCellState.initial(cellContext.cellData)) {
on<TextCellEvent>(
(event, emit) async {
await event.map(

View File

@ -8,22 +8,22 @@ import 'number_cell.dart';
import 'selection_cell/selection_cell.dart';
import 'text_cell.dart';
GridCellWidget buildGridCell(GridCellDataContext cellDataContext, {GridCellStyle? style}) {
final key = ValueKey(cellDataContext.cellId);
final fieldType = cellDataContext.cellData.field.fieldType;
GridCellWidget buildGridCell(GridCellContext cellContext, {GridCellStyle? style}) {
final key = ValueKey(cellContext.cellId);
final fieldType = cellContext.cellData.field.fieldType;
switch (fieldType) {
case FieldType.Checkbox:
return CheckboxCell(cellDataContext: cellDataContext, key: key);
return CheckboxCell(cellContext: cellContext, key: key);
case FieldType.DateTime:
return DateCell(cellDataContext: cellDataContext, key: key);
return DateCell(cellContext: cellContext, key: key);
case FieldType.MultiSelect:
return MultiSelectCell(cellDataContext: cellDataContext, style: style, key: key);
return MultiSelectCell(cellContext: cellContext, style: style, key: key);
case FieldType.Number:
return NumberCell(cellDataContext: cellDataContext, key: key);
return NumberCell(cellContext: cellContext, key: key);
case FieldType.RichText:
return GridTextCell(cellDataContext: cellDataContext, style: style, key: key);
return GridTextCell(cellContext: cellContext, style: style, key: key);
case FieldType.SingleSelect:
return SingleSelectCell(cellDataContext: cellDataContext, style: style, key: key);
return SingleSelectCell(cellContext: cellContext, style: style, key: key);
default:
throw UnimplementedError;
}

View File

@ -7,10 +7,10 @@ import 'package:flutter_bloc/flutter_bloc.dart';
import 'cell_builder.dart';
class CheckboxCell extends GridCellWidget {
final GridCellDataContext cellDataContext;
final GridCellContext cellContext;
CheckboxCell({
required this.cellDataContext,
required this.cellContext,
Key? key,
}) : super(key: key);
@ -23,7 +23,7 @@ class _CheckboxCellState extends State<CheckboxCell> {
@override
void initState() {
_cellBloc = getIt<CheckboxCellBloc>(param1: widget.cellDataContext)..add(const CheckboxCellEvent.initial());
_cellBloc = getIt<CheckboxCellBloc>(param1: widget.cellContext)..add(const CheckboxCellEvent.initial());
super.initState();
}

View File

@ -14,10 +14,10 @@ abstract class GridCellDelegate {
}
class DateCell extends GridCellWidget {
final GridCellDataContext cellDataContext;
final GridCellContext cellContext;
DateCell({
required this.cellDataContext,
required this.cellContext,
Key? key,
}) : super(key: key);
@ -30,7 +30,7 @@ class _DateCellState extends State<DateCell> {
@override
void initState() {
_cellBloc = getIt<DateCellBloc>(param1: widget.cellDataContext)..add(const DateCellEvent.initial());
_cellBloc = getIt<DateCellBloc>(param1: widget.cellContext)..add(const DateCellEvent.initial());
super.initState();
}

View File

@ -8,10 +8,10 @@ import 'package:flutter_bloc/flutter_bloc.dart';
import 'cell_builder.dart';
class NumberCell extends GridCellWidget {
final GridCellDataContext cellDataContext;
final GridCellContext cellContext;
NumberCell({
required this.cellDataContext,
required this.cellContext,
Key? key,
}) : super(key: key);
@ -27,7 +27,7 @@ class _NumberCellState extends State<NumberCell> {
@override
void initState() {
_cellBloc = getIt<NumberCellBloc>(param1: widget.cellDataContext)..add(const NumberCellEvent.initial());
_cellBloc = getIt<NumberCellBloc>(param1: widget.cellContext)..add(const NumberCellEvent.initial());
_controller = TextEditingController(text: _cellBloc.state.content);
_focusNode = FocusNode();
_focusNode.addListener(() {

View File

@ -18,11 +18,11 @@ class SelectOptionCellStyle extends GridCellStyle {
}
class SingleSelectCell extends GridCellWidget {
final GridCellDataContext cellDataContext;
final GridCellContext cellContext;
late final SelectOptionCellStyle? cellStyle;
SingleSelectCell({
required this.cellDataContext,
required this.cellContext,
GridCellStyle? style,
Key? key,
}) : super(key: key) {
@ -42,7 +42,7 @@ class _SingleSelectCellState extends State<SingleSelectCell> {
@override
void initState() {
_cellBloc = getIt<SelectionCellBloc>(param1: widget.cellDataContext)..add(const SelectionCellEvent.initial());
_cellBloc = getIt<SelectionCellBloc>(param1: widget.cellContext)..add(const SelectionCellEvent.initial());
super.initState();
}
@ -89,11 +89,11 @@ class _SingleSelectCellState extends State<SingleSelectCell> {
//----------------------------------------------------------------
class MultiSelectCell extends GridCellWidget {
final GridCellDataContext cellDataContext;
final GridCellContext cellContext;
late final SelectOptionCellStyle? cellStyle;
MultiSelectCell({
required this.cellDataContext,
required this.cellContext,
GridCellStyle? style,
Key? key,
}) : super(key: key) {
@ -113,7 +113,7 @@ class _MultiSelectCellState extends State<MultiSelectCell> {
@override
void initState() {
_cellBloc = getIt<SelectionCellBloc>(param1: widget.cellDataContext)..add(const SelectionCellEvent.initial());
_cellBloc = getIt<SelectionCellBloc>(param1: widget.cellContext)..add(const SelectionCellEvent.initial());
super.initState();
}

View File

@ -14,10 +14,10 @@ class GridTextCellStyle extends GridCellStyle {
}
class GridTextCell extends GridCellWidget {
final GridCellDataContext cellDataContext;
final GridCellContext cellContext;
late final GridTextCellStyle? cellStyle;
GridTextCell({
required this.cellDataContext,
required this.cellContext,
GridCellStyle? style,
Key? key,
}) : super(key: key) {
@ -41,7 +41,7 @@ class _GridTextCellState extends State<GridTextCell> {
@override
void initState() {
_cellBloc = getIt<TextCellBloc>(param1: widget.cellDataContext);
_cellBloc = getIt<TextCellBloc>(param1: widget.cellContext);
_cellBloc.add(const TextCellEvent.initial());
_controller = TextEditingController(text: _cellBloc.state.content);
_focusNode = FocusNode();

View File

@ -178,14 +178,14 @@ class _RowCells extends StatelessWidget {
expander = _CellExpander(onExpand: onExpand);
}
final cellDataContext = GridCellDataContext(
final cellContext = GridCellContext(
cellData: cellData,
cellCache: cellCache,
);
return CellContainer(
width: cellData.field.width.toDouble(),
child: buildGridCell(cellDataContext),
child: buildGridCell(cellContext),
expander: expander,
);
},

View File

@ -94,11 +94,11 @@ class _PropertyList extends StatelessWidget {
controller: _scrollController,
itemCount: state.cellDatas.length,
itemBuilder: (BuildContext context, int index) {
final cellDataContext = GridCellDataContext(
final cellContext = GridCellContext(
cellData: state.cellDatas[index],
cellCache: cellCache,
);
return _RowDetailCell(cellDataContext: cellDataContext);
return _RowDetailCell(cellContext: cellContext);
},
separatorBuilder: (BuildContext context, int index) {
return const VSpace(2);
@ -111,16 +111,16 @@ class _PropertyList extends StatelessWidget {
}
class _RowDetailCell extends StatelessWidget {
final GridCellDataContext cellDataContext;
const _RowDetailCell({required this.cellDataContext, Key? key}) : super(key: key);
final GridCellContext cellContext;
const _RowDetailCell({required this.cellContext, Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
final theme = context.watch<AppTheme>();
final cell = buildGridCell(
cellDataContext,
style: _buildCellStyle(theme, cellDataContext.fieldType),
cellContext,
style: _buildCellStyle(theme, cellContext.fieldType),
);
return SizedBox(
height: 36,
@ -130,7 +130,7 @@ class _RowDetailCell extends StatelessWidget {
children: [
SizedBox(
width: 150,
child: FieldCellButton(field: cellDataContext.field, onTap: () => _showFieldEditor(context)),
child: FieldCellButton(field: cellContext.field, onTap: () => _showFieldEditor(context)),
),
const HSpace(10),
Expanded(
@ -146,10 +146,10 @@ class _RowDetailCell extends StatelessWidget {
void _showFieldEditor(BuildContext context) {
FieldEditor(
gridId: cellDataContext.gridId,
gridId: cellContext.gridId,
fieldContextLoader: FieldContextLoaderAdaptor(
gridId: cellDataContext.gridId,
field: cellDataContext.field,
gridId: cellContext.gridId,
field: cellContext.field,
),
).show(context);
}