-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathShellTests.Android.cs
More file actions
755 lines (620 loc) · 23.7 KB
/
ShellTests.Android.cs
File metadata and controls
755 lines (620 loc) · 23.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
using System;
using System.Linq;
using System.Threading.Tasks;
using Android.Views;
using AndroidX.AppCompat.Widget;
using AndroidX.CoordinatorLayout.Widget;
using AndroidX.Core.View;
using AndroidX.Core.Widget;
using AndroidX.DrawerLayout.Widget;
using AndroidX.RecyclerView.Widget;
using AndroidX.ViewPager2.Widget;
using Google.Android.Material.AppBar;
using Google.Android.Material.BottomNavigation;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Handlers.Compatibility;
using Microsoft.Maui.Controls.Platform;
using Microsoft.Maui.Controls.Platform.Compatibility;
using Microsoft.Maui.DeviceTests.Stubs;
using Microsoft.Maui.Handlers;
using Microsoft.Maui.Platform;
using Xunit;
using static Microsoft.Maui.Controls.Platform.Compatibility.ShellFlyoutTemplatedContentRenderer;
using static Microsoft.Maui.DeviceTests.AssertHelpers;
using AView = Android.Views.View;
using ShellHandler = Microsoft.Maui.Controls.Handlers.Compatibility.ShellRenderer;
namespace Microsoft.Maui.DeviceTests
{
[Category(TestCategory.Shell)]
public partial class ShellTests
{
[Fact(DisplayName = "No crash going back using 'Shell.Current.GoToAsync(\"..\")'")]
public async Task GoingBackUsingGoToAsyncMethod()
{
SetupBuilder();
var page1 = new ContentPage();
var page2Content = new Label { Text = "Test" };
var page2 = new ContentPage { Content = page2Content };
var pointerGestureRecognizer = new PointerGestureRecognizer();
pointerGestureRecognizer.PointerPressed += (sender, args) =>
{
Console.WriteLine("Page Content pressed");
};
page2Content.GestureRecognizers.Add(pointerGestureRecognizer);
var shell = await CreateShellAsync((shell) =>
{
shell.Items.Add(new TabBar()
{
Items =
{
new ShellContent()
{
Route = "Item1",
Content = page1
},
new ShellContent()
{
Route = "Item2",
Content = page2
},
}
});
});
await CreateHandlerAndAddToWindow<ShellHandler>(shell, async (handler) =>
{
await OnLoadedAsync(page1);
await shell.GoToAsync("//Item2");
await shell.GoToAsync("..");
await shell.GoToAsync("//Item1");
await shell.GoToAsync("//Item2");
await shell.Navigation.PopAsync();
await shell.GoToAsync("//Item1");
await shell.GoToAsync("//Item2");
await shell.GoToAsync("..");
});
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public async Task CanHideNavBarShadow(bool navBarHasShadow)
{
SetupBuilder();
var contentPage = new ContentPage() { Title = "Flyout Item" };
var shell = await CreateShellAsync(shell =>
{
shell.CurrentItem = new FlyoutItem() { Items = { contentPage } };
shell.FlyoutContent = new VerticalStackLayout()
{
new Label(){ Text = "Flyout Content"}
};
Shell.SetNavBarHasShadow(contentPage, navBarHasShadow);
});
await CreateHandlerAndAddToWindow<ShellRenderer>(shell, async (handler) =>
{
await Task.Delay(100);
var platformToolbar = GetPlatformToolbar(handler);
var appBar = platformToolbar.Parent.GetParentOfType<AppBarLayout>();
if (navBarHasShadow)
Assert.True(appBar.Elevation > 0);
else
Assert.True(appBar.Elevation == 0);
});
}
protected async Task CheckFlyoutState(ShellRenderer handler, bool desiredState)
{
var drawerLayout = GetDrawerLayout(handler);
var flyout = drawerLayout.GetChildAt(1);
if (drawerLayout.IsDrawerOpen(flyout) == desiredState)
{
Assert.Equal(desiredState, drawerLayout.IsDrawerOpen(flyout));
return;
}
var taskCompletionSource = new TaskCompletionSource<bool>();
flyout.LayoutChange += OnLayoutChanged;
try
{
await taskCompletionSource.Task.WaitAsync(TimeSpan.FromSeconds(2));
}
catch (TimeoutException)
{
}
flyout.LayoutChange -= OnLayoutChanged;
Assert.Equal(desiredState, drawerLayout.IsDrawerOpen(flyout));
return;
void OnLayoutChanged(object sender, global::Android.Views.View.LayoutChangeEventArgs e)
{
if (drawerLayout.IsDrawerOpen(flyout) == desiredState)
{
taskCompletionSource.SetResult(true);
flyout.LayoutChange -= OnLayoutChanged;
}
}
}
[Fact(DisplayName = "FlyoutItems Render When FlyoutBehavior Starts As Locked")]
public async Task FlyoutItemsRendererWhenFlyoutBehaviorStartsAsLocked()
{
SetupBuilder();
var shell = await CreateShellAsync(shell =>
{
shell.CurrentItem = new FlyoutItem() { Items = { new ContentPage() }, Title = "Flyout Item" };
shell.FlyoutBehavior = FlyoutBehavior.Locked;
});
await CreateHandlerAndAddToWindow<ShellRenderer>(shell, async (handler) =>
{
await Task.Delay(100);
var dl = GetDrawerLayout(handler);
var flyoutContainer = GetFlyoutMenuReyclerView(handler);
Assert.True(flyoutContainer.MeasuredWidth > 0);
Assert.True(flyoutContainer.MeasuredHeight > 0);
});
}
[Fact(DisplayName = "Shell with Flyout Disabled Doesn't Render Flyout")]
public async Task ShellWithFlyoutDisabledDoesntRenderFlyout()
{
SetupBuilder();
var shell = await CreateShellAsync((shell) =>
{
shell.Items.Add(new ContentPage());
});
shell.FlyoutBehavior = FlyoutBehavior.Disabled;
await CreateHandlerAndAddToWindow<ShellRenderer>(shell, (handler) =>
{
var dl = GetDrawerLayout(handler);
Assert.Equal(1, dl.ChildCount);
shell.FlyoutBehavior = FlyoutBehavior.Flyout;
Assert.Equal(2, dl.ChildCount);
return Task.CompletedTask;
});
}
[Fact(DisplayName = "FooterTemplate Measures to Set Flyout Width When Flyout Locked")]
public async Task FooterTemplateMeasuresToSetFlyoutWidth()
{
SetupBuilder();
VerticalStackLayout footer = new VerticalStackLayout()
{
new Label(){ Text = "Hello there"}
};
var shell = await CreateShellAsync(shell =>
{
shell.CurrentItem = new FlyoutItem() { Items = { new ContentPage() }, Title = "Flyout Item" };
shell.FlyoutBehavior = FlyoutBehavior.Locked;
shell.FlyoutWidth = 20;
shell.FlyoutFooter = footer;
});
await CreateHandlerAndAddToWindow<ShellRenderer>(shell, async (handler) =>
{
await OnFrameSetToNotEmpty(footer);
Assert.True(Math.Abs(20 - footer.Frame.Width) < 1);
Assert.True(footer.Frame.Height > 0);
});
}
[Fact(DisplayName = "Flyout Footer and Default Flyout Items Render")]
public async Task FlyoutFooterRenderersWithDefaultFlyoutItems()
{
SetupBuilder();
VerticalStackLayout footer = new VerticalStackLayout()
{
new Label() { Text = "Hello there"}
};
var shell = await CreateShellAsync(shell =>
{
shell.CurrentItem = new FlyoutItem() { Items = { new ContentPage() }, Title = "Flyout Item" };
shell.FlyoutFooter = footer;
});
await CreateHandlerAndAddToWindow<ShellRenderer>(shell, async (handler) =>
{
await Task.Delay(100);
var dl = GetDrawerLayout(handler);
await OpenFlyout(handler);
var flyoutContainer = GetFlyoutMenuReyclerView(handler);
Assert.True(flyoutContainer.MeasuredWidth > 0);
Assert.True(flyoutContainer.MeasuredHeight > 0);
});
}
[Fact]
public async Task FlyoutItemsRenderWhenFlyoutHeaderIsSet()
{
SetupBuilder();
VerticalStackLayout header = new VerticalStackLayout()
{
new Label() { Text = "Hello there"}
};
var shell = await CreateShellAsync(shell =>
{
shell.CurrentItem = new FlyoutItem() { Items = { new ContentPage() }, Title = "Flyout Item" };
shell.FlyoutHeader = header;
});
await CreateHandlerAndAddToWindow<ShellRenderer>(shell, async (handler) =>
{
await Task.Delay(100);
var dl = GetDrawerLayout(handler);
await OpenFlyout(handler);
var flyoutContainer = GetFlyoutMenuReyclerView(handler);
Assert.True(flyoutContainer.MeasuredWidth > 0);
Assert.True(flyoutContainer.MeasuredHeight > 0);
});
}
[Fact]
public async Task FlyoutHeaderRendersCorrectSizeWithFlyoutContentSet()
{
SetupBuilder();
VerticalStackLayout header = new VerticalStackLayout()
{
new Label() { Text = "Flyout Header"}
};
var shell = await CreateShellAsync(shell =>
{
shell.CurrentItem = new FlyoutItem() { Items = { new ContentPage() }, Title = "Flyout Item" };
shell.FlyoutHeader = header;
shell.FlyoutContent = new VerticalStackLayout()
{
new Label(){ Text = "Flyout Content"}
};
shell.FlyoutFooter = new VerticalStackLayout()
{
new Label(){ Text = "Flyout Footer"}
};
});
await CreateHandlerAndAddToWindow<ShellRenderer>(shell, async (handler) =>
{
await Task.Delay(100);
var headerPlatformView = header.ToPlatform();
var appBar = headerPlatformView.GetParentOfType<AppBarLayout>();
Assert.Equal(appBar.MeasuredHeight - appBar.PaddingTop, headerPlatformView.MeasuredHeight);
});
}
[Fact]
public async Task SwappingOutAndroidContextDoesntCrash()
{
SetupBuilder();
var shell = await CreateShellAsync(shell =>
{
shell.Items.Add(new FlyoutItem() { Route = "FlyoutItem1", Items = { new ContentPage() }, Title = "Flyout Item" });
shell.Items.Add(new FlyoutItem() { Route = "FlyoutItem2", Items = { new ContentPage() }, Title = "Flyout Item" });
});
var window = new Controls.Window(shell);
var mauiContextStub1 = ContextStub.CreateNew(MauiContext);
await CreateHandlerAndAddToWindow<IWindowHandler>(window, async (handler) =>
{
await OnLoadedAsync(shell.CurrentPage);
await OnNavigatedToAsync(shell.CurrentPage);
await Task.Delay(100);
await shell.GoToAsync("//FlyoutItem2");
}, mauiContextStub1);
var mauiContextStub2 = ContextStub.CreateNew(MauiContext);
await CreateHandlerAndAddToWindow<IWindowHandler>(window, async (handler) =>
{
await OnLoadedAsync(shell.CurrentPage);
await OnNavigatedToAsync(shell.CurrentPage);
await Task.Delay(100);
await shell.GoToAsync("//FlyoutItem1");
await shell.GoToAsync("//FlyoutItem2");
}, mauiContextStub2);
}
[Fact]
public async Task ChangingBottomTabAttributesDoesntRecreateBottomTabs()
{
SetupBuilder();
var shell = await CreateShellAsync(shell =>
{
shell.Items.Add(new Tab() { Items = { new ContentPage() }, Title = "Tab 1", Icon = "red.png" });
shell.Items.Add(new Tab() { Items = { new ContentPage() }, Title = "Tab 2", Icon = "red.png" });
});
await CreateHandlerAndAddToWindow<ShellRenderer>(shell, async (handler) =>
{
var menu = GetDrawerLayout(handler).GetFirstChildOfType<BottomNavigationView>().Menu;
var menuItem1 = menu.GetItem(0);
var menuItem2 = menu.GetItem(1);
var icon1 = menuItem1.Icon;
var icon2 = menuItem2.Icon;
var title1 = menuItem1.TitleFormatted;
var title2 = menuItem2.TitleFormatted;
shell.CurrentItem.Items[0].Title = "new Title 1";
shell.CurrentItem.Items[0].Icon = "blue.png";
shell.CurrentItem.Items[1].Title = "new Title 2";
shell.CurrentItem.Items[1].Icon = "blue.png";
// let the icon and title propagate
await AssertEventually(() => menuItem1.Icon != icon1);
menu = GetDrawerLayout(handler).GetFirstChildOfType<BottomNavigationView>().Menu;
Assert.Equal(menuItem1, menu.GetItem(0));
Assert.Equal(menuItem2, menu.GetItem(1));
menuItem1.Icon.AssertColorAtCenter(global::Android.Graphics.Color.Blue);
menuItem2.Icon.AssertColorAtCenter(global::Android.Graphics.Color.Blue);
Assert.NotEqual(icon1, menuItem1.Icon);
Assert.NotEqual(icon2, menuItem2.Icon);
Assert.NotEqual(title1, menuItem1.TitleFormatted);
Assert.NotEqual(title2, menuItem2.TitleFormatted);
});
}
[Fact]
public async Task RemovingBottomTabDoesntRecreateMenu()
{
SetupBuilder();
var shell = await CreateShellAsync(shell =>
{
shell.Items.Add(new Tab() { Items = { new ContentPage() }, Title = "Tab 1", Icon = "red.png" });
shell.Items.Add(new Tab() { Items = { new ContentPage() }, Title = "Tab 2", Icon = "red.png" });
shell.Items.Add(new Tab() { Items = { new ContentPage() }, Title = "Tab 3", Icon = "red.png" });
});
await CreateHandlerAndAddToWindow<ShellRenderer>(shell, async (handler) =>
{
var bottomView = GetDrawerLayout(handler).GetFirstChildOfType<BottomNavigationView>();
var menu = bottomView.Menu;
var menuItem1 = menu.GetItem(0);
var menuItem2 = menu.GetItem(1);
shell.CurrentItem.Items.RemoveAt(2);
// let the change propagate
await AssertEventually(() => bottomView.Menu.Size() == 2);
menu = bottomView.Menu;
Assert.Equal(menuItem1, menu.GetItem(0));
Assert.Equal(menuItem2, menu.GetItem(1));
});
}
[Fact]
public async Task AddingBottomTabDoesntRecreateMenu()
{
SetupBuilder();
var shell = await CreateShellAsync(shell =>
{
shell.Items.Add(new Tab() { Items = { new ContentPage() }, Title = "Tab 1", Icon = "red.png" });
shell.Items.Add(new Tab() { Items = { new ContentPage() }, Title = "Tab 3", Icon = "red.png" });
});
await CreateHandlerAndAddToWindow<ShellRenderer>(shell, async (handler) =>
{
var bottomView = GetDrawerLayout(handler).GetFirstChildOfType<BottomNavigationView>();
var menu = bottomView.Menu;
var menuItem1 = menu.GetItem(0);
var menuItem2 = menu.GetItem(1);
var menuItem2Icon = menuItem2.Icon;
shell.CurrentItem.Items.Insert(1, new Tab() { Items = { new ContentPage() }, Title = "Tab 2", Icon = "green.png" });
// let the change propagate
await AssertEventually(() => bottomView.Menu.GetItem(1).Icon != menuItem2Icon);
menu = bottomView.Menu;
Assert.Equal(menuItem1, menu.GetItem(0));
Assert.Equal(menuItem2, menu.GetItem(1));
menu.GetItem(1).Icon.AssertColorAtCenter(global::Android.Graphics.Color.Green);
menu.GetItem(2).Icon.AssertColorAtCenter(global::Android.Graphics.Color.Red);
});
}
//src/Compatibility/Core/tests/Android/ShellTests.cs
[Fact(DisplayName = "Flyout Header Changes When Updated")]
public async Task FlyoutHeaderReactsToChanges()
{
SetupBuilder();
var initialHeader = new Label() { Text = "Hello" };
var newHeader = new Label() { Text = "Hello Part 2" };
var shell = await CreateShellAsync(shell =>
{
shell.CurrentItem = new FlyoutItem() { Items = { new ContentPage() }, Title = "Flyout Item" };
shell.FlyoutHeader = initialHeader;
});
await CreateHandlerAndAddToWindow<ShellRenderer>(shell, async (handler) =>
{
var initialHeaderPlatformView = initialHeader.ToPlatform();
Assert.NotNull(initialHeaderPlatformView);
Assert.NotNull(initialHeader.Handler);
shell.FlyoutHeader = newHeader;
var newHeaderPlatformView = newHeader.ToPlatform();
Assert.NotNull(newHeaderPlatformView);
Assert.NotNull(newHeader.Handler);
Assert.Null(initialHeader.Handler);
await OpenFlyout(handler);
var appBar = newHeaderPlatformView.GetParentOfType<AppBarLayout>();
Assert.NotNull(appBar);
});
}
//src/Compatibility/Core/tests/Android/ShellTests.cs
[Fact(DisplayName = "Ensure Default Colors are White for BottomNavigationView")]
public async Task ShellTabColorsDefaultToWhite()
{
SetupBuilder();
var shell = await CreateShellAsync(shell =>
{
shell.Items.Add(new Tab() { Items = { new ContentPage() }, Title = "Tab 1" });
});
await CreateHandlerAndAddToWindow<ShellRenderer>(shell, (handler) =>
{
var bottomNavigationView = GetDrawerLayout(handler).GetFirstChildOfType<BottomNavigationView>();
Assert.NotNull(bottomNavigationView);
var background = bottomNavigationView.Background;
Assert.NotNull(background);
if (background is ColorChangeRevealDrawable changeRevealDrawable)
{
Assert.Equal(global::Android.Graphics.Color.White, changeRevealDrawable.EndColor);
}
});
}
[Fact(DisplayName = "ShellContentFragment.Destroy handles null _shellContext gracefully")]
public async Task ShellContentFragmentDestroyHandlesNullShellContext()
{
SetupBuilder();
var shell = await CreateShellAsync(shell =>
{
shell.Items.Add(new TabBar()
{
Items =
{
new ShellContent()
{
Route = "Item1",
Content = new ContentPage { Title = "Page 1" }
},
new ShellContent()
{
Route = "Item2",
Content = new ContentPage { Title = "Page 2" }
},
}
});
});
await CreateHandlerAndAddToWindow<ShellHandler>(shell, async (handler) =>
{
await OnLoadedAsync(shell.CurrentPage);
await OnNavigatedToAsync(shell.CurrentPage);
// Navigate to trigger fragment creation
await shell.GoToAsync("//Item2");
await OnNavigatedToAsync(shell.CurrentPage);
// Test normal destruction - should work without issues
await shell.GoToAsync("//Item1");
await OnNavigatedToAsync(shell.CurrentPage);
// Test null context scenario
var exception = Record.Exception(() =>
{
// Create fragment with null context - this should not throw
Page page = new ContentPage();
var fragment = new ShellContentFragment((IShellContext)null, page);
// Dispose the fragment which calls Destroy internally
// This validates the null-conditional operators in Destroy method
fragment.Dispose();
});
// Verify no exception was thrown - validates (_shellContext?.Shell as IShellController)?.RemoveAppearanceObserver(this);
Assert.Null(exception);
});
}
protected AView GetFlyoutPlatformView(ShellRenderer shellRenderer)
{
var drawerLayout = GetDrawerLayout(shellRenderer);
return drawerLayout.GetChildrenOfType<ShellFlyoutLayout>().First();
}
internal Graphics.Rect GetFlyoutFrame(ShellRenderer shellRenderer)
{
var platformView = GetFlyoutPlatformView(shellRenderer);
var context = platformView.Context;
return new Graphics.Rect(0, 0,
context.FromPixels(platformView.MeasuredWidth- (platformView.PaddingLeft + platformView.PaddingRight)),
context.FromPixels(platformView.MeasuredHeight - (platformView.PaddingTop + platformView.PaddingBottom)));
}
internal Graphics.Rect GetFrameRelativeToFlyout(ShellRenderer shellRenderer, IView view)
{
var platformView = (view.Handler as IPlatformViewHandler).PlatformView;
return platformView.GetFrameRelativeTo(GetFlyoutPlatformView(shellRenderer));
}
protected async Task OpenFlyout(ShellRenderer shellRenderer, TimeSpan? timeOut = null)
{
var flyoutView = GetFlyoutPlatformView(shellRenderer);
var drawerLayout = GetDrawerLayout(shellRenderer);
if (!drawerLayout.FlyoutFirstDrawPassFinished)
await Task.Delay(10);
var hamburger =
GetPlatformToolbar((IPlatformViewHandler)shellRenderer).GetChildrenOfType<AppCompatImageButton>().FirstOrDefault() ??
throw new InvalidOperationException("Unable to find Drawer Button");
timeOut = timeOut ?? TimeSpan.FromSeconds(2);
TaskCompletionSource<object> taskCompletionSource = new TaskCompletionSource<object>();
drawerLayout.DrawerOpened += OnDrawerOpened;
hamburger.PerformClick();
await taskCompletionSource.Task.WaitAsync(timeOut.Value);
void OnDrawerOpened(object sender, DrawerLayout.DrawerOpenedEventArgs e)
{
drawerLayout.DrawerOpened -= OnDrawerOpened;
taskCompletionSource.SetResult(true);
}
}
protected async Task<double> ScrollFlyoutToBottom(ShellRenderer shellRenderer)
{
IShellContext shellContext = shellRenderer;
DrawerLayout dl = shellContext.CurrentDrawerLayout;
var viewGroup = dl.GetChildAt(1) as ViewGroup;
var scrollView = viewGroup?.GetChildAt(0);
if (scrollView is RecyclerView rv)
{
return await ScrollRecyclerViewToBottom(rv);
}
else if (scrollView is MauiScrollView mauisv)
{
return await ScrollMauiScrollViewToBottom(mauisv);
}
else
{
throw new Exception("RecyclerView or MauiScrollView not found");
}
}
async Task<double> ScrollRecyclerViewToBottom(RecyclerView flyoutItems)
{
TaskCompletionSource<object> result = new TaskCompletionSource<object>();
flyoutItems.ScrollChange += OnFlyoutItemsScrollChange;
flyoutItems.ScrollToPosition(flyoutItems.GetAdapter().ItemCount - 1);
await result.Task.WaitAsync(TimeSpan.FromSeconds(2));
await Task.Delay(10);
void OnFlyoutItemsScrollChange(object sender, AView.ScrollChangeEventArgs e)
{
flyoutItems.ScrollChange -= OnFlyoutItemsScrollChange;
result.TrySetResult(true);
}
// The appbar layout won't offset if you programmatically scroll the RecyclerView
// I haven't found a way to match the exact behavior when you touch and scroll
// I think we'd have to actually send touch events through adb
var coordinatorLayout = flyoutItems.Parent.GetParentOfType<CoordinatorLayout>();
var appbarLayout = coordinatorLayout.GetFirstChildOfType<AppBarLayout>();
var clLayoutParams = appbarLayout.LayoutParameters as CoordinatorLayout.LayoutParams;
var behavior = clLayoutParams.Behavior as AppBarLayout.Behavior;
var headerContainer = appbarLayout.GetFirstChildOfType<HeaderContainer>();
var verticalOffset = flyoutItems.ComputeVerticalScrollOffset();
behavior.OnNestedPreScroll(coordinatorLayout, appbarLayout, flyoutItems, 0, verticalOffset, new int[2], ViewCompat.TypeTouch);
await Task.Delay(10);
return verticalOffset;
}
async Task<double> ScrollMauiScrollViewToBottom(MauiScrollView flyoutItems)
{
TaskCompletionSource<object> result = new TaskCompletionSource<object>();
flyoutItems.ScrollChange += OnFlyoutItemsScrollChange;
flyoutItems.ScrollTo(0, flyoutItems.Height);
await result.Task.WaitAsync(TimeSpan.FromSeconds(2));
await Task.Delay(10);
void OnFlyoutItemsScrollChange(object sender, NestedScrollView.ScrollChangeEventArgs e)
{
flyoutItems.ScrollChange -= OnFlyoutItemsScrollChange;
result.TrySetResult(true);
}
// The appbar layout won't offset if you programmatically scroll the RecyclerView
// I haven't found a way to match the exact behavior when you touch and scroll
// I think we'd have to actually send touch events through adb
var coordinatorLayout = flyoutItems.Parent.GetParentOfType<CoordinatorLayout>();
var appbarLayout = coordinatorLayout.GetFirstChildOfType<AppBarLayout>();
var clLayoutParams = appbarLayout.LayoutParameters as CoordinatorLayout.LayoutParams;
var behavior = clLayoutParams.Behavior as AppBarLayout.Behavior;
var headerContainer = appbarLayout.GetFirstChildOfType<HeaderContainer>();
#pragma warning disable XAOBS001 // Obsolete
var verticalOffset = flyoutItems.ComputeVerticalScrollOffset();
#pragma warning restore XAOBS001 // Obsolete
behavior.OnNestedPreScroll(coordinatorLayout, appbarLayout, flyoutItems, 0, verticalOffset, new int[2], ViewCompat.TypeTouch);
await Task.Delay(10);
return verticalOffset;
}
ShellFlyoutRenderer GetDrawerLayout(ShellRenderer shellRenderer)
{
IShellContext shellContext = shellRenderer;
return (ShellFlyoutRenderer)shellContext.CurrentDrawerLayout;
}
RecyclerView GetFlyoutMenuReyclerView(ShellRenderer shellRenderer)
{
IShellContext shellContext = shellRenderer;
DrawerLayout dl = shellContext.CurrentDrawerLayout;
var flyout = dl.GetChildAt(0);
RecyclerView flyoutContainer = null;
var ViewGroup = dl.GetChildAt(1) as ViewGroup;
var rc = ViewGroup.GetChildAt(0) as RecyclerView;
if (dl.GetChildAt(1) is ViewGroup vg1 &&
vg1.GetChildAt(0) is RecyclerView rvc)
{
flyoutContainer = rvc;
}
return flyoutContainer ?? throw new Exception("RecyclerView not found");
}
async Task TapToSelect(ContentPage page)
{
var shellContent = page.Parent as ShellContent;
var shellSection = shellContent.Parent as ShellSection;
var shellItem = shellSection.Parent as ShellItem;
var shell = shellItem.Parent as Shell;
await OnNavigatedToAsync(shell.CurrentPage);
if (shellItem != shell.CurrentItem)
throw new NotImplementedException();
if (shellSection != shell.CurrentItem.CurrentItem)
throw new NotImplementedException();
var pagerParent = (shell.CurrentPage.Handler as IPlatformViewHandler)
.PlatformView.GetParentOfType<ViewPager2>();
pagerParent.CurrentItem = shellSection.Items.IndexOf(shellContent);
await OnNavigatedToAsync(page);
}
}
}