refresh<T> function

ThunkAction<SwipeFeedState<T>> refresh<T>({
  1. Function? onComplete,
})

Refreshes the feed, ensure null value at the end of the list

Implementation

ThunkAction<SwipeFeedState<T>> refresh<T>({Function? onComplete}) {
  return (Store<SwipeFeedState<T>> store) async {
    bool loading = store.state.loading;
    if(!loading){

      store.dispatch(_SetLoadingEvent(true));

      // Ensure one element is present inside of the list
      final showItem = SwipeFeedCardState.tower();
      final placeholder = Tuple2(null, showItem);
      store.dispatch(SetItemsEvent<T>([placeholder]));
      Store<SwipeFeedCardState> lastItem = store.state.items[0].item2;


      // Wait time for loading card to go from hiding state to show state
      // May need to be minipulated depending on the state
      // If the card is initially loading then wait 500 miliseconds
      // If the card is going from no items state to loading state after reset is called should it wait 500 ms
      await Future.delayed(Duration(milliseconds: 500)).then((value){
        lastItem.dispatch(SetSwipeFeedCardState(SwipeCardShowState()));
      });

      // Load more items
      Tuple2<List<T>, String?> loaded = await (store as Store<SwipeFeedState<T>>).state.loader(SwipeFeedState.LENGTH_INCREASE_FACTOR, null);

      // New Items Loaded
      List<T> newItems = loaded.item1;

      // Old items will be empty but just a procaution
      // This will just be the null placeholder
      // The set items event seen above ensures this
      List<Tuple2<T?, Store<SwipeFeedCardState>>> oldItems = store.state.items;


      String? pageToken = loaded.item2;

      //If there is no next page, then has more is false
      //Has to be greater then 10 to have has more not get set to false ** This has been removed
      if(pageToken == null){
        store.dispatch(_SetHasMoreEvent(false));
      }

      //Set page token
      store.dispatch(_SetPageTokenEvent(pageToken));

      /// Generate new items
      List<Tuple2<T, Store<SwipeFeedCardState>>> items =
        List<Tuple2<T, Store<SwipeFeedCardState>>>.generate(
        newItems.length, (i) => Tuple2(newItems[i], SwipeFeedCardState.tower()));

      // Shift add new items into state
      final newState = shiftAdd(oldItems, items);

      // Show first card
      if(newState[0].item1 != null && items.isNotEmpty){
        newState.firstWhere((element) => element.item1 == null).item2.dispatch(SetSwipeFeedCardState(SwipeCardHideState()));
        newState[0] = Tuple2(newState[0].item1, SwipeFeedCardState.tower(SwipeCardShowState()));
        store.dispatch(SetItemsEvent(newState));
      }
      else {
        /// Animate from loading card back to hide state
        lastItem.dispatch(SetSwipeFeedCardState(SwipeCardHideState()));

        /// Duration between animating back to hide state and displaying no items or connectivity
        await Future.delayed(Duration(milliseconds: 200));

        /// Set card back to hide state because no items loaded in
        lastItem.dispatch(SetSwipeFeedCardState(SwipeCardHideState(!store.state.connectivity ? store.state.connectivityError : store.state.noMoreItems)));
      }

      store.dispatch(_SetLoadingEvent(false));
    }

    if(onComplete != null){
      onComplete();
    }
  };
}