-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
CorePageView.cs
168 lines (148 loc) · 5.52 KB
/
CorePageView.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Internals;
namespace Maui.Controls.Sample
{
[Preserve(AllMembers = true)]
internal class CorePageView : ListView
{
[Preserve(AllMembers = true)]
internal class GalleryPageFactory
{
public GalleryPageFactory(Func<Page> create, string title)
{
Realize = () =>
{
var p = create();
p.Title = title;
return p;
};
Title = title;
TitleAutomationId = $"{Title}AutomationId";
}
public Func<Page> Realize { get; set; }
public string Title { get; set; }
public string TitleAutomationId
{
get;
set;
}
public override string ToString()
{
// a11y: let Narrator read a friendly string instead of the default ToString()
return Title;
}
}
List<GalleryPageFactory> _pages = new List<GalleryPageFactory> {
// Concepts & Abstracts
new GalleryPageFactory(() => new BorderGallery(), "Border Gallery"),
new GalleryPageFactory(() => new DragAndDropGallery(), "Drag and Drop Gallery"),
new GalleryPageFactory(() => new GestureRecognizerGallery(), "Gesture Recognizer Gallery"),
new GalleryPageFactory(() => new InputTransparencyGalleryPage(), "Input Transparency Gallery"),
// Elements
new GalleryPageFactory(() => new ActivityIndicatorCoreGalleryPage(), "ActivityIndicator Gallery"),
new GalleryPageFactory(() => new BoxViewCoreGalleryPage(), "Box Gallery"),
new GalleryPageFactory(() => new ButtonCoreGalleryPage(), "Button Gallery"),
new GalleryPageFactory(() => new CarouselViewCoreGalleryPage(), "CarouselView Gallery"),
new GalleryPageFactory(() => new CheckBoxCoreGalleryPage(), "CheckBox Gallery"),
new GalleryPageFactory(() => new CollectionViewCoreGalleryPage(), "CollectionView Gallery"),
new GalleryPageFactory(() => new DatePickerCoreGalleryPage(), "Date Picker Gallery"),
new GalleryPageFactory(() => new EditorCoreGalleryPage(), "Editor Gallery"),
new GalleryPageFactory(() => new EntryCoreGalleryPage(), "Entry Gallery"),
new GalleryPageFactory(() => new FrameCoreGalleryPage(), "Frame Gallery"),
new GalleryPageFactory(() => new ImageButtonCoreGalleryPage(), "Image Button Gallery"),
new GalleryPageFactory(() => new ImageCoreGalleryPage(), "Image Gallery"),
new GalleryPageFactory(() => new LabelCoreGalleryPage(), "Label Gallery"),
new GalleryPageFactory(() => new ListViewCoreGalleryPage(), "ListView Gallery"),
new GalleryPageFactory(() => new PickerCoreGalleryPage(), "Picker Gallery"),
new GalleryPageFactory(() => new ProgressBarCoreGalleryPage(), "Progress Bar Gallery"),
new GalleryPageFactory(() => new RadioButtonCoreGalleryPage(), "RadioButton Gallery"),
new GalleryPageFactory(() => new ScrollViewCoreGalleryPage(), "ScrollView Gallery"),
new GalleryPageFactory(() => new SearchBarCoreGalleryPage(), "Search Bar Gallery"),
new GalleryPageFactory(() => new SliderCoreGalleryPage(), "Slider Gallery"),
new GalleryPageFactory(() => new StepperCoreGalleryPage(), "Stepper Gallery"),
new GalleryPageFactory(() => new SwitchCoreGalleryPage(), "Switch Gallery"),
new GalleryPageFactory(() => new SwipeViewCoreGalleryPage(), "SwipeView Gallery"),
new GalleryPageFactory(() => new TimePickerCoreGalleryPage(), "Time Picker Gallery"),
new GalleryPageFactory(() => new WebViewCoreGalleryPage(), "WebView Gallery"),
};
public CorePageView(Page rootPage)
{
_titleToPage = _pages.ToDictionary(o => o.Title.ToLowerInvariant());
_pages.Sort((x, y) => string.Compare(x.Title, y.Title, StringComparison.OrdinalIgnoreCase));
var template = new DataTemplate(() =>
{
var cell = new TextCell();
cell.ContextActions.Add(new MenuItem
{
Text = "Select Visual",
Command = new Command(async () =>
{
var buttons = typeof(VisualMarker).GetProperties().Select(p => p.Name);
var selection = await rootPage.DisplayActionSheet("Select Visual", "Cancel", null, buttons.ToArray());
if (cell.BindingContext is GalleryPageFactory pageFactory)
{
var page = pageFactory.Realize();
if (typeof(VisualMarker).GetProperty(selection)?.GetValue(null) is IVisual visual)
{
page.Visual = visual;
}
await PushPage(page);
}
})
});
return cell;
});
template.SetBinding(TextCell.TextProperty, "Title");
template.SetBinding(TextCell.AutomationIdProperty, "TitleAutomationId");
BindingContext = _pages;
ItemTemplate = template;
ItemsSource = _pages;
ItemSelected += async (sender, args) =>
{
if (SelectedItem == null)
{
return;
}
var item = args.SelectedItem;
if (item is GalleryPageFactory page)
{
var realize = page.Realize();
if (realize is Shell)
{
Application.Current.MainPage = realize;
}
else
{
await PushPage(realize);
}
}
SelectedItem = null;
};
}
async Task PushPage(Page contentPage)
{
await Navigation.PushAsync(contentPage);
}
readonly Dictionary<string, GalleryPageFactory> _titleToPage;
public async Task<bool> PushPage(string pageTitle)
{
if (_titleToPage.TryGetValue(pageTitle.ToLowerInvariant(), out GalleryPageFactory pageFactory))
{
var page = pageFactory.Realize();
await PushPage(page);
return true;
}
return false;
}
public void FilterPages(string filter)
{
ItemsSource = string.IsNullOrWhiteSpace(filter)
? _pages
: _pages.Where(p => p.Title.Contains(filter, StringComparison.OrdinalIgnoreCase));
}
}
}