Skip to content

Commit

Permalink
refactor: top level namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
K12f committed Sep 24, 2024
1 parent c37eada commit b7dd3d0
Show file tree
Hide file tree
Showing 27 changed files with 1,308 additions and 1,539 deletions.
188 changes: 93 additions & 95 deletions src/BlueCatKoKo.Ui/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,121 +16,119 @@
using Serilog;
using Wpf.Ui;

namespace BlueCatKoKo.Ui
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
private static readonly IHost _host = Host.CreateDefaultBuilder()
.ConfigureHostConfiguration(builder =>
{
// builder.AddJsonFile("appsettings.json");
})
.ConfigureLogging(logging =>
{
logging.ClearProviders();
Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
.WriteTo.File(
"log.txt", rollingInterval: RollingInterval.Day, encoding: Encoding.UTF8
)
.CreateLogger();
logging.Services.AddSingleton(Log.Logger);
})
.ConfigureServices((context, container) =>
{
container.AddHostedService<ApplicationHostService>();
// configuration
// 绑定 AppConfig 配置段
var appConfig = new AppConfig();
context.Configuration.GetSection(nameof(AppConfig)).Bind(appConfig);
namespace BlueCatKoKo.Ui;

// 获取程序集版本
var version = Assembly.GetExecutingAssembly().GetName().Version?.ToString();
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
private static readonly IHost _host = Host.CreateDefaultBuilder()
.ConfigureHostConfiguration(builder =>
{
// builder.AddJsonFile("appsettings.json");
})
.ConfigureLogging(logging =>
{
logging.ClearProviders();
Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
.WriteTo.File(
"log.txt", rollingInterval: RollingInterval.Day, encoding: Encoding.UTF8
)
.CreateLogger();
logging.Services.AddSingleton(Log.Logger);
})
.ConfigureServices((context, container) =>
{
container.AddHostedService<ApplicationHostService>();
// configuration
// 绑定 AppConfig 配置段
var appConfig = new AppConfig();
context.Configuration.GetSection(nameof(AppConfig)).Bind(appConfig);

appConfig.Version = version; // 将版本赋值给 AppConfig
// 获取程序集版本
var version = Assembly.GetExecutingAssembly().GetName().Version?.ToString();

// container.Configure<AppConfig>(context.Configuration.GetSection(nameof(AppConfig)));
appConfig.Version = version; // 将版本赋值给 AppConfig

container.AddSingleton(appConfig);
container.Configure<AppConfig>(config =>
{
config.Name = appConfig.Name;
config.TrayTitle = appConfig.TrayTitle;
config.Description = appConfig.Description;
config.DownloadPath = appConfig.DownloadPath;
config.RepositoryUrl = appConfig.RepositoryUrl;
config.Version = appConfig.Version;
});
container.AddSingleton<AppConfigService>();
// container.Configure<AppConfig>(context.Configuration.GetSection(nameof(AppConfig)));
Log.Information("AppConfig: {@appConfig}", appConfig);
container.Configure<AppConfig>(config =>
{
config.Name = appConfig.Name;
config.TrayTitle = appConfig.TrayTitle;
config.Description = appConfig.Description;
config.DownloadPath = appConfig.DownloadPath;
config.RepositoryUrl = appConfig.RepositoryUrl;
config.Version = appConfig.Version;
});
container.AddSingleton<AppConfigService>();

// Service containing
container.AddSingleton<INavigationService, NavigationService>();
container.AddTransient<DouYinShortVideoService>();
container.AddTransient<KuaiShouShortVideoService>();
// Service containing
container.AddSingleton<INavigationService, NavigationService>();
container.AddTransient<DouYinShortVideoService>();
container.AddTransient<KuaiShouShortVideoService>();

// Main window with navigation
container.AddSingleton<MainWindowViewModel>();
container.AddSingleton<INavigationWindow, MainWindow>();
// Main window with navigation
container.AddSingleton<MainWindowViewModel>();
container.AddSingleton<INavigationWindow, MainWindow>();


//view and viewmodels
container.AddSingleton<HomePage>();
container.AddSingleton<HomeViewModel>();
//view and viewmodels
container.AddSingleton<HomePage>();
container.AddSingleton<HomeViewModel>();

container.AddSingleton<VideoPage>();
container.AddSingleton<VideoViewModel>();
container.AddSingleton<VideoPage>();
container.AddSingleton<VideoViewModel>();

container.AddSingleton<CookiesPage>();
container.AddSingleton<CookiesViewModel>();
container.AddSingleton<CookiesPage>();
container.AddSingleton<CookiesViewModel>();

container.AddSingleton<SettingsPage>();
container.AddSingleton<SettingsViewModel>();
container.AddSingleton<SettingsPage>();
container.AddSingleton<SettingsViewModel>();

container.AddSingleton<AboutPage>();
container.AddSingleton<AboutViewModel>();
container.AddSingleton<AboutPage>();
container.AddSingleton<AboutViewModel>();

// messager
container.AddSingleton<WeakReferenceMessenger>();
container.AddSingleton<IMessenger>(sp => sp.GetRequiredService<WeakReferenceMessenger>());
// messager
container.AddSingleton<WeakReferenceMessenger>();
container.AddSingleton<IMessenger>(sp => sp.GetRequiredService<WeakReferenceMessenger>());

container.AddSingleton(_ => Current.Dispatcher);
}).Build();
container.AddSingleton(_ => Current.Dispatcher);
}).Build();

