loadMore<T> function
Implementation
ThunkAction<SwipeFeedState<T>> loadMore<T>() {
return (Store<SwipeFeedState<T>> store) async {
// Ensure not loading
if(!store.state.loading && store.state.hasMore){
// Loading
store.dispatch(_SetLoadingEvent(true));
// Add PlaceHolder Card at the end of the list
bool wasEmpty = store.state.items.isEmpty;
bool wasLast = store.state.items.isNotEmpty && store.state.items[0].item1 == null;
Tower<SwipeFeedCardState>? showItem;
var placeholder;
// Add last card if it does not already exsist
// This should never be ran but is an ensurance
if(wasEmpty || store.state.items.last.item1 != null){
showItem = SwipeFeedCardState.tower();
placeholder = Tuple2<T?, Store<SwipeFeedCardState>>(null, showItem);
store.dispatch(SetItemsEvent<T>([...store.state.items, placeholder]));
}
else if(wasLast){
// If load more is called on the last item in the list then dispatch loading state
store.state.items[0].item2.dispatch(SetSwipeFeedCardState(SwipeCardShowState()));
}
// Load More Items
Tuple2<List<T>, String?> loaded = await store.state.loader(SwipeFeedState.LENGTH_INCREASE_FACTOR, store.state.pageToken);
// New items
List<T> newItems = loaded.item1;
// Old items will not be empty here and could have more then just the null value
// This differs from the refresh function
List<Tuple2<T?, Store<SwipeFeedCardState>>> oldItems = store.state.items;
// Page token
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
if(pageToken == null){
store.dispatch(_SetHasMoreEvent(false));
}
// Set the 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 the new items to the list to ensure the null value is still present
store.dispatch(SetItemsEvent(shiftAdd(oldItems, items)));
if(wasLast){
store.state.items[0].item2.dispatch(SetSwipeFeedCardState(SwipeCardShowState()));
}
store.dispatch(_SetLoadingEvent(false));
}
};
}