|
@@ -6,15 +6,19 @@ typedef GridDateCellContext = _GridCellContext<DateCellData, CalendarData>;
|
|
|
typedef GridURLCellContext = _GridCellContext<URLCellData, String>;
|
|
|
|
|
|
class GridCellContextBuilder {
|
|
|
- final GridCellCacheService _cellCache;
|
|
|
final GridCell _gridCell;
|
|
|
- GridCellContextBuilder({
|
|
|
- required GridCellCacheService cellCache,
|
|
|
- required GridCell gridCell,
|
|
|
- }) : _cellCache = cellCache,
|
|
|
+ final GridCellsCache _cellCache;
|
|
|
+ final GridFieldCache _fieldCache;
|
|
|
+
|
|
|
+ GridCellContextBuilder(
|
|
|
+ {required GridCell gridCell, required GridCellsCache cellCache, required GridFieldCache fieldCache})
|
|
|
+ : _cellCache = cellCache,
|
|
|
+ _fieldCache = fieldCache,
|
|
|
_gridCell = gridCell;
|
|
|
|
|
|
_GridCellContext build() {
|
|
|
+ final cellFieldNotifier = GridCellFieldNotifier(notifier: _GridFieldChangedNotifierImpl(_fieldCache));
|
|
|
+
|
|
|
switch (_gridCell.field.fieldType) {
|
|
|
case FieldType.Checkbox:
|
|
|
final cellDataLoader = GridCellDataLoader(
|
|
@@ -25,6 +29,7 @@ class GridCellContextBuilder {
|
|
|
gridCell: _gridCell,
|
|
|
cellCache: _cellCache,
|
|
|
cellDataLoader: cellDataLoader,
|
|
|
+ cellFieldNotifier: cellFieldNotifier,
|
|
|
cellDataPersistence: CellDataPersistence(gridCell: _gridCell),
|
|
|
);
|
|
|
case FieldType.DateTime:
|
|
@@ -38,18 +43,20 @@ class GridCellContextBuilder {
|
|
|
gridCell: _gridCell,
|
|
|
cellCache: _cellCache,
|
|
|
cellDataLoader: cellDataLoader,
|
|
|
+ cellFieldNotifier: cellFieldNotifier,
|
|
|
cellDataPersistence: DateCellDataPersistence(gridCell: _gridCell),
|
|
|
);
|
|
|
case FieldType.Number:
|
|
|
final cellDataLoader = GridCellDataLoader(
|
|
|
gridCell: _gridCell,
|
|
|
parser: StringCellDataParser(),
|
|
|
- config: const GridCellDataConfig(reloadOnCellChanged: true, reloadOnFieldChanged: true),
|
|
|
+ config: const GridCellDataConfig(reloadOnFieldChanged: true),
|
|
|
);
|
|
|
return GridCellContext(
|
|
|
gridCell: _gridCell,
|
|
|
cellCache: _cellCache,
|
|
|
cellDataLoader: cellDataLoader,
|
|
|
+ cellFieldNotifier: cellFieldNotifier,
|
|
|
cellDataPersistence: CellDataPersistence(gridCell: _gridCell),
|
|
|
);
|
|
|
case FieldType.RichText:
|
|
@@ -61,6 +68,7 @@ class GridCellContextBuilder {
|
|
|
gridCell: _gridCell,
|
|
|
cellCache: _cellCache,
|
|
|
cellDataLoader: cellDataLoader,
|
|
|
+ cellFieldNotifier: cellFieldNotifier,
|
|
|
cellDataPersistence: CellDataPersistence(gridCell: _gridCell),
|
|
|
);
|
|
|
case FieldType.MultiSelect:
|
|
@@ -75,6 +83,7 @@ class GridCellContextBuilder {
|
|
|
gridCell: _gridCell,
|
|
|
cellCache: _cellCache,
|
|
|
cellDataLoader: cellDataLoader,
|
|
|
+ cellFieldNotifier: cellFieldNotifier,
|
|
|
cellDataPersistence: CellDataPersistence(gridCell: _gridCell),
|
|
|
);
|
|
|
|
|
@@ -87,6 +96,7 @@ class GridCellContextBuilder {
|
|
|
gridCell: _gridCell,
|
|
|
cellCache: _cellCache,
|
|
|
cellDataLoader: cellDataLoader,
|
|
|
+ cellFieldNotifier: cellFieldNotifier,
|
|
|
cellDataPersistence: CellDataPersistence(gridCell: _gridCell),
|
|
|
);
|
|
|
}
|
|
@@ -99,14 +109,17 @@ class GridCellContextBuilder {
|
|
|
// ignore: must_be_immutable
|
|
|
class _GridCellContext<T, D> extends Equatable {
|
|
|
final GridCell gridCell;
|
|
|
- final GridCellCacheService cellCache;
|
|
|
- final _GridCellCacheKey _cacheKey;
|
|
|
- final IGridCellDataLoader<T> cellDataLoader;
|
|
|
- final _GridCellDataPersistence<D> cellDataPersistence;
|
|
|
+ final GridCellsCache _cellsCache;
|
|
|
+ final GridCellCacheKey _cacheKey;
|
|
|
final FieldService _fieldService;
|
|
|
+ final GridCellFieldNotifier _cellFieldNotifier;
|
|
|
+ // final GridCellFieldNotifier _fieldNotifier;
|
|
|
+ final IGridCellDataLoader<T> _cellDataLoader;
|
|
|
+ final IGridCellDataPersistence<D> _cellDataPersistence;
|
|
|
|
|
|
late final CellListener _cellListener;
|
|
|
late final ValueNotifier<T?>? _cellDataNotifier;
|
|
|
+
|
|
|
bool isListening = false;
|
|
|
VoidCallback? _onFieldChangedFn;
|
|
|
Timer? _loadDataOperation;
|
|
@@ -114,18 +127,25 @@ class _GridCellContext<T, D> extends Equatable {
|
|
|
|
|
|
_GridCellContext({
|
|
|
required this.gridCell,
|
|
|
- required this.cellCache,
|
|
|
- required this.cellDataLoader,
|
|
|
- required this.cellDataPersistence,
|
|
|
- }) : _fieldService = FieldService(gridId: gridCell.gridId, fieldId: gridCell.field.id),
|
|
|
- _cacheKey = _GridCellCacheKey(rowId: gridCell.rowId, fieldId: gridCell.field.id);
|
|
|
+ required GridCellsCache cellCache,
|
|
|
+ required GridCellFieldNotifier cellFieldNotifier,
|
|
|
+ required IGridCellDataLoader<T> cellDataLoader,
|
|
|
+ required IGridCellDataPersistence<D> cellDataPersistence,
|
|
|
+ // required GridFieldChangedNotifier notifierDelegate,
|
|
|
+ }) : _cellsCache = cellCache,
|
|
|
+ _cellDataLoader = cellDataLoader,
|
|
|
+ _cellDataPersistence = cellDataPersistence,
|
|
|
+ _cellFieldNotifier = cellFieldNotifier,
|
|
|
+ _fieldService = FieldService(gridId: gridCell.gridId, fieldId: gridCell.field.id),
|
|
|
+ _cacheKey = GridCellCacheKey(rowId: gridCell.rowId, fieldId: gridCell.field.id);
|
|
|
|
|
|
_GridCellContext<T, D> clone() {
|
|
|
return _GridCellContext(
|
|
|
gridCell: gridCell,
|
|
|
- cellDataLoader: cellDataLoader,
|
|
|
- cellCache: cellCache,
|
|
|
- cellDataPersistence: cellDataPersistence);
|
|
|
+ cellDataLoader: _cellDataLoader,
|
|
|
+ cellCache: _cellsCache,
|
|
|
+ cellFieldNotifier: _cellFieldNotifier,
|
|
|
+ cellDataPersistence: _cellDataPersistence);
|
|
|
}
|
|
|
|
|
|
String get gridId => gridCell.gridId;
|
|
@@ -145,10 +165,18 @@ class _GridCellContext<T, D> extends Equatable {
|
|
|
Log.error("Already started. It seems like you should call clone first");
|
|
|
return null;
|
|
|
}
|
|
|
-
|
|
|
isListening = true;
|
|
|
- _cellDataNotifier = ValueNotifier(cellCache.get(_cacheKey));
|
|
|
+
|
|
|
+ /// The cell data will be changed by two reasons:
|
|
|
+ /// 1. User edit the cell
|
|
|
+ /// 2. User edit the field
|
|
|
+ /// For example: The number cell reload the cell data that carries the format
|
|
|
+ /// user input: 12
|
|
|
+ /// cell display: $12
|
|
|
+ _cellDataNotifier = ValueNotifier(_cellsCache.get(_cacheKey));
|
|
|
_cellListener = CellListener(rowId: gridCell.rowId, fieldId: gridCell.field.id);
|
|
|
+
|
|
|
+ /// Listen on user edit event and load the new cell data if needed.
|
|
|
_cellListener.start(onCellChanged: (result) {
|
|
|
result.fold(
|
|
|
(_) => _loadData(),
|
|
@@ -156,21 +184,14 @@ class _GridCellContext<T, D> extends Equatable {
|
|
|
);
|
|
|
});
|
|
|
|
|
|
- if (cellDataLoader.config.reloadOnFieldChanged) {
|
|
|
- _onFieldChangedFn = () {
|
|
|
- _loadData();
|
|
|
- };
|
|
|
- cellCache.addFieldListener(_cacheKey, _onFieldChangedFn!);
|
|
|
- }
|
|
|
-
|
|
|
- onCellChangedFn() {
|
|
|
- onCellChanged(_cellDataNotifier?.value);
|
|
|
-
|
|
|
- if (cellDataLoader.config.reloadOnCellChanged) {
|
|
|
- _loadData();
|
|
|
- }
|
|
|
+ /// Listen on the field event and load the cell data if needed.
|
|
|
+ if (_cellDataLoader.config.reloadOnFieldChanged) {
|
|
|
+ _onFieldChangedFn = () => _loadData();
|
|
|
+ _cellFieldNotifier.addFieldListener(_cacheKey, _onFieldChangedFn!);
|
|
|
}
|
|
|
|
|
|
+ /// Notify the listener, the cell data was changed.
|
|
|
+ onCellChangedFn() => onCellChanged(_cellDataNotifier?.value);
|
|
|
_cellDataNotifier?.addListener(onCellChangedFn);
|
|
|
return onCellChangedFn;
|
|
|
}
|
|
@@ -180,7 +201,7 @@ class _GridCellContext<T, D> extends Equatable {
|
|
|
}
|
|
|
|
|
|
T? getCellData({bool loadIfNoCache = true}) {
|
|
|
- final data = cellCache.get(_cacheKey);
|
|
|
+ final data = _cellsCache.get(_cacheKey);
|
|
|
if (data == null && loadIfNoCache) {
|
|
|
_loadData();
|
|
|
}
|
|
@@ -195,13 +216,13 @@ class _GridCellContext<T, D> extends Equatable {
|
|
|
if (deduplicate) {
|
|
|
_loadDataOperation?.cancel();
|
|
|
_loadDataOperation = Timer(const Duration(milliseconds: 300), () async {
|
|
|
- final result = await cellDataPersistence.save(data);
|
|
|
+ final result = await _cellDataPersistence.save(data);
|
|
|
if (resultCallback != null) {
|
|
|
resultCallback(result);
|
|
|
}
|
|
|
});
|
|
|
} else {
|
|
|
- final result = await cellDataPersistence.save(data);
|
|
|
+ final result = await _cellDataPersistence.save(data);
|
|
|
if (resultCallback != null) {
|
|
|
resultCallback(result);
|
|
|
}
|
|
@@ -211,9 +232,9 @@ class _GridCellContext<T, D> extends Equatable {
|
|
|
void _loadData() {
|
|
|
_loadDataOperation?.cancel();
|
|
|
_loadDataOperation = Timer(const Duration(milliseconds: 10), () {
|
|
|
- cellDataLoader.loadData().then((data) {
|
|
|
+ _cellDataLoader.loadData().then((data) {
|
|
|
_cellDataNotifier?.value = data;
|
|
|
- cellCache.insert(_GridCellCacheObject(key: _cacheKey, object: data));
|
|
|
+ _cellsCache.insert(_GridCellCacheObject(key: _cacheKey, object: data));
|
|
|
});
|
|
|
});
|
|
|
}
|
|
@@ -224,11 +245,36 @@ class _GridCellContext<T, D> extends Equatable {
|
|
|
_saveDataOperation?.cancel();
|
|
|
|
|
|
if (_onFieldChangedFn != null) {
|
|
|
- cellCache.removeFieldListener(_cacheKey, _onFieldChangedFn!);
|
|
|
+ _cellFieldNotifier.removeFieldListener(_cacheKey, _onFieldChangedFn!);
|
|
|
_onFieldChangedFn = null;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@override
|
|
|
- List<Object> get props => [cellCache.get(_cacheKey) ?? "", cellId];
|
|
|
+ List<Object> get props => [_cellsCache.get(_cacheKey) ?? "", cellId];
|
|
|
+}
|
|
|
+
|
|
|
+class _GridFieldChangedNotifierImpl extends GridFieldChangedNotifier {
|
|
|
+ final GridFieldCache _cache;
|
|
|
+ FieldChangesetCallback? _onChangesetFn;
|
|
|
+
|
|
|
+ _GridFieldChangedNotifierImpl(GridFieldCache cache) : _cache = cache;
|
|
|
+
|
|
|
+ @override
|
|
|
+ void dispose() {
|
|
|
+ if (_onChangesetFn != null) {
|
|
|
+ _cache.removeListener(onChangsetListener: _onChangesetFn!);
|
|
|
+ _onChangesetFn = null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @override
|
|
|
+ void onFieldChanged(void Function(Field p1) callback) {
|
|
|
+ _onChangesetFn = (GridFieldChangeset changeset) {
|
|
|
+ for (final updatedField in changeset.updatedFields) {
|
|
|
+ callback(updatedField);
|
|
|
+ }
|
|
|
+ };
|
|
|
+ _cache.addListener(onChangeset: _onChangesetFn);
|
|
|
+ }
|
|
|
}
|