public void ListMethodTest() { var source = new ModelB(); var target = new TestViewFactory(); // bind to collection var binding = new CollectionBinding("List", target); binding.Bind(source); Assert.AreEqual(0, target.ViewCount); // add item source.List.Add(new ModelA() { IntValue = 1 }); source.List.Add(new ModelA() { IntValue = 2 }); source.List.Add(new ModelA() { IntValue = 3 }); // do shuffle TestUtility.Shuffle(source.List); // do sort source.List.Sort((x, y) => x.IntValue.CompareTo(y.IntValue)); target.Destroy(); }
public void BindToCollection() { var source = new ModelB(); var target = new TestViewFactory(); // bind to collection var binding = new CollectionBinding("List", target); binding.Bind(source); Assert.AreEqual(0, target.viewList.Count); // add item source.List.Add(new ModelA()); Assert.AreEqual(1, target.viewList.Count); Assert.AreEqual(1, target.nextIndex); // notify list changed binding.HandleSourcePropertyChanged(source, "List"); Assert.AreEqual(2, target.nextIndex); Assert.AreEqual(1, target.viewList.Count); // notify with null property name binding.HandleSourcePropertyChanged(source, null); Assert.AreEqual(3, target.nextIndex); Assert.AreEqual(1, target.viewList.Count); target.Clear(); }
/// <summary> /// Find collection properties that can be data-bound. /// </summary> public static PropertyInfo[] FindBindableCollectionProperties(CollectionBinding target) { return(TypeResolver.FindBindableProperties(target) .Where(property => typeof(IEnumerable).IsAssignableFrom(property.PropertyType)) .Where(property => !typeof(string).IsAssignableFrom(property.PropertyType)) .ToArray()); }
public void AddAndRemoveTest() { var source = new ModelB(); var target = new TestViewFactory(); // bind to collection var binding = new CollectionBinding("List", target); binding.Bind(source); Assert.AreEqual(0, target.ViewCount); // add item source.List.Add(new ModelA()); Assert.AreEqual(1, target.ViewCount); // add item source.List.Add(new ModelA()); Assert.AreEqual(2, target.ViewCount); // remove item source.List.RemoveAt(0); Assert.AreEqual(1, target.ViewCount); // add item source.List.Add(new ModelA()); Assert.AreEqual(2, target.ViewCount); // clear source.List.Clear(); Assert.AreEqual(0, target.ViewCount); target.Destroy(); }
private PropertyInfo[] GetBindableViewModelCollections(CollectionBinding target) { return(target.GetAvailableViewModelTypes() .SelectMany(type => type.GetProperties(BindingFlags.Public | BindingFlags.Instance)) .Where(property => typeof(IEnumerable).IsAssignableFrom(property.PropertyType)) .Where(property => !typeof(string).IsAssignableFrom(property.PropertyType)) .ToArray()); }
protected override void OnEnabled() { // Initialise everything _targetScript = (CollectionBinding)target; _templateInitialPoolCountProperty = serializedObject.FindProperty("_templateInitialPoolCount"); _itemsContainerProperty = serializedObject.FindProperty("_itemsContainer"); _templatesProperty = serializedObject.FindProperty("_templates"); }
public void DuplicateItemTest() { var source = new ModelB(); var target = new TestViewFactory(); // bind to collection var binding = new CollectionBinding("List", target); binding.Bind(source); // get ref dictionary var field = typeof(CollectionBinding).GetField("viewReferenceCountDictionary", BindingFlags.NonPublic | BindingFlags.Instance); var refDictionary = field.GetValue(binding) as Dictionary <GameObject, int>; // check initial state { Assert.AreEqual(0, target.ViewCount); Assert.IsTrue(refDictionary == null); } var item = new ModelA(); // add item { source.List.Add(item); source.List.Add(item); // verify view count Assert.AreEqual(1, binding.BindingDictionary.Count); // check ref count refDictionary = field.GetValue(binding) as Dictionary <GameObject, int>; Assert.IsTrue(refDictionary != null); Assert.AreEqual(2, refDictionary.First().Value); } // remove item { source.List.RemoveAt(0); Assert.AreEqual(1, binding.BindingDictionary.Count); // check ref count Assert.AreEqual(1, refDictionary.First().Value); } // remove item { source.List.RemoveAt(0); Assert.AreEqual(0, binding.BindingDictionary.Count); // check ref count Assert.AreEqual(0, refDictionary.Count); } target.Destroy(); }
public void UpdateItemValueTest() { var source = new ModelB(); var target = new TestViewFactory(); // bind to collection var binding = new CollectionBinding("List", target); binding.Bind(source); Assert.AreEqual(0, target.ViewCount); // add item var itemA = new ModelA(); source.List.Add(itemA); // add item var itemB = new ModelA(); source.List.Add(itemB); // add item view A var viewA = new ModelA(); var bindingA = new Binding("IntValue", viewA, "IntValue"); var dcA = binding.BindingDictionary[itemA].GetComponent <IDataContext>(); dcA.AddBinding(bindingA); // add item view B var viewB = new ModelA(); var bindingB = new Binding("IntValue", viewB, "IntValue"); var dcB = binding.BindingDictionary[itemB].GetComponent <IDataContext>(); dcB.AddBinding(bindingB); // verify state Assert.IsTrue(bindingA.IsBound); Assert.IsTrue(bindingB.IsBound); Assert.AreEqual(1, dcA.BindingList.Count); Assert.AreEqual(1, dcB.BindingList.Count); Assert.AreEqual(0, viewA.IntValue); Assert.AreEqual(0, viewB.IntValue); // update value itemA.IntValue = 1; itemB.IntValue = 2; Assert.AreEqual(1, viewA.IntValue); Assert.AreEqual(2, viewB.IntValue); target.Destroy(); }
public void TestItemNestedPropertyChanged() { var source = new ModelD(); var target = new TestViewFactory(); var go = new GameObject("Test"); var dc = go.AddComponent <DataContext>(); // create collection binding var binding = new CollectionBinding("List", target); dc.AddBinding(binding); BindingManager.Instance.AddSource(source, "Test"); BindingManager.Instance.AddDataContext(dc, "Test"); // add item var item = new ModelC(); item.NestedValue.IntValue = 2; source.List.Add(item); var itemView = binding.BindingDictionary[item]; var itemDataContext = itemView.GetComponent <IDataContext>(); // add item binding var itemTarget = new ModelA(); var itemBinding = new Binding("NestedValue.IntValue", itemTarget, "IntValue"); itemDataContext.AddBinding(itemBinding); // check item value Assert.AreEqual(2, itemTarget.IntValue); // update value item.NestedValue.IntValue = 3; Assert.AreEqual(2, itemTarget.IntValue); // notify BindingManager.Instance.NotifyItemPropertyChanged(source, source, source.List, item, item.NestedValue, "IntValue"); Assert.AreEqual(3, itemTarget.IntValue); // update value item.NestedValue.IntValue = 4; Assert.AreEqual(3, itemTarget.IntValue); // notify with null BindingManager.Instance.NotifyItemPropertyChanged(source, source, source.List, item, item.NestedValue, null); Assert.AreEqual(4, itemTarget.IntValue); BindingManager.Instance.Clear(); target.Clear(); GameObject.DestroyImmediate(go); }
private void AddHook(IJob item) { if (_nextJobs != null) { var samples = ProtocolSynchronization.GetAffectedSamples(_context, item); _hooks.Add(item, CollectionBinding.Create( _nextJobs.Jobs.Where(j => ProtocolSynchronization.GetAffectedSamples(_context, j).Intersect(samples).Any(), j => ProtocolSynchronization.GetAffectedSamples(_context, j).Intersect(samples).Any()), item.Next)); } }
/// <summary> /// Bind the items of a UICollectionView to a collection. /// </summary> /// <typeparam name="T">The type of item in the collection.</typeparam> /// <param name="bindings">The binding manager.</param> /// <param name="control">The ListView to bind.</param> /// <param name="output">The collection to which to bind.</param> /// <param name="selection">Get current selection.</param> /// <param name="selected">Set the current selection.</param> /// <param name="bind">The delegate that binds each item of the collection.</param> public static void BindItems <T>( this BindingManager bindings, UITableView control, Func <IEnumerable <T> > output, Func <T> selection, Action <T> selected, Action <UITableViewCell, T, BindingManager> bind) { var binding = new CollectionBinding <T>(control, bind, selected); control.Source = binding; bindings.Bind(() => output().ToList(), items => binding.UpdateItems(items), binding); }
/// <summary> /// Show dropdown for selecting a collection to bind to. /// </summary> private void ShowCollectionSelector(CollectionBinding targetScript, PropertyInfo[] bindableCollections) { EditorGUILayout.BeginHorizontal(); EditorGUILayout.PrefixLabel("Collection to bind to"); var dropdownPosition = GUILayoutUtility.GetLastRect(); dropdownPosition.x += dropdownPosition.width; if (GUILayout.Button(new GUIContent(targetScript.viewModelPropertyName), EditorStyles.popup)) { ShowCollectionSelectorDropdown(targetScript, bindableCollections, dropdownPosition); } EditorGUILayout.EndHorizontal(); }
/// <summary> /// Draws the dropdown for selecting a method from bindableViewModelCollections /// </summary> private void ShowCollectionSelectorDropdown(CollectionBinding targetScript, PropertyInfo[] bindableViewModelCollections, Rect position) { var selectedIndex = Array.IndexOf( bindableViewModelCollections.Select(m => m.ReflectedType + m.Name).ToArray(), targetScript.viewModelName + targetScript.viewModelPropertyName ); var options = bindableViewModelCollections.Select(m => new InspectorUtils.MenuItem( new GUIContent(m.ReflectedType + "/" + m.Name), true ) ).ToArray(); InspectorUtils.ShowCustomSelectionMenu( index => SetViewModelProperty(targetScript, bindableViewModelCollections[index]), options, selectedIndex, position); }
public void OrderTest() { var source = new ModelB(); var target = new TestViewFactory(); // bind to collection var binding = new CollectionBinding("List", target); binding.Bind(source); Assert.AreEqual(0, target.ViewCount); // add item source.List.Add(new ModelA()); source.List.Add(new ModelA()); var sourceA = source.List[0]; var sourceB = source.List[1]; var viewA = binding.BindingDictionary[sourceA]; Assert.AreEqual(0, viewA.transform.GetSiblingIndex()); var viewB = binding.BindingDictionary[sourceB]; Assert.AreEqual(1, viewB.transform.GetSiblingIndex()); // switch items source.List[0] = sourceB; source.List[1] = sourceA; viewA = binding.BindingDictionary[sourceA]; Assert.AreEqual(1, viewA.transform.GetSiblingIndex()); viewB = binding.BindingDictionary[sourceB]; Assert.AreEqual(0, viewB.transform.GetSiblingIndex()); target.Destroy(); }
private void OnEnable() { // Initialise everything targetScript = (CollectionBinding)target; }
protected override void OnEnable() { base.OnEnable(); Target = target as CollectionBinding; }
internal static global::System.Runtime.InteropServices.HandleRef getCPtr(CollectionBinding obj) { return((obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr); }
/// <summary> /// Set up the viewModelName and viewModelPropertyname in the TwoWayPropertyBinding we're editing. /// </summary> private void SetViewModelProperty(CollectionBinding target, PropertyInfo property) { target.viewModelName = property.ReflectedType.Name; target.viewModelPropertyName = property.Name; }
/// <summary> /// Find collection properties that can be data-bound. /// </summary> public static BindableMember <PropertyInfo>[] FindBindableCollectionProperties(CollectionBinding target) { return(FindBindableProperties(target) .Where(p => typeof(IEnumerable).IsAssignableFrom(p.Member.PropertyType)) .Where(p => !typeof(string).IsAssignableFrom(p.Member.PropertyType)) .ToArray()); }
public CollectionBindingSubscriber(CollectionBinding <T> binding) { this.binding = binding; }