Skip to content

Commit

Permalink
Merge pull request #14 from AvantiPoint/dev/ds/view-factory
Browse files Browse the repository at this point in the history
Open Up View Factory
  • Loading branch information
dansiegel authored Jan 27, 2024
2 parents c67c313 + 712174f commit b8dfe8a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 23 deletions.
35 changes: 13 additions & 22 deletions src/MauiMicroMvvm/Internals/ViewFactory.cs
Original file line number Diff line number Diff line change
@@ -1,47 +1,38 @@
#nullable enable
namespace MauiMicroMvvm.Internals;

internal class ViewFactory : IViewFactory
public class ViewFactory(IServiceProvider services, IEnumerable<ViewMapping> mappings) : IViewFactory
{
internal static readonly BindableProperty NavigationKeyProperty =
public static readonly BindableProperty NavigationKeyProperty =
BindableProperty.CreateAttached("NavigationKey", typeof(string), typeof(ViewFactory), null);

internal static string? GetNavigationKey(BindableObject bindable) =>
public static string? GetNavigationKey(BindableObject bindable) =>
(string?)bindable.GetValue(NavigationKeyProperty);

internal static void SetNavigationKey(BindableObject bindable, string? value) =>
public static void SetNavigationKey(BindableObject bindable, string? value) =>
bindable.SetValue(NavigationKeyProperty, value);

private IServiceProvider _services { get; }
private IEnumerable<ViewMapping> _mappings { get; }

public ViewFactory(IServiceProvider services, IEnumerable<ViewMapping> mappings)
{
_services = services;
_mappings = mappings;
}
protected IServiceProvider Services { get; } = services;
protected IEnumerable<ViewMapping> Mappings { get; } = mappings;

public TView CreateView<TView>()
where TView : VisualElement
{
var view = ActivatorUtilities.CreateInstance<TView>(_services);
var view = ActivatorUtilities.CreateInstance<TView>(Services);
Configure(view);
return view;
}

public VisualElement CreateView(string key)
{
var mapping = _mappings.FirstOrDefault(x => x.Name == key);
if(mapping is null)
throw new KeyNotFoundException(key);

var view = (VisualElement)ActivatorUtilities.CreateInstance(_services, mapping.View);
var mapping = Mappings.FirstOrDefault(x => x.Name == key) ?? throw new KeyNotFoundException(key);
var view = (VisualElement)ActivatorUtilities.CreateInstance(Services, mapping.View);
SetNavigationKey(view, key);
Configure(view);
return view;
}

public TView Configure<TView>(TView view)
public virtual TView Configure<TView>(TView view)
where TView : VisualElement
{
if(view.BindingContext is null && (!view.IsSet(Xaml.MauiMicro.AutowireProperty) || Xaml.MauiMicro.GetAutowire(view)))
Expand Down Expand Up @@ -93,14 +84,14 @@ private void SetBindingContext(VisualElement view)
if (mapping?.ViewModel is null)
return;

view.BindingContext = _services.GetRequiredService(mapping.ViewModel);
view.BindingContext = Services.GetRequiredService(mapping.ViewModel);
}

private ViewMapping? GetViewMapping(VisualElement view)
{
var key = GetNavigationKey(view);
return string.IsNullOrEmpty(key)
? _mappings.FirstOrDefault(x => x.View == view.GetType())
: _mappings.FirstOrDefault(x => x.Name == key);
? Mappings.FirstOrDefault(x => x.View == view.GetType())
: Mappings.FirstOrDefault(x => x.Name == key);
}
}
2 changes: 1 addition & 1 deletion src/MauiMicroMvvm/Internals/ViewMapping.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#nullable enable
namespace MauiMicroMvvm.Internals;

internal record ViewMapping(string Name, Type View, Type? ViewModel = null);
public record ViewMapping(string Name, Type View, Type? ViewModel = null);
4 changes: 4 additions & 0 deletions src/MauiMicroMvvm/MauiMicroMvvm.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
<Title>MauiMicroMvvm</Title>
</PropertyGroup>

<ItemGroup>
<None Include="build\Package.*" Pack="true" PackagePath="buildTransitive\$(PackageId)%(Extension)" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Maui.Controls" Version="8.0.3" />
</ItemGroup>
Expand Down
5 changes: 5 additions & 0 deletions src/MauiMicroMvvm/build/Package.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Project>
<ItemGroup Condition=" '$(ImplicitUsings)' == 'true' OR '$(ImplicitUsings)' == 'enable' ">
<Using Include="MauiMicroMvvm"/>
</ItemGroup>
</Project>

0 comments on commit b8dfe8a

Please sign in to comment.