From 0d32e6a5c3a737e60bcfc3b091fab9059c6c198c Mon Sep 17 00:00:00 2001 From: AliveDevil Date: Fri, 9 Feb 2024 01:23:38 +0100 Subject: [PATCH] Avalonia Boilerplate --- Directory.Packages.props | 2 ++ app/InkForge.Common/App.axaml | 1 - app/InkForge.Common/App.axaml.cs | 28 +++++++++---------- .../Controllers/WorkspaceController.cs | 5 ++++ .../Data/NoteDbContextFactory.cs | 15 ++++++++++ app/InkForge.Common/InkForge.Common.csproj | 1 + .../InkForgeServiceCollection.cs | 19 +++++++++++++ .../Properties/ConfigContext.cs | 7 +++++ .../ViewModels/AppViewModel.cs | 20 +++++++++++++ .../ViewModels/LandingViewModel.cs | 8 ++++++ app/InkForge.Desktop/InkForge.Desktop.csproj | 2 +- app/InkForge.Desktop/Program.cs | 10 +++---- app/InkForge.Desktop/Views/MainWindow.axaml | 9 ++++++ .../Views/MainWindow.axaml.cs | 13 +++++++++ 14 files changed, 117 insertions(+), 23 deletions(-) create mode 100644 app/InkForge.Common/Controllers/WorkspaceController.cs create mode 100644 app/InkForge.Common/Data/NoteDbContextFactory.cs create mode 100644 app/InkForge.Common/Microsoft/Extensions/DependencyInjection/InkForgeServiceCollection.cs create mode 100644 app/InkForge.Common/Properties/ConfigContext.cs create mode 100644 app/InkForge.Common/ViewModels/AppViewModel.cs create mode 100644 app/InkForge.Common/ViewModels/LandingViewModel.cs create mode 100644 app/InkForge.Desktop/Views/MainWindow.axaml create mode 100644 app/InkForge.Desktop/Views/MainWindow.axaml.cs diff --git a/Directory.Packages.props b/Directory.Packages.props index 43990f0..e68bcaf 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -6,6 +6,7 @@ + @@ -19,6 +20,7 @@ + diff --git a/app/InkForge.Common/App.axaml b/app/InkForge.Common/App.axaml index 9cccb4b..0f70103 100644 --- a/app/InkForge.Common/App.axaml +++ b/app/InkForge.Common/App.axaml @@ -1,6 +1,5 @@ diff --git a/app/InkForge.Common/App.axaml.cs b/app/InkForge.Common/App.axaml.cs index fa1c765..136dadf 100644 --- a/app/InkForge.Common/App.axaml.cs +++ b/app/InkForge.Common/App.axaml.cs @@ -3,6 +3,10 @@ using Avalonia.Controls; using Avalonia.Controls.ApplicationLifetimes; using Avalonia.Markup.Xaml; +using InkForge.Common.ViewModels; + +using Microsoft.Extensions.DependencyInjection; + using ReactiveUI; namespace InkForge.Common; @@ -20,21 +24,15 @@ public partial class App : Application public override void OnFrameworkInitializationCompleted() { - // var viewModel = Services.Activate(); - // var view = ViewLocator.Current.ResolveView(viewModel); - // switch (ApplicationLifetime) - // { - // case IClassicDesktopStyleApplicationLifetime desktop: - // desktop.MainWindow = view as Window; - // break; - - // case ISingleViewApplicationLifetime singleView: - // singleView.MainView = view as Control; - // break; - - // default: - // throw new NotSupportedException(); - // } + var viewModel = ActivatorUtilities.GetServiceOrCreateInstance(ServiceProvider); + var view = ViewLocator.Current.ResolveView(viewModel)!; + view.ViewModel = viewModel; + _ = ApplicationLifetime switch + { + IClassicDesktopStyleApplicationLifetime desktop => desktop.MainWindow = view as Window, + ISingleViewApplicationLifetime singleView => singleView.MainView = view as Control, + _ => throw new NotSupportedException(), + }; base.OnFrameworkInitializationCompleted(); } diff --git a/app/InkForge.Common/Controllers/WorkspaceController.cs b/app/InkForge.Common/Controllers/WorkspaceController.cs new file mode 100644 index 0000000..01c8be3 --- /dev/null +++ b/app/InkForge.Common/Controllers/WorkspaceController.cs @@ -0,0 +1,5 @@ +namespace InkForge.Common.Controllers; + +public class WorkspaceController +{ +} diff --git a/app/InkForge.Common/Data/NoteDbContextFactory.cs b/app/InkForge.Common/Data/NoteDbContextFactory.cs new file mode 100644 index 0000000..8de299e --- /dev/null +++ b/app/InkForge.Common/Data/NoteDbContextFactory.cs @@ -0,0 +1,15 @@ +using InkForge.Data; + +using Microsoft.EntityFrameworkCore; + +namespace InkForge.Common.Data; + +public class NoteDbContextFactory : IDbContextFactory +{ + + public NoteDbContext CreateDbContext() + { + return new NoteDbContext(null); + } + +} diff --git a/app/InkForge.Common/InkForge.Common.csproj b/app/InkForge.Common/InkForge.Common.csproj index cee79e5..9847d58 100644 --- a/app/InkForge.Common/InkForge.Common.csproj +++ b/app/InkForge.Common/InkForge.Common.csproj @@ -8,6 +8,7 @@ + diff --git a/app/InkForge.Common/Microsoft/Extensions/DependencyInjection/InkForgeServiceCollection.cs b/app/InkForge.Common/Microsoft/Extensions/DependencyInjection/InkForgeServiceCollection.cs new file mode 100644 index 0000000..26a8152 --- /dev/null +++ b/app/InkForge.Common/Microsoft/Extensions/DependencyInjection/InkForgeServiceCollection.cs @@ -0,0 +1,19 @@ +using InkForge.Common.Controllers; +using InkForge.Common.Data; +using InkForge.Data; + +namespace Microsoft.Extensions.DependencyInjection; + +public static class InkForgeServiceCollections +{ + public static IServiceCollection AddInkForge(this IServiceCollection services) + { + services.AddHttpClient(); + + services.AddDbContextFactory(); + + services.AddSingleton(); + + return services; + } +} diff --git a/app/InkForge.Common/Properties/ConfigContext.cs b/app/InkForge.Common/Properties/ConfigContext.cs new file mode 100644 index 0000000..31c97df --- /dev/null +++ b/app/InkForge.Common/Properties/ConfigContext.cs @@ -0,0 +1,7 @@ +using System.Text.Json.Serialization; + +namespace InkForge.Common.Properties; + +[JsonSerializable(typeof(IDictionary))] +[JsonSourceGenerationOptions(GenerationMode = JsonSourceGenerationMode.Metadata)] +public partial class ConfigContext : JsonSerializerContext; diff --git a/app/InkForge.Common/ViewModels/AppViewModel.cs b/app/InkForge.Common/ViewModels/AppViewModel.cs new file mode 100644 index 0000000..88a9a6a --- /dev/null +++ b/app/InkForge.Common/ViewModels/AppViewModel.cs @@ -0,0 +1,20 @@ +using InkForge.Common.Controllers; + +using ReactiveUI; + +namespace InkForge.Common.ViewModels; + +public class AppViewModel : ReactiveObject +{ + private object _view; + + public object View + { + get => _view; + set => this.RaiseAndSetIfChanged(ref _view, value); + } + + public AppViewModel(WorkspaceController workspace) + { + } +} diff --git a/app/InkForge.Common/ViewModels/LandingViewModel.cs b/app/InkForge.Common/ViewModels/LandingViewModel.cs new file mode 100644 index 0000000..0d4de9a --- /dev/null +++ b/app/InkForge.Common/ViewModels/LandingViewModel.cs @@ -0,0 +1,8 @@ +using ReactiveUI; + +namespace InkForge.Common.ViewModels; + +public class LandingViewModel : ReactiveObject +{ + +} diff --git a/app/InkForge.Desktop/InkForge.Desktop.csproj b/app/InkForge.Desktop/InkForge.Desktop.csproj index 9d2d36f..3e749c6 100644 --- a/app/InkForge.Desktop/InkForge.Desktop.csproj +++ b/app/InkForge.Desktop/InkForge.Desktop.csproj @@ -17,4 +17,4 @@ - + \ No newline at end of file diff --git a/app/InkForge.Desktop/Program.cs b/app/InkForge.Desktop/Program.cs index bb5a5f0..b5b9da6 100644 --- a/app/InkForge.Desktop/Program.cs +++ b/app/InkForge.Desktop/Program.cs @@ -1,8 +1,9 @@ -using Avalonia; +using Avalonia; using Avalonia.ReactiveUI; using Avalonia.Threading; using InkForge.Common; +using InkForge.Data; using Microsoft.Extensions.DependencyInjection; @@ -23,6 +24,7 @@ static class Program => AppBuilder.Configure() .UsePlatformDetect() .UseReactiveUI() + .WithInterFont() .LogToTrace(); private static void ConfigureServices(IServiceCollection services) @@ -32,11 +34,7 @@ static class Program mutableResolver.InitializeSplat(); mutableResolver.InitializeReactiveUI(); - services.AddHttpClient(); - - // services.UseFactories(); - // services.AddViewModelFactory(); - // services.AddTransient, MainWindow>(); + services.AddInkForge(); } private static void OnSetup(this IServiceCollection services, AppBuilder appBuilder) diff --git a/app/InkForge.Desktop/Views/MainWindow.axaml b/app/InkForge.Desktop/Views/MainWindow.axaml new file mode 100644 index 0000000..668eae7 --- /dev/null +++ b/app/InkForge.Desktop/Views/MainWindow.axaml @@ -0,0 +1,9 @@ + + + \ No newline at end of file diff --git a/app/InkForge.Desktop/Views/MainWindow.axaml.cs b/app/InkForge.Desktop/Views/MainWindow.axaml.cs new file mode 100644 index 0000000..65b2d21 --- /dev/null +++ b/app/InkForge.Desktop/Views/MainWindow.axaml.cs @@ -0,0 +1,13 @@ +using Avalonia.ReactiveUI; + +using InkForge.Common.ViewModels; + +namespace InkForge.Desktop.Views; + +public partial class MainWindow : ReactiveWindow +{ + public MainWindow() + { + InitializeComponent(); + } +}