-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add preliminary MvxHost and MvxHostBuilder inspired by Shiny and MAUI
- Loading branch information
1 parent
662e331
commit de08dcb
Showing
8 changed files
with
119 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#nullable enable | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace MvvmCross.Hosting; | ||
|
||
public interface IMvxHost : IDisposable | ||
{ | ||
IServiceProvider Services { get; } | ||
ILoggerFactory Logging { get; } | ||
|
||
void Run(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#nullable enable | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace MvvmCross.Hosting; | ||
|
||
public interface IMvxHostBuilder | ||
{ | ||
Func<IServiceCollection, IServiceProvider>? ConfigureContainer { get; } | ||
IServiceCollection Services { get; } | ||
|
||
IMvxHost Build(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#nullable enable | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using MvvmCross.Base; | ||
|
||
namespace MvvmCross.Hosting; | ||
|
||
public sealed class MvxHost : IMvxHost | ||
{ | ||
private static IMvxHost? _currentHost; | ||
|
||
public static IMvxHost Current | ||
{ | ||
get | ||
{ | ||
if (_currentHost == null) | ||
throw new InvalidOperationException("MvxHost has not been created yet. This means MvvmCross hasn't been initialized properly."); | ||
|
||
return _currentHost; | ||
} | ||
private set => _currentHost = value; | ||
} | ||
|
||
public static bool IsInitialized => _currentHost != null; | ||
public static IServiceProvider ServiceProvider => Current.Services; | ||
public static ILoggerFactory LoggingFactory => Current.Logging; | ||
public static T? GetService<T>() => ServiceProvider.GetService<T>(); | ||
|
||
public IServiceProvider Services { get; init; } | ||
public ILoggerFactory Logging { get; init; } | ||
|
||
public MvxHost(IServiceProvider serviceProvider, ILoggerFactory loggerFactory) | ||
{ | ||
Services = serviceProvider; | ||
Logging = loggerFactory; | ||
} | ||
|
||
public void Run() | ||
{ | ||
Current = this; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
Services.DisposeIfDisposable(); | ||
Logging.Dispose(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#nullable enable | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Logging.Abstractions; | ||
|
||
namespace MvvmCross.Hosting; | ||
|
||
public class MvxHostBuilder : IMvxHostBuilder | ||
{ | ||
public Func<IServiceCollection, IServiceProvider> ConfigureContainer { get; } | ||
public IServiceCollection Services { get; } | ||
|
||
public MvxHostBuilder( | ||
IServiceCollection? serviceCollection = null, | ||
Func<IServiceCollection, IServiceProvider>? configureContainer = null) | ||
{ | ||
Services = serviceCollection ?? new ServiceCollection(); | ||
ConfigureContainer = configureContainer ?? (s => s.BuildServiceProvider()); | ||
} | ||
|
||
public virtual IMvxHost Build() | ||
{ | ||
ConfigureDefaultNullLogging(Services); | ||
var serviceCollection = ConfigureContainer(Services); | ||
var loggerFactory = serviceCollection.GetRequiredService<ILoggerFactory>(); | ||
|
||
var host = new MvxHost(serviceCollection, loggerFactory); | ||
|
||
#if NET7_0_OR_GREATER | ||
#endif | ||
|
||
host.Run(); | ||
|
||
return host; | ||
} | ||
|
||
private static void ConfigureDefaultNullLogging(IServiceCollection services) | ||
{ | ||
services.AddSingleton<ILoggerFactory, NullLoggerFactory>(); | ||
services.AddSingleton(typeof(ILogger<>), typeof(NullLogger<>)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters