addOrUpdateEmit method
Adds or updates an item in the list.
Implementation
@override
void addOrUpdateEmit(T item, {int index = 0}) {
final currentState = state;
if (currentState is! SmartPaginationLoaded<T>) return;
final updated = List<T>.from(currentState.allItems);
final existingIndex = updated.indexWhere((element) => element == item);
if (existingIndex != -1) {
// Update existing item - if sorting is active, may need to reposition
final order = _orders?.activeOrder;
if (order != null) {
// Remove and re-insert at correct sorted position
updated.removeAt(existingIndex);
final newIndex = _findSortedInsertIndex(updated, item, fallbackIndex: existingIndex);
updated.insert(newIndex, item);
} else {
// No sorting, just update in place
updated[existingIndex] = item;
}
} else {
// New item - use sorted insertion if sorting is active
final insertIndex = _findSortedInsertIndex(updated, item, fallbackIndex: index);
updated.insert(insertIndex, item);
}
_onInsertionCallback?.call(updated);
_refreshDataAge();
emit(
currentState.copyWith(
allItems: updated,
items: updated,
lastUpdate: DateTime.now(),
fetchedAt: _lastFetchTime,
dataExpiredAt: _getDataExpiredAt(),
),
);
}