Skip to content

Commit

Permalink
Remove quintuplicated ContentDialog and group it on a method for Navi…
Browse files Browse the repository at this point in the history
…gationPage
  • Loading branch information
marticliment committed May 30, 2024
1 parent 42ba2b5 commit 1d6f935
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 187 deletions.
45 changes: 45 additions & 0 deletions src/UniGetUI/Interface/MainView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
using UniGetUI.PackageEngine.Enums;
using UniGetUI.Core.Tools;
using UniGetUI.Interface.SoftwarePages;
using UniGetUI;
using Microsoft.UI.Xaml.Media;

// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.
Expand Down Expand Up @@ -208,6 +210,49 @@ public async Task ManageIgnoredUpdatesDialog()
UpdatesDialog = null;
}

public async Task<ContentDialogResult> ShowOperationFailedDialog(IEnumerable<string> processOutput, string dialogTitle, string shortDescription)
{
ContentDialog dialog = new();
dialog.Style = Application.Current.Resources["DefaultContentDialogStyle"] as Style;
dialog.XamlRoot = XamlRoot;
dialog.Resources["ContentDialogMaxWidth"] = 750;
dialog.Resources["ContentDialogMaxHeight"] = 1000;
dialog.Title = dialogTitle;

StackPanel panel = new() { Spacing = 16 };
panel.Children.Add(new TextBlock { TextWrapping = TextWrapping.WrapWholeWords, Text = $"{shortDescription}. " + CoreTools.Translate("Please see the Command-line Output or refer to the Operation History for further information about the issue.") });

Expander expander = new() { CornerRadius = new CornerRadius(8) };

StackPanel HeaderPanel = new() { Orientation = Orientation.Horizontal, Spacing = 8 };
HeaderPanel.Children.Add(new LocalIcon("console") { VerticalAlignment = VerticalAlignment.Center, Height = 24, Width = 24, HorizontalAlignment = HorizontalAlignment.Left });
HeaderPanel.Children.Add(new TextBlock { Text = CoreTools.Translate("Command-line Output"), HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center });

expander.Header = HeaderPanel;
expander.HorizontalAlignment = HorizontalAlignment.Stretch;
expander.VerticalAlignment = VerticalAlignment.Stretch;
expander.MaxHeight = 200;
panel.Children.Add(expander);

RichTextBlock output = new() { FontFamily = new FontFamily("Consolas"), TextWrapping = TextWrapping.Wrap };
ScrollViewer sv = new();
sv.VerticalAlignment = VerticalAlignment.Stretch;
Paragraph par = new();
foreach (string line in processOutput)
par.Inlines.Add(new Run { Text = line + "\x0a" });
output.Blocks.Add(par);

sv.Content = output;
expander.Content = sv;

dialog.Content = panel;
dialog.PrimaryButtonText = CoreTools.Translate("Retry");
dialog.CloseButtonText = CoreTools.Translate("Close");
dialog.DefaultButton = ContentDialogButton.Primary;

return await MainApp.Instance.MainWindow.ShowDialogAsync(dialog);
}

