removeItem<T> function

ThunkAction<SwipeFeedState<T>> removeItem<T>([
  1. AdjustList<T>? then
])

Removes a card from the list, do not use when the card is being swiped

Implementation

ThunkAction<SwipeFeedState<T>> removeItem<T>([AdjustList<T>? then]){
  return (Store<SwipeFeedState<T>> store) async {

    // State
    var items = [...store.state.items];

    if(items.isNotEmpty && items.length > 1){
      // The item that is about to be removed
      // Set it to hide state
      items[0].item2.dispatch(SetSwipeFeedCardState(SwipeCardHideState()));

      // How long it takes for the current card being remove to enter hide state
      // Do we need this when you remove the last item??
      await Future.delayed(Duration(milliseconds: 400));

      if(!items.isEmpty || items[0].item1 != null){
        items = [
          items[0],
          // Sublist that is added into the state after the current item is removed
          ...((then?.call(items.sublist(1))) ?? items.sublist(1)),
          Tuple2(null, SwipeFeedCardState.tower())
        ];

        assert(items.isNotEmpty);

        if(items.length > 1){
          items[0] = Tuple2(items[1].item1, items[0].item2);
          items.removeAt(1);
        }

        // Set new items
        store.dispatch(SetItemsEvent(items));
      }

      //Maximizes the card
      //Duration before the next card is shown
      //This works in correlation with "one animation to rule them all"
      await Future.delayed(Duration(milliseconds: 400)).then((value){
        if(store.state.items[0].item1 == null){
          store.state.items[0].item2.dispatch(
            SetSwipeFeedCardState(SwipeCardHideState(!store.state.connectivity ? store.state.connectivityError : store.state.noMoreItems))
          );
        }
        else{
          store.state.items[0].item2.dispatch(SetSwipeFeedCardState(SwipeCardShowState()));
        }
      });
    }
  };
}