Repository navigation
Expand file tree
/
Copy pathCfMultiPickerPopup.xaml.cs
More file actions
164 lines (133 loc) · 5.89 KB
/
Copy pathCfMultiPickerPopup.xaml.cs
File metadata and controls
164 lines (133 loc) · 5.89 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
using System.Collections;
using System.Collections.ObjectModel;
using System.Windows.Input;
using CommunityToolkit.Maui.Views;
using CraftUI.Library.Maui.Common;
using CraftUI.Library.Maui.Common.Extensions;
using CraftUI.Library.Maui.Controls.Popups;
namespace CraftUI.Library.Maui.Controls;
public partial class CfMultiPickerPopup
{
private CfCollectionMultiSelectionPopup? _collectionPopup;
private readonly TapGestureRecognizer _tapGestureRecognizer;
public static readonly BindableProperty TitleProperty = BindableProperty.Create(nameof(Title), typeof(string), typeof(CfMultiPickerPopup));
public static readonly BindableProperty SelectedItemsProperty = BindableProperty.Create(nameof(SelectedItems), typeof(IList<object>), typeof(CfMultiPickerPopup), defaultBindingMode: BindingMode.TwoWay, propertyChanged: SelectedItemsPropertyChanged, coerceValue: CoerceSelectedItems, defaultValueCreator: DefaultValueCreator);
public static readonly BindableProperty ItemDisplayProperty = BindableProperty.Create(nameof(ItemDisplay), typeof(string), typeof(CfMultiPickerPopup), defaultBindingMode: BindingMode.OneWay);
public static readonly BindableProperty DefaultValueProperty = BindableProperty.Create(nameof(DefaultValue), typeof(string), typeof(CfMultiPickerPopup), defaultBindingMode: BindingMode.OneWay);
public static readonly BindableProperty ItemsSourceProperty = BindableProperty.Create(nameof(ItemsSource), typeof(IList), typeof(CfMultiPickerPopup), defaultBindingMode: BindingMode.OneWay);
public static readonly BindableProperty SelectionChangedCommandProperty = BindableProperty.Create(nameof(SelectionChangedCommand), typeof(ICommand), typeof(CfMultiPickerPopup));
public ObservableCollection<string> SelectedStrings { get; set; }
public IList? ItemsSource
{
get => (IList?)GetValue(ItemsSourceProperty);
set => SetValue(ItemsSourceProperty, value);
}
public IList<object>? SelectedItems
{
get => (IList<object>?)GetValue(SelectedItemsProperty);
set => SetValue(SelectedItemsProperty, new SelectionList(this, value));
}
public string ItemDisplay
{
get => (string)GetValue(ItemDisplayProperty);
set => SetValue(ItemDisplayProperty, value);
}
public string DefaultValue
{
get => (string)GetValue(DefaultValueProperty);
set => SetValue(DefaultValueProperty, value);
}
public string Title
{
get => (string)GetValue(TitleProperty);
set => SetValue(TitleProperty, value);
}
public ICommand? SelectionChangedCommand
{
get => (ICommand?)GetValue(SelectionChangedCommandProperty);
set => SetValue(SelectionChangedCommandProperty, value);
}
public CfMultiPickerPopup()
{
InitializeComponent();
_tapGestureRecognizer = new TapGestureRecognizer();
_tapGestureRecognizer.Tapped += OnTapped;
SelectedStrings = new ObservableCollection<string>();
GestureRecognizers.Add(_tapGestureRecognizer);
}
protected override void OnBindingContextChanged()
{
base.OnBindingContextChanged();
ActionIconSource ??= "chevron_bottom.png";
ActionIconCommand ??= new Command(() => OnTapped(null, EventArgs.Empty));
}
private static void SelectedItemsPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
var selectableItemsView = (CfMultiPickerPopup)bindable;
var oldSelection = (IList<object>)oldValue;
var newSelection = (IList<object>)newValue;
selectableItemsView.SelectedItemsPropertyChanged(oldSelection, newSelection);
}
private static object CoerceSelectedItems(BindableObject bindable, object? value)
{
if (value == null)
{
return new SelectionList((CfMultiPickerPopup)bindable);
}
if (value is SelectionList)
{
return value;
}
return new SelectionList((CfMultiPickerPopup)bindable, value as IList<object>);
}
private static object DefaultValueCreator(BindableObject bindable)
{
return new SelectionList((CfMultiPickerPopup)bindable);
}
internal void SelectedItemsPropertyChanged(IList<object> oldSelection, IList<object> newSelection)
{
UpdateSelectedStrings();
OnPropertyChanged(SelectedItemsProperty.PropertyName);
}
private void UpdateSelectedStrings()
{
SelectedStrings.Clear();
if (SelectedItems is null)
{
return;
}
foreach (var item in SelectedItems)
{
var displayValue = item.GetDisplayString(ItemDisplay);
if (!string.IsNullOrEmpty(displayValue) && !SelectedStrings.Contains(displayValue))
{
SelectedStrings.Add(displayValue);
}
}
OnPropertyChanged(nameof(SelectedStrings));
// MainLayout.InvalidateMeasure();
// MainLayout.PlatformSizeChanged();
// InvalidateMeasure();
// PlatformSizeChanged();
// PlatformSizeChangedCanvasView();
InvalidateSurfaceForCanvasView();
}
private void OnTapped(object? sender, EventArgs e)
{
_collectionPopup = new CfCollectionMultiSelectionPopup
{
BindingContext = this,
Title = !string.IsNullOrEmpty(Title) ? Title : Label,
ItemsSource = ItemsSource,
SelectedItems = SelectedItems,
ItemDisplay = ItemDisplay
};
_collectionPopup.Closed += (_, _) =>
{
SelectionChangedCommand?.Execute(null);
};
_collectionPopup.SetBinding(CfCollectionMultiSelectionPopup.ItemsSourceProperty, path: nameof(ItemsSource));
_collectionPopup.SetBinding(CfCollectionMultiSelectionPopup.SelectedItemsProperty, path: nameof(SelectedItems));
Shell.Current.ShowPopup(_collectionPopup);
}
}