Giter Site home page Giter Site logo

Comments (4)

fermoya avatar fermoya commented on May 17, 2024 1

I'm gonna start supporting RandomAccessCollection with Int indexes on version 1.13.0. I do the conversion to an Array internally. See version 1.13.0-beta.1

As far as I can see in your gist, this would be enough for you. Then, if you implement onPageChanged, you could call your updateCurrentIndex in this callback:

@State var page = 0 // set here the start index
@State var data: MyRandomAccessCollectionImplementation

Pager(page: $page, data: data, id: \.id) {
        // return your View here
    }
    .onPageChanged { index in
        data.updateCurrentIndex(index)
    }

from swiftuipager.

fermoya avatar fermoya commented on May 17, 2024

@TheNoim I'm a bit confused by "A way to use this without passing an array". How is Pager supposed to know what populates the pages, otherwise? What you're seeing as an argument of the view builder is not an index but a data item. This is needed to populate the page.

All in all, the way you describe Pager behavior is incorrect. In the example provided, which I think you've got from the documentation and I'm gonna rename, index it's not just a random integer but a data item. So some might want to page integers from 0 to 10, some other users might want to iterate them from 10 to 20, some others might want to iterate months as you want. This is why "A way to use this without passing an array" doesn't quite makes sense. Pager takes an data array, a binding and a view builder block. The array is used to build the page at a specific index (that is, next/previous pages). The binding allows you to keep track of the current index.

Just for further clarification:

@State var page: Int = 0
var items = Array(0..<10)

var body: some View {
    Pager(page: $page,
          data: items,
          id: \.identifier,
          content: { index in
              //
              // This isn't just an index. It's an item from data. In this example is an integer
              //
              Text("Page: \(index)")
     })
 }

What you would need to do is something like this:

// 9 as we're in October
@State var page: Int = CalendaryMonth.current.monthIndex
@State var oldPage: Int = CalendaryMonth.current.monthIndex
@State var year = 2020
let months = [CalendarMonth(.january), ..., CalendarMonth(.october), CalendarMonth(.november), CalendarMonth(.december)]

var body: some View {
    Pager(page: $page,
          data: months,
          id: \.monthIndex,
          content: { month in
              // Render month accordingly depending on the year
              CalendarViewMonth(month: month, year: year)
     })
     .loopPages()
     .onPageChanged { newPage in
         if newPage == 0 && page == 11 { year += 1 }
         if newPage == 11 && oldPage == 0 { year -= 1 }
         oldPage = newPage
    }
 }

from swiftuipager.

TheNoim avatar TheNoim commented on May 17, 2024

Pager takes an data array, a binding and a view builder block. The array is used to build the page at a specific index (that is, next/previous pages). The binding allows you to keep track of the current index.

This is not really against my post. My suggestion was more like this:
Allow to pass an object conforming to the RandomAccessCollection protocol. This would allow to define your own end and start of Indicies and even updating them dynamically. Currently, you can only pass fixed Data with the index start of zero and a known index end. It would be much cooler if the pager gets no data and the content gets rendered completely dynamic with a passed index. For an infinitive scenario, the pager doesn't need to know the length of an array. It only needs to render the last, the current and next item. It doesn't really need to know what kind of data it is or how many items the data source contains.

from swiftuipager.

fermoya avatar fermoya commented on May 17, 2024

I'm not following, could you please show me an example? Even just pseudo-code. I wanna see how you dynamically add data and change the index.

As far as I know, I think you could have a @State variable for you data and change it dynamically. This is what I do in the example code, InfiniteExampleView.swift, where I append more items on the fly. If going backwards, you could detect that (the way I was doing for year in my previous snippet) and insert more items in your array while incrementing the index (so if you append 5 previous months, then your new index is your current index plus 5).

from swiftuipager.

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.