Services: Add Utils.RunForeground() to factor Task.Run and try-catch-Blocks out
Test / Run tests (push) Successful in 2m2s
Test / Run tests (push) Successful in 2m2s
This commit is contained in:
@@ -7,6 +7,13 @@ namespace Elwig.Services {
|
||||
public static class InteractionService {
|
||||
|
||||
public static Func<string, string, string, string?>? Override;
|
||||
public static Action<string, string>? NextInformation;
|
||||
public static Action<string, string>? NextWarning;
|
||||
public static Action<string, string>? NextError;
|
||||
public static Func<string, string, bool>? NextContinue;
|
||||
public static Func<string, string, bool>? NextConfirmation;
|
||||
public static Func<string, string, bool>? NextQuestion;
|
||||
public static Func<string, string, string?>? NextSave;
|
||||
|
||||
public static readonly Dictionary<string, string> ExtensionFilters = new() {
|
||||
["pdf"] = "PDF-Datei (*.pdf)|*.pdf",
|
||||
@@ -19,7 +26,10 @@ namespace Elwig.Services {
|
||||
};
|
||||
|
||||
public static void ShowInformation(string title, string text) {
|
||||
if (Override != null) {
|
||||
if (NextInformation != null) {
|
||||
NextInformation(title, text);
|
||||
NextInformation = null;
|
||||
} else if (Override != null) {
|
||||
Override("information", title, text);
|
||||
} else {
|
||||
MessageBox.Show(text, title, MessageBoxButton.OK, MessageBoxImage.Information);
|
||||
@@ -27,7 +37,11 @@ namespace Elwig.Services {
|
||||
}
|
||||
|
||||
public static bool AskContinue(string title, string text) {
|
||||
if (Override != null) {
|
||||
if (NextContinue != null) {
|
||||
var r = NextContinue(title, text);
|
||||
NextContinue = null;
|
||||
return r;
|
||||
} else if (Override != null) {
|
||||
return Override("continue", title, text) != null;
|
||||
} else {
|
||||
return MessageBox.Show(text, title, MessageBoxButton.OKCancel, MessageBoxImage.Warning, MessageBoxResult.Cancel) == MessageBoxResult.OK;
|
||||
@@ -35,7 +49,11 @@ namespace Elwig.Services {
|
||||
}
|
||||
|
||||
public static bool AskConfirmation(string title, string text) {
|
||||
if (Override != null) {
|
||||
if (NextConfirmation != null) {
|
||||
var r = NextConfirmation(title, text);
|
||||
NextConfirmation = null;
|
||||
return r;
|
||||
} else if (Override != null) {
|
||||
return Override("confirm", title, text) != null;
|
||||
} else {
|
||||
return MessageBox.Show(text, title, MessageBoxButton.YesNo, MessageBoxImage.Warning, MessageBoxResult.No) == MessageBoxResult.Yes;
|
||||
@@ -43,7 +61,11 @@ namespace Elwig.Services {
|
||||
}
|
||||
|
||||
public static bool AskQuestion(string title, string text, bool defaultResult) {
|
||||
if (Override != null) {
|
||||
if (NextQuestion != null) {
|
||||
var r = NextQuestion(title, text);
|
||||
NextQuestion = null;
|
||||
return r;
|
||||
} else if (Override != null) {
|
||||
return Override("question", title, text) != null;
|
||||
} else {
|
||||
return MessageBox.Show(text, title, MessageBoxButton.YesNo, MessageBoxImage.Question, defaultResult ? MessageBoxResult.Yes : MessageBoxResult.No) == MessageBoxResult.Yes;
|
||||
@@ -51,7 +73,10 @@ namespace Elwig.Services {
|
||||
}
|
||||
|
||||
public static void ShowWarning(string title, string text) {
|
||||
if (Override != null) {
|
||||
if (NextWarning != null) {
|
||||
NextWarning(title, text);
|
||||
NextWarning = null;
|
||||
} else if (Override != null) {
|
||||
Override("warning", title, text);
|
||||
} else {
|
||||
MessageBox.Show(text, title, MessageBoxButton.OK, MessageBoxImage.Warning);
|
||||
@@ -59,7 +84,10 @@ namespace Elwig.Services {
|
||||
}
|
||||
|
||||
public static void ShowError(string title, string text) {
|
||||
if (Override != null) {
|
||||
if (NextError != null) {
|
||||
NextError(title, text);
|
||||
NextError = null;
|
||||
} else if (Override != null) {
|
||||
Override("error", title, text);
|
||||
} else {
|
||||
MessageBox.Show(text, title, MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
@@ -71,7 +99,7 @@ namespace Elwig.Services {
|
||||
}
|
||||
|
||||
public static void ShowException(string title, Exception exc, bool showExcType = false, bool isError = true) {
|
||||
ShowException(title, exc, showExcType, isError);
|
||||
ShowException(title, null, exc, showExcType, isError);
|
||||
}
|
||||
|
||||
public static void ShowException(string title, string? text, Exception exc, bool showExcType = false, bool isError = true) {
|
||||
@@ -101,7 +129,11 @@ namespace Elwig.Services {
|
||||
}
|
||||
|
||||
public static string? SaveFile(string title, string defaultFileName, string extension) {
|
||||
if (Override != null) {
|
||||
if (NextSave != null) {
|
||||
var r = NextSave(title, $"{defaultFileName}.{extension}");
|
||||
NextSave = null;
|
||||
return r;
|
||||
} else if (Override != null) {
|
||||
return Override("save", title, $"{defaultFileName}.{extension}");
|
||||
} else {
|
||||
var d = new SaveFileDialog() {
|
||||
|
||||
Reference in New Issue
Block a user