mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
chore: cache cell data
This commit is contained in:
@ -14,10 +14,16 @@ part 'cell_service.freezed.dart';
|
|||||||
class GridCellContext {
|
class GridCellContext {
|
||||||
GridCell cellData;
|
GridCell cellData;
|
||||||
GridCellCache cellCache;
|
GridCellCache cellCache;
|
||||||
|
late GridCellCacheKey _cacheKey;
|
||||||
GridCellContext({
|
GridCellContext({
|
||||||
required this.cellData,
|
required this.cellData,
|
||||||
required this.cellCache,
|
required this.cellCache,
|
||||||
});
|
}) {
|
||||||
|
_cacheKey = GridCellCacheKey(
|
||||||
|
objectId: "$hashCode",
|
||||||
|
fieldId: cellData.field.id,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
String get gridId => cellData.gridId;
|
String get gridId => cellData.gridId;
|
||||||
|
|
||||||
@ -31,22 +37,22 @@ class GridCellContext {
|
|||||||
|
|
||||||
Field get field => cellData.field;
|
Field get field => cellData.field;
|
||||||
|
|
||||||
GridCellCacheKey get cacheKey => GridCellCacheKey(rowId: cellData.rowId, fieldId: cellData.field.id);
|
GridCellCacheKey get cacheKey => _cacheKey;
|
||||||
|
|
||||||
T? getCacheData<T>() {
|
T? getCacheData<T>() {
|
||||||
return cellCache.get(cacheKey);
|
return cellCache.get(cacheKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
void setCacheData(dynamic data) {
|
void setCacheData(dynamic data) {
|
||||||
cellCache.insert(GridCellCacheData(key: cacheKey, value: data));
|
cellCache.insert(GridCellCacheData(key: cacheKey, object: data));
|
||||||
}
|
}
|
||||||
|
|
||||||
void onFieldChanged(VoidCallback callback) {
|
void onFieldChanged(VoidCallback callback) {
|
||||||
cellCache.addListener(fieldId, rowId, callback);
|
cellCache.addListener(cacheKey, callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
void removeListener() {
|
void removeListener() {
|
||||||
cellCache.removeListener(fieldId, rowId);
|
cellCache.removeListener(cacheKey);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -55,22 +61,20 @@ typedef CellDataMap = LinkedHashMap<String, GridCell>;
|
|||||||
|
|
||||||
class GridCellCacheData {
|
class GridCellCacheData {
|
||||||
GridCellCacheKey key;
|
GridCellCacheKey key;
|
||||||
dynamic value;
|
dynamic object;
|
||||||
GridCellCacheData({
|
GridCellCacheData({
|
||||||
required this.key,
|
required this.key,
|
||||||
required this.value,
|
required this.object,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
class GridCellCacheKey {
|
class GridCellCacheKey {
|
||||||
final String fieldId;
|
final String fieldId;
|
||||||
final String rowId;
|
final String objectId;
|
||||||
GridCellCacheKey({
|
GridCellCacheKey({
|
||||||
required this.fieldId,
|
required this.fieldId,
|
||||||
required this.rowId,
|
required this.objectId,
|
||||||
});
|
});
|
||||||
|
|
||||||
String get cellId => "$rowId + $fieldId";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract class GridCellFieldDelegate {
|
abstract class GridCellFieldDelegate {
|
||||||
@ -101,18 +105,18 @@ class GridCellCache {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void addListener(String fieldId, String rowId, VoidCallback callback) {
|
void addListener(GridCellCacheKey cacheKey, VoidCallback callback) {
|
||||||
var map = _cellListenerByFieldId[fieldId];
|
var map = _cellListenerByFieldId[cacheKey.fieldId];
|
||||||
if (map == null) {
|
if (map == null) {
|
||||||
_cellListenerByFieldId[fieldId] = {};
|
_cellListenerByFieldId[cacheKey.fieldId] = {};
|
||||||
map = _cellListenerByFieldId[fieldId];
|
map = _cellListenerByFieldId[cacheKey.fieldId];
|
||||||
}
|
}
|
||||||
|
|
||||||
map![rowId] = callback;
|
map![cacheKey.objectId] = callback;
|
||||||
}
|
}
|
||||||
|
|
||||||
void removeListener(String fieldId, String rowId) {
|
void removeListener(GridCellCacheKey cacheKey) {
|
||||||
_cellListenerByFieldId[fieldId]?.remove(rowId);
|
_cellListenerByFieldId[cacheKey.fieldId]?.remove(cacheKey.objectId);
|
||||||
}
|
}
|
||||||
|
|
||||||
void insert<T extends GridCellCacheData>(T item) {
|
void insert<T extends GridCellCacheData>(T item) {
|
||||||
@ -122,7 +126,7 @@ class GridCellCache {
|
|||||||
map = _cellCacheByFieldId[item.key.fieldId];
|
map = _cellCacheByFieldId[item.key.fieldId];
|
||||||
}
|
}
|
||||||
|
|
||||||
map![item.key.cellId] = item.value;
|
map![item.key.objectId] = item.object;
|
||||||
}
|
}
|
||||||
|
|
||||||
T? get<T>(GridCellCacheKey key) {
|
T? get<T>(GridCellCacheKey key) {
|
||||||
@ -130,7 +134,7 @@ class GridCellCache {
|
|||||||
if (map == null) {
|
if (map == null) {
|
||||||
return null;
|
return null;
|
||||||
} else {
|
} else {
|
||||||
final object = map[key.cellId];
|
final object = map[key.objectId];
|
||||||
if (object is T) {
|
if (object is T) {
|
||||||
return object;
|
return object;
|
||||||
} else {
|
} else {
|
||||||
|
@ -159,7 +159,7 @@ class _RowCells extends StatelessWidget {
|
|||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return BlocBuilder<RowBloc, RowState>(
|
return BlocBuilder<RowBloc, RowState>(
|
||||||
buildWhen: (previous, current) => previous.cellDataMap != current.cellDataMap,
|
buildWhen: (previous, current) => previous.cellDataMap.length != current.cellDataMap.length,
|
||||||
builder: (context, state) {
|
builder: (context, state) {
|
||||||
return Row(
|
return Row(
|
||||||
mainAxisSize: MainAxisSize.min,
|
mainAxisSize: MainAxisSize.min,
|
||||||
|
Reference in New Issue
Block a user