diff options
author | Eric Maupin <ermaup@microsoft.com> | 2019-03-29 21:59:20 +0300 |
---|---|---|
committer | Eric Maupin <ermaup@microsoft.com> | 2019-04-24 22:03:17 +0300 |
commit | 1ee3dc571f4e7d65b87afae5c701b9a1fa4e5eea (patch) | |
tree | abb137f9de15ce957326bb93a57640b66d0923dc | |
parent | c6e3613c6ea5bc087e026d2b4ed1f0e7d3133049 (diff) |
[Core] Add GetIsLastVariant for UI
4 files changed, 197 insertions, 7 deletions
diff --git a/Xamarin.PropertyEditing.Tests/PanelViewModelTests.cs b/Xamarin.PropertyEditing.Tests/PanelViewModelTests.cs index 0ecb6a1..cd271db 100644 --- a/Xamarin.PropertyEditing.Tests/PanelViewModelTests.cs +++ b/Xamarin.PropertyEditing.Tests/PanelViewModelTests.cs @@ -691,6 +691,139 @@ namespace Xamarin.PropertyEditing.Tests Assert.That (group, Is.Not.Null); } + + [Test] + public void UncommonVariantsAddedWhenPropertyIs () + { + var variations = new[] { + new PropertyVariationOption ("Width", "Compact"), + new PropertyVariationOption ("Width", "Regular"), + new PropertyVariationOption ("Gamut", "P3"), + new PropertyVariationOption ("Gamut", "sRGB"), + }; + + var property = new Mock<IPropertyInfo> (); + property.SetupGet (p => p.Name).Returns ("Variation"); + property.SetupGet (p => p.Type).Returns (typeof (string)); + property.SetupGet (p => p.RealType).Returns (typeof (string).ToTypeInfo ()); + property.SetupGet (p => p.CanWrite).Returns (true); + property.SetupGet (p => p.ValueSources).Returns (ValueSources.Default | ValueSources.Local); + property.SetupGet (p => p.Variations).Returns (variations); + property.SetupGet (p => p.IsUncommon).Returns (true); + + var properties = new ObservableCollection<IPropertyInfo> (); + + var variants = new[] { + new PropertyVariation (variations[0]), + new PropertyVariation (variations[0], variations[2]) + }; + + var target = new object (); + var editor = new Mock<IObjectEditor> (); + editor.SetupGet (oe => oe.TargetType).Returns (typeof (object).ToTypeInfo ()); + editor.SetupGet (oe => oe.Target).Returns (target); + editor.SetupGet (oe => oe.Properties).Returns (properties); + editor.Setup (oe => oe.GetPropertyVariantsAsync (property.Object)).ReturnsAsync (variants); + editor.Setup (oe => oe.GetValueAsync<string> (property.Object, null)).ReturnsAsync ( + new ValueInfo<string> { + Value = "Any", + Source = ValueSource.Local + }); + editor.Setup (oe => oe.GetValueAsync<string> (property.Object, variants[0])).ReturnsAsync ( + new ValueInfo<string> { + Value = "Compact", + Source = ValueSource.Local + }); + editor.Setup (oe => oe.GetValueAsync<string> (property.Object, variants[1])).ReturnsAsync ( + new ValueInfo<string> { + Value = "Compact+P3", + Source = ValueSource.Local + }); + + var provider = new Mock<IEditorProvider> (); + provider.Setup (p => p.GetObjectEditorAsync (target)).ReturnsAsync (editor.Object); + + var vm = CreateVm (provider.Object); + vm.ArrangeMode = PropertyArrangeMode.Category; + vm.SelectedObjects.Add (target); + Assume.That (vm.Properties, Is.Empty); + + properties.Add (property.Object); + + var stringVms = vm.ArrangedEditors[0].UncommonEditors.OfType<StringPropertyViewModel> ().ToList (); + Assert.That (stringVms.Count, Is.EqualTo (3), "Not including correct number of properties with variants"); + Assert.That (stringVms.Count (svm => svm.Variation == null), Is.EqualTo (1), "Did not include neutral property"); + Assert.That (stringVms.Count (svm => svm.Variation == variants[0]), Is.EqualTo (1), "Missing variant property"); + Assert.That (stringVms.Count (svm => svm.Variation == variants[1]), Is.EqualTo (1), "Missing variant property"); + } + + [Test] + public void GetIsLastVariant ([Values (true, false)] bool isUncommon, [Values (true, false)] bool isLast) + { + var variations = new[] { + new PropertyVariationOption ("Width", "Compact"), + new PropertyVariationOption ("Width", "Regular"), + new PropertyVariationOption ("Gamut", "P3"), + new PropertyVariationOption ("Gamut", "sRGB"), + }; + + var property = new Mock<IPropertyInfo> (); + property.SetupGet (p => p.Name).Returns ("Variation"); + property.SetupGet (p => p.Type).Returns (typeof (string)); + property.SetupGet (p => p.RealType).Returns (typeof (string).ToTypeInfo ()); + property.SetupGet (p => p.CanWrite).Returns (true); + property.SetupGet (p => p.ValueSources).Returns (ValueSources.Default | ValueSources.Local); + property.SetupGet (p => p.Variations).Returns (variations); + property.SetupGet (p => p.IsUncommon).Returns (isUncommon); + + var properties = new ObservableCollection<IPropertyInfo> (); + + var variants = new[] { + new PropertyVariation (variations[0]), + new PropertyVariation (variations[0], variations[2]) + }; + + var target = new object (); + var editor = new Mock<IObjectEditor> (); + editor.SetupGet (oe => oe.TargetType).Returns (typeof (object).ToTypeInfo ()); + editor.SetupGet (oe => oe.Target).Returns (target); + editor.SetupGet (oe => oe.Properties).Returns (properties); + editor.Setup (oe => oe.GetPropertyVariantsAsync (property.Object)).ReturnsAsync (variants); + editor.Setup (oe => oe.GetValueAsync<string> (property.Object, null)).ReturnsAsync ( + new ValueInfo<string> { + Value = "Any", + Source = ValueSource.Local + }); + editor.Setup (oe => oe.GetValueAsync<string> (property.Object, variants[0])).ReturnsAsync ( + new ValueInfo<string> { + Value = "Compact", + Source = ValueSource.Local + }); + editor.Setup (oe => oe.GetValueAsync<string> (property.Object, variants[1])).ReturnsAsync ( + new ValueInfo<string> { + Value = "Compact+P3", + Source = ValueSource.Local + }); + + var provider = new Mock<IEditorProvider> (); + provider.Setup (p => p.GetObjectEditorAsync (target)).ReturnsAsync (editor.Object); + + var vm = CreateVm (provider.Object); + vm.ArrangeMode = PropertyArrangeMode.Category; + vm.SelectedObjects.Add (target); + Assume.That (vm.Properties, Is.Empty); + + properties.Add (property.Object); + + var stringVms = ((isUncommon) ? vm.ArrangedEditors[0].UncommonEditors : vm.ArrangedEditors[0].Editors) + .OfType<StringPropertyViewModel> (); + var prvm = (isLast) + ? stringVms.Last (pvm => pvm.Property == property.Object) + : stringVms.Skip(1).First (pvm => pvm.Property == property.Object); + + Assert.That (vm.GetIsLastVariant (prvm), Is.EqualTo (isLast)); + } + internal override PanelViewModel CreateVm (TargetPlatform platform) { return new PanelViewModel (platform); diff --git a/Xamarin.PropertyEditing/ViewModels/PanelViewModel.cs b/Xamarin.PropertyEditing/ViewModels/PanelViewModel.cs index 1d9e7a1..7b41e74 100644 --- a/Xamarin.PropertyEditing/ViewModels/PanelViewModel.cs +++ b/Xamarin.PropertyEditing/ViewModels/PanelViewModel.cs @@ -57,7 +57,7 @@ namespace Xamarin.PropertyEditing.ViewModels if (editor == null) throw new ArgumentNullException (nameof(editor)); - var list = GetList (editor); + var list = GetListCore (editor); if (editor is PropertyViewModel pvm && this.targetPlatform.GroupedTypes != null && this.targetPlatform.GroupedTypes.TryGetValue (pvm.Property.Type, out string groupName)) { var group = list.OfType<PropertyGroupViewModel> ().FirstOrDefault (gvm => gvm.Category == groupName); if (group != null) { @@ -99,6 +99,14 @@ namespace Xamarin.PropertyEditing.ViewModels this.isExpanded[mode] = expanded; } + public IReadOnlyList<EditorViewModel> GetList (EditorViewModel evm) + { + if (evm == null) + throw new ArgumentNullException (nameof(evm)); + + return (IReadOnlyList<EditorViewModel>)GetListCore (evm); + } + private Dictionary<PropertyArrangeMode, bool> isExpanded; private readonly ObservableCollectionEx<EditorViewModel> editors = new ObservableCollectionEx<EditorViewModel> (); private readonly ObservableCollectionEx<EditorViewModel> uncommonEditors = new ObservableCollectionEx<EditorViewModel> (); @@ -119,7 +127,7 @@ namespace Xamarin.PropertyEditing.ViewModels if (editor == null) throw new ArgumentNullException (nameof (editor)); - var list = GetList (editor); + var list = GetListCore (editor); if (editor is PropertyViewModel pvm && this.targetPlatform.GroupedTypes != null && this.targetPlatform.GroupedTypes.TryGetValue (pvm.Property.Type, out string groupName)) { var group = list.OfType<PropertyGroupViewModel> ().FirstOrDefault (gvm => gvm.Category == groupName); if (group != null) @@ -133,7 +141,7 @@ namespace Xamarin.PropertyEditing.ViewModels OnPropertyChanged (nameof(HasUncommonElements)); } - private IList<EditorViewModel> GetList (EditorViewModel evm) + private IList<EditorViewModel> GetListCore (EditorViewModel evm) { if (this.separateUncommon && evm is PropertyViewModel pvm) return pvm.Property.IsUncommon ? this.uncommonEditors : this.editors; @@ -341,6 +349,30 @@ namespace Xamarin.PropertyEditing.ViewModels ArrangedPropertiesChanged?.Invoke (this, EventArgs.Empty); } + internal override bool GetIsLastVariant (PropertyViewModel viewModel) + { + if (viewModel == null) + throw new ArgumentNullException (nameof (viewModel)); + if (!viewModel.IsVariant) + throw new ArgumentException ($"{nameof (viewModel)} is not a variant", nameof (viewModel)); + + string groupKey = GetGroup (viewModel); + PanelGroupViewModel group = this.arranged[groupKey]; + + var list = group.GetList (viewModel); + + int index = list.IndexOf (viewModel); + if (index == -1) + throw new KeyNotFoundException ($"{nameof (viewModel)} was not found"); + + if (++index == list.Count) + return true; + if (list[index] is PropertyViewModel pvm) { + return !Equals (viewModel.Property, pvm.Property); + } else + return false; + } + private readonly OrderedDictionary<string, PanelGroupViewModel> arranged = new OrderedDictionary<string, PanelGroupViewModel> (); private PropertyArrangeMode arrangeMode; diff --git a/Xamarin.PropertyEditing/ViewModels/PropertiesViewModel.cs b/Xamarin.PropertyEditing/ViewModels/PropertiesViewModel.cs index 1c694d8..bc8e2e0 100644 --- a/Xamarin.PropertyEditing/ViewModels/PropertiesViewModel.cs +++ b/Xamarin.PropertyEditing/ViewModels/PropertiesViewModel.cs @@ -216,6 +216,14 @@ namespace Xamarin.PropertyEditing.ViewModels { } + /// <summary> + /// Gets whether the <paramref name="viewModel"/> is the last-arranged variant property of its base property + /// </summary> + internal virtual bool GetIsLastVariant (PropertyViewModel viewModel) + { + throw new NotSupportedException(); + } + private INameableObject nameable; private bool nameReadOnly; private bool eventsEnabled; @@ -581,10 +589,10 @@ namespace Xamarin.PropertyEditing.ViewModels if (baseVm.HasVariations) { using (await AsyncWork.RequestAsyncWork (this)) { var variants = await GetVariationsAsync (property); - baseVm.HasVariantChildren = variants.Count > 0; - - foreach (PropertyVariation variant in variants) { - vms.Add (CreateViewModel (property, variant)); + if (baseVm.HasVariantChildren = variants.Count > 0) { + foreach (PropertyVariation variant in variants) { + vms.Add (CreateViewModel (property, variant)); + } } } } @@ -610,6 +618,7 @@ namespace Xamarin.PropertyEditing.ViewModels else vm = new StringPropertyViewModel (TargetPlatform, property, this.objEditors, variant); + vm.Parent = this; vm.VariantsChanged += OnVariantsChanged; return vm; } diff --git a/Xamarin.PropertyEditing/ViewModels/PropertyViewModel.cs b/Xamarin.PropertyEditing/ViewModels/PropertyViewModel.cs index 7d9067f..593aa51 100644 --- a/Xamarin.PropertyEditing/ViewModels/PropertyViewModel.cs +++ b/Xamarin.PropertyEditing/ViewModels/PropertyViewModel.cs @@ -636,6 +636,12 @@ namespace Xamarin.PropertyEditing.ViewModels public bool IsVariant => Variation != null; + public PropertiesViewModel Parent + { + get; + internal set; + } + public abstract Resource Resource { get; @@ -724,6 +730,16 @@ namespace Xamarin.PropertyEditing.ViewModels get; } + public bool GetIsLastVariant () + { + if (Variation == null) + return false; + if (Parent == null) + throw new InvalidOperationException ($"{nameof(Parent)} must be set in order to determine last variant"); + + return Parent.GetIsLastVariant (this); + } + public override int CompareTo (EditorViewModel other) { int compare = base.CompareTo (other); |