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

fix: fix #21 circulation dependence #22

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
15 changes: 8 additions & 7 deletions src/MauiMicroMvvm/Internals/DefaultNavigation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,23 @@ namespace MauiMicroMvvm.Internals;
[EditorBrowsable(EditorBrowsableState.Never)]
public class DefaultNavigation<TShell> : INavigation where TShell : Shell
{
private readonly TShell _shell;
private readonly Lazy<TShell> _lazyShell;
private TShell Shell => _lazyShell.Value;

public DefaultNavigation(TShell shell)
public DefaultNavigation(IServiceProvider services)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should avoid injecting the IServiceProvider unless it is absolutely necessary. In this case it make more sense to simply add a registration for Lazy<TShell> and inject it directly.

{
_shell = shell;
_lazyShell = new Lazy<TShell>(services.GetRequiredService<TShell>);
}

public async Task GoToAsync(string uri) =>
await _shell.GoToAsync(uri);
await Shell.GoToAsync(uri);

public async Task GoToAsync(string uri, IDictionary<string, object> parameters) =>
await _shell.GoToAsync(uri, parameters);
await Shell.GoToAsync(uri, parameters);

public async Task GoToAsync(string uri, bool animate) =>
await _shell.GoToAsync(uri, animate);
await Shell.GoToAsync(uri, animate);

public async Task GoToAsync(string uri, bool animate, IDictionary<string, object> parameters) =>
await _shell.GoToAsync(uri, animate, parameters);
await Shell.GoToAsync(uri, animate, parameters);
}
19 changes: 10 additions & 9 deletions src/MauiMicroMvvm/Internals/PageDialogs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,29 @@

internal class PageDialogs<TShell> : IPageDialogs where TShell : Shell
{
private readonly TShell _shell;
private readonly Lazy<TShell> _lazyShell;
private TShell Shell => _lazyShell.Value;

public PageDialogs(TShell shell)
public PageDialogs(IServiceProvider services)
{
_shell = shell;
_lazyShell = new Lazy<TShell>(services.GetRequiredService<TShell>);
}

public Task<string> DisplayActionSheet(string title, string cancel, string destruction, params string[] buttons) =>
_shell.DisplayActionSheet(title, cancel, destruction, buttons);
Shell.DisplayActionSheet(title, cancel, destruction, buttons);

public Task<string> DisplayActionSheet(string title, string cancel, string destruction, FlowDirection flowDirection, params string[] buttons) =>
_shell.DisplayActionSheet(title, cancel, destruction, flowDirection, buttons);
Shell.DisplayActionSheet(title, cancel, destruction, flowDirection, buttons);

public Task DisplayAlert(string title, string message, string cancel) =>
_shell.DisplayAlert(title, message, cancel);
Shell.DisplayAlert(title, message, cancel);

public Task DisplayAlert(string title, string message, string cancel, FlowDirection flowDirection) =>
_shell.DisplayAlert(title, message, cancel, flowDirection);
Shell.DisplayAlert(title, message, cancel, flowDirection);

public Task<bool> DisplayAlert(string title, string message, string accept, string cancel) =>
_shell.DisplayAlert(title, message, accept, cancel);
Shell.DisplayAlert(title, message, accept, cancel);

public Task<bool> DisplayAlert(string title, string message, string accept, string cancel, FlowDirection flowDirection) =>
_shell.DisplayAlert(title, message, accept, cancel, flowDirection);
Shell.DisplayAlert(title, message, accept, cancel, flowDirection);
}
16 changes: 9 additions & 7 deletions src/MauiMicroMvvm/MauiMicroBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,19 @@ public static IServiceCollection MapView<TView, TViewModel>(this IServiceCollect
if (typeof(TView).IsAssignableTo(typeof(Page)))
Routing.RegisterRoute(key, typeof(TView));

return services.AddTransient<TView>(sp =>
{
var viewFactory = sp.GetRequiredService<IViewFactory>();
var view = viewFactory.CreateView<TView>();
viewFactory.Configure(view);
return view;
})
return (typeof(TView).IsAssignableTo(typeof(Shell)) ? services.AddSingleton(CreateView<TView>) : services.AddTransient(CreateView<TView>))
.AddSingleton(new ViewMapping(key, typeof(TView), typeof(TViewModel)))
.AddTransient<TViewModel>();
}

private static TView CreateView<TView>(IServiceProvider sp) where TView : VisualElement
{
var viewFactory = sp.GetRequiredService<IViewFactory>();
var view = viewFactory.CreateView<TView>();
viewFactory.Configure(view);
return view;
}

public static IServiceCollection ApplyBehavior<TView, TBehavior>(this IServiceCollection services)
where TView : VisualElement
where TBehavior : Behavior
Expand Down
Loading