Giter Club home page Giter Club logo

Comments (6)

mac-gallagher avatar mac-gallagher commented on August 15, 2024 2

Hey @CJxD, this is a great question and one I have thought about quite a lot.

TL;DR - Not yet

The SwipeCardStack class is modeled after UITableView which only displays a finite amount of cells at a time. If you consider the cards as cells, this question can be rephrased as

Can you have an infinitely scrolling UITableView?

The answer is, of course, yes, but how?

The solution is Pagination. One way to paginate is to fetch your next cell's data model on a background thread whenever you run out of cells while scrolling. To update your tableview, you append the fetched data to your data model list and call reloadData on the main thread.

Note: this is not the best solution. Ideally you would pre-fetch your data (check out UITableViewDataSourcePrefetching!), but let's run with this for now.

So you might have something like this:

func cellForRow(at indexPath: IndexPath) -> UITableViewCell?
   if indexPath.row >= models.currentCount - 1 { // asking for the last cell, so we need more data soon!
      fetchMoreModels()
   }
    return YourCell(data: models[indexPath.row])
  }

Unfortunately, this approach doesn't work for SwipeCardStack. Any call to reloadData removes the swipe history, so calling reloadData would just displays the card stack as before, just with the new cards queued up.

This is a very difficult problem and one I hope we can solve eventually! It's particularly difficult when you factor in the card stack shift and undo functions.


By the way, the reason it crashes is because the SwipeCardStack tries to capture the state of your swipe history after every swipe (and upon first load). It tries to create an array of size Int.max. The card stack is actually only loads those images which it is currently displaying.

Give it a try with 10,000 cards - it should be fine (although potentially slow).

from shuffle.

CJxD avatar CJxD commented on August 15, 2024 2

from shuffle.

mac-gallagher avatar mac-gallagher commented on August 15, 2024 1

Hey @CJxD and others. Happy to announce that infinite card stacks are now supported as of version 3.0.1. Take a look at the documentation on Adding News Cards

Cheers,
Mac

from shuffle.

dfmarulanda avatar dfmarulanda commented on August 15, 2024

How did you solve the undo function when you're at the first item of the new stack of data @CJxD ?

from shuffle.

CJxD avatar CJxD commented on August 15, 2024

Hi @dfmarulanda, sorry for the slow reply.

For me I haven't actually needed this ability (I use undo to return a card back to its original position after swiping up to share), however, I would recommend using a ring buffer that's larger than your stack size by the number of times you allow undos, and reload the data when undo is called.

The data source for the stack will be all cards in between the head and tail of the buffer.

If undo is asking for a card that is not in the current stack, then the head of the buffer is moved back one and the data is reloaded.

For example, if you allow 2 undos, and your stack is of size 5, then your ring buffer is size 7:

First load:
([1], [2], [3], [4], [5], [ ], [ ])
  ^head               ^tail

Second load:
([8], [9], [10], [4], [5], [6], [7])
            ^tail           ^head

Undo 1:
([8], [9], [10], [4], [5], [6], [7])
            ^tail      ^head

Undo 2:
([8], [9], [10], [4], [5], [6], [7])
            ^tail ^head

Undo 3: not allowed, does nothing.

from shuffle.

CJxD avatar CJxD commented on August 15, 2024

Awesome!!

from shuffle.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.