Skip to content

Commit

Permalink
Update services
Browse files Browse the repository at this point in the history
  • Loading branch information
Nice3point committed Nov 14, 2023
1 parent 8e7c003 commit e036cdc
Show file tree
Hide file tree
Showing 63 changed files with 1,210 additions and 827 deletions.
104 changes: 23 additions & 81 deletions RevitLookup.UI.Demo/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT.
// Copyright (C) Leszek Pomianowski and WPF UI Contributors.
// All Rights Reserved.
// Copyright 2003-2023 by Autodesk, Inc.
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted,
// provided that the above copyright notice appears in all copies and
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
//
// Use, duplication, or disclosure by the U.S. Government is subject to
// restrictions set forth in FAR 52.227-19 (Commercial Computer
// Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
// (Rights in Technical Data and Computer Software), as applicable.

using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.Versioning;
using System.Windows;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using RevitLookup.Services;
using RevitLookup.Services.Contracts;
using RevitLookup.UI.Demo.Moq;
using RevitLookup.Utils;
using RevitLookup.ViewModels.Pages;
using RevitLookup.Views;
using RevitLookup.UI.Demo.Services;
using RevitLookup.Views.Pages;
using Wpf.Ui.Contracts;
using Wpf.Ui.Services;

namespace RevitLookup.UI.Demo;

Expand All @@ -30,12 +34,10 @@ public sealed partial class App
private void OnStartup(object sender, StartupEventArgs e)
{
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomainOnAssemblyResolve;
var host = CreateHost();
Host.StartHost(host);
var host = HostProvider.CreateHost();

var window = Host.GetService<IWindow>();
window.Show();
window.ServiceProvider.GetService<INavigationService>().Navigate(typeof(DashboardView));
Host.StartHost(host);
Host.GetService<MoqLookupService>().Show<DashboardView>();
}

private void OnExit(object sender, ExitEventArgs e)
Expand All @@ -46,66 +48,6 @@ private void OnExit(object sender, ExitEventArgs e)
Host.StopHost();
}

