Skip to content

Commit

Permalink
AmbiguousMatchException fixed
Browse files Browse the repository at this point in the history
- hiding the base class 'Model' peropert, by using the `public new PackageManagerSearchElement Model` inside the PackageManagerSearchElementViewModel was the cause of the bug. This is a legacy code and the issue must have been present all along
- renaming the Model to SearchElementModel fixes the issue
- also created ViewModel properties corresponding to the Model properties for View binding
  • Loading branch information
dnenov committed Oct 22, 2023
1 parent d59734c commit aaaf8a6
Show file tree
Hide file tree
Showing 12 changed files with 149 additions and 100 deletions.
26 changes: 13 additions & 13 deletions src/DynamoCoreWpf/UI/Converters.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Media;
using Dynamo.Configuration;
using Dynamo.Graph.Nodes;
using Dynamo.Graph.Workspaces;
Expand All @@ -25,6 +13,18 @@
using Dynamo.Wpf.ViewModels;
using DynamoUnits;
using FontAwesome5;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Media;
using Color = System.Windows.Media.Color;
using FlowDirection = System.Windows.FlowDirection;
using HorizontalAlignment = System.Windows.HorizontalAlignment;
Expand Down Expand Up @@ -1500,7 +1500,7 @@ public class DateToPackageLabelConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (!(value is PackageManagerSearchElement packageManagerSearchElement)) return Visibility.Collapsed;
if (!(value is Dynamo.PackageManager.PackageManagerSearchElement packageManagerSearchElement)) return String.Empty;
if (packageManagerSearchElement.IsDeprecated) return Resources.PackageManagerPackageDeprecated;

