Skip to content

Commit

Permalink
Registration updates
Browse files Browse the repository at this point in the history
  • Loading branch information
dansiegel committed Aug 17, 2020
1 parent 493b91f commit bc2fadb
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 21 deletions.
21 changes: 11 additions & 10 deletions src/Forms/Prism.Forms.Regions/Ioc/RegionRegistrationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,21 @@ public static class RegionRegistrationExtensions
/// Registers the default RegionManager
/// </summary>
/// <param name="containerRegistry">The current <see cref="IContainerRegistry" /></param>
/// <param name="configureAdapters">A configuration delegate to Register Adapter Mappings.</param>
/// <param name="configureBehaviors">A configuration delegate to Add custom Region Behaviors.</param>
/// <returns>The current <see cref="IContainerRegistry" /></returns>
public static IContainerRegistry RegisterRegionServices(this IContainerRegistry containerRegistry)
public static IContainerRegistry RegisterRegionServices(this IContainerRegistry containerRegistry, Action<RegionAdapterMappings> configureAdapters = null, Action<IRegionBehaviorFactory> configureBehaviors = null)
{
containerRegistry.RegisterSingleton<RegionAdapterMappings>(p =>
{
var regionAdapterMappings = new RegionAdapterMappings();
regionAdapterMappings.RegisterMapping<CarouselView, CarouselViewRegionAdapter>();
regionAdapterMappings.RegisterMapping<CollectionView, CollectionViewRegionAdapter>();
regionAdapterMappings.RegisterMapping<FlexLayout, LayoutViewRegionAdapter>();
regionAdapterMappings.RegisterMapping<StackLayout, LayoutViewRegionAdapter>();
regionAdapterMappings.RegisterMapping<ScrollView, ScrollViewRegionAdapter>();
regionAdapterMappings.RegisterMapping<ContentView, ContentViewRegionAdapter>();
regionAdapterMappings.RegisterMapping<Frame, ContentViewRegionAdapter>();
regionAdapterMappings.RegisterMapping<RefreshView, ContentViewRegionAdapter>();
configureAdapters?.Invoke(regionAdapterMappings);
regionAdapterMappings.RegisterDefaultMapping<CarouselView, CarouselViewRegionAdapter>();
regionAdapterMappings.RegisterDefaultMapping<CollectionView, CollectionViewRegionAdapter>();
regionAdapterMappings.RegisterDefaultMapping<Layout<View>, LayoutViewRegionAdapter>();
regionAdapterMappings.RegisterDefaultMapping<ScrollView, ScrollViewRegionAdapter>();
regionAdapterMappings.RegisterDefaultMapping<ContentView, ContentViewRegionAdapter>();
return regionAdapterMappings;
});

Expand All @@ -47,7 +48,7 @@ public static IContainerRegistry RegisterRegionServices(this IContainerRegistry
regionBehaviors.AddIfMissing<ClearChildViewsRegionBehavior>(ClearChildViewsRegionBehavior.BehaviorKey);
regionBehaviors.AddIfMissing<AutoPopulateRegionBehavior>(AutoPopulateRegionBehavior.BehaviorKey);
regionBehaviors.AddIfMissing<DestructibleRegionBehavior>(DestructibleRegionBehavior.BehaviorKey);
configureBehaviors?.Invoke(regionBehaviors);
return regionBehaviors;
});
containerRegistry.Register<IRegionNavigationJournalEntry, RegionNavigationJournalEntry>();
Expand Down
14 changes: 5 additions & 9 deletions src/Forms/Prism.Forms.Regions/Ioc/RegionResolverOverrides.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System;
using System.Collections.Generic;
using System.Text;
using Prism.Navigation;
using Prism.Regions;
using Prism.Regions.Navigation;

namespace Prism.Ioc
{
Expand All @@ -18,17 +16,15 @@ internal class RegionResolverOverrides : IResolverOverridesHelper, IActiveRegion
return Array.Empty<(Type Type, object Instance)>();
}

