DynamicPageViewController icon indicating copy to clipboard operation
DynamicPageViewController copied to clipboard

Port to C#

Open alexrainman opened this issue 10 years ago • 17 comments
trafficstars

Can you do it?

alexrainman avatar May 27 '15 20:05 alexrainman

Hi, i ported this to C# myself and it works. A few things left to the side bacause i'm not an expert on Swift and it seems are not needed on Xamarin ( init(), DMDynamicPageViewControllerDelegate protocol). I still get "Index out of bounds" if i try to delete a Child when List is empty but i can handle that. Here is the link: https://gist.github.com/alexrainman/df03bd8d03a21783237d

Take a look, and maybe you can improve it.

Thank you!

alexrainman avatar May 28 '15 15:05 alexrainman

Oh, cool! I appreciate that and hope people will find it useful. I'm not very good with C# so I cannot help much here, though.

nsobadzhiev avatar May 28 '15 20:05 nsobadzhiev

It's very useful as there's not default ViewPager on iOS/Xamarin/C# so, i hope people use it :)

alexrainman avatar May 28 '15 21:05 alexrainman

I should add a README file to the project and include a link to your port so that people are able to find it more easily. If that's OK with you, of course...

nsobadzhiev avatar May 29 '15 07:05 nsobadzhiev

Of course you can :)

alexrainman avatar May 29 '15 13:05 alexrainman

Hi Nik, i made a change to the port. CurrentPage wasn't updating so, i changed ScrollView Scrolled event to this:

[Foundation.Export ("scrollViewDidScroll:")] public void Scrolled (UIScrollView scrollView) { // Update the page when more than 50% of the previous/next page is visible var page = Math.Floor((containerScrollView.ContentOffset.X - pageWidth / 2) / pageWidth) + 1;

/*if (CurrentPage != (int)page) {
    // Check the page to avoid "index out of bounds" exception.
    if (page >= 0 && (int)page < ViewControllers.Count)
    {
        //self.notifyDelegateDidSwitchPage()
    }
}*/

// Check whether the current view controller is fully presented.
if ((int)containerScrollView.ContentOffset.X % (int)pageWidth == 0)
{
    CurrentPage = (int)page;
    //FullySwitchedPage = CurrentPage;
}

}

By the way, what "index out of bounds" code does?

alexrainman avatar Jun 01 '15 15:06 alexrainman

Hi Nik, There's something this thing needs. It needs to behave like android viewpager and recycle rendered screens. That means that only 3 screens will be rendered at a time: previous, current and next.

Can you implement it on yours so i can port it to C#?

Thanks.

alexrainman avatar Jul 07 '15 17:07 alexrainman

maybe you can take a look at this: http://stackoverflow.com/questions/14755782/releasing-unused-pages-in-uipageviewcontroller and we can work it out together :)

alexrainman avatar Jul 07 '15 17:07 alexrainman

That sounds like a nice idea!

And you know what's going to be a good way to achieve that? Those "lazy" variables that Swift inherits from functional programming. We can make the viewControllers array generate it's next/previous item "on-demand". That's going to be sweet.

However, I'm not sure you will be able to easily port to C#. Does it have similar functionality?

nsobadzhiev avatar Jul 07 '15 17:07 nsobadzhiev

C# supports Lazy<T>, i don't know if that's the equivalent. Can you give an example?

alexrainman avatar Jul 07 '15 17:07 alexrainman

I was referring to Swift lazy collections. But now that I think about it, it's not going to help us all that much. Maybe it would be better to just use a data source like the conventional UIPageViewController.

nsobadzhiev avatar Jul 08 '15 10:07 nsobadzhiev

How can i add a listener to this thing so, when i am at the last controller, if i swipe/drag from right to left, load more items?

alexrainman avatar Jul 10 '15 14:07 alexrainman

Take a look at this, it may help you with the loading and recycling views. The code seems to be complicated comparing with yours: https://github.com/nicklockwood/SwipeView

alexrainman avatar Jul 10 '15 18:07 alexrainman

I was able to load more with this code:

[Foundation.Export ("scrollViewWillBeginDragging:")] public void DraggingStarted (UIKit.UIScrollView scrollView) { _contentOffsetX = scrollView.ContentOffset.X; }

[Foundation.Export ("scrollViewDidEndDragging:willDecelerate:")] public async void DraggingEnded (UIKit.UIScrollView scrollView, bool willDecelerate) { if (_contentOffsetX < scrollView.ContentOffset.X) { } }

alexrainman avatar Jul 10 '15 19:07 alexrainman

I've pushed some changes in a new branch - dataSourceFeature. It's far from ready, but it outlines my basic idea.

nsobadzhiev avatar Jul 14 '15 16:07 nsobadzhiev

K, i will take a look, Tanke

alexrainman avatar Jul 14 '15 16:07 alexrainman

Nik,

I added this line of code to your library ViewDidLoad event to solve dragging problem when only 1 view:

containerScrollView.AlwaysBounceHorizontal = true;

I think we may create this thing inheriting from UIViewPageController that already has the view reusing functionality that we need. You may add your logic to Add/Remove views.

What do you think?

Tanke.

alexrainman avatar Jul 16 '15 19:07 alexrainman