mirrored_repositories/flutter_showcase/example/apps/particle_swipe
// Keeps a Dart List in sync with an AnimatedList.
// copied from https://flutter.dev/docs/catalog/samples/animated-list
// and modified to add duration

import 'package:flutter/material.dart';

class ListModel<E> {
  ListModel({
    @required this.listKey,
    @required this.removedItemBuilder,
    Iterable<E> initialItems,
  })  : assert(listKey != null),
        assert(removedItemBuilder != null),
        _items = List<E>.from(initialItems ?? <E>[]);

  final GlobalKey<AnimatedListState> listKey;
  final dynamic removedItemBuilder;
  final List<E> _items;

  AnimatedListState get _animatedList => listKey.currentState;

  void insert(int index, E item, {Duration duration}) {
    _items.insert(index, item);
    _animatedList.insertItem(index, duration: duration);
  }

  E removeAt(int index, {Duration duration}) {
    final E removedItem = _items.removeAt(index);
    if (removedItem != null) {
      _animatedList.removeItem(index,
          (BuildContext context, Animation<double> animation) {
        return removedItemBuilder(removedItem, context, animation);
      }, duration: duration);
    }
    return removedItem;
  }

  int get length => _items.length;

  E operator [](int index) => _items[index];

  int indexOf(E item) => _items.indexOf(item);
}