public static new App Current => (App)Application.Current;
public new static App Current => (App)Application.Current;

/// <summary>
/// Gets services.
/// </summary>
public static IServiceProvider Services => _host.Services;
/// <summary>
/// Gets services.
/// </summary>
public static IServiceProvider Services => _host.Services;


/// <summary>
/// Occurs when the application is loading.
/// </summary>
private async void OnStartup(object sender, StartupEventArgs e)
{
await _host.StartAsync();
}
/// <summary>
/// Occurs when the application is loading.
/// </summary>
private async void OnStartup(object sender, StartupEventArgs e)
{
await _host.StartAsync();
}

/// <summary>
/// Occurs when the application is closing.
/// </summary>
private async void OnExit(object sender, ExitEventArgs e)
{
await _host.StopAsync();
/// <summary>
/// Occurs when the application is closing.
/// </summary>
private async void OnExit(object sender, ExitEventArgs e)
{
await _host.StopAsync();

_host.Dispose();
}
_host.Dispose();
}

/// <summary>
/// Occurs when an exception is thrown by an application but not handled.
/// </summary>
private void OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
// For more info see https://docs.microsoft.com/en-us/dotnet/api/system.windows.application.dispatcherunhandledexception?view=windowsdesktop-6.0
}
/// <summary>
/// Occurs when an exception is thrown by an application but not handled.
/// </summary>
private void OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
// For more info see https://docs.microsoft.com/en-us/dotnet/api/system.windows.application.dispatcherunhandledexception?view=windowsdesktop-6.0
}
}
21 changes: 10 additions & 11 deletions src/BlueCatKoKo.Ui/Models/AppConfig.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
namespace BlueCatKoKo.Ui.Models
namespace BlueCatKoKo.Ui.Models;

public class AppConfig
{
public class AppConfig
{
public string? Name { get; set; }
public string? TrayTitle { get; set; }
public string? Version { get; set; }
public string? Description { get; set; }
public string? DownloadPath { get; set; }
public string? RepositoryUrl { get; set; }
public List<string>? Cookies { get; set; }
}
public string? Name { get; set; }
public string? TrayTitle { get; set; }
public string? Version { get; set; }
public string? Description { get; set; }
public string? DownloadPath { get; set; }
public string? RepositoryUrl { get; set; }
public List<string>? Cookies { get; set; }
}
17 changes: 17 additions & 0 deletions src/BlueCatKoKo.Ui/Models/AppLogging.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Newtonsoft.Json;

namespace BlueCatKoKo.Ui.Models;

public class AppLogging
{
[JsonProperty("LogLevel")] public LogLevel LogLevel { get; set; }
}

public class LogLevel
{
[JsonProperty("Microsoft")] public string Microsoft { get; set; }

[JsonProperty("Default")] public string Default { get; set; }

[JsonProperty("System")] public string System { get; set; }
}
12 changes: 12 additions & 0 deletions src/BlueCatKoKo.Ui/Models/AppSetting.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// YApi QuickType插件生成,具体参考文档:https://plugins.jetbrains.com/plugin/18847-yapi-quicktype/documentation

using Newtonsoft.Json;

namespace BlueCatKoKo.Ui.Models;

public class AppSetting
{
[JsonProperty("Logging")] public AppLogging AppLogging { get; set; }

[JsonProperty("AppConfig")] public AppConfig AppConfig { get; set; }
}
Loading

0 comments on commit b7dd3d0

Please sign in to comment.