reworked start up
This commit is contained in:
parent
079c63188d
commit
6b835b1895
38
App.axaml.cs
38
App.axaml.cs
@ -1,8 +1,10 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using Avalonia;
|
||||
using Avalonia.Controls.ApplicationLifetimes;
|
||||
using Avalonia.Data.Core.Plugins;
|
||||
using Avalonia.Markup.Xaml;
|
||||
using Avalonia.Threading;
|
||||
using BetterRaid.Extensions;
|
||||
using BetterRaid.Services;
|
||||
using BetterRaid.Services.Implementations;
|
||||
@ -14,43 +16,49 @@ namespace BetterRaid;
|
||||
|
||||
public class App : Application
|
||||
{
|
||||
private static readonly ServiceCollection Services = [];
|
||||
private static ServiceProvider? _serviceProvider;
|
||||
|
||||
public static IServiceProvider? ServiceProvider => _serviceProvider;
|
||||
private ServiceProvider? _serviceProvider;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
InitializeServices();
|
||||
|
||||
_serviceProvider = InitializeServices();
|
||||
AvaloniaXamlLoader.Load(_serviceProvider, this);
|
||||
}
|
||||
|
||||
private void InitializeServices()
|
||||
private ServiceProvider InitializeServices()
|
||||
{
|
||||
var Services = new ServiceCollection();
|
||||
Services.AddSingleton<ITwitchDataService, TwitchDataService>();
|
||||
Services.AddSingleton<ITwitchPubSubService, TwitchPubSubService>();
|
||||
Services.AddTransient<MainWindowViewModel>();
|
||||
Services.AddSingleton<ISynchronizaionService, DispatcherService>(serviceProvider => new DispatcherService(Dispatcher.UIThread));
|
||||
Services.AddTransient<IMainViewModelFactory, MainWindowViewModelFactory>();
|
||||
|
||||
_serviceProvider = Services.BuildServiceProvider();
|
||||
return Services.BuildServiceProvider();
|
||||
}
|
||||
|
||||
public override void OnFrameworkInitializationCompleted()
|
||||
{
|
||||
BindingPlugins.DataValidators.RemoveAt(0);
|
||||
|
||||
if(_serviceProvider == null)
|
||||
{
|
||||
throw new FieldAccessException($"\"{nameof(_serviceProvider)}\" was null");
|
||||
}
|
||||
|
||||
var viewModelFactory = _serviceProvider.GetRequiredService<IMainViewModelFactory>();
|
||||
var mainWindowViewModel = viewModelFactory.CreateMainWindowViewModel();
|
||||
var mainWindow = new MainWindow()
|
||||
{
|
||||
DataContext = mainWindowViewModel
|
||||
};
|
||||
|
||||
switch (ApplicationLifetime)
|
||||
{
|
||||
case IClassicDesktopStyleApplicationLifetime desktop:
|
||||
desktop.MainWindow = new MainWindow();
|
||||
desktop.MainWindow.InjectDataContext<MainWindowViewModel>();
|
||||
|
||||
desktop.MainWindow = mainWindow;
|
||||
break;
|
||||
|
||||
case ISingleViewApplicationLifetime singleViewPlatform:
|
||||
singleViewPlatform.MainView = new MainWindow();
|
||||
singleViewPlatform.MainView.InjectDataContext<MainWindowViewModel>();
|
||||
|
||||
singleViewPlatform.MainView = mainWindow;
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -1,21 +0,0 @@
|
||||
using Avalonia;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace BetterRaid.Extensions;
|
||||
|
||||
public static class DataContextExtensions
|
||||
{
|
||||
public static T? GetDataContextAs<T>(this T obj) where T : StyledElement
|
||||
{
|
||||
return obj.DataContext as T;
|
||||
}
|
||||
|
||||
public static void InjectDataContext<T>(this StyledElement e) where T : class
|
||||
{
|
||||
if (App.ServiceProvider == null)
|
||||
return;
|
||||
|
||||
var vm = App.ServiceProvider.GetRequiredService<T>();
|
||||
e.DataContext = vm;
|
||||
}
|
||||
}
|
@ -15,7 +15,7 @@ public static class Tools
|
||||
private static HttpListener? _oauthListener;
|
||||
|
||||
// Source: https://stackoverflow.com/a/43232486
|
||||
public static void StartOAuthLogin(string url, Action? callback = null, CancellationToken token = default)
|
||||
public static void StartOAuthLogin(string url, ITwitchDataService twitchDataService, Action? callback = null, CancellationToken token = default)
|
||||
{
|
||||
if (_oauthListener == null)
|
||||
{
|
||||
@ -23,13 +23,13 @@ public static class Tools
|
||||
_oauthListener.Prefixes.Add(Constants.TwitchOAuthRedirectUrl + "/");
|
||||
_oauthListener.Start();
|
||||
|
||||
Task.Run(() => WaitForCallback(callback, token), token);
|
||||
Task.Run(() => WaitForCallback(callback, token, twitchDataService), token);
|
||||
}
|
||||
|
||||
OpenUrl(url);
|
||||
}
|
||||
|
||||
private static async Task WaitForCallback(Action? callback, CancellationToken token)
|
||||
private static async Task WaitForCallback(Action? callback, CancellationToken token, ITwitchDataService twitchDataService)
|
||||
{
|
||||
if (_oauthListener == null)
|
||||
return;
|
||||
@ -107,11 +107,7 @@ public static class Tools
|
||||
|
||||
var accessToken = jsonData["access_token"]?.ToString();
|
||||
|
||||
var dataService = App.ServiceProvider?.GetService(typeof(ITwitchDataService));
|
||||
if (dataService is ITwitchDataService twitchDataService)
|
||||
{
|
||||
twitchDataService.ConnectApiAsync(Constants.TwitchClientId, accessToken!);
|
||||
}
|
||||
twitchDataService.ConnectApiAsync(Constants.TwitchClientId, accessToken!);
|
||||
|
||||
res.StatusCode = 200;
|
||||
res.Close();
|
||||
|
@ -19,6 +19,7 @@ public class MainWindowViewModel : ViewModelBase
|
||||
private ObservableCollection<TwitchChannel> _channels = [];
|
||||
private readonly BetterRaidDatabase? _db;
|
||||
private readonly ITwitchPubSubService _pubSub;
|
||||
private readonly ISynchronizaionService _synchronizaionService;
|
||||
|
||||
public BetterRaidDatabase? Database
|
||||
{
|
||||
@ -50,10 +51,11 @@ public class MainWindowViewModel : ViewModelBase
|
||||
|
||||
public bool IsLoggedIn => DataService.UserChannel != null;
|
||||
|
||||
public MainWindowViewModel(ITwitchPubSubService pubSub, ITwitchDataService dataService)
|
||||
public MainWindowViewModel(ITwitchPubSubService pubSub, ITwitchDataService dataService, ISynchronizaionService synchronizaionService)
|
||||
{
|
||||
_pubSub = pubSub;
|
||||
DataService = dataService;
|
||||
_synchronizaionService = synchronizaionService;
|
||||
DataService.PropertyChanged += OnDataServicePropertyChanged;
|
||||
|
||||
Database = BetterRaidDatabase.LoadFromFile(Constants.DatabaseFilePath);
|
||||
@ -75,7 +77,7 @@ public class MainWindowViewModel : ViewModelBase
|
||||
|
||||
public void LoginWithTwitch()
|
||||
{
|
||||
Tools.StartOAuthLogin(DataService.GetOAuthUrl(), OnTwitchLoginCallback, CancellationToken.None);
|
||||
Tools.StartOAuthLogin(DataService.GetOAuthUrl(), DataService, OnTwitchLoginCallback, CancellationToken.None);
|
||||
}
|
||||
|
||||
private void OnTwitchLoginCallback()
|
||||
|
Reference in New Issue
Block a user