DateTime.TryParse(packageManagerSearchElement.LatestVersionCreated, out DateTime dateLastUpdated);
Expand Down
2 changes: 1 addition & 1 deletion src/DynamoCoreWpf/UI/GuidedTour/GuidesValidationMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ internal static void ExecuteViewDetailsSideBar(Step stepInfo, StepUIAutomation u
if (packageManagerSearchElementViewModel == null) return;

if (packageDetailsWindow == null)
packageManagerViewModel.ViewPackageDetailsCommand.Execute(packageManagerSearchElementViewModel.Model);
packageManagerViewModel.ViewPackageDetailsCommand.Execute(packageManagerSearchElementViewModel.SearchElementModel);

//The PackageDetails sidebar is using events when is being shown then we need to execute those events before setting the Popup.PlacementTarget.
//otherwise the sidebar will not be present (and we don't have host for the Popup) and the Popup will be located out of the Dynamo window
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using Dynamo.ViewModels;
using Dynamo.Wpf.ViewModels;
using Greg.Responses;
using Prism.Commands;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows.Input;
using Dynamo.ViewModels;
using Dynamo.Wpf.ViewModels;
using Greg.Responses;
using Prism.Commands;

namespace Dynamo.PackageManager.ViewModels
{
Expand All @@ -21,7 +21,46 @@ public class PackageManagerSearchElementViewModel : BrowserItemViewModel, IEquat
public ICommand VisitRepositoryCommand { get; set; }
public ICommand DownloadLatestToCustomPathCommand { get; set; }

public new PackageManagerSearchElement Model { get; internal set; }
/// <summary>
/// The PackageManagerSearchElement Model this VM links to
/// </summary>
public PackageManagerSearchElement SearchElementModel { get; internal set; }
/// <summary>
/// VM IsDeprecated property
/// </summary>
public bool IsDeprecated { get; set; }
/// <summary>
/// VM Hosts property
/// </summary>
public List<string> Hosts { get; private set; }
/// <summary>
/// VM LatestVersionCreated property
/// </summary>
public string LatestVersionCreated { get; private set; }
/// <summary>
/// VM Downloads property
/// </summary>
public int Downloads { get; private set; }
/// <summary>
/// VM Votes property
/// </summary>
public int Votes { get; private set; }
/// <summary>
/// VM Package Version property
/// </summary>
public IEnumerable<string> PkgVersion { get; private set; }
/// <summary>
/// VM Maintainers property
/// </summary>
public string Maintainers { get; private set; }
/// <summary>
/// VM LatestVersion property
/// </summary>
public string LatestVersion { get; private set; }
/// <summary>
/// VM Name Property
/// </summary>
public string Name { get; private set; }


/// <summary>
Expand All @@ -40,28 +79,46 @@ public class PackageManagerSearchElementViewModel : BrowserItemViewModel, IEquat
public PackageManagerSearchElementViewModel(PackageManagerSearchElement element, bool canLogin, bool install, bool isEnabledForInstall = true)
: base(element)
{
this.Model = element;
this.SearchElementModel = element;
CanInstall = install;
IsEnabledForInstall = isEnabledForInstall;

this.SelectedVersion = this.Model.LatestVersion;
this.SelectedVersion = this.SearchElementModel.LatestVersion;

this.ToggleIsExpandedCommand = new DelegateCommand(() => this.Model.IsExpanded = !this.Model.IsExpanded);
this.ToggleIsExpandedCommand = new DelegateCommand(() => this.SearchElementModel.IsExpanded = !this.SearchElementModel.IsExpanded);

this.DownloadLatestCommand = new DelegateCommand(
() => OnRequestDownload(Model.Header.versions.First(x => x.version.Equals(SelectedVersion)), false),
() => !Model.IsDeprecated && CanInstall);
this.DownloadLatestToCustomPathCommand = new DelegateCommand(() => OnRequestDownload(Model.Header.versions.First(x => x.version.Equals(SelectedVersion)), true));
() => OnRequestDownload(SearchElementModel.Header.versions.First(x => x.version.Equals(SelectedVersion)), false),
() => !SearchElementModel.IsDeprecated && CanInstall);
this.DownloadLatestToCustomPathCommand = new DelegateCommand(() => OnRequestDownload(SearchElementModel.Header.versions.First(x => x.version.Equals(SelectedVersion)), true));

this.UpvoteCommand = new DelegateCommand(Model.Upvote, () => canLogin);
this.UpvoteCommand = new DelegateCommand(SearchElementModel.Upvote, () => canLogin);

// TODO: Remove the initialization of the UI command in Dynamo 3.0
this.DownvoteCommand = new DelegateCommand(Model.Downvote, () => canLogin);
this.DownvoteCommand = new DelegateCommand(SearchElementModel.Downvote, () => canLogin);

this.VisitSiteCommand =
new DelegateCommand(() => GoToUrl(FormatUrl(Model.SiteUrl)), () => !String.IsNullOrEmpty(Model.SiteUrl));
new DelegateCommand(() => GoToUrl(FormatUrl(SearchElementModel.SiteUrl)), () => !String.IsNullOrEmpty(SearchElementModel.SiteUrl));
this.VisitRepositoryCommand =
new DelegateCommand(() => GoToUrl(FormatUrl(Model.RepositoryUrl)), () => !String.IsNullOrEmpty(Model.RepositoryUrl));
new DelegateCommand(() => GoToUrl(FormatUrl(SearchElementModel.RepositoryUrl)), () => !String.IsNullOrEmpty(SearchElementModel.RepositoryUrl));

SetModelProperties();
}

/// <summary>
/// Sets the VM properties to be bound to
/// </summary>
private void SetModelProperties()
{
IsDeprecated = this.SearchElementModel.IsDeprecated;
Hosts = this.SearchElementModel.Hosts;
LatestVersionCreated = this.SearchElementModel.LatestVersionCreated;
Downloads = this.SearchElementModel.Downloads;
Votes = this.SearchElementModel.Votes;
PkgVersion = this.SearchElementModel.PackageVersions;
Maintainers = this.SearchElementModel.Maintainers;
LatestVersion = this.SearchElementModel.LatestVersion;
Name = this.SearchElementModel.Name;
}

/// <summary>
Expand Down Expand Up @@ -132,7 +189,7 @@ public List<Tuple<PackageVersion, DelegateCommand<object>>> Versions
get
{
return
Model.Header.versions.Select(
SearchElementModel.Header.versions.Select(
x => new Tuple<PackageVersion, DelegateCommand<object>>(
x, new DelegateCommand<object>((p) => OnRequestDownload(x, p.Equals("true")))
)).Reverse().ToList();
Expand All @@ -158,7 +215,7 @@ public void OnRequestDownload(PackageVersion version, bool downloadToCustomPath)
}

if (RequestDownload != null)
RequestDownload(this.Model, version, downloadPath);
RequestDownload(this.SearchElementModel, version, downloadPath);
}

/// <summary>
Expand All @@ -170,7 +227,7 @@ public void OnRequestDownload(PackageVersion version, bool downloadToCustomPath)
public bool Equals(PackageManagerSearchElementViewModel other)
{
if (other == null) return false;
return this.Model.Id == other.Model.Id;
return this.SearchElementModel.Id == other.SearchElementModel.Id;
}

/// <summary>
Expand All @@ -179,7 +236,7 @@ public bool Equals(PackageManagerSearchElementViewModel other)
/// <returns>HashCode of package</returns>
public override int GetHashCode()
{
return Model.Id.GetHashCode();
return SearchElementModel.Id.GetHashCode();
}

private string GetDownloadPath()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1017,9 +1017,9 @@ public void RefreshAndSearchAsync()
ClearSearchResults();
foreach (var result in t.Result)
{
if (result.Model != null)
if (result.SearchElementModel != null)
{
AddPackageToSearchIndex(result.Model, iDoc);
AddPackageToSearchIndex(result.SearchElementModel, iDoc);
}
this.AddToSearchResults(result);
}
Expand Down Expand Up @@ -1169,7 +1169,7 @@ void canInstallHandler(object o, PropertyChangedEventArgs eArgs)
// the Downloads collection before Download/Install begins.
if (eArgs.PropertyName == nameof(PackageDownloadHandle.DownloadState))
{
PackageManagerSearchElementViewModel sr = SearchResults.FirstOrDefault(x => x.Model.Name == handle.Name);
PackageManagerSearchElementViewModel sr = SearchResults.FirstOrDefault(x => x.SearchElementModel.Name == handle.Name);
if (sr == null) return;

sr.CanInstall = CanInstallPackage(o as PackageDownloadHandle);
Expand Down Expand Up @@ -1318,7 +1318,7 @@ internal IEnumerable<PackageManagerSearchElementViewModel> Filter(IEnumerable<Pa
IEnumerable<PackageManagerSearchElementViewModel> filteredList = null;

filteredList = filteredList ??
list.Where(x => x.Model.Hosts != null && SelectedHosts.Intersect(x.Model.Hosts).Count() == SelectedHosts.Count()) ?? Enumerable.Empty<PackageManagerSearchElementViewModel>();
list.Where(x => x.SearchElementModel.Hosts != null && SelectedHosts.Intersect(x.SearchElementModel.Hosts).Count() == SelectedHosts.Count()) ?? Enumerable.Empty<PackageManagerSearchElementViewModel>();

return filteredList;
}
Expand Down Expand Up @@ -1491,27 +1491,27 @@ private static void Sort(List<PackageManagerSearchElementViewModel> results, Pac
switch (key)
{
case PackageSortingKey.Name:
results.Sort((e1, e2) => e1.Model.Name.ToLower().CompareTo(e2.Model.Name.ToLower()));
results.Sort((e1, e2) => e1.SearchElementModel.Name.ToLower().CompareTo(e2.SearchElementModel.Name.ToLower()));
break;
case PackageSortingKey.Downloads:
results.Sort((e1, e2) => e1.Model.Downloads.CompareTo(e2.Model.Downloads));
results.Sort((e1, e2) => e1.SearchElementModel.Downloads.CompareTo(e2.SearchElementModel.Downloads));
break;
case PackageSortingKey.LastUpdate:
results.Sort((e1, e2) => e1.Versions.FirstOrDefault().Item1.created.CompareTo(e2.Versions.FirstOrDefault().Item1.created));
break;
case PackageSortingKey.Votes:
results.Sort((e1, e2) => e1.Model.Votes.CompareTo(e2.Model.Votes));
results.Sort((e1, e2) => e1.SearchElementModel.Votes.CompareTo(e2.SearchElementModel.Votes));
break;
case PackageSortingKey.Maintainers:
results.Sort((e1, e2) => e1.Model.Maintainers.ToLower().CompareTo(e2.Model.Maintainers.ToLower()));
results.Sort((e1, e2) => e1.SearchElementModel.Maintainers.ToLower().CompareTo(e2.SearchElementModel.Maintainers.ToLower()));
break;
//This sorting key is applied to search results when user submits a search query on package manager search window,
//it sorts in the following order: Not Deprecated Packages > search query in Name > Recently Updated
case PackageSortingKey.Search:
results.Sort((e1, e2) => {
int ret = e1.Model.IsDeprecated.CompareTo(e2.Model.IsDeprecated);
int i1 = e1.Model.Name.ToLower().IndexOf(query.ToLower(), StringComparison.InvariantCultureIgnoreCase);
int i2 = e2.Model.Name.ToLower().IndexOf(query.ToLower(), StringComparison.InvariantCultureIgnoreCase);
int ret = e1.SearchElementModel.IsDeprecated.CompareTo(e2.SearchElementModel.IsDeprecated);
int i1 = e1.SearchElementModel.Name.ToLower().IndexOf(query.ToLower(), StringComparison.InvariantCultureIgnoreCase);
int i2 = e2.SearchElementModel.Name.ToLower().IndexOf(query.ToLower(), StringComparison.InvariantCultureIgnoreCase);
ret = ret != 0 ? ret : ((i1 == -1) ? int.MaxValue : i1).CompareTo((i2 == -1) ? int.MaxValue : i2);
ret = ret != 0 ? ret : -e1.Versions.FirstOrDefault().Item1.created.CompareTo(e2.Versions.FirstOrDefault().Item1.created);
return ret;
Expand Down Expand Up @@ -1554,7 +1554,7 @@ public void ExecuteSelected()
if (SearchResults.Count <= SelectedIndex)
return;

SearchResults[SelectedIndex].Model.Execute();
SearchResults[SelectedIndex].SearchElementModel.Execute();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,6 @@
d:DesignWidth="800" >
<UserControl.Resources>
<ResourceDictionary>
<controls:EmptyStringToCollapsedConverter x:Key="EmptyStringToCollapsedConverter" />
<controls:DateToVisibilityCollapsedConverter x:Key="DateToVisibilityCollapsedConverter" />
<controls:DateToPackageLabelConverter x:Key="DateToPackageLabelCollapsedConverter" />
<controls:PrettyDateConverter x:Key="PrettyDateConverter" />
<controls:DependencyListToStringConverter x:Key="DependencyListToStringConverter" />
<controls:ListHasMoreThanNItemsToVisibilityConverter x:Key="ListHasMoreThanNItemsToVisibilityConverter" />

<ResourceDictionary.MergedDictionaries>
<ui:SharedResourceDictionary Source="{x:Static ui:SharedDictionaryManager.DynamoConvertersDictionaryUri}" />
<ui:SharedResourceDictionary Source="{x:Static ui:SharedDictionaryManager.DynamoModernDictionaryUri}" />
Expand Down
Loading

0 comments on commit aaaf8a6

Please sign in to comment.