100 lines
3.4 KiB
C#
100 lines
3.4 KiB
C#
using System.Windows;
|
|
using MaterialDesignThemes.Wpf;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace Livia.Views.Utility;
|
|
|
|
public enum WarningWindowKind
|
|
{
|
|
Error,
|
|
Warning,
|
|
Confirmation,
|
|
Info
|
|
}
|
|
|
|
public interface IWarningSystem
|
|
{
|
|
bool? ShowDialogString(WarningWindowKind kind, bool waitForResponse, string message);
|
|
bool? ShowDialog(WarningWindowKind kind, bool waitForResponse, string index, params object[] args);
|
|
void AddSnackbarMessage(string index, params object[] args);
|
|
void SetSnackbar(Snackbar? snackbar);
|
|
void AddSnackbarMessage(TimeSpan duration, string index, params object[] args);
|
|
}
|
|
|
|
public class WarningSystem(ILogger logger) : IWarningSystem
|
|
{
|
|
private Snackbar? _snackbar;
|
|
|
|
public bool? ShowDialogString(WarningWindowKind kind, bool waitForResponse, string message)
|
|
{
|
|
return Application.Current.Dispatcher.Invoke(delegate
|
|
{
|
|
try
|
|
{
|
|
WarningDialog dialog = new(kind, message, waitForResponse);
|
|
if (waitForResponse)
|
|
{
|
|
//enable black mask in mainWindow
|
|
//MainWindowViewModel mainWindow = ActivatorUtilities.GetServiceOrCreateInstance<MainWindowViewModel>(ServiceProviderFactory.ServiceProvider);
|
|
//mainWindow.BackgroundBlackMaskVisible = true;
|
|
bool? result = dialog.ShowDialog();
|
|
//mainWindow.BackgroundBlackMaskVisible = false;
|
|
return result;
|
|
}
|
|
|
|
dialog.Show();
|
|
dialog.Activate();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
logger.LogError("Error showing warning dialog, error = {e}", e);
|
|
}
|
|
return null;
|
|
});
|
|
}
|
|
|
|
public bool? ShowDialog(WarningWindowKind kind, bool waitForResponse, string index, params object[] args)
|
|
{
|
|
logger.LogInformation("Showing dialog {index}", index);
|
|
string msg = (string)(Application.Current.TryFindResource(index) ?? Application.Current.TryFindResource("Error") ?? "");
|
|
string message = string.Format(msg, args);
|
|
return ShowDialogString(kind, waitForResponse, message);
|
|
}
|
|
|
|
public void AddSnackbarMessage(TimeSpan duration, string index, params object[] args)
|
|
{
|
|
try
|
|
{
|
|
//clear old messages. Queue is blocked when window is not activated, so there is a chance we flood it with messages when switched back.
|
|
_snackbar?.MessageQueue?.Clear();
|
|
_snackbar?.MessageQueue?.Enqueue(string.Format((string)Application.Current.TryFindResource(index), args), null, null, null, false, false, duration);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
logger.LogError("Error AddSnackbarMessage, error = {e}", e);
|
|
}
|
|
}
|
|
|
|
public void AddSnackbarMessage(string index, params object[] args)
|
|
{
|
|
try
|
|
{
|
|
_snackbar?.MessageQueue?.Clear();
|
|
_snackbar?.MessageQueue?.Enqueue(string.Format((string)Application.Current.TryFindResource(index), args));
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
logger.LogError("Error AddSnackbarMessage, error = {e}", e);
|
|
}
|
|
}
|
|
|
|
public void SetSnackbar(Snackbar? snackbar)
|
|
{
|
|
if (snackbar == null)
|
|
return;
|
|
|
|
_snackbar = snackbar;
|
|
if (_snackbar?.MessageQueue != null)
|
|
_snackbar.MessageQueue.DiscardDuplicates = true;
|
|
}
|
|
} |