This repository has been archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
SelectableItemsViewController.cs
177 lines (152 loc) · 4.12 KB
/
SelectableItemsViewController.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
using System;
using System.Collections.Generic;
using Foundation;
using UIKit;
namespace Xamarin.Forms.Platform.iOS
{
public class SelectableItemsViewController<TItemsView> : StructuredItemsViewController<TItemsView>
where TItemsView : SelectableItemsView
{
public SelectableItemsViewController(TItemsView selectableItemsView, ItemsViewLayout layout)
: base(selectableItemsView, layout)
{
}
protected override UICollectionViewDelegateFlowLayout CreateDelegator()
{
return new SelectableItemsViewDelegator<TItemsView, SelectableItemsViewController<TItemsView>>(ItemsViewLayout, this);
}
// _Only_ called if the user initiates the selection change; will not be called for programmatic selection
public override void ItemSelected(UICollectionView collectionView, NSIndexPath indexPath)
{
FormsSelectItem(indexPath);
}
// _Only_ called if the user initiates the selection change; will not be called for programmatic selection
public override void ItemDeselected(UICollectionView collectionView, NSIndexPath indexPath)
{
FormsDeselectItem(indexPath);
}
// Called by Forms to mark an item selected
internal void SelectItem(object selectedItem)
{
var index = GetIndexForItem(selectedItem);
if (index.Section > -1 && index.Item > -1)
{
CollectionView.SelectItem(index, true, UICollectionViewScrollPosition.None);
}
}
// Called by Forms to clear the native selection
internal void ClearSelection()
{
var selectedItemIndexes = CollectionView.GetIndexPathsForSelectedItems();
foreach (var index in selectedItemIndexes)
{
CollectionView.DeselectItem(index, true);
}
}
void FormsSelectItem(NSIndexPath indexPath)
{
var mode = ItemsView.SelectionMode;
switch (mode)
{
case SelectionMode.None:
break;
case SelectionMode.Single:
ItemsView.SelectedItem = GetItemAtIndex(indexPath);
break;
case SelectionMode.Multiple:
ItemsView.SelectedItems.Add(GetItemAtIndex(indexPath));
break;
}
}
void FormsDeselectItem(NSIndexPath indexPath)
{
var mode = ItemsView.SelectionMode;
switch (mode)
{
case SelectionMode.None:
break;
case SelectionMode.Single:
break;
case SelectionMode.Multiple:
ItemsView.SelectedItems.Remove(GetItemAtIndex(indexPath));
break;
}
}
internal void UpdateNativeSelection()
{
if (ItemsView == null)
{
return;
}
var mode = ItemsView.SelectionMode;
switch (mode)
{
case SelectionMode.None:
return;
case SelectionMode.Single:
var selectedItem = ItemsView.SelectedItem;
if (selectedItem != null)
{
SelectItem(selectedItem);
}
else
{
// SelectedItem has been set to null; if an item is selected, we need to de-select it
ClearSelection();
}
return;
case SelectionMode.Multiple:
SynchronizeNativeSelectionWithSelectedItems();
break;
}
}
internal void UpdateSelectionMode()
{
var mode = ItemsView.SelectionMode;
switch (mode)
{
case SelectionMode.None:
CollectionView.AllowsSelection = false;
CollectionView.AllowsMultipleSelection = false;
break;
case SelectionMode.Single:
CollectionView.AllowsSelection = true;
CollectionView.AllowsMultipleSelection = false;
break;
case SelectionMode.Multiple:
CollectionView.AllowsSelection = true;
CollectionView.AllowsMultipleSelection = true;
break;
}
UpdateNativeSelection();
}
void SynchronizeNativeSelectionWithSelectedItems()
{
var selectedItems = ItemsView.SelectedItems;
var selectedIndexPaths = CollectionView.GetIndexPathsForSelectedItems();
foreach (var path in selectedIndexPaths)
{
var itemAtPath = GetItemAtIndex(path);
if (ShouldNotBeSelected(itemAtPath, selectedItems))
{
CollectionView.DeselectItem(path, true);
}
}
foreach (var item in selectedItems)
{
SelectItem(item);
}
}
bool ShouldNotBeSelected(object item, IList<object> selectedItems)
{
for (int n = 0; n < selectedItems.Count; n++)
{
if (selectedItems[n] == item)
{
return false;
}
}
return true;
}
}
}