Skip to content

Commit

Permalink
Refactor SelfPackageConfiguration to encapsulate equality check.
Browse files Browse the repository at this point in the history
  • Loading branch information
maraf committed Dec 18, 2018
1 parent 87ae89a commit 7c91b59
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 18 additions & 1 deletion src/PackageManager/Services/SelfPackageConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Neptuo;
using PackageManager.Models;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -7,13 +8,29 @@

namespace PackageManager.Services
{
public class SelfPackageConfiguration
public class SelfPackageConfiguration : IEquatable<IPackageIdentity>
{
public string PackageId { get; }

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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
));
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/PackageManager/ViewModels/Commands/ReinstallCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
2 changes: 1 addition & 1 deletion src/PackageManager/ViewModels/Commands/UninstallCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down

0 comments on commit 7c91b59

Please sign in to comment.