-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathShellSearchControlPage.xaml.cs
More file actions
269 lines (225 loc) · 7.48 KB
/
Copy pathShellSearchControlPage.xaml.cs
File metadata and controls
269 lines (225 loc) · 7.48 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
namespace Maui.Controls.Sample;
public class SampleSearchHandler : SearchHandler
{
internal static readonly string[] Fruits =
[
"Apple", "Apricot", "Avocado", "Banana", "Blackberry",
"Blueberry", "Cherry", "Coconut", "Cranberry", "Date",
"Fig", "Grape", "Grapefruit", "Guava", "Kiwi",
"Lemon", "Lime", "Mango", "Melon", "Nectarine",
"Orange", "Papaya", "Peach", "Pear", "Pineapple",
"Plum", "Pomegranate", "Raspberry", "Strawberry", "Watermelon"
];
internal static readonly string[] Birds =
[
"Blue Jay", "Cardinal", "Crow", "Eagle", "Falcon",
"Finch", "Flamingo", "Hawk", "Heron", "Hummingbird",
"Kingfisher", "Magpie", "Nightingale", "Oriole", "Owl",
"Parrot", "Peacock", "Pelican", "Penguin", "Pigeon",
"Raven", "Robin", "Sparrow", "Starling", "Swan",
"Toucan", "Turkey", "Vulture", "Woodpecker", "Wren"
];
ShellViewModel _viewModel = null!;
public void SetViewModel(ShellViewModel vm)
{
_viewModel?.PropertyChanged -= OnViewModelPropertyChanged;
_viewModel = vm;
vm.PropertyChanged += OnViewModelPropertyChanged;
}
void OnViewModelPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(ShellViewModel.ItemsSourceMode))
{
// Re-run the current query against the new data source
OnQueryChanged(Query ?? string.Empty, Query ?? string.Empty);
}
}
protected override void OnQueryChanged(string oldValue, string newValue)
{
base.OnQueryChanged(oldValue, newValue);
if (_viewModel is null)
{
return;
}
_viewModel.QueryChangedLog = $"{oldValue}\u2192{newValue}";
if (string.IsNullOrEmpty(newValue))
{
ItemsSource = null;
return;
}
var mode = _viewModel.ItemsSourceMode;
IEnumerable<string> source = mode switch
{
"Fruits" => Fruits,
"Birds" => Birds,
"Null" => null,
_ => Fruits.Concat(Birds) // "Query" mode searches both
};
ItemsSource = source?
.Where(s => s.Contains(newValue, StringComparison.OrdinalIgnoreCase))
.ToList();
}
protected override async void OnItemSelected(object item)
{
base.OnItemSelected(item);
if (_viewModel is null)
{
return;
}
var itemName = item?.ToString();
_viewModel.SelectedItem = itemName;
if (!string.IsNullOrEmpty(itemName))
{
try
{
await Shell.Current.GoToAsync(
ShellSearchControlPage.DetailRoute,
new Dictionary<string, object>
{
[nameof(ShellSearchControlPage.SearchDetailPage.ItemName)] = itemName
});
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Navigation failed: {ex.Message}");
}
}
}
}
public partial class ShellSearchControlPage : Shell
{
internal const string DetailRoute = "searchdetail";
private readonly ShellViewModel _viewModel;
public ShellSearchControlPage()
{
_viewModel = new ShellViewModel();
BindingContext = _viewModel;
InitializeComponent();
Routing.RegisterRoute(DetailRoute, typeof(SearchDetailPage));
SearchHandlerInstance.SetViewModel(_viewModel);
SearchHandlerInstance.Focused += OnSearchHandlerFocused;
SearchHandlerInstance.Unfocused += OnSearchHandlerUnfocused;
// SearchBoxVisibility=Collapsible requires a scrollable content view to hide/reveal
// the search bar via scroll gesture on iOS/MacCatalyst. On Windows a ScrollView here isn't needed and causes a persistent scrollbar to render in CI
// so only wrap the content in a ScrollView for the platforms that require it.
if (DeviceInfo.Platform == DevicePlatform.iOS || DeviceInfo.Platform == DevicePlatform.MacCatalyst)
{
SearchContentPage.Content = new ScrollView { Content = MainStack };
}
}
void OnSearchHandlerFocused(object sender, EventArgs e)
{
_viewModel.FocusStatus = "Focused";
_viewModel.IsFocused = true;
}
void OnSearchHandlerUnfocused(object sender, EventArgs e)
{
_viewModel.FocusStatus = "Unfocused";
_viewModel.IsFocused = false;
}
async void OnOptionsClicked(object sender, EventArgs e)
{
SearchHandlerInstance.Unfocus();
SearchHandlerInstance.HideSoftInputAsync();
_viewModel.Reset();
// Reset toggle button labels to match default state
ToggleQueryIconBtn.Text = "QueryIcon:Default";
ToggleClearIconBtn.Text = "ClearIcon:Default";
ToggleClearPHIconBtn.Text = "ClearPHIcon:Default";
ToggleShowsResultsBtn.Text = "ShowsResults:False";
await Navigation.PushAsync(new ShellSearchOptionsPage(_viewModel));
}
// ── Action buttons ────────────────────────────────────────────────────────────
void OnShowSoftInputClicked(object sender, EventArgs e)
=> SearchHandlerInstance.ShowSoftInputAsync();
void OnHideSoftInputClicked(object sender, EventArgs e)
=> SearchHandlerInstance.HideSoftInputAsync();
void OnSetQueryClicked(object sender, EventArgs e)
=> _viewModel.Query = "Robin";
void OnTriggerClearPlaceholderClicked(object sender, EventArgs e)
=> ((ISearchHandlerController)SearchHandlerInstance).ClearPlaceholderClicked();
void OnToggleQueryIconClicked(object sender, EventArgs e)
{
var btn = (Button)sender;
bool isCustom = _viewModel.QueryIcon != null;
_viewModel.QueryIcon = isCustom ? null : ImageSource.FromFile("coffee.png");
btn.Text = isCustom ? "QueryIcon:Default" : "QueryIcon:Custom";
}
void OnToggleClearIconClicked(object sender, EventArgs e)
{
var btn = (Button)sender;
bool isCustom = _viewModel.ClearIcon != null;
_viewModel.ClearIcon = isCustom ? null : ImageSource.FromFile("bank.png");
btn.Text = isCustom ? "ClearIcon:Default" : "ClearIcon:Custom";
}
void OnToggleClearPlaceholderIconClicked(object sender, EventArgs e)
{
var btn = (Button)sender;
bool isCustom = _viewModel.ClearPlaceholderIcon != null;
_viewModel.ClearPlaceholderIcon = isCustom ? null : ImageSource.FromFile("avatar.png");
btn.Text = isCustom ? "ClearPHIcon:Default" : "ClearPHIcon:Custom";
}
void OnToggleShowsResultsClicked(object sender, EventArgs e)
{
var btn = (Button)sender;
_viewModel.ShowsResults = !_viewModel.ShowsResults;
btn.Text = _viewModel.ShowsResults ? "ShowsResults:True" : "ShowsResults:False";
}
// ── Search Detail Page (navigated to when a search result is tapped) ─────────
[QueryProperty(nameof(ItemName), nameof(ItemName))]
internal sealed class SearchDetailPage : ContentPage
{
readonly Label _nameLabel;
readonly Label _categoryLabel;
string _itemName;
public string ItemName
{
get => _itemName;
set
{
_itemName = value;
UpdateContent();
}
}
public SearchDetailPage()
{
AutomationId = "SearchDetailPage";
_nameLabel = new Label
{
FontSize = 24,
FontAttributes = FontAttributes.Bold,
HorizontalTextAlignment = TextAlignment.Center,
AutomationId = "DetailName"
};
_categoryLabel = new Label
{
FontSize = 16,
HorizontalTextAlignment = TextAlignment.Center,
AutomationId = "DetailCategory"
};
var backButton = new Button
{
Text = "Back to Search",
AutomationId = "BackToSearchButton",
};
backButton.Clicked += async (_, _) => await Shell.Current.GoToAsync("..");
Content = new VerticalStackLayout
{
Padding = new Thickness(20),
Spacing = 16,
Children = { _nameLabel, _categoryLabel, backButton }
};
}
void UpdateContent()
{
if (string.IsNullOrEmpty(_itemName))
{
return;
}
bool isFruit = SampleSearchHandler.Fruits.Contains(_itemName, StringComparer.OrdinalIgnoreCase);
Title = _itemName;
_nameLabel.Text = _itemName;
_categoryLabel.Text = isFruit ? "Fruit" : "Bird";
}
}
}