private static IHost CreateHost()
{
var host = Microsoft.Extensions.Hosting.Host
.CreateDefaultBuilder()
.ConfigureAppConfiguration(builder =>
{
var assembly = Assembly.GetExecutingAssembly();
var assemblyLocation = assembly.Location;
var addinVersion = FileVersionInfo.GetVersionInfo(assemblyLocation).ProductVersion;
#if RELEASE
var version = addinVersion.Split('.')[0];
if (version == "1") version = "Develop";
var programDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
var userDataLocation = Path.Combine(programDataPath, @"Autodesk\Revit\Addins\", version, "RevitLookup");
#else
var userDataLocation = Path.GetDirectoryName(assemblyLocation)!;
#endif
var appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
var writeAccess = AccessUtils.CheckWriteAccess(assemblyLocation) && !assemblyLocation.StartsWith(appDataPath);

var targetFrameworkAttributes = assembly.GetCustomAttributes(typeof(TargetFrameworkAttribute), true);
var targetFrameworkAttribute = (TargetFrameworkAttribute) targetFrameworkAttributes.First();
var targetFramework = targetFrameworkAttribute.FrameworkDisplayName;

builder.AddInMemoryCollection(new KeyValuePair<string, string>[]
{
new("Assembly", assemblyLocation),
new("Framework", targetFramework),
new("AddinVersion", addinVersion),
new("ConfigFolder", Path.Combine(userDataLocation, "Config")),
new("DownloadFolder", Path.Combine(userDataLocation, "Downloads")),
new("FolderAccess", writeAccess ? "Write" : "Read")
});
})
.ConfigureServices((_, services) =>
{
services.AddSingleton<ISettingsService, SettingsService>();
services.AddSingleton<ISoftwareUpdateService, SoftwareUpdateService>();

services.AddScoped<IWindowController, WindowController>();
services.AddScoped<INavigationService, NavigationService>();
services.AddScoped<ISnackbarService, SnackbarService>();
services.AddScoped<IContentDialogService, ContentDialogService>();

services.AddScoped<AboutView>();
services.AddScoped<AboutViewModel>();
services.AddScoped<DashboardView>();
services.AddScoped<DashboardViewModel>();
services.AddScoped<SettingsView>();
services.AddScoped<SettingsViewModel>();
services.AddScoped<EventsView>();
services.AddScoped<EventsViewModel>();
services.AddScoped<SnoopView>();
services.AddScoped<ISnoopService, MoqSnoopViewModel>();

services.AddTransient<IWindow, RevitLookupView>();
}).Build();
return host;
}

private Assembly CurrentDomainOnAssemblyResolve(object sender, ResolveEventArgs args)
{
var assemblyName = new AssemblyName(args.Name);
Expand Down
91 changes: 91 additions & 0 deletions RevitLookup.UI.Demo/HostProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.Versioning;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using RevitLookup.Services;
using RevitLookup.Services.Contracts;
using RevitLookup.UI.Demo.Services;
using RevitLookup.Utils;
using RevitLookup.ViewModels.Contracts;
using RevitLookup.ViewModels.Pages;
using RevitLookup.Views;
using RevitLookup.Views.Pages;
using Wpf.Ui.Contracts;
using Wpf.Ui.Services;
using MoqSnoopViewModel = RevitLookup.UI.Demo.ViewModels.MoqSnoopViewModel;

namespace RevitLookup.UI.Demo;

public static class HostProvider
{
public static IHost CreateHost()
{
var host = Microsoft.Extensions.Hosting.Host
.CreateDefaultBuilder()
.ConfigureAppConfiguration(SetConfiguration)
.ConfigureServices(AddServices)
.Build();

return host;
}

private static void AddServices(HostBuilderContext _, IServiceCollection services)
{
services.AddSingleton<ISettingsService, SettingsService>();
services.AddSingleton<ISoftwareUpdateService, SoftwareUpdateService>();

services.AddScoped<INavigationService, NavigationService>();
services.AddScoped<ISnackbarService, SnackbarService>();
services.AddScoped<IContentDialogService, ContentDialogService>();
services.AddScoped<NotificationService>();

services.AddScoped<ISnoopService, MoqSnoopService>();
services.AddScoped<AboutView>();
services.AddScoped<AboutViewModel>();
services.AddScoped<DashboardView>();
services.AddScoped<DashboardViewModel>();
services.AddScoped<SettingsView>();
services.AddScoped<SettingsViewModel>();
services.AddScoped<EventsView>();
services.AddScoped<EventsViewModel>();
services.AddScoped<SnoopView>();
services.AddScoped<ISnoopViewModel, MoqSnoopViewModel>();
services.AddScoped<IWindow, RevitLookupView>();

services.AddTransient<MoqLookupService>();
}

private static void SetConfiguration(IConfigurationBuilder builder)
{
var assembly = Assembly.GetExecutingAssembly();
var assemblyLocation = assembly.Location;
var addinVersion = FileVersionInfo.GetVersionInfo(assemblyLocation).ProductVersion;
#if RELEASE
var version = addinVersion.Split('.')[0];
if (version == "1") version = "Develop";
var programDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
var userDataLocation = Path.Combine(programDataPath, @"Autodesk\Revit\Addins\", version, "RevitLookup");
#else
var userDataLocation = Path.GetDirectoryName(assemblyLocation)!;
#endif
var appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
var writeAccess = AccessUtils.CheckWriteAccess(assemblyLocation) && !assemblyLocation.StartsWith(appDataPath);

var targetFrameworkAttributes = assembly.GetCustomAttributes(typeof(TargetFrameworkAttribute), true);
var targetFrameworkAttribute = (TargetFrameworkAttribute) targetFrameworkAttributes.First();
var targetFramework = targetFrameworkAttribute.FrameworkDisplayName;

builder.AddInMemoryCollection(new KeyValuePair<string, string>[]
{
new("Assembly", assemblyLocation),
new("Framework", targetFramework),
new("AddinVersion", addinVersion),
new("ConfigFolder", Path.Combine(userDataLocation, "Config")),
new("DownloadFolder", Path.Combine(userDataLocation, "Downloads")),
new("FolderAccess", writeAccess ? "Write" : "Read")
});
}
}
Loading

0 comments on commit e036cdc

Please sign in to comment.