From 7c91b5926bd6f67612becf084fbfbfb9df3f99d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Fi=C5=A1era?= Date: Tue, 18 Dec 2018 17:57:18 +0100 Subject: [PATCH] Refactor SelfPackageConfiguration to encapsulate equality check. --- .../Views/Converters/SelfPackageConverter.cs | 4 ++-- .../Services/SelfPackageConfiguration.cs | 19 ++++++++++++++++++- .../Commands/RefreshUpdatesCommand.cs | 6 +++--- .../ViewModels/Commands/ReinstallCommand.cs | 2 +- .../ViewModels/Commands/UninstallCommand.cs | 2 +- 5 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/PackageManager.UI/Views/Converters/SelfPackageConverter.cs b/src/PackageManager.UI/Views/Converters/SelfPackageConverter.cs index d14c74e..34e2393 100644 --- a/src/PackageManager.UI/Views/Converters/SelfPackageConverter.cs +++ b/src/PackageManager.UI/Views/Converters/SelfPackageConverter.cs @@ -34,14 +34,14 @@ private static bool IsSelfPackage(object value) string packageId = value as string; if (packageId == null) { - IPackage package = value as IPackage; + var package = value as IPackageIdentity; if (package == null) return false; packageId = package.Id; } - return Configuration.PackageId == packageId; + return Configuration.Equals(packageId); } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) diff --git a/src/PackageManager/Services/SelfPackageConfiguration.cs b/src/PackageManager/Services/SelfPackageConfiguration.cs index 73737fa..d3b571a 100644 --- a/src/PackageManager/Services/SelfPackageConfiguration.cs +++ b/src/PackageManager/Services/SelfPackageConfiguration.cs @@ -1,4 +1,5 @@ using Neptuo; +using PackageManager.Models; using System; using System.Collections.Generic; using System.Linq; @@ -7,7 +8,7 @@ namespace PackageManager.Services { - public class SelfPackageConfiguration + public class SelfPackageConfiguration : IEquatable { public string PackageId { get; } @@ -15,5 +16,21 @@ public SelfPackageConfiguration(string packageId) { PackageId = packageId; } + + public bool Equals(IPackageIdentity packageIdentity) + { + if (packageIdentity == null) + return false; + + return Equals(packageIdentity.Id); + } + + public bool Equals(string packageId) + { + if (PackageId == null || packageId == null) + return false; + + return PackageId == packageId; + } } } diff --git a/src/PackageManager/ViewModels/Commands/RefreshUpdatesCommand.cs b/src/PackageManager/ViewModels/Commands/RefreshUpdatesCommand.cs index c902e9c..798ba43 100644 --- a/src/PackageManager/ViewModels/Commands/RefreshUpdatesCommand.cs +++ b/src/PackageManager/ViewModels/Commands/RefreshUpdatesCommand.cs @@ -50,9 +50,9 @@ protected override async Task ExecuteAsync(CancellationToken cancellationToken) if (latest.Version != current.Definition.Version) { viewModel.Packages.Add(new PackageUpdateViewModel( - current.Definition, - latest, - selfPackageConfiguration.PackageId != null && current.Definition.Id == selfPackageConfiguration.PackageId + current.Definition, + latest, + selfPackageConfiguration.Equals(current.Definition) )); } } diff --git a/src/PackageManager/ViewModels/Commands/ReinstallCommand.cs b/src/PackageManager/ViewModels/Commands/ReinstallCommand.cs index 67c10f4..b7b00a6 100644 --- a/src/PackageManager/ViewModels/Commands/ReinstallCommand.cs +++ b/src/PackageManager/ViewModels/Commands/ReinstallCommand.cs @@ -25,7 +25,7 @@ public ReinstallCommand(IInstallService service, SelfPackageConfiguration selfPa } protected override bool CanExecuteOverride(IPackage package) - => package != null && service.IsInstalled(package) && selfPackageConfiguration.PackageId != package.Id; + => package != null && service.IsInstalled(package) && !selfPackageConfiguration.Equals(package); protected override async Task ExecuteAsync(IPackage package, CancellationToken cancellationToken) { diff --git a/src/PackageManager/ViewModels/Commands/UninstallCommand.cs b/src/PackageManager/ViewModels/Commands/UninstallCommand.cs index d80670e..f9bf11a 100644 --- a/src/PackageManager/ViewModels/Commands/UninstallCommand.cs +++ b/src/PackageManager/ViewModels/Commands/UninstallCommand.cs @@ -28,7 +28,7 @@ public UninstallCommand(IInstallService service, SelfPackageConfiguration selfPa } protected override bool CanExecuteOverride(IPackage package) - => package != null && service.IsInstalled(package) && selfPackageConfiguration.PackageId != package.Id; + => package != null && service.IsInstalled(package) && !selfPackageConfiguration.Equals(package.Id); protected override async Task ExecuteAsync(IPackage package, CancellationToken cancellationToken) {