Skip to content

Commit

Permalink
Merge pull request chocolatey#851 from punker76/feature/IDialogService
Browse files Browse the repository at this point in the history
Introduce DialogService
  • Loading branch information
gep13 authored May 8, 2021
2 parents d015ab3 + a8ad728 commit fb68c4a
Show file tree
Hide file tree
Showing 13 changed files with 190 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@
<Compile Include="..\SolutionInfo.cs">
<Link>Properties\SolutionInfo.cs</Link>
</Compile>
<Compile Include="Services\DialogService.cs" />
<Compile Include="Services\IDialogService.cs" />
<Compile Include="Utilities\ToolTipBehavior.cs" />
<Compile Include="Bootstrapper.cs" />
<Compile Include="Commands\CommandExecutionManager.cs" />
Expand Down
88 changes: 88 additions & 0 deletions Source/ChocolateyGui.Common.Windows/Services/DialogService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright company="Chocolatey" file="DialogService.cs">
// Copyright 2017 - Present Chocolatey Software, LLC
// Copyright 2014 - 2017 Rob Reynolds, the maintainers of Chocolatey, and RealDimensions Software, LLC
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

using System.Threading.Tasks;
using System.Windows;
using ChocolateyGui.Common.Properties;
using ChocolateyGui.Common.Windows.Views;
using MahApps.Metro.Controls.Dialogs;
using Microsoft.VisualStudio.Threading;

namespace ChocolateyGui.Common.Windows.Services
{
public class DialogService : IDialogService
{
private readonly AsyncSemaphore _lock;

public DialogService()
{
_lock = new AsyncSemaphore(1);
}

public ShellView ShellView { get; set; }

/// <inheritdoc />
public async Task<MessageDialogResult> ShowMessageAsync(string title, string message)
{
using (await _lock.EnterAsync())
{
if (ShellView != null)
{
var dialogSettings = new MetroDialogSettings
{
AffirmativeButtonText = Resources.ChocolateyDialog_OK
};

return await ShellView.ShowMessageAsync(title, message, MessageDialogStyle.Affirmative, dialogSettings);
}

return MessageBox.Show(message, title) == MessageBoxResult.OK
? MessageDialogResult.Affirmative
: MessageDialogResult.Negative;
}
}

/// <inheritdoc />
public async Task<MessageDialogResult> ShowConfirmationMessageAsync(string title, string message)
{
using (await _lock.EnterAsync())
{
if (ShellView != null)
{
var dialogSettings = new MetroDialogSettings
{
AffirmativeButtonText = Resources.Dialog_Yes,
NegativeButtonText = Resources.Dialog_No
};

return await ShellView.ShowMessageAsync(title, message, MessageDialogStyle.AffirmativeAndNegative, dialogSettings);
}

return MessageBox.Show(message, title, MessageBoxButton.YesNo) == MessageBoxResult.Yes
? MessageDialogResult.Affirmative
: MessageDialogResult.Negative;
}
}

/// <inheritdoc />
public async Task<LoginDialogData> ShowLoginAsync(string title, string message, LoginDialogSettings settings = null)
{
using (await _lock.EnterAsync())
{
if (ShellView != null)
{
return await ShellView.ShowLoginAsync(
Resources.SettingsViewModel_SetSourceUsernameAndPasswordTitle,
Resources.SettingsViewModel_SetSourceUsernameAndPasswordMessage,
settings);
}

return null;
}
}
}
}
43 changes: 43 additions & 0 deletions Source/ChocolateyGui.Common.Windows/Services/IDialogService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright company="Chocolatey" file="IDialogService.cs">
// Copyright 2017 - Present Chocolatey Software, LLC
// Copyright 2014 - 2017 Rob Reynolds, the maintainers of Chocolatey, and RealDimensions Software, LLC
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

using System.Threading.Tasks;
using ChocolateyGui.Common.Windows.Views;
using MahApps.Metro.Controls.Dialogs;

