Giter VIP home page Giter VIP logo

Comments (17)

Narek1994 avatar Narek1994 commented on May 24, 2024

and second question, if I have a fixed data,I mean for example for 5 items, can I have an infinite logic?
becuase as I understood infinite example is really relays on infinite data, but can we achive same logic for none infinite data?

from parchment.

rechsteiner avatar rechsteiner commented on May 24, 2024

Yeah, turns out there was a bug with the way I registered the size delegate. Should be fixed in #135. Will have a new release out shortly ✌️

As for the infinite behavior you’re describing it is kind of possible. Items in Parchment have to be unique so you can’t rely solely on the title, but you can create a data source that subtracts or adds to an index and take the modulo of that index to get the title. Here's an example using the DelegateExample target:

extension ViewController: PagingViewControllerInfiniteDataSource {
  
  func pagingViewController<T>(_ pagingViewController: PagingViewController<T>, pagingItemAfterPagingItem pagingItem: T) -> T? {
    let item = pagingItem as! PagingIndexItem
    let index = item.index + 1
    let title = cities[abs(index % cities.count)]
    return PagingIndexItem(index: index, title: title) as? T
  }
  
  func pagingViewController<T>(_ pagingViewController: PagingViewController<T>, pagingItemBeforePagingItem pagingItem: T) -> T? {
    let item = pagingItem as! PagingIndexItem
    let index = item.index - 1
    let title = cities[abs(index % cities.count)]
    return PagingIndexItem(index: index, title: title) as? T
  }
  
  func pagingViewController<T>(_ pagingViewController: PagingViewController<T>, viewControllerForPagingItem pagingItem: T) -> UIViewController {
    let item = pagingItem as! PagingIndexItem
    return CityViewController(title: item.title)
  }
  
}

This will create the illusion of scrolling infinitely with just those items. The problem with this approach is that it will not update the selected cell indicator as you scroll in the header, as each item is unique. I’m not sure how to best add support for this. Either we can add a specific option for a «looping scroll mode», or support having multiple items with the same paging item. I need to get back to you on that.

from parchment.

rechsteiner avatar rechsteiner commented on May 24, 2024

Released v1.0.3 with the fix now!

from parchment.

Narek1994 avatar Narek1994 commented on May 24, 2024

First of all thanks for help, this is what I need.
in my case I dont have a scroll option in header, so its fully ok for me ) but I guess it would be nice to have that option.
Thanks a lot

from parchment.

rechsteiner avatar rechsteiner commented on May 24, 2024

Awesome! I'll create another issue for the looping scrolling 👍

from parchment.

Narek1994 avatar Narek1994 commented on May 24, 2024

Thanks, also I would suggest in this type of implementation to have an option to use already created controllers in list, I mean
func pagingViewController(_ pagingViewController: PagingViewController, viewControllerForPagingItem pagingItem: T) -> UIViewController {
let item = pagingItem as! PagingIndexItem
return controllers[item.index]
}
to not create each view controller every time
Problem I faced here, that I dont have real index of the item, item.index can be everything, even -1,-2...
Thanks

from parchment.

rechsteiner avatar rechsteiner commented on May 24, 2024

You could use the same approach as above where you take the modulo of the index:

return controllers[abs(item.index % controllers.count)]

from parchment.

Narek1994 avatar Narek1994 commented on May 24, 2024

Yea I have tried it, but its not working properly
func pagingViewController(_ pagingViewController: PagingViewController, viewControllerForPagingItem pagingItem: T) -> UIViewController {
let item = pagingItem as! PagingIndexItem
let index = item.index
print(abs(index % cities.count))
return controllers[abs(index % cities.count)]
}
in this case when I am swiping to right(to view previus one) its shows me white empty view controller, and my collectionView dataSource also shows wrong title, I wonder, do we have any logic that each PagingIndexItem should have unique ViewController?
in our example if I have a this list of titles
fileprivate let titles = [
"SHOWS",
"CHATS",
"COMICS",
"PLAYLISTS",
"CONTINUE"
]
should I select it like this in viewDidLoad
pagingViewController.select(pagingItem: PagingIndexItem(index: 0, title: "SHOWS"))
I dont get this select logic, should I set this index to be 0, or something like
pagingViewController.select(pagingItem: PagingIndexItem(index: titles.count, title: "SHOWS"))
Thanks.