public async void WarnAboutAdminRights()
{
ContentDialog AdminDialog = new();
Expand Down
132 changes: 21 additions & 111 deletions src/UniGetUI/PackageEngine/Operations/PackageOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,57 +132,28 @@ protected override async Task<AfterFinshAction> HandleFailure()
Package.SetTag(PackageTag.Failed);

if (!Settings.Get("DisableErrorNotifications") && !Settings.Get("DisableNotifications"))
{
try
{
new ToastContentBuilder()
.AddArgument("action", "OpenUniGetUI")
.AddArgument("notificationId", CoreData.VolatileNotificationIdCounter)
.AddText(CoreTools.Translate("Installation failed"))
.AddText(CoreTools.Translate("{package} could not be installed", new Dictionary<string, object?>{ { "package", Package.Name } })).Show();
.AddText(CoreTools.Translate("{package} could not be installed", new Dictionary<string, object?> { { "package", Package.Name } })).Show();

}
catch (Exception ex)
{
Logger.Warn("Failed to show toast notification");
Logger.Warn(ex);
}
ContentDialog dialog = new();
dialog.Style = Application.Current.Resources["DefaultContentDialogStyle"] as Style;
dialog.XamlRoot = XamlRoot;
dialog.Resources["ContentDialogMaxWidth"] = 750;
dialog.Resources["ContentDialogMaxHeight"] = 1000;
dialog.Title = CoreTools.Translate("{package} installation failed", new Dictionary<string, object?>{ { "package", Package.Name } });

StackPanel panel = new() { Spacing = 16 };
panel.Children.Add(new TextBlock { TextWrapping = TextWrapping.WrapWholeWords, Text = CoreTools.Translate("{package} could not be installed", new Dictionary<string, object?>{ { "package", Package.Name } }) + ". " + CoreTools.Translate("Please see the Command-line Output or refer to the Operation History for further information about the issue.") });

Expander expander = new() { CornerRadius = new CornerRadius(8) };

StackPanel HeaderPanel = new() { Orientation = Orientation.Horizontal, Spacing = 8 };
HeaderPanel.Children.Add(new LocalIcon("console") { VerticalAlignment = VerticalAlignment.Center, Height = 24, Width = 24, HorizontalAlignment = HorizontalAlignment.Left });
HeaderPanel.Children.Add(new TextBlock { Text = CoreTools.Translate("Command-line Output"), HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center });

expander.Header = HeaderPanel;
expander.HorizontalAlignment = HorizontalAlignment.Stretch;
panel.Children.Add(expander);

RichTextBlock output = new() { FontFamily = new FontFamily("Consolas"), TextWrapping = TextWrapping.Wrap };
ScrollViewer sv = new();
sv.MaxHeight = 500;
Paragraph par = new();
foreach (string line in ProcessOutput)
par.Inlines.Add(new Run { Text = line + "\x0a" });
output.Blocks.Add(par);

sv.Content = output;
expander.Content = sv;

dialog.Content = panel;
dialog.PrimaryButtonText = CoreTools.Translate("Retry");
dialog.CloseButtonText = CoreTools.Translate("Close");
dialog.DefaultButton = ContentDialogButton.Primary;
}

ContentDialogResult result = await MainApp.Instance.MainWindow.ShowDialogAsync(dialog);
ContentDialogResult result = await MainApp.Instance.MainWindow.NavigationPage.ShowOperationFailedDialog(
ProcessOutput,
CoreTools.Translate("{package} installation failed", new Dictionary<string, object?> { { "package", Package.Name } }),
CoreTools.Translate("{package} could not be installed", new Dictionary<string, object?> { { "package", Package.Name } })
);

if (result == ContentDialogResult.Primary)
return AfterFinshAction.Retry;
Expand Down Expand Up @@ -277,57 +248,28 @@ protected override async Task<AfterFinshAction> HandleFailure()
Package.SetTag(PackageTag.Failed);

if (!Settings.Get("DisableErrorNotifications") && !Settings.Get("DisableNotifications"))
{
try
{
new ToastContentBuilder()
.AddArgument("action", "OpenUniGetUI")
.AddArgument("notificationId", CoreData.VolatileNotificationIdCounter)
.AddText(CoreTools.Translate("Update failed"))
.AddText(CoreTools.Translate("{package} could not be updated", new Dictionary<string, object?>{ { "package", Package.Name } })).Show();
.AddText(CoreTools.Translate("{package} could not be updated", new Dictionary<string, object?> { { "package", Package.Name } })).Show();

}
catch (Exception ex)
{
Logger.Warn("Failed to show toast notification");
Logger.Warn(ex);
}
ContentDialog dialog = new();
dialog.Style = Application.Current.Resources["DefaultContentDialogStyle"] as Style;
dialog.XamlRoot = XamlRoot;
dialog.Resources["ContentDialogMaxWidth"] = 750;
dialog.Resources["ContentDialogMaxHeight"] = 1000;
dialog.Title = CoreTools.Translate("{package} update failed", new Dictionary<string, object?>{ { "package", Package.Name } });

StackPanel panel = new() { Spacing = 16 };
panel.Children.Add(new TextBlock { TextWrapping = TextWrapping.WrapWholeWords, Text = CoreTools.Translate("{package} could not be updated", new Dictionary<string, object?>{ { "package", Package.Name } }) + ". " + CoreTools.Translate("Please see the Command-line Output or refer to the Operation History for further information about the issue.") });

Expander expander = new() { CornerRadius = new CornerRadius(8) };

StackPanel HeaderPanel = new() { Orientation = Orientation.Horizontal, Spacing = 8 };
HeaderPanel.Children.Add(new LocalIcon("console") { VerticalAlignment = VerticalAlignment.Center, Height = 24, Width = 24, HorizontalAlignment = HorizontalAlignment.Left });
HeaderPanel.Children.Add(new TextBlock { Text = CoreTools.Translate("Command-line Output"), HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center });

expander.Header = HeaderPanel;
expander.HorizontalAlignment = HorizontalAlignment.Stretch;
panel.Children.Add(expander);

RichTextBlock output = new() { FontFamily = new FontFamily("Consolas"), TextWrapping = TextWrapping.Wrap };
ScrollViewer sv = new();
sv.MaxHeight = 500;
Paragraph par = new();
foreach (string line in ProcessOutput)
par.Inlines.Add(new Run { Text = line + "\x0a" });
output.Blocks.Add(par);

sv.Content = output;
expander.Content = sv;

dialog.Content = panel;
dialog.PrimaryButtonText = CoreTools.Translate("Retry");
dialog.CloseButtonText = CoreTools.Translate("Close");
dialog.DefaultButton = ContentDialogButton.Primary;
}

ContentDialogResult result = await MainApp.Instance.MainWindow.ShowDialogAsync(dialog);
ContentDialogResult result = await MainApp.Instance.MainWindow.NavigationPage.ShowOperationFailedDialog(
ProcessOutput,
CoreTools.Translate("{package} update failed", new Dictionary<string, object?> { { "package", Package.Name } }),
CoreTools.Translate("{package} could not be updated", new Dictionary<string, object?> { { "package", Package.Name } })
);

if (result == ContentDialogResult.Primary)
return AfterFinshAction.Retry;
Expand Down Expand Up @@ -445,43 +387,11 @@ protected override async Task<AfterFinshAction> HandleFailure()
Logger.Warn(ex);
}

