Startup error handling
This commit is contained in:
@@ -9,7 +9,6 @@ namespace FileTime.GuiApp.ViewModels;
|
||||
|
||||
public interface IMainWindowViewModel : IMainWindowViewModelBase
|
||||
{
|
||||
string Title { get; }
|
||||
IGuiAppState AppState { get; }
|
||||
IItemPreviewService ItemPreviewService { get; }
|
||||
IDialogService DialogService { get; }
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
using DeclarativeProperty;
|
||||
|
||||
namespace FileTime.GuiApp.ViewModels;
|
||||
|
||||
public interface IMainWindowViewModelBase
|
||||
{
|
||||
DeclarativeProperty<string> Title { get; }
|
||||
bool Loading { get; }
|
||||
IObservable<string?> MainFont { get; }
|
||||
DeclarativeProperty<string> FatalError { get; }
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Reactive.Subjects;
|
||||
using DeclarativeProperty;
|
||||
|
||||
namespace FileTime.GuiApp.ViewModels;
|
||||
|
||||
@@ -6,4 +7,6 @@ public class MainWindowLoadingViewModel : IMainWindowViewModelBase
|
||||
{
|
||||
public bool Loading => true;
|
||||
public IObservable<string?> MainFont { get; } = new BehaviorSubject<string?>("");
|
||||
public DeclarativeProperty<string> Title { get; } = new("Loading...");
|
||||
public DeclarativeProperty<string> FatalError { get; } = new();
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
using System.Reactive.Linq;
|
||||
using System.Reflection;
|
||||
using Avalonia.Input;
|
||||
using DeclarativeProperty;
|
||||
using FileTime.App.CommandPalette.Services;
|
||||
using FileTime.App.Core.Services;
|
||||
using FileTime.App.Core.UserCommand;
|
||||
@@ -38,8 +39,9 @@ public partial class MainWindowViewModel : IMainWindowViewModel
|
||||
{
|
||||
public bool Loading => false;
|
||||
public IObservable<string?> MainFont => _fontService.MainFont.Select(x => x ?? "");
|
||||
public DeclarativeProperty<string> FatalError { get; } = new();
|
||||
public IGuiAppState AppState => _appState;
|
||||
public string Title { get; private set; }
|
||||
public DeclarativeProperty<string> Title { get; } = new();
|
||||
public Action? FocusDefaultElement { get; set; }
|
||||
|
||||
partial void OnInitialize()
|
||||
@@ -57,11 +59,13 @@ public partial class MainWindowViewModel : IMainWindowViewModel
|
||||
}
|
||||
}
|
||||
|
||||
Title = "FileTime " + versionString;
|
||||
var title = "FileTime " + versionString;
|
||||
#if DEBUG
|
||||
Title += " (Debug)";
|
||||
title += " (Debug)";
|
||||
#endif
|
||||
|
||||
Title.SetValueSafe(title);
|
||||
|
||||
_modalService.AllModalClosed += (_, _) => FocusDefaultElement?.Invoke();
|
||||
|
||||
Task.Run(async () => await _lifecycleService.InitStartupHandlersAsync()).Wait();
|
||||
|
||||
@@ -53,7 +53,7 @@
|
||||
|
||||
<Grid PointerPressed="HeaderPointerPressed">
|
||||
<Rectangle Fill="#01000000" />
|
||||
<TextBlock Margin="15,10" Text="{Binding Title}" />
|
||||
<TextBlock Margin="15,10" Text="{Binding Title.Value}" />
|
||||
</Grid>
|
||||
|
||||
<Grid Grid.Column="1" PointerPressed="HeaderPointerPressed">
|
||||
@@ -998,7 +998,12 @@
|
||||
<TextBlock
|
||||
HorizontalAlignment="Center"
|
||||
Margin="50"
|
||||
Text="Loading..." />
|
||||
Text="{Binding Title.Value}" />
|
||||
<TextBlock
|
||||
Foreground="Red"
|
||||
HorizontalAlignment="Center"
|
||||
Margin="50"
|
||||
Text="{Binding FatalError.Value}" />
|
||||
</StackPanel>
|
||||
</Grid>
|
||||
</Grid>
|
||||
|
||||
@@ -75,10 +75,23 @@ public partial class MainWindow : Window, IUiAccessor
|
||||
_logger?.LogInformation(
|
||||
$"{nameof(MainWindow)} opened, starting {nameof(MainWindowViewModel)} initialization...");
|
||||
|
||||
try
|
||||
{
|
||||
var viewModel = DI.ServiceProvider.GetRequiredService<MainWindowViewModel>();
|
||||
viewModel.FocusDefaultElement = () => Focus();
|
||||
ViewModel = viewModel;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger?.LogError(ex, "Error initializing {ViewModel}", nameof(MainWindowViewModel));
|
||||
if (DataContext is IMainWindowViewModelBase mainWindowViewModelBase)
|
||||
{
|
||||
mainWindowViewModelBase.FatalError.SetValueSafe(
|
||||
$"Error initializing {nameof(MainWindowViewModel)}: " + ex.Message
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void OnKeyDown(object sender, KeyEventArgs e)
|
||||
|
||||
Reference in New Issue
Block a user