# Advanced Usage * [Animations](#animations) * [Inserting and Deleting Cards](#inserting-and-deleting-cards) * [Swipe Recognition](#swipe-recognition) ## Animations // TODO ## Inserting and Deleting Cards If you're using an external API to retrieve your `SwipeCard` data models, chances are you'll need to update the card stack occasionally as new models come in. As of version [0.4.0](https://github.com/mac-gallagher/Shuffle/releases/tag/v0.4.0), Shuffle includes the following methods on `SwipeCardStack`: ```swift func insertCard(atIndex index: Int, position: Int) func appendCards(atIndices indices: [Int]) // Index refers to the index of the card in the data source ``` ```swift func deleteCards(atIndices indices: [Int]) func deleteCards(atPositions positions: [Int]) // Position refers to the position of the card in the stack ``` Using the insert methods in particular, we can give the illusion of an "infinite" card stack. Let's look at an example. ### External API Example Suppose we have a utility which fetches raw data models and decodes them into an array of `CardModels`: ```swift struct NetworkUtility { static func fetchNewCardModels(@escaping completion: ([CardModel]) -> ()) { // Decode network models into array of CardModels and return result in completion block } } ``` The following view controller displays a `SwipeCardStack` and holds a reference to the card models. In this example, the new models are fetched and added to the card stack after every 10 swipes: ```swift class ViewController: UIViewController: SwipeCardStackDataSource, SwipeCardStackDelegate { let cardStack = SwipeCardStack() var cardModels: [CardModel] var swipedCount: Int = 0 func viewDidLoad() { super.viewDidLoad() cardStack.dataSource = self cardStack.delegate = self // Layout cardStack on view addCards() } // MARK: SwipeCardStackDataSource func numberOfCards(in cardStack: SwipeCardStack) -> Int { return cardModels.count } func cardStack(_ cardStack: SwipeCardStack, cardForIndexAt index: Int) -> SwipeCard { let card = SwipeCard() card.model = cardModels[index] return card } // MARK: SwipeCardStackDelegate func didSwipeCard(atIndex index: Int) { swipedCount += 1 if swipedCount % 10 == 0 { addCards() } } private func addCards() { NetworkUtility.fetchNewCardModels { [weak self] newModels in guard let strongSelf = self else { return } let oldModelsCount = strongSelf.cardModels.count let newModelsCount = oldModelCount + newModels.count DispatchQueue.main.async { strongSelf.cardModels.append(contentsOf: newModels) let newIndices = Array(oldModelsCount..