var overrides = new List<(Type Type, object Instance)>
{
(typeof(IRegionNavigationService), ActiveRegion.NavigationService)
};

if (ActiveRegion is INavigationServiceAware nsa && nsa.NavigationService != null)
{
overrides.Add((typeof(INavigationService), nsa.NavigationService));
return new List<(Type Type, object Instance)>
{
(typeof(INavigationService), nsa.NavigationService)
};
}

return overrides;
return Array.Empty<(Type Type, object Instance)>(); ;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Prism.Regions.Adapters
/// Adapter that creates a new <see cref="SingleActiveRegion"/> and monitors its
/// active view to set it on the adapted <see cref="ContentView"/>.
/// </summary>
public class ContentViewRegionAdapter : RegionAdapterBase<ContentView>
public class ContentViewRegionAdapter : ContentViewRegionAdapter<ContentView>
{
/// <summary>
/// Initializes a new instance of <see cref="ContentViewRegionAdapter"/>.
Expand All @@ -21,13 +21,30 @@ public ContentViewRegionAdapter(IRegionBehaviorFactory regionBehaviorFactory)
: base(regionBehaviorFactory)
{
}
}

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

/// <summary>
/// Adapts a <see cref="ContentView"/> 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, ContentView regionTarget)
protected override void Adapt(IRegion region, TContentView regionTarget)
{
if (regionTarget == null)
throw new ArgumentNullException(nameof(regionTarget));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class RegionAdapterMappings
/// </summary>
/// <typeparam name="TControl">The type of the control</typeparam>
/// <typeparam name="TAdapter">The type of the IRegionAdapter to use with the TControl</typeparam>
/// <exception cref="InvalidOperationException">Throws <see cref="InvalidOperationException"/> when a mapping has already been defined for a specified control type.</exception>
public void RegisterMapping<TControl, TAdapter>() where TAdapter : IRegionAdapter
{
var controlType = typeof(TControl);
Expand All @@ -31,6 +32,34 @@ public void RegisterMapping<TControl, TAdapter>() where TAdapter : IRegionAdapte
mappings.Add(controlType, adapter);
}

/// <summary>
/// Removes an existing Registration if one exists and registers the new mapping between a type and an adapter.
/// </summary>
/// <typeparam name="TControl">The type of the control</typeparam>
/// <typeparam name="TAdapter">The type of the IRegionAdapter to use with the TControl</typeparam>
public void RegisterOrReplaceMapping<TControl, TAdapter>() where TAdapter : IRegionAdapter
{
var controlType = typeof(TControl);
var adapter = ContainerLocator.Container.Resolve<TAdapter>();

if (mappings.ContainsKey(controlType))
mappings.Remove(controlType);

mappings.Add(controlType, adapter);
}

internal void RegisterDefaultMapping<TControl, TAdapter>() where TAdapter : IRegionAdapter
{
var controlType = typeof(TControl);

if (mappings.ContainsKey(controlType))
return;

var adapter = ContainerLocator.Container.Resolve<TAdapter>();

mappings.Add(controlType, adapter);
}

/// <summary>
/// Returns the adapter associated with the type provided.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,18 @@ public interface INavigationContext
/// <value>The navigation URI.</value>
Uri Uri { get; }
}

public static class INavigationContextExtensions
{
public static string NavigatedName(this INavigationContext context)
{
var uri = context.Uri;
if (!uri.IsAbsoluteUri)
{
uri = new Uri(new Uri("nav://local.app"), context.Uri);
}

return uri.LocalPath.StartsWith("/") ? uri.LocalPath.Substring(1) : uri.LocalPath;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ protected virtual object CreateNewRegionItem(string candidateTargetContract)

newRegionItem = view;
}
catch(ContainerResolutionException)
{
throw;
}
catch (Exception e)
{
throw new InvalidOperationException(
Expand Down

0 comments on commit bc2fadb

Please sign in to comment.