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