from parchment.

rechsteiner avatar rechsteiner commented on May 24, 2024

The white empty view controller sounds like you are trying to navigate to the same view controller that you are currently on. I'm not sure exactly what's causing this for you, but here is an example of how you could achieve this:

class ViewController: UIViewController {
  
  let viewControllers: [UIViewController] = [
    CityViewController(title: "Oslo"),
    CityViewController(title: "Shanghai"),
    CityViewController(title: "Mumbai"),
    CityViewController(title: "Amsterdam"),
    CityViewController(title: "Sydney"),
  ]
  
  override func viewDidLoad() {
    super.viewDidLoad()
    
    let pagingViewController = PagingViewController<PagingIndexItem>()
    pagingViewController.infiniteDataSource = self
    
    addChildViewController(pagingViewController)
    view.addSubview(pagingViewController.view)
    view.constrainToEdges(pagingViewController.view)
    pagingViewController.didMove(toParentViewController: self)
    
    let item = PagingIndexItem(index: 0, title: viewControllers[0].title ?? "")
    pagingViewController.select(pagingItem: item)
  }
  
}

extension ViewController: PagingViewControllerInfiniteDataSource {
  
  func pagingViewController<T>(_ pagingViewController: PagingViewController<T>, pagingItemAfterPagingItem pagingItem: T) -> T? {
    let item = pagingItem as! PagingIndexItem
    let index = item.index + 1
    let title = viewControllers[abs(index % viewControllers.count)].title ?? ""
    return PagingIndexItem(index: index, title: title) as? T
  }
  
  func pagingViewController<T>(_ pagingViewController: PagingViewController<T>, pagingItemBeforePagingItem pagingItem: T) -> T? {
    let item = pagingItem as! PagingIndexItem
    let index = item.index - 1
    let title = viewControllers[abs(index % viewControllers.count)].title ?? ""
    return PagingIndexItem(index: index, title: title) as? T
  }
  
  func pagingViewController<T>(_ pagingViewController: PagingViewController<T>, viewControllerForPagingItem pagingItem: T) -> UIViewController {
    let item = pagingItem as! PagingIndexItem
    return viewControllers[abs(item.index % viewControllers.count)]
  }
  
}

Here I've replaced the titles array with just an array of view controllers and then use the title property for the menu items. Does that work for you?

from parchment.

Narek1994 avatar Narek1994 commented on May 24, 2024

Unfortunately no, if you try this code in that example, and change CityViewController view background color for example to black, and swipe to right, you will see that bug when it shows white screens.
I dont know how to upload recorded video here

from parchment.

rechsteiner avatar rechsteiner commented on May 24, 2024

Oh okay. I’ll take look and see if I can find a solution 👍

from parchment.

Narek1994 avatar Narek1994 commented on May 24, 2024

Its correctly working when you are swiping to left(viewing next items), and its starting to work correctly if we swipe right few items(I guess after 5 or 6 items) but when you are swiping from first item previus 6 items it has this bug

from parchment.

Narek1994 avatar Narek1994 commented on May 24, 2024

ok Thanks a lot

from parchment.

Narek1994 avatar Narek1994 commented on May 24, 2024

I just find the way to fix this, very hacky way, just putted first selected item index to be Int.max/2 ))
the problem is with -index 0 and +index,when I put selected item to be Int.max/2 I just dont have that problem with -index))

from parchment.

Narek1994 avatar Narek1994 commented on May 24, 2024

Just wanted to know,if in this case will I have increasing memory usage? or its reuse already created viewcontrollers and cells?

from parchment.

rechsteiner avatar rechsteiner commented on May 24, 2024

Ah yeah that makes sense. There shouldn’t be any increased memory usage of that approach, so that sounds like a good solution for now. The only thing you need to think about is how many view controllers you are allocating up-front by storing it in an array. But it shouldn’t be an issue with the number of view controllers you’re having.

I’ll explore some ways of making this easier in the future 👍

from parchment.

Narek1994 avatar Narek1994 commented on May 24, 2024

great, thanks a lot

from parchment.

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.