Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cast to a less derived type on TabbedPageManager #13689

Merged
merged 8 commits into from
Mar 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/Controls/src/Core/Platform/Android/TabbedPageManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,12 @@ void OnChildrenCollectionChanged(object sender, NotifyCollectionChangedEventArgs
e.Apply((o, i, c) => SetupPage((Page)o), (o, i) => TeardownPage((Page)o), Reset);

ViewPager2 pager = _viewPager;
var adapter = (MultiPageFragmentStateAdapter<Page>)pager.Adapter;
adapter.CountOverride = Element.Children.Count;

if (pager.Adapter is MultiPageFragmentStateAdapter<Page> adapter)
{
adapter.CountOverride = Element.Children.Count;
PureWeen marked this conversation as resolved.
Show resolved Hide resolved
}

if (IsBottomTabPlacement)
{
BottomNavigationView bottomNavigationView = _bottomNavigationView;
Expand Down Expand Up @@ -329,14 +333,15 @@ void OnChildrenCollectionChanged(object sender, NotifyCollectionChangedEventArgs

void NotifyDataSetChanged()
{
if (_viewPager?.Adapter is MultiPageFragmentStateAdapter<Page> adapter)
var adapter = _viewPager?.Adapter;
if (adapter is not null)
{
var currentIndex = Element.Children.IndexOf(Element.CurrentPage);

// If the modification to the backing collection has changed the position of the current item
// then we need to update the viewpager so it remains selected
if (_viewPager.CurrentItem != currentIndex && currentIndex < Element.Children.Count && currentIndex >= 0)
_viewPager.SetCurrentItem(Element.Children.IndexOf(Element.CurrentPage), false);
_viewPager.SetCurrentItem(currentIndex, false);

adapter.NotifyDataSetChanged();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Android.Content;
using Android.OS;
using AndroidX.CoordinatorLayout.Widget;
using AndroidX.Fragment.App;
using AndroidX.RecyclerView.Widget;
using AndroidX.ViewPager2.Adapter;
using AndroidX.ViewPager2.Widget;
using Google.Android.Material.BottomNavigation;
using Microsoft.Maui.Controls;
using Microsoft.Maui.DeviceTests.Stubs;
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Handlers;
using Microsoft.Maui.Hosting;
using Microsoft.Maui.Platform;
using Xunit;

Expand Down Expand Up @@ -78,6 +84,32 @@ await CreateHandlerAndAddToWindow<TabbedViewHandler>(tabbedPage, async (handler)
});
}

[Fact(DisplayName = "Custom RecyclerView Adapter Doesn't Crash")]
public async Task CustomRecyclerViewAdapterDoesNotCrash()
{
SetupBuilder(builder =>
{
builder.ConfigureMauiHandlers(handler =>
{
handler.AddHandler<TabbedPage, CustomTestAdapterHandler>();
});
});

var tabbedPage = CreateBasicTabbedPage();

await CreateHandlerAndAddToWindow<WindowHandlerStub>(new Window(tabbedPage), async (handler) =>
{
// If you currently try to modify the children too early.
// This will sometimes cause `NotifyDataSourceChanged` on the
// adapter to get called while it's already processing
await Task.Delay(50);

tabbedPage.Children.Add(new ContentPage());

// make sure changes have time to propagate
await Task.Delay(50);
});
}

BottomNavigationView GetBottomNavigationView(TabbedViewHandler tabViewHandler)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Expand All @@ -21,7 +22,7 @@ namespace Microsoft.Maui.DeviceTests
[Category(TestCategory.TabbedPage)]
public partial class TabbedPageTests : ControlsHandlerTestBase
{
void SetupBuilder()
void SetupBuilder(Action<MauiAppBuilder> additionalCreationActions = null)
{
EnsureHandlerCreated(builder =>
{
Expand All @@ -41,6 +42,8 @@ void SetupBuilder()
handlers.AddHandler(typeof(NavigationPage), typeof(NavigationViewHandler));
#endif
});

additionalCreationActions?.Invoke(builder);
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using AndroidX.Fragment.App;
using AndroidX.RecyclerView.Widget;
using AndroidX.ViewPager2.Adapter;
using AndroidX.ViewPager2.Widget;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Handlers;
using Microsoft.Maui.Platform;

namespace Microsoft.Maui.DeviceTests
{
public class CustomTestAdapterHandler : TabbedViewHandler
{
public override void SetVirtualView(IView view)
{
base.SetVirtualView(view);
(PlatformView as ViewPager2).Adapter = new TestAdapter((TabbedPage)VirtualView);
}

class TestAdapter : FragmentStateAdapter
{
readonly TabbedPage _tabbedPage;

public TestAdapter(TabbedPage tabbedPage)
: base(tabbedPage.Handler.MauiContext.GetFragmentManager(), (tabbedPage.Handler.MauiContext.GetActivity() as FragmentActivity)!.Lifecycle)
{
_tabbedPage = tabbedPage;
}

public override void OnAttachedToRecyclerView(RecyclerView recyclerView)
{
base.OnAttachedToRecyclerView(recyclerView);
}

public override int ItemCount => _tabbedPage.Children.Count;

public override Fragment CreateFragment(int p0)
{
return new ScopedFragment(_tabbedPage.Children[p0], _tabbedPage.FindMauiContext());
}

public override long GetItemId(int position)
{
return position;
}
}
}
}