From 572e39ad4f44b4d0eef5096c9f3d9ef85efe3740 Mon Sep 17 00:00:00 2001 From: Dan Siegel Date: Sat, 27 Jan 2024 09:18:07 -0600 Subject: [PATCH 1/2] feat: open up the ViewFactory --- src/MauiMicroMvvm/Internals/ViewFactory.cs | 35 ++++++++-------------- src/MauiMicroMvvm/Internals/ViewMapping.cs | 2 +- 2 files changed, 14 insertions(+), 23 deletions(-) diff --git a/src/MauiMicroMvvm/Internals/ViewFactory.cs b/src/MauiMicroMvvm/Internals/ViewFactory.cs index beee30e..b9b3a06 100644 --- a/src/MauiMicroMvvm/Internals/ViewFactory.cs +++ b/src/MauiMicroMvvm/Internals/ViewFactory.cs @@ -1,47 +1,38 @@ #nullable enable namespace MauiMicroMvvm.Internals; -internal class ViewFactory : IViewFactory +public class ViewFactory(IServiceProvider services, IEnumerable 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 _mappings { get; } - - public ViewFactory(IServiceProvider services, IEnumerable mappings) - { - _services = services; - _mappings = mappings; - } + protected IServiceProvider Services { get; } = services; + protected IEnumerable Mappings { get; } = mappings; public TView CreateView() where TView : VisualElement { - var view = ActivatorUtilities.CreateInstance(_services); + var view = ActivatorUtilities.CreateInstance(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 view) + public virtual TView Configure(TView view) where TView : VisualElement { if(view.BindingContext is null && (!view.IsSet(Xaml.MauiMicro.AutowireProperty) || Xaml.MauiMicro.GetAutowire(view))) @@ -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); } } diff --git a/src/MauiMicroMvvm/Internals/ViewMapping.cs b/src/MauiMicroMvvm/Internals/ViewMapping.cs index ab3ceb1..7d4279f 100644 --- a/src/MauiMicroMvvm/Internals/ViewMapping.cs +++ b/src/MauiMicroMvvm/Internals/ViewMapping.cs @@ -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); From 712174f3aead36cedac70b2bbdc51bf25db05ff5 Mon Sep 17 00:00:00 2001 From: Dan Siegel Date: Sat, 27 Jan 2024 09:19:08 -0600 Subject: [PATCH 2/2] feat: support Implicit Usings --- src/MauiMicroMvvm/MauiMicroMvvm.csproj | 4 ++++ src/MauiMicroMvvm/build/Package.targets | 5 +++++ 2 files changed, 9 insertions(+) create mode 100644 src/MauiMicroMvvm/build/Package.targets diff --git a/src/MauiMicroMvvm/MauiMicroMvvm.csproj b/src/MauiMicroMvvm/MauiMicroMvvm.csproj index e224089..76bc292 100644 --- a/src/MauiMicroMvvm/MauiMicroMvvm.csproj +++ b/src/MauiMicroMvvm/MauiMicroMvvm.csproj @@ -11,6 +11,10 @@ MauiMicroMvvm + + + + diff --git a/src/MauiMicroMvvm/build/Package.targets b/src/MauiMicroMvvm/build/Package.targets new file mode 100644 index 0000000..0789d0d --- /dev/null +++ b/src/MauiMicroMvvm/build/Package.targets @@ -0,0 +1,5 @@ + + + + +