using Avalonia; using Avalonia.Controls.ApplicationLifetimes; using Avalonia.Data.Core.Plugins; using Avalonia.Markup.Xaml; using Discovery.Loader.Models; using Discovery.Loader.Services; using Discovery.Loader.ViewModels; using Discovery.Loader.Windows; namespace Discovery.Loader; public partial class App : Application { private UpdateService _updateService = new(); public override void Initialize() { AvaloniaXamlLoader.Load(this); } public override async void OnFrameworkInitializationCompleted() { BindingPlugins.DataValidators.RemoveAt(0); if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) { var installed = _updateService.IsInstalled(); // update-check Update? update = null; try { update = await _updateService.GetUpdateAsync(); } catch (Exception ex) { Console.WriteLine(ex); } try { // if not installed (need update to install) if (!installed) { if (update == null) { // set window var vm = new MainViewModel(); var mw = new MainWindow { DataContext = vm }; desktop.MainWindow = mw; // display major error mw.Show(); await vm.ErrorAsync("Install failed (Source unavailable)"); mw.Close(); } else { // set window var vm = new MainViewModel(); var mw = new MainWindow { DataContext = vm }; desktop.MainWindow = mw; // display install progress mw.Show(); await vm.InstallAsync(_updateService, update); mw.Close(); // start _updateService.StartExecutable(); } } // if installed (should update if needed) else { if (update == null) { // set window var vm = new MainViewModel(); var mw = new MainWindow { DataContext = vm }; desktop.MainWindow = mw; // display cant fetch updates, start afterwards mw.Show(); await vm.ErrorAsync("Failed to fetch updates."); mw.Close(); // start anyway _updateService.StartExecutable(); } else { // compare versions var isUpdated = _updateService.CompareVersion(update); if (!isUpdated) { // set window var vm = new MainViewModel(); var mw = new MainWindow { DataContext = vm }; desktop.MainWindow = mw; // display update mw.Show(); await vm.UpdateAsync(_updateService, update); mw.Close(); } else { // on current-version just start _updateService.StartExecutable(); } } } } catch (Exception ex) { Console.WriteLine(ex); } finally { //mw.Close(); //if (_updateService.IsInstalled()) // _updateService.StartExecutable(); desktop.Shutdown(); } } base.OnFrameworkInitializationCompleted(); } }