Skip to content

Commit

Permalink
(#828) Add confirmation for update all action
Browse files Browse the repository at this point in the history
Due to the fact that the action of updating all packages, depending on
the current number of outdated packages, can take a long time to
complete, we should prompt the user to confirm whether they want to
continue or not.
  • Loading branch information
gep13 committed Apr 8, 2021
1 parent 45703b8 commit 59ddbf7
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
using ChocolateyGui.Common.ViewModels.Items;
using ChocolateyGui.Common.Windows.Services;
using ChocolateyGui.Common.Windows.Utilities.Extensions;
using MahApps.Metro.Controls.Dialogs;
using Serilog;

namespace ChocolateyGui.Common.Windows.ViewModels
Expand All @@ -45,6 +46,7 @@ public sealed class LocalSourceViewModel : Screen, ISourceViewModelBase, IHandle
private readonly IAllowedCommandsService _allowedCommandsService;
private readonly IEventAggregator _eventAggregator;
private readonly IMapper _mapper;
private readonly IDialogCoordinator _dialogCoordinator;
private bool _exportAll = true;
private bool _hasLoaded;
private bool _matchWord;
Expand All @@ -68,7 +70,8 @@ public LocalSourceViewModel(
IAllowedCommandsService allowedCommandsService,
IEventAggregator eventAggregator,
string displayName,
IMapper mapper)
IMapper mapper,
IDialogCoordinator dialogCoordinator)
{
_chocolateyService = chocolateyService;
_progressService = progressService;
Expand All @@ -91,6 +94,7 @@ public LocalSourceViewModel(

_eventAggregator = eventAggregator;
_mapper = mapper;
_dialogCoordinator = dialogCoordinator;
_eventAggregator.Subscribe(this);
}

Expand Down Expand Up @@ -182,30 +186,44 @@ public async void UpdateAll()
{
try
{
await _progressService.StartLoading(Resources.LocalSourceViewModel_Packages, true);
IsLoading = true;

_progressService.WriteMessage(Resources.LocalSourceViewModel_FetchingPackages);
var token = _progressService.GetCancellationToken();
var packages = Packages.Where(p => p.CanUpdate && !p.IsPinned).ToList();
double current = 0.0f;
foreach (var package in packages)
var result = await _dialogCoordinator.ShowMessageAsync(
this,
Resources.Dialog_AreYouSureTitle,
Resources.Dialog_AreYouSureUpdateAllMessage,
MessageDialogStyle.AffirmativeAndNegative,
new MetroDialogSettings
{
AffirmativeButtonText = Resources.Dialog_Yes,
NegativeButtonText = Resources.Dialog_No
});

if (result == MessageDialogResult.Affirmative)
{
if (token.IsCancellationRequested)
await _progressService.StartLoading(Resources.LocalSourceViewModel_Packages, true);
IsLoading = true;

_progressService.WriteMessage(Resources.LocalSourceViewModel_FetchingPackages);
var token = _progressService.GetCancellationToken();
var packages = Packages.Where(p => p.CanUpdate && !p.IsPinned).ToList();
double current = 0.0f;
foreach (var package in packages)
{
await _progressService.StopLoading();
IsLoading = false;
return;
if (token.IsCancellationRequested)
{
await _progressService.StopLoading();
IsLoading = false;
return;
}

_progressService.Report(Math.Min(current++ / packages.Count, 100));
await package.Update();
}

_progressService.Report(Math.Min(current++ / packages.Count, 100));
await package.Update();
await _progressService.StopLoading();
IsLoading = false;
ShowOnlyPackagesWithUpdate = false;
RefreshPackages();
}

await _progressService.StopLoading();
IsLoading = false;
ShowOnlyPackagesWithUpdate = false;
RefreshPackages();
}
catch (Exception ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
mc:Ignorable="d"
d:DesignHeight="768" d:DesignWidth="1366"
d:DataContext="{d:DesignInstance viewModels:LocalSourceViewModel}"
Background="{DynamicResource MahApps.Brushes.ThemeBackground}">
Background="{DynamicResource MahApps.Brushes.ThemeBackground}"
mahapps:DialogParticipation.Register="{Binding}">

<UserControl.Resources>
<utilities:BindingProxy x:Key="BindingProxy" Data="{Binding}" />
Expand Down
9 changes: 9 additions & 0 deletions Source/ChocolateyGui.Common/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Source/ChocolateyGui.Common/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1044,4 +1044,7 @@ Please contact your System Administrator to enable this operation.</value>
<value>This action will remove the '{0}' source from both Chocolatey and Chocolatey GUI. Are you sure you want to proceed (this action cannot be undone)?</value>
<comment>{0} = The id of the source which is away to be deleted, i.e. chocolatey, chocolatey.licensed, etc</comment>
</data>
<data name="Dialog_AreYouSureUpdateAllMessage" xml:space="preserve">
<value>This action, depending on the number of currently outdated packages, may take a long time to complete. Are you sure that you want to continue?</value>
</data>
</root>

0 comments on commit 59ddbf7

Please sign in to comment.