-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathTMDropDown.xaml.cs
More file actions
395 lines (344 loc) · 12.7 KB
/
Copy pathTMDropDown.xaml.cs
File metadata and controls
395 lines (344 loc) · 12.7 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
using System.Collections;
using System.Windows.Input;
using Trimble.Modus.Components.Helpers;
using Trimble.Modus.Components.Popup.Services;
namespace Trimble.Modus.Components;
public partial class TMDropDown : ContentView
{
#region Private fields
private double desiredHeight;
//private Label label;
//private Border innerBorder;
//private StackLayout indicatorButton;
private IEnumerable items;
#if WINDOWS
private Thickness margin = new Thickness(0, 154, 0, 0);
#else
private Thickness margin = new Thickness(0, 128, 0, 0);
#endif
private uint AnimationDuration { get; set; } = 250;
private object previousSelection;
#endregion
#region Public Properties
public event EventHandler<DropDownSelectionChangedEventArgs> SelectionChanged;
public IEnumerable ItemsSource
{
get { return (IEnumerable)GetValue(ItemsSourceProperty); }
set { SetValue(ItemsSourceProperty, value); }
}
/// <summary>
/// Gets or sets the title text
/// </summary>
public string TitleText
{
get => (string)GetValue(TitleTextProperty);
set => SetValue(TitleTextProperty, value);
}
/// <summary>
/// Gets or sets the required property
/// </summary>
public bool IsRequired
{
get => (bool)GetValue(IsRequiredProperty);
set => SetValue(IsRequiredProperty, value);
}
public object SelectedItem
{
get => GetValue(SelectedItemProperty);
set => SetValue(SelectedItemProperty, value);
}
internal new Color BackgroundColor
{
get => (Color)GetValue(BackgroundColorProperty);
set => SetValue(BackgroundColorProperty, value);
}
internal Color BorderColor
{
get => (Color)GetValue(BackgroundColorProperty);
set => SetValue(BackgroundColorProperty, value);
}
internal Color TextAndIconColor
{
get => (Color)GetValue(BackgroundColorProperty);
set => SetValue(BackgroundColorProperty, value);
}
public new double WidthRequest
{
get { return (double)GetValue(WidthRequestProperty); }
set { SetValue(WidthRequestProperty, value); }
}
public int SelectedIndex
{
get { return (int)GetValue(SelectedIndexProperty); }
set { SetValue(SelectedIndexProperty, value); }
}
public ICommand SelectionChangedCommand
{
get => (ICommand)GetValue(SelectionChangedCommandProperty);
set => SetValue(SelectionChangedCommandProperty, value);
}
#endregion
#region Bindable Properties
public static readonly BindableProperty SelectedIndexProperty =
BindableProperty.Create(nameof(SelectedIndex), typeof(int), typeof(TMDropDown), -1, propertyChanged: OnSelectedIndexChanged);
public new static readonly BindableProperty WidthRequestProperty =
BindableProperty.Create(nameof(WidthRequest), typeof(double), typeof(TMDropDown), 240.0, propertyChanged: OnWidthRequestChanged);
public static readonly BindableProperty ItemsSourceProperty =
BindableProperty.Create(nameof(ItemsSource), typeof(IEnumerable), typeof(TMDropDown), null, propertyChanged: OnItemsSourceChanged);
/// <summary>
/// Gets or sets the text for the title label in the control
/// </summary>
public static readonly BindableProperty TitleTextProperty =
BindableProperty.Create(nameof(TitleText), typeof(string), typeof(TMDropDown), null);
/// <summary>
/// Gets or sets the required boolean for the label in the control
/// </summary>
public static readonly BindableProperty IsRequiredProperty =
BindableProperty.Create(nameof(IsRequired), typeof(bool), typeof(TMDropDown), false);
public static readonly BindableProperty SelectedItemProperty =
BindableProperty.Create(nameof(SelectedItem), typeof(object), typeof(TMDropDown), null, propertyChanged: OnSelectedItemChanged);
public static readonly BindableProperty SelectionChangedCommandProperty =
BindableProperty.Create(nameof(SelectionChangedCommand), typeof(ICommand), typeof(TMDropDown));
public new static readonly BindableProperty BackgroundColorProperty =
BindableProperty.Create(nameof(BackgroundColor), typeof(Color), typeof(TMDropDown), Colors.Transparent, BindingMode.Default, null, propertyChanged: OnBackgroundColorPropertyChanged);
public static readonly BindableProperty BorderColorProperty =
BindableProperty.Create(nameof(BorderColor), typeof(Color), typeof(TMDropDown), Colors.Transparent, BindingMode.Default, null, propertyChanged: OnBorderColorPropertyChanged);
public static readonly BindableProperty TextAndIconColorProperty =
BindableProperty.Create(nameof(TextAndIconColor), typeof(Color), typeof(TMDropDown), Colors.Transparent, BindingMode.Default, null, propertyChanged: OnTextAndIconColorPropertyChanged);
#endregion
#region Constructor
public TMDropDown()
{
SelectedItem = new object { };
previousSelection = null;
InitializeComponent();
this.SetDynamicResource(StyleProperty, "DefaultStyle");
}
#endregion
#region Private Methods
private static void UpdateSelectedIndexBasedOnSelectedItem(TMDropDown dropDown)
{
var index = dropDown.items.Cast<object>().ToList().IndexOf(dropDown.SelectedItem);
if (index != dropDown.SelectedIndex)
{
dropDown.SelectedIndex = index;
}
}
private static void UpdateSelectedItemBasedOnSelectedIndex(TMDropDown dropDown)
{
var list = dropDown.items.Cast<object>().ToList();
if (list.Count > 0 && dropDown.SelectedIndex >= 0)
{
var selectedItem = list[dropDown.SelectedIndex];
if (selectedItem != dropDown.SelectedItem)
{
dropDown.SelectedItem = selectedItem;
}
}
}
private static void OnTextAndIconColorPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
if (bindable is TMDropDown dropDown)
{
dropDown.label.TextColor = (Color)newValue;
}
}
private static void OnSelectedItemChanged(BindableObject bindable, object oldValue, object newValue)
{
if (bindable is TMDropDown dropDown && dropDown.ItemsSource != null)
{
UpdateSelectedIndexBasedOnSelectedItem(dropDown);
}
}
private static void OnBorderColorPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
if (bindable is TMDropDown dropDown)
{
dropDown.innerBorder.Stroke = (Color)newValue;
}
}
private static void OnBackgroundColorPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
if (bindable is TMDropDown dropDown)
{
dropDown.innerBorder.BackgroundColor = (Color)newValue;
}
}
private void OnSelected(object sender, SelectedItemChangedEventArgs e)
{
previousSelection = SelectedItem;
var listView = (ListView)sender;
var newItem = e.SelectedItem;
var newIndex = e.SelectedItemIndex;
// Always update (even if newItem is null – that represents clearing)
SelectedItem = newItem;
SelectedIndex = newIndex;
RaiseSelectionChangedEvent(previousSelection, newIndex);
UpdateCellColor(listView);
label.Text = newItem?.ToString() ?? string.Empty;
if (PopupService.Instance.PopupStack.Count > 0)
{
PopupService.Instance?.DismissAsync();
Close();
}
}
private async void Close()
{
await Task.WhenAll(
indicatorButton.RotateTo(0, AnimationDuration)
);
}
DropDownContents dropDownContents;
private bool _isLoading;
private async void Open()
{
if (!_isLoading)
{
_isLoading = true;
var locationFetcher = new LocationFetcher();
var loc = locationFetcher.GetCoordinates(this);
var height = Application.Current.MainPage.Window.Height;
dropDownContents = new DropDownContents(innerBorder, Enums.ModalPosition.Bottom)
{
ItemSource = this.ItemsSource,
SelectedIndex = this.SelectedIndex,
SelectedItem = this.SelectedItem,
Margin = margin,
DesiredHeight = desiredHeight,
DesiredWidth = innerBorder.Width,
SelectedEventHandler = OnSelected,
YPosition = loc.Y,
Height = height,
ChangeIndicatorWhenPopupRemove = OnPopupRemoved
};
dropDownContents.Build();
await Task.WhenAll(
indicatorButton.RotateTo(-180, AnimationDuration)
);
await PopupService.Instance?.PresentAsync(dropDownContents, true);
dropDownContents.dropDownListView.ScrollTo(dropDownContents.dropDownListView.SelectedItem, ScrollToPosition.End, true);
_isLoading = false;
}
}
private void OnPopupRemoved()
{
Close();
}
private void RaiseSelectionChangedEvent(object previousSelection, int index)
{
DropDownSelectionChangedEventArgs args = new DropDownSelectionChangedEventArgs
{
PreviousSelection = previousSelection,
CurrentSelection = SelectedItem,
SelectedIndex = index
};
SelectionChanged?.Invoke(this, args);
SelectionChangedCommand?.Execute(args);
}
private static void OnItemsSourceChanged(BindableObject bindable, object oldValue, object newValue)
{
if (bindable is TMDropDown tmDropDown)
{
tmDropDown.items = newValue as IEnumerable;
tmDropDown.UpdateSelectedItemValue(tmDropDown);
tmDropDown.UpdateListBorderHeight(tmDropDown.items);
}
}
private static void OnSelectedIndexChanged(BindableObject bindable, object oldValue, object newValue)
{
if (bindable is TMDropDown dropDown)
{
dropDown.UpdateSelectedItemText(dropDown);
}
}
private static void OnWidthRequestChanged(BindableObject bindable, object oldValue, object newValue)
{
if (bindable is TMDropDown dropDown && dropDown != null)
{
dropDown.innerBorder.WidthRequest = (double)newValue;
}
}
private void UpdateSelectedItemText(TMDropDown dropDown)
{
if (dropDown.SelectedIndex >= 0 && dropDown.SelectedItem != null)
{
label.Text = items.Cast<object>().ElementAt(dropDown.SelectedIndex).ToString();
}
else
{
label.Text = string.Empty;
}
}
private void UpdateSelectedItemValue(TMDropDown dropDown)
{
if (dropDown.items != null)
{
UpdateSelectedIndexBasedOnSelectedItem(dropDown);
UpdateSelectedItemBasedOnSelectedIndex(dropDown);
UpdateSelectedItemText(dropDown);
}
}
private void UpdateListBorderHeight(IEnumerable items)
{
if (items != null)
{
int itemCount = items.Cast<object>().Count();
desiredHeight = 180.0;
if (itemCount < 4)
{
desiredHeight = itemCount * 56;
margin = new Thickness(0, ((itemCount - 1) * 56) + 4, 10, 0);
#if WINDOWS
margin = new Thickness(0, ((itemCount - 1) * 56) + 30, 10, 0);
#endif
}
}
}
private void UpdateCellColor(ListView list)
{
foreach (var item in list.TemplatedItems)
{
if (item is DropDownViewCell textCell)
{
if (SelectedItem == textCell.BindingContext)
{
textCell.UpdateHighlightBackgroundColor();
}
else
{
textCell.UpdateDefaultBackgroundColor();
}
}
}
}
#endregion
private void TapGestureRecognizer_Tapped(object sender, TappedEventArgs e)
{
if (ItemsSource != null && ItemsSource.Cast<object>().Any())
{
Open();
}
}
protected override void OnParentSet()
{
base.OnParentSet();
if (Parent == null) Dispose();
}
protected override void OnHandlerChanged()
{
base.OnHandlerChanged();
if (Handler == null) Dispose();
}
public void Dispose()
{
SelectionChanged = null;
SelectionChangedCommand = null;
ItemsSource = null;
if (dropDownContents != null)
{
dropDownContents.ChangeIndicatorWhenPopupRemove = null;
dropDownContents = null;
}
previousSelection = null;
}
}