Shuffle icon indicating copy to clipboard operation
Shuffle copied to clipboard

Append card without UI interference

Open LoopingLouieX opened this issue 5 years ago • 11 comments

Hi,

from time to time I am appending some cards to the cardStack with the cardModels.append(newModel) method. As you have written in the docs i have to update the card stack occasionally. But if I update the cardstack with cardStack.reloadData() in the same moment as a user is dragging the top card in the UI i.e a little bit to the right side so that the overlay is visible (but didn't finished the swipe) then the top card catches back to the middle automatically. Is there any possibility to avoid that ? Maybe reloading the cardstack in the background without interfering the UI?

LoopingLouieX avatar Aug 04 '20 14:08 LoopingLouieX

Hi @LoopingLouieX. You should not be calling reloadData after appending your cards, as it will remove all of your swipe history. The append method on SwipeCardStack will take care of reloading the data for you.

Any call to reloadData will immediately re-fetch the data from the data source - this is intentional. It should also only be called from the main thread.

mac-gallagher avatar Aug 05 '20 04:08 mac-gallagher

Understand! Just tried that out but adding the cards in the background without disturbing the UI is still not possible. The top card is always dragged back automatically to the original position. Is there any way to avoid that ? That's really disturbing for users I think.

Edit: I mean it's understandable for me if there is just one or two cards in the stack as example. But if there's a stack with 20 cards and I append one after the last card so that 21 are in the stack then the top card get's still interrupted and the user could be irritated.

LoopingLouieX avatar Aug 05 '20 07:08 LoopingLouieX

Hey @LoopingLouieX,

I think I see the issue now. I am considering adding cardStackDidBeginAnimating and cardStackDidFinishAnimating delegate methods or something similar so that one can refresh only when the user is not interacting with the card stack. This is similar to the scrollViewDidEndScrollingAnimation: method on UIScrollViewDelegate.

Your code would look something like this:

let cardStack = SwipeCardStack()

var cardModels: [CardModel] = [cardModel1, cardModel2 cardModel3]
var cardModelsToAdd: [CardModel] = []

var isAnimating = false {
  didSet {
    if !isAnimating && !cardModelsToAdd.isEmpty {
      cardStack.appendCards(cardModelsToAdd)
      cardModelsToAdd.removeAll()
    }
  }
}

// Call to fetch new card models
func fetchNewCardModels() {
  NetworkUtility.fetchCardModels { cardModels in
    self.cardModelsToAdd.append(cardModels)
  }
}

// MARK: - SwipeCardStackDelegate

func cardStackDidBeginAnimating(_ cardStack: SwipeCardStack) {
  isAnimating = true
}

func cardStackDidEndAnimating(_ cardStack: SwipeCardStack) {
  isAnimating = false
}

Let me know if this would resolve your situation

mac-gallagher avatar Aug 13 '20 04:08 mac-gallagher

Sorry, I didn't find the cardstackdidbiginanimating method,I think I can try the way you say

LiuSky avatar Oct 12 '20 09:10 LiuSky

It works, but I don't think it's perfect。

LiuSky avatar Oct 12 '20 10:10 LiuSky

@LiuSky How did you managed to get this working? I'm also unable to find cardStackDidBeginAnimating

LoopingLouieX avatar Oct 12 '20 10:10 LoopingLouieX

截屏2020-10-13 上午9 07 44 截屏2020-10-13 上午9 11 26 截屏2020-10-13 上午9 11 44

LiuSky avatar Oct 13 '20 01:10 LiuSky

@LoopingLouieX I'll fork one if you need it

LiuSky avatar Oct 13 '20 01:10 LiuSky

@LiuSky Thanks for the help! Now I got endSwiping recognized correctly, but somehow cardStackDidBeginAnimating & cardStackDidEndAnimating is still not getting executed. Can you please so kind to fork this for me? Thanks!

LoopingLouieX avatar Oct 13 '20 19:10 LoopingLouieX

https://github.com/LiuSky/Shuffle It works, but I don't think it's perfect。I'm going to deal with dynamic logic when I'm free。@LoopingLouieX

LiuSky avatar Oct 14 '20 14:10 LiuSky

@LiuSky Thanks a lot! But it's still not perfect.. Anyway, any updates on this?

LoopingLouieX avatar Jan 04 '21 21:01 LoopingLouieX