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

Map LayoutViewRegionAdapter to Maui Layout #3160

Merged
merged 2 commits into from
Jun 11, 2024
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Diagnostics.CodeAnalysis;
using Microsoft.Maui.Controls.Compatibility;
using Prism.Mvvm;
using Prism.Navigation.Regions;
using Prism.Navigation.Regions.Adapters;
Expand Down Expand Up @@ -115,7 +114,8 @@ internal static IContainerRegistry RegisterRegionServices(this IContainerRegistr
// TODO: CollectionView is buggy with only last View showing despite multiple Active Views
// BUG: iOS Crash with CollectionView https://github.com/xamarin/Xamarin.Forms/issues/9970
//regionAdapterMappings.RegisterDefaultMapping<CollectionView, CollectionViewRegionAdapter>();
regionAdapterMappings.RegisterDefaultMapping<Layout<View>, LayoutViewRegionAdapter>();
regionAdapterMappings.RegisterDefaultMapping<Microsoft.Maui.Controls.Compatibility.Layout<View>, LayoutViewRegionAdapter>();
regionAdapterMappings.RegisterDefaultMapping<Layout, LayoutRegionAdapter>();
regionAdapterMappings.RegisterDefaultMapping<ScrollView, ScrollViewRegionAdapter>();
regionAdapterMappings.RegisterDefaultMapping<ContentView, ContentViewRegionAdapter>();
return regionAdapterMappings;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using Prism.Properties;

namespace Prism.Navigation.Regions.Adapters;

/// <summary>
/// Adapter that creates a new <see cref="Region"/> and monitors its
/// active view to set it on the adapted <see cref="Layout"/>.
/// </summary>
public class LayoutRegionAdapter : RegionAdapterBase<Layout>
{
/// <summary>
/// Initializes a new instance of <see cref="LayoutRegionAdapter"/>.
/// </summary>
/// <param name="regionBehaviorFactory">The factory used to create the region behaviors to attach to the created regions.</param>
public LayoutRegionAdapter(IRegionBehaviorFactory regionBehaviorFactory)
: base(regionBehaviorFactory)
{
}

/// <summary>
/// Adapts a <see cref="Layout"/> to an <see cref="IRegion"/>.
/// </summary>
/// <param name="region">The new region being used.</param>
/// <param name="regionTarget">The object to adapt.</param>
protected override void Adapt(IRegion region, Layout regionTarget)
{
if (region == null)
throw new ArgumentNullException(nameof(region));

if (regionTarget == null)
throw new ArgumentNullException(nameof(regionTarget));

bool itemsSourceIsSet = regionTarget.Children?.Any() ?? false || regionTarget.IsSet(BindableLayout.ItemsSourceProperty);

if (itemsSourceIsSet)
{
throw new InvalidOperationException(Resources.LayoutViewHasChildrenException);
}

BindableLayout.SetItemsSource(regionTarget, region.Views);
BindableLayout.SetItemTemplate(regionTarget, new RegionItemsSourceTemplate());
}

/// <summary>
/// Creates a new instance of <see cref="IRegion"/>.
/// </summary>
/// <returns>A new instance of <see cref="Region"/>.</returns>
protected override IRegion CreateRegion(IContainerProvider container) =>
container.Resolve<Region>();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Microsoft.Maui.Controls.Compatibility;
using Prism.Ioc;
using Microsoft.Maui.Controls.Compatibility;
using Prism.Properties;

namespace Prism.Navigation.Regions.Adapters;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Prism.DryIoc.Maui.Tests.Mocks.ViewModels;
using Prism.DryIoc.Maui.Tests.Mocks.ViewModels;
using Prism.DryIoc.Maui.Tests.Mocks.Views;
using Prism.Navigation.Xaml;

Expand Down Expand Up @@ -53,6 +53,33 @@ public void FrameRegion_CreatedBy_RegisterViewWithRegion()
Assert.IsType<MockRegionViewAViewModel>(page.FrameRegion.Content.BindingContext);
}

[Fact]
public void Issue3159_LayoutRegion_CreatedBy_RegisterViewWithRegion()
{
var mauiApp = CreateBuilder(prism =>
prism.RegisterTypes(container =>
{
container.RegisterForNavigation<MockContentRegionPage, MockContentRegionPageViewModel>();
container.RegisterForRegionNavigation<MockRegionViewA, MockRegionViewAViewModel>();
})
.OnInitialized(container =>
{
var regionManager = container.Resolve<IRegionManager>();
regionManager.RegisterViewWithRegion("LayoutRegion", "MockRegionViewA");
})
.CreateWindow("MockContentRegionPage"))
.Build();
var window = GetWindow(mauiApp);

Assert.IsType<MockContentRegionPage>(window.Page);
var page = window.Page as MockContentRegionPage;

Assert.NotNull(page.LayoutRegion.Children);
Assert.NotEmpty(page.LayoutRegion.Children);
Assert.IsType<ContentView>(page.LayoutRegion.Children.First());
Assert.IsType<MockRegionViewAViewModel>(((ContentView)page.LayoutRegion.First()).Content.BindingContext);
}

[Fact]
public void RegionsShareContainer_WithPage()
{
Expand All @@ -76,7 +103,7 @@ public void RegionsShareContainer_WithPage()

var regionManager = mauiApp.Services.GetRequiredService<IRegionManager>();
var regions = regionManager.Regions.Cast<ITargetAwareRegion>();
Assert.Equal(2, regions.Count());
Assert.Equal(3, regions.Count());
foreach (var region in regions)
{
Assert.Same(page.GetContainerProvider(), region.Container);
Expand Down Expand Up @@ -110,7 +137,7 @@ public void RegionViewModel_HasPageAccessor_WithCorrectPage()
}

[Fact]
public void RegionManager_HasTwoRegions()
public void RegionManager_HasRegionsAmount()
{
var mauiApp = CreateBuilder(prism =>
prism.RegisterTypes(container =>
Expand All @@ -122,7 +149,7 @@ public void RegionManager_HasTwoRegions()
var window = GetWindow(mauiApp);

var regionManager = mauiApp.Services.GetRequiredService<IRegionManager>();
Assert.Equal(2, regionManager.Regions.Count());
Assert.Equal(3, regionManager.Regions.Count());
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Prism.DryIoc.Maui.Tests.Mocks.Views;
namespace Prism.DryIoc.Maui.Tests.Mocks.Views;

public class MockContentRegionPage : ContentPage
{
Expand All @@ -10,14 +10,20 @@ public MockContentRegionPage()
FrameRegion = new Frame();
FrameRegion.SetValue(Prism.Navigation.Regions.Xaml.RegionManager.RegionNameProperty, nameof(FrameRegion));

LayoutRegion = new StackLayout();
LayoutRegion.SetValue(Prism.Navigation.Regions.Xaml.RegionManager.RegionNameProperty, nameof(LayoutRegion));

Content = new StackLayout
{
ContentRegion,
FrameRegion
FrameRegion,
LayoutRegion
};
}

public ContentView ContentRegion { get; }

public Frame FrameRegion { get; }

public Layout LayoutRegion { get; }
}
Loading