namespace ChocolateyGui.Common.Windows.Services
{
public interface IDialogService
{
ShellView ShellView { get; set; }

/// <summary>
/// Creates a Message dialog with an OK button inside of the ShellView.
/// </summary>
/// <param name="title">The title of the Dialog.</param>
/// <param name="message">The message contained within the Dialog.</param>
/// <returns>A task promising the result of which button was pressed.</returns>
Task<MessageDialogResult> ShowMessageAsync(string title, string message);

/// <summary>
/// Creates a Message dialog with Yes/No buttons inside of the ShellView.
/// </summary>
/// <param name="title">The title of the Dialog.</param>
/// <param name="message">The message contained within the Dialog.</param>
/// <returns>A task promising the result of which button was pressed.</returns>
Task<MessageDialogResult> ShowConfirmationMessageAsync(string title, string message);

/// <summary>
/// Creates a Login dialog inside of the ShellView.
/// </summary>
/// <param name="title">The title of the Dialog.</param>
/// <param name="message">The message contained within the Dialog.</param>
/// <param name="settings">Optional settings that override the global dialog settings.</param>
/// <returns>The text that was entered or null (Nothing in Visual Basic) if the user cancelled the operation.</returns>
Task<LoginDialogData> ShowLoginAsync(string title, string message, LoginDialogSettings settings = null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,21 @@
using System.Threading.Tasks;
using ChocolateyGui.Common.Controls;
using ChocolateyGui.Common.Models;
using MahApps.Metro.Controls.Dialogs;
using ChocolateyGui.Common.Windows.Views;

namespace ChocolateyGui.Common.Windows.Services
{
public interface IProgressService : INotifyPropertyChanged, IProgress<double>
{
ShellView ShellView { get; set; }

bool IsLoading { get; }

ObservableRingBufferCollection<PowerShellOutputLine> Output { get; }

[SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate", Justification = "Not appropriate")]
CancellationToken GetCancellationToken();

Task<MessageDialogResult> ShowMessageAsync(string title, string message);

Task<MessageDialogResult> ShowConfirmationMessageAsync(string title, string message);

Task StartLoading(string title = null, bool isCancelable = false);

Task StopLoading();
Expand Down
51 changes: 1 addition & 50 deletions Source/ChocolateyGui.Common.Windows/Services/ProgressService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,13 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using Caliburn.Micro;
using ChocolateyGui.Common.Base;
using ChocolateyGui.Common.Controls;
using ChocolateyGui.Common.Models;
using ChocolateyGui.Common.Properties;
using ChocolateyGui.Common.Windows.Controls.Dialogs;
using ChocolateyGui.Common.Windows.Utilities.Extensions;
using ChocolateyGui.Common.Windows.Views;
using MahApps.Metro.Controls.Dialogs;
using Microsoft.VisualStudio.Threading;
using Serilog;
using Serilog.Events;
Expand Down Expand Up @@ -76,47 +73,6 @@ public void Report(double value)
NotifyPropertyChanged("Progress");
}

public async Task<MessageDialogResult> ShowMessageAsync(string title, string message)
{
using (await _lock.EnterAsync())
{
if (ShellView != null)
{
var dialogSettings = new MetroDialogSettings
{
AffirmativeButtonText = Resources.ChocolateyDialog_OK
};

return await RunOnUIAsync(() => ShellView.ShowMessageAsync(title, message, MessageDialogStyle.Affirmative, dialogSettings));
}

return MessageBox.Show(message, title) == MessageBoxResult.OK
? MessageDialogResult.Affirmative
: MessageDialogResult.Negative;
}
}

public async Task<MessageDialogResult> ShowConfirmationMessageAsync(string title, string message)
{
using (await _lock.EnterAsync())
{
if (ShellView != null)
{
var dialogSettings = new MetroDialogSettings
{
AffirmativeButtonText = Resources.Dialog_Yes,
NegativeButtonText = Resources.Dialog_No
};

return await RunOnUIAsync(() => ShellView.ShowMessageAsync(title, message, MessageDialogStyle.AffirmativeAndNegative, dialogSettings));
}

return MessageBox.Show(message, title, MessageBoxButton.YesNo) == MessageBoxResult.Yes
? MessageDialogResult.Affirmative
: MessageDialogResult.Negative;
}
}

public async Task StartLoading(string title = null, bool isCancelable = false)
{
using (await _lock.EnterAsync())
Expand Down Expand Up @@ -156,7 +112,7 @@ public async Task StopLoading()
var currentCount = Interlocked.Decrement(ref _loadingItems);
if (currentCount == 0)
{
await RunOnUIAsync(() => _progressController.CloseAsync());
await _progressController.CloseAsync();
_progressController = null;
Report(0);

Expand Down Expand Up @@ -184,10 +140,5 @@ private static Task RunOnUIAsync(Func<Task> action)
{
return action.RunOnUIThreadAsync();
}

private static Task<T> RunOnUIAsync<T>(Func<Task<T>> action)
{
return action.RunOnUIThreadAsync();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ protected override void Load(ContainerBuilder builder)
builder.Register<IEventAggregator>(c => new EventAggregator()).InstancePerLifetimeScope();

// Register Services
builder.RegisterType<DialogService>().As<IDialogService>().SingleInstance();
builder.RegisterType<ProgressService>().As<IProgressService>().SingleInstance();
builder.RegisterType<PersistenceService>().As<IPersistenceService>().SingleInstance();
builder.RegisterType<LiteDBFileStorageService>().As<IFileStorageService>().SingleInstance();
Expand Down Expand Up @@ -104,7 +105,6 @@ protected override void Load(ContainerBuilder builder)
});

builder.RegisterType<BundledThemeService>().As<IBundledThemeService>().SingleInstance();
builder.RegisterInstance(DialogCoordinator.Instance).As<IDialogCoordinator>();
builder.RegisterInstance(mapperConfiguration.CreateMapper()).As<IMapper>();

try
Expand Down
Loading

0 comments on commit fb68c4a

Please sign in to comment.