ContentDialog dialog = new();
dialog.Style = Application.Current.Resources["DefaultContentDialogStyle"] as Style;
dialog.XamlRoot = XamlRoot;
dialog.Resources["ContentDialogMaxWidth"] = 750;
dialog.Resources["ContentDialogMaxHeight"] = 1000;
dialog.Title = CoreTools.Translate("{package} uninstall failed", new Dictionary<string, object?>{ { "package", Package.Name } });

StackPanel panel = new() { Spacing = 16 };
panel.Children.Add(new TextBlock { TextWrapping = TextWrapping.WrapWholeWords, Text = CoreTools.Translate("{package} could not be uninstalled", new Dictionary<string, object?>{ { "package", Package.Name } }) + ". " + CoreTools.Translate("Please see the Command-line Output or refer to the Operation History for further information about the issue.") });

Expander expander = new() { CornerRadius = new CornerRadius(8) };

StackPanel HeaderPanel = new() { Orientation = Orientation.Horizontal, Spacing = 8 };
HeaderPanel.Children.Add(new LocalIcon("console") { VerticalAlignment = VerticalAlignment.Center, Height = 24, Width = 24, HorizontalAlignment = HorizontalAlignment.Left });
HeaderPanel.Children.Add(new TextBlock { Text = CoreTools.Translate("Command-line Output"), HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center });

expander.Header = HeaderPanel;
expander.HorizontalAlignment = HorizontalAlignment.Stretch;
panel.Children.Add(expander);

RichTextBlock output = new() { FontFamily = new FontFamily("Consolas"), TextWrapping = TextWrapping.Wrap };
ScrollViewer sv = new();
sv.MaxHeight = 500;
Paragraph par = new();
foreach (string line in ProcessOutput)
par.Inlines.Add(new Run { Text = line + "\x0a" });
output.Blocks.Add(par);

sv.Content = output;
expander.Content = sv;

dialog.Content = panel;
dialog.PrimaryButtonText = CoreTools.Translate("Retry");
dialog.CloseButtonText = CoreTools.Translate("Close");
dialog.DefaultButton = ContentDialogButton.Primary;

ContentDialogResult result = await MainApp.Instance.MainWindow.ShowDialogAsync(dialog);
ContentDialogResult result = await MainApp.Instance.MainWindow.NavigationPage.ShowOperationFailedDialog(
ProcessOutput,
CoreTools.Translate("{package} uninstall failed", new Dictionary<string, object?> { { "package", Package.Name } }),
CoreTools.Translate("{package} could not be uninstalled", new Dictionary<string, object?> { { "package", Package.Name } })
);

if (result == ContentDialogResult.Primary)
return AfterFinshAction.Retry;
Expand Down
Loading

0 comments on commit 1d6f935

Please sign in to comment.