Skip to content

Commit

Permalink
feat: IDialogService.Close returns boolean indicating success (#116)
Browse files Browse the repository at this point in the history
IDialogService.Close returns false instead of throwing an exception when failing to close a dialog. This behavior aligns with the behavior of IDialogService.Activate.
  • Loading branch information
rogiardi authored and FantasticFiasco committed Aug 19, 2020
1 parent b7715ad commit 89bbb09
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ This project adheres to [Semantic Versioning](http://semver.org/) and is followi

NOTE: Features with breaking changes are found on branch [releases/v8](https://github.com/FantasticFiasco/mvvm-dialogs/tree/releases/v8) and will stay there until a new major version is released. Until then the features have been published to [www.nuget.org](https://www.nuget.org/packages/MvvmDialogs/) as pre-releases to v8.

### :syringe: Changed

- [#116](https://github.com/FantasticFiasco/mvvm-dialogs/issues/116) [BREAKING CHANGE] `IDialogService.Close` returns `false` instead of throwing an exception when failing to close a dialog. This behavior aligns with the behavior of `IDialogService.Activate`.

### :syringe: Fixed

- Specify dependency groups in nuspec for each supported framework version
Expand Down
15 changes: 10 additions & 5 deletions src/net/DialogService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,25 +162,30 @@ public bool Activate(INotifyPropertyChanged viewModel)
}

/// <inheritdoc />
public void Close(INotifyPropertyChanged viewModel)
public bool Close(INotifyPropertyChanged viewModel)
{
if (viewModel == null) throw new ArgumentNullException(nameof(viewModel));

foreach (Window? window in Application.Current.Windows)
{
if (window == null)
if (window == null || !viewModel.Equals(window.DataContext))
{
continue;
}

if (viewModel.Equals(window.DataContext))
try
{
window.Close();
return;
return true;
}
catch (Exception e)
{
Logger.Write($"Failed to close dialog: {e}");
break;
}
}

throw new DialogNotFoundException();
return false;
}

/// <inheritdoc />
Expand Down
8 changes: 4 additions & 4 deletions src/net/IDialogService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,10 @@ void Show(
/// <see cref="Show{T}"/> or <see cref="ShowCustom{T}"/>.
/// </summary>
/// <param name="viewModel">The view model of the dialog to close.</param>
/// <exception cref="DialogNotFoundException">
/// No dialog is found with specified view model as data context.
/// </exception>
void Close(INotifyPropertyChanged viewModel);
/// <returns>
/// true if the <see cref="Window"/> was successfully closed; otherwise, false.
/// </returns>
bool Close(INotifyPropertyChanged viewModel);

/// <summary>
/// Displays a message box that has a message, title bar caption, button, and icon; and
Expand Down

0 comments on commit 89bbb09

Please sign in to comment.