This repository has been archived by the owner on May 1, 2024. It is now read-only.
/
CollectionViewRenderer.cs
184 lines (153 loc) · 5.04 KB
/
CollectionViewRenderer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using System;
using System.Linq;
using Foundation;
using UIKit;
namespace Xamarin.Forms.Platform.iOS
{
public class CarouselViewRenderer
{
public CarouselViewRenderer()
{
CollectionView.VerifyCollectionViewFlagEnabled(nameof(CarouselViewRenderer));
}
}
//// TODO hartez 2018/05/30 09:05:38 Think about whether this Controller and/or the new Adapter should be internal or public
public class CollectionViewRenderer : ViewRenderer<CollectionView, UIView>
{
CollectionViewController _collectionViewController;
ItemsViewLayout _layout;
bool _disposed;
public CollectionViewRenderer()
{
CollectionView.VerifyCollectionViewFlagEnabled(nameof(CollectionViewRenderer));
}
public override UIViewController ViewController => _collectionViewController;
public override SizeRequest GetDesiredSize(double widthConstraint, double heightConstraint)
{
return Control.GetSizeRequest(widthConstraint, heightConstraint, 0, 0);
}
protected override void OnElementChanged(ElementChangedEventArgs<CollectionView> e)
{
TearDownOldElement(e.OldElement);
SetUpNewElement(e.NewElement);
base.OnElementChanged(e);
}
protected virtual ItemsViewLayout SelectLayout(IItemsLayout layoutSpecification)
{
if (layoutSpecification is GridItemsLayout gridItemsLayout)
{
return new GridViewLayout(gridItemsLayout);
}
if (layoutSpecification is ListItemsLayout listItemsLayout)
{
return new ListViewLayout(listItemsLayout);
}
// Fall back to vertical list
return new ListViewLayout(new ListItemsLayout(ItemsLayoutOrientation.Vertical));
}
void TearDownOldElement(CollectionView oldElement)
{
if (oldElement == null)
{
return;
}
// Stop listening for ScrollTo requests
oldElement.ScrollToRequested -= ScrollToRequested;
}
void SetUpNewElement(CollectionView newElement)
{
if (newElement == null)
{
return;
}
_layout = SelectLayout(newElement.ItemsLayout);
_collectionViewController = new CollectionViewController(newElement, _layout);
SetNativeControl(_collectionViewController.View);
_collectionViewController.CollectionView.BackgroundColor = UIColor.Clear;
_collectionViewController.CollectionView.Delegate = _layout;
// Listen for ScrollTo requests
newElement.ScrollToRequested += ScrollToRequested;
}
NSIndexPath DetermineIndex(ScrollToRequestEventArgs args)
{
if (args.Mode == ScrollToMode.Position)
{
// TODO hartez 2018/09/17 16:42:54 This will need to be overridden to account for grouping
// TODO hartez 2018/09/17 16:21:19 Handle LTR
return NSIndexPath.Create(0, args.Index);
}
return _collectionViewController.GetIndexForItem(args.Item);
}
void ScrollToRequested(object sender, ScrollToRequestEventArgs args)
{
var indexPath = DetermineIndex(args);
if (indexPath.Row < 0 || indexPath.Section < 0)
{
// Nothing found, nowhere to scroll to
return;
}
_collectionViewController.CollectionView.ScrollToItem(indexPath,
args.ScrollToPosition.ToCollectionViewScrollPosition(_layout.ScrollDirection),
args.IsAnimated);
}
protected override void Dispose(bool disposing)
{
if (_disposed)
{
return;
}
_disposed = true;
if (disposing)
{
if (Element != null)
{
TearDownOldElement(Element);
}
}
base.Dispose(disposing);
}
}
public static class ScrollToPositionExtensions
{
public static UICollectionViewScrollPosition ToCollectionViewScrollPosition(this ScrollToPosition scrollToPosition,
UICollectionViewScrollDirection scrollDirection = UICollectionViewScrollDirection.Vertical, bool isLtr = false)
{
CollectionView.VerifyCollectionViewFlagEnabled();
if (scrollDirection == UICollectionViewScrollDirection.Horizontal)
{
return scrollToPosition.ToHorizontalCollectionViewScrollPosition(isLtr);
}
return scrollToPosition.ToVerticalCollectionViewScrollPosition();
}
public static UICollectionViewScrollPosition ToHorizontalCollectionViewScrollPosition(this ScrollToPosition scrollToPosition, bool isLtr)
{
CollectionView.VerifyCollectionViewFlagEnabled();
switch (scrollToPosition)
{
case ScrollToPosition.MakeVisible:
case ScrollToPosition.Start:
return isLtr ? UICollectionViewScrollPosition.Right : UICollectionViewScrollPosition.Left;
case ScrollToPosition.End:
return isLtr ? UICollectionViewScrollPosition.Left : UICollectionViewScrollPosition.Right;
case ScrollToPosition.Center:
default:
return UICollectionViewScrollPosition.CenteredHorizontally;
}
}
public static UICollectionViewScrollPosition ToVerticalCollectionViewScrollPosition(this ScrollToPosition scrollToPosition)
{
CollectionView.VerifyCollectionViewFlagEnabled();
switch (scrollToPosition)
{
case ScrollToPosition.MakeVisible:
case ScrollToPosition.Start:
return UICollectionViewScrollPosition.Top;
case ScrollToPosition.End:
return UICollectionViewScrollPosition.Bottom;
case ScrollToPosition.Center:
default:
return UICollectionViewScrollPosition.CenteredVertically;
}
}
}
}