Toast message + error display

This commit is contained in:
2022-05-29 08:02:40 +02:00
parent 74a8f66db1
commit 3f0c17b6cc
7 changed files with 51 additions and 7 deletions

View File

@@ -7,4 +7,5 @@ public interface IDialogService : IInputInterface
{
IObservable<ReadInputsViewModel?> ReadInput { get; }
void ReadInputs(IEnumerable<IInputElement> inputs, Action inputHandler, Action? cancelHandler = null);
void ShowToastMessage(string text);
}

View File

@@ -1,3 +1,4 @@
using System.Collections.ObjectModel;
using FileTime.App.Core.Models;
using FileTime.App.Core.ViewModels;
using FileTime.GuiApp.Configuration;
@@ -13,4 +14,5 @@ public interface IGuiAppState : IAppState
List<CommandBindingConfiguration> PossibleCommands { get; set; }
BindedCollection<RootDriveInfo, string> RootDriveInfos { get; set; }
IReadOnlyList<PlaceInfo> Places { get; set; }
ObservableCollection<string> PopupTexts { get; }
}

View File

@@ -41,6 +41,7 @@ public static class Startup
serviceCollection.TryAddSingleton<IModalService, ModalService>();
serviceCollection.TryAddSingleton<IDialogService, DialogService>();
serviceCollection.TryAddSingleton<ISystemClipboardService, SystemClipboardService>();
serviceCollection.TryAddSingleton<ToastMessageSink>();
serviceCollection.TryAddSingleton<IInputInterface>(s => s.GetRequiredService<IDialogService>());
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
@@ -102,6 +103,6 @@ public static class Startup
this LoggerSinkConfiguration loggerConfiguration,
IServiceProvider serviceProvider)
{
return loggerConfiguration.Sink(serviceProvider.GetService<ToastMessageSink>());
return loggerConfiguration.Sink(serviceProvider.GetRequiredService<ToastMessageSink>());
}
}

View File

@@ -1,3 +1,4 @@
using System.Collections.ObjectModel;
using FileTime.App.Core.Models;
using FileTime.App.Core.ViewModels;
using FileTime.GuiApp.Configuration;
@@ -22,4 +23,5 @@ public partial class GuiAppState : AppStateBase, IGuiAppState
[Property] private IReadOnlyList<PlaceInfo> _places;
public List<KeyConfig> PreviousKeys { get; } = new();
public ObservableCollection<string> PopupTexts { get; } = new();
}

View File

@@ -1,3 +1,4 @@
using FileTime.GuiApp.Services;
using Serilog.Core;
using Serilog.Events;
@@ -5,19 +6,19 @@ namespace FileTime.GuiApp.Logging;
public class ToastMessageSink : ILogEventSink
{
//private readonly IDialogService dialogService;
private readonly IDialogService dialogService;
public ToastMessageSink(/*IDialogService dialogService*/)
public ToastMessageSink(IDialogService dialogService)
{
//this.dialogService = dialogService;
this.dialogService = dialogService;
}
public void Emit(LogEvent logEvent)
{
/*if (logEvent.Level >= LogEventLevel.Error)
if (logEvent.Level >= LogEventLevel.Error)
{
var message = logEvent.RenderMessage();
dialogService.ShowToastMessage(message);
}*/
}
}
}

View File

@@ -1,4 +1,5 @@
using System.Reactive.Linq;
using Avalonia.Threading;
using DynamicData;
using FileTime.App.Core.Services;
using FileTime.Core.Interactions;
@@ -9,12 +10,14 @@ namespace FileTime.GuiApp.Services;
public class DialogService : IDialogService
{
private readonly IModalService _modalService;
private readonly IGuiAppState _guiAppState;
public IObservable<ReadInputsViewModel?> ReadInput { get; }
public DialogService(IModalService modalService)
public DialogService(IModalService modalService, IGuiAppState guiAppState)
{
_modalService = modalService;
_guiAppState = guiAppState;
ReadInput = modalService
.OpenModals
.ToCollection()
@@ -37,6 +40,16 @@ public class DialogService : IDialogService
_modalService.OpenModal(modalViewModel);
}
public void ShowToastMessage(string text)
{
Task.Run(async () =>
{
await Dispatcher.UIThread.InvokeAsync(() => _guiAppState.PopupTexts.Add(text));
await Task.Delay(5000);
await Dispatcher.UIThread.InvokeAsync(() => _guiAppState.PopupTexts.Remove(text));
});
}
private void HandleReadInputsSuccess(ReadInputsViewModel readInputsViewModel)
{
_modalService.CloseModal(readInputsViewModel);

View File

@@ -443,6 +443,30 @@
</Grid>
</Grid>
</Grid>
<ItemsRepeater Items="{Binding AppState.PopupTexts}" Margin="0,0,0,20" HorizontalAlignment="Center" VerticalAlignment="Top" IsVisible="{Binding AppState.PopupTexts.Count,Converter={StaticResource NotEqualsConverter}, ConverterParameter=0}">
<ItemsRepeater.Styles>
<Style Selector="TextBlock">
<Style.Animations>
<Animation Duration="0:0:1">
<KeyFrame Cue="0%">
<Setter Property="Opacity" Value="0.0"/>
</KeyFrame>
<KeyFrame Cue="100%">
<Setter Property="Opacity" Value="1.0"/>
</KeyFrame>
</Animation>
</Style.Animations>
</Style>
</ItemsRepeater.Styles>
<ItemsRepeater.ItemTemplate>
<DataTemplate x:DataType="x:String">
<Border Background="{DynamicResource ContainerGradientBackgroundBrush}" Margin="5" Padding="5">
<TextBlock Text="{Binding}" Foreground="{DynamicResource AccentComplementBrush}" HorizontalAlignment="Center"/>
</Border>
</DataTemplate>
</ItemsRepeater.ItemTemplate>
</ItemsRepeater>
</Grid>
<Grid Grid.Row="3">