updateItemEmit method

  1. @override
bool updateItemEmit(
  1. bool matcher(
    1. T item
    ),
  2. T updater(
    1. T item
    )
)
override

Updates an item in the list using a matcher and updater function. Returns true if an item was found and updated.

Implementation

@override
bool updateItemEmit(
    bool Function(T item) matcher, T Function(T item) updater) {
  final currentState = state;
  if (currentState is! SmartPaginationLoaded<T>) return false;

  final updated = List<T>.from(currentState.allItems);
  final index = updated.indexWhere(matcher);

  if (index == -1) return false;

  final updatedItem = updater(updated[index]);

  // If sorting is active, reposition the item to maintain sort order
  final order = _orders?.activeOrder;
  if (order != null) {
    updated.removeAt(index);
    final newIndex = _findSortedInsertIndex(updated, updatedItem, fallbackIndex: index);
    updated.insert(newIndex, updatedItem);
  } else {
    updated[index] = updatedItem;
  }

  _onInsertionCallback?.call(updated);

  _refreshDataAge();
  emit(
    currentState.copyWith(
      allItems: updated,
      items: updated,
      lastUpdate: DateTime.now(),
      fetchedAt: _lastFetchTime,
      dataExpiredAt: _getDataExpiredAt(),
    ),
  );
  return true;
}