Example #1
0
        public void SetUp()
        {
            _serverUpdateStream = new Subject<Either<FullUpdate, DeltaUpdate>>();

            _dictionaryModificationStream = _serverUpdateStream
                .Select(either =>
                {
                    if (either.IsLeft)
                    {
                        return DictionaryModification.Replace(either.Left.Values["key"], (Update)either.Left);
                    }
                    else
                    {
                        return DictionaryModification.Upset(either.Right.Values["key"], (Update)either.Right);
                    }
                });

            _observableDictionary = _dictionaryModificationStream.ToObservableDictionary((key, existing, update) =>
            {
                var fullUpdate = new FullUpdate(key)
                {
                    Values = new Dictionary<string, string>(existing.Values)
                };

                foreach (var kvp in update)
                {
                    fullUpdate.Values[kvp.Key] = kvp.Value;
                }

                return fullUpdate;
            });
        }
Example #2
0
        public void SetUp()
        {
            _serverUpdateStream = new Subject <Either <FullUpdate, DeltaUpdate> >();

            _dictionaryModificationStream = _serverUpdateStream
                                            .Select(either =>
            {
                if (either.IsLeft)
                {
                    return(DictionaryModification.Replace(either.Left.Values["key"], (Update)either.Left));
                }
                else
                {
                    return(DictionaryModification.Upset(either.Right.Values["key"], (Update)either.Right));
                }
            });

            _observableDictionary = _dictionaryModificationStream.ToObservableDictionary((key, existing, update) =>
            {
                var fullUpdate = new FullUpdate(key)
                {
                    Values = new Dictionary <string, string>(existing.Values)
                };

                foreach (var kvp in update)
                {
                    fullUpdate.Values[kvp.Key] = kvp.Value;
                }

                return(fullUpdate);
            });
        }
        /// <summary>
        /// Forwards the <paramref name="source" /> changes to the <paramref name="target" />.
        /// </summary>
        /// <typeparam name="TKey">The type of the key.</typeparam>
        /// <typeparam name="TValue">The type of the value.</typeparam>
        /// <param name="source">The source observable dictionary.</param>
        /// <param name="target">The target <see cref="IEnhancedBindingList{TValue}"/>.</param>
        /// <param name="dictionaryChangesFilterPredicate">A filter function to test each <paramref name="source" /> change for whether or not to forward it to the <paramref name="target" />.</param>
        /// <param name="includeItemChanges">if set to <c>true</c> individual items' changes will be propagated to the <paramref name="target" />.</param>
        /// <param name="addRangePredicateForResets">This filter predicate tests which elements of the source <see cref="IObservableDictionary{TKey,TValue}"/> to add
        /// whenever a <see cref="ObservableDictionaryChangeType.Reset"/> is received. A reset is forwarded by clearing the <paramref name="target"/> completely and re-filling it with
        /// the source's values, and this predicate determines which ones are added. If no filter predicate is provided, all source values will be re-added to the <paramref name="target"/>.</param>
        /// <param name="addDistinctValuesOnResetOnly">if set to <c>true</c> only distinct values will be re-added on <see cref="ObservableDictionaryChangeType.Reset" /> changes.</param>
        /// <param name="valueComparerForResets">The value equality comparer to use for reset changes and if <paramref name="valueComparerForResets"/> is set to [true]. If none is provided, the default one for the value type will be used</param>
        /// <param name="scheduler">The scheduler to schedule notifications and changes on.</param>
        /// <returns>An <see cref="IDisposable"/> which will forward the changes to the <paramref name="target"/> as long as <see cref="IDisposable.Dispose"/> hasn't been called.</returns>
        public static IDisposable ForwardDictionaryChangesTo <TKey, TValue>(
            this IObservableDictionary <TKey, TValue> source,
            IEnhancedBindingList <TValue> target,
            Func <IObservableDictionaryChange <TKey, TValue>, bool> dictionaryChangesFilterPredicate,
            bool includeItemChanges = false,
            Func <KeyValuePair <TKey, TValue>, bool> addRangePredicateForResets = null,
            bool addDistinctValuesOnResetOnly = true,
            IEqualityComparer <TValue> valueComparerForResets = null,
            IScheduler scheduler = null)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (target == null)
            {
                throw new ArgumentNullException(nameof(target));
            }

            var sourceObservable = scheduler != null
                ? source.DictionaryChanges.ObserveOn(scheduler)
                : source.DictionaryChanges;

            if (dictionaryChangesFilterPredicate != null)
            {
                sourceObservable = sourceObservable.Where(dictionaryChangesFilterPredicate);
            }

            return(sourceObservable.ForwardDictionaryChangesTo(target, includeItemChanges, addRangePredicateForResets, addDistinctValuesOnResetOnly, valueComparerForResets));
        }
Example #4
0
        private void DeregisterRunResultsEvents(IRun run)
        {
            IObservableDictionary <string, IItem> dict = run.Results;

            dict.ItemsAdded      -= RunOnResultChanged;
            dict.ItemsRemoved    -= RunOnResultRemoved;
            dict.ItemsReplaced   -= RunOnResultChanged;
            dict.CollectionReset -= RunOnResultChanged;
        }
Example #5
0
        private void RegisterRunParametersEvents(IRun run)
        {
            IObservableDictionary <string, IItem> dict = run.Parameters;

            dict.ItemsAdded      += RunOnParameterChanged;
            dict.ItemsRemoved    += RunOnParameterRemoved;
            dict.ItemsReplaced   += RunOnParameterChanged;
            dict.CollectionReset += RunOnParameterChanged;
        }
 public ReadOnlyObservableDictionary(IObservableDictionary <TKey, TValue> dictionary)
 {
     if (dictionary == null)
     {
         throw new ArgumentNullException();
     }
     dict = dictionary;
     RegisterEvents();
 }
        /// <summary>
        /// Forwards the <paramref name="source" /> changes to the <paramref name="target" />.
        /// </summary>
        /// <typeparam name="TKey">The type of the key.</typeparam>
        /// <typeparam name="TValue">The type of the value.</typeparam>
        /// <param name="source">The source observable dictionary.</param>
        /// <param name="target">The target <see cref="IEnhancedBindingList{TValue}"/>.</param>
        /// <param name="includeItemChanges">if set to <c>true</c> individual items' changes will be propagated to the <paramref name="target" />.</param>
        /// <param name="scheduler">The scheduler to schedule notifications and changes on.</param>
        /// <returns>An <see cref="IDisposable"/> which will forward the changes to the <paramref name="target"/> as long as <see cref="IDisposable.Dispose"/> hasn't been called.</returns>
        public static IDisposable ForwardDictionaryChangesTo <TKey, TValue>(
            this IObservableDictionary <TKey, TValue> source,
            IEnhancedBindingList <TValue> target,
            bool includeItemChanges = false,
            IScheduler scheduler    = null)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (target == null)
            {
                throw new ArgumentNullException(nameof(target));
            }

            var sourceObservable = scheduler != null
                ? source.DictionaryChanges.ObserveOn(scheduler)
                : source.DictionaryChanges;

            return(sourceObservable.ForwardDictionaryChangesTo(target, includeItemChanges));
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="ObservableDictionaryChange{TKey,TValue}" /> class.
        /// </summary>
        /// <param name="dictionary">The dictionary for this change.</param>
        /// <param name="changeType">Type of the change.</param>
        /// <param name="key">The key of the changed value.</param>
        /// <param name="value">The added, removed or changed, new value.</param>
        /// <param name="oldValue">The replaced value, only applicable if <paramref name="changeType" /> is <see cref="ObservableDictionaryChangeType.ItemValueReplaced" />.</param>
        /// <param name="changedPropertyName">The changed property name, only applicable if <paramref name="changeType" /> is <see cref="ObservableDictionaryChangeType.ItemValueChanged" />.</param>
        /// <exception cref="System.ArgumentNullException"></exception>
        /// <exception cref="System.ArgumentOutOfRangeException">
        /// $Item Adds, Key-/Value Changes or Removes must have a (non-default) '{nameof(key)}'
        /// or
        /// $Resets must not have a '{nameof(value)}'
        /// or
        /// $Only Changes may have a '{nameof(oldValue)}'
        /// or
        /// $Only '{ObservableDictionaryChangeType.ItemValueChanged}' and '{ObservableDictionaryChangeType.ItemKeyChanged}' Changes may have a '{nameof(changedPropertyName)}'
        /// </exception>
        protected ObservableDictionaryChange(IObservableDictionary <TKey, TValue> dictionary, ObservableDictionaryChangeType changeType, TKey key = default(TKey), TValue value = default(TValue), TValue oldValue = default(TValue), string changedPropertyName = "")
        {
            if (dictionary == null)
            {
                throw new ArgumentNullException(nameof(dictionary));
            }

            if ((changeType != ObservableDictionaryChangeType.Reset && changeType != ObservableDictionaryChangeType.ItemValueChanged) &&
                (KeyIsValueType.Value == false && Equals(key, default(TKey))))
            {
                throw new ArgumentOutOfRangeException(nameof(key), $"Item Adds, Key-/Value Changes or Removes must have a (non-default) '{nameof(key)}'");
            }

            if (changeType == ObservableDictionaryChangeType.Reset && (ValueIsValueType.Value == false && !Equals(value, default(TValue))))
            {
                throw new ArgumentOutOfRangeException(nameof(value), $"Resets must not have a '{nameof(value)}'");
            }

            if (changeType != ObservableDictionaryChangeType.ItemValueReplaced &&
                (ValueIsValueType.Value == false && !Equals(oldValue, default(TValue))))
            {
                throw new ArgumentOutOfRangeException(nameof(oldValue), $"Only Changes may have a '{nameof(oldValue)}'");
            }

            if (changeType != ObservableDictionaryChangeType.ItemValueChanged && changeType != ObservableDictionaryChangeType.ItemKeyChanged && !string.IsNullOrWhiteSpace(changedPropertyName))
            {
                throw new ArgumentOutOfRangeException(nameof(changedPropertyName), $"Only '{ObservableDictionaryChangeType.ItemValueChanged}' and '{ObservableDictionaryChangeType.ItemKeyChanged}' Changes may have a '{nameof(changedPropertyName)}'");
            }

            Dictionary = dictionary;
            ChangeType = changeType;

            Key = key;

            Value    = value;
            OldValue = oldValue;

            ChangedPropertyName = changedPropertyName ?? string.Empty;
        }
Example #9
0
 /// <inheritdoc />
 public void Init()
 {
     _currencies = new ObservableDictionary <GameId, int>(Data.Currencies);
 }
Example #10
0
 /// <inheritdoc />
 public void Init()
 {
     Ids = new ObservableDictionary <UniqueId, GameId>(Data.GameIds);
 }
 /// <summary>${WP_pubilc_Constructors_Initializes} <see cref="ElementsLayer">ElementsLayer</see>
 ///     ${WP_pubilc_Constructors_instance}</summary>
 public ElementsLayer()
 {
     Children = new ObservableCollection<UIElement>();
     Children.CollectionChanged += new NotifyCollectionChangedEventHandler(children_CollectionChanged);
     IsAutoAvoidance = false;
     BoundsCollection = new TObservableDictionary<object, Rectangle2D>();
 }
Example #12
0
        /// <summary>
        /// Converts the given <paramref name="observableDictionaryChange" /> to its <see cref="IObservableCollectionChange{T}" /> counterpart.
        /// </summary>
        /// <typeparam name="TKey">The type of the key.</typeparam>
        /// <typeparam name="TValue">The type of the value.</typeparam>
        /// <param name="observableDictionaryChange">The observable dictionary change.</param>
        /// <param name="dictionary">The sender <see cref="IObservableDictionary{TKey,TValue}" />.</param>
        /// <param name="valueComparer">The <typeparamref name="TValue"/> equality comparer.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException">observableDictionaryChange</exception>
        /// <exception cref="System.ArgumentOutOfRangeException">$Only {ObservableDictionaryChangeType.ItemAdded}, {ObservableDictionaryChangeType.ItemValueChanged}, {ObservableDictionaryChangeType.ItemRemoved} and {ObservableDictionaryChangeType.Reset} are supported.</exception>
        public static IList <IObservableCollectionChange <KeyValuePair <TKey, TValue> > > ToObservableCollectionChanges <TKey, TValue>(
            this IObservableDictionaryChange <TKey, TValue> observableDictionaryChange,
            IObservableDictionary <TKey, TValue> dictionary,
            IEqualityComparer <TValue> valueComparer)
        {
            if (observableDictionaryChange == null)
            {
                throw new ArgumentNullException(nameof(observableDictionaryChange));
            }
            if (dictionary == null)
            {
                throw new ArgumentNullException(nameof(dictionary));
            }
            if (valueComparer == null)
            {
                throw new ArgumentNullException(nameof(valueComparer));
            }

            var result = new List <IObservableCollectionChange <KeyValuePair <TKey, TValue> > >();

            switch (observableDictionaryChange.ChangeType)
            {
            case ObservableDictionaryChangeType.ItemAdded:
                result.Add(new ObservableCollectionChange <KeyValuePair <TKey, TValue> >(
                               ObservableCollectionChangeType.ItemAdded,
                               new KeyValuePair <TKey, TValue>(observableDictionaryChange.Key, observableDictionaryChange.Value)));
                break;

            case ObservableDictionaryChangeType.ItemKeyChanged:
            {
                TValue valueForKey;
                if (((IDictionary <TKey, TValue>)dictionary).TryGetValue(observableDictionaryChange.Key, out valueForKey))
                {
                    result.Add(new ObservableCollectionChange <KeyValuePair <TKey, TValue> >(
                                   ObservableCollectionChangeType.ItemChanged,
                                   new KeyValuePair <TKey, TValue>(observableDictionaryChange.Key, valueForKey)));
                }
                break;
            }

            case ObservableDictionaryChangeType.ItemValueChanged:
            {
                result.AddRange(dictionary.GetKeysForValue(observableDictionaryChange.Value, valueComparer).Select(key =>
                                                                                                                   new ObservableCollectionChange <KeyValuePair <TKey, TValue> >(
                                                                                                                       ObservableCollectionChangeType.ItemChanged,
                                                                                                                       new KeyValuePair <TKey, TValue>(key, observableDictionaryChange.Value))));
            }
            break;

            case ObservableDictionaryChangeType.ItemValueReplaced:
                result.Add(new ObservableCollectionChange <KeyValuePair <TKey, TValue> >(
                               ObservableCollectionChangeType.ItemRemoved,
                               new KeyValuePair <TKey, TValue>(observableDictionaryChange.Key, observableDictionaryChange.OldValue)));
                result.Add(new ObservableCollectionChange <KeyValuePair <TKey, TValue> >(
                               ObservableCollectionChangeType.ItemAdded,
                               new KeyValuePair <TKey, TValue>(observableDictionaryChange.Key, observableDictionaryChange.Value)));
                break;

            case ObservableDictionaryChangeType.ItemRemoved:
                result.Add(new ObservableCollectionChange <KeyValuePair <TKey, TValue> >(
                               ObservableCollectionChangeType.ItemRemoved,
                               new KeyValuePair <TKey, TValue>(observableDictionaryChange.Key, observableDictionaryChange.OldValue)));
                break;

            case ObservableDictionaryChangeType.Reset:
                result.Add(new ObservableCollectionChange <KeyValuePair <TKey, TValue> >(
                               ObservableCollectionChangeType.Reset));
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(observableDictionaryChange),
                                                      $"Only {ObservableDictionaryChangeType.ItemAdded}, {ObservableDictionaryChangeType.ItemValueChanged}, {ObservableDictionaryChangeType.ItemRemoved} and {ObservableDictionaryChangeType.Reset} are supported.");
            }

            return(result);
        }
 public void RemoveDictionaryNotifiers(IObservableDictionary <string, TestItem> obvDict)
 {
     obvDict.DictionaryChanged -= OnDictionaryChanged;
 }
Example #14
0
 public static IReadOnlyObservableDictionary <TKey, TValue> ToReadOnly <TKey, TValue>
     (this IObservableDictionary <TKey, TValue> self)
 {
     return((IReadOnlyObservableDictionary <TKey, TValue>)self);
 }
Example #15
0
 /// <summary>
 /// Returns all items in the observable dictionary after it has finished initialising, or immediately if already initialised.
 /// </summary>
 /// <typeparam name="TKey"></typeparam>
 /// <typeparam name="TValue"></typeparam>
 /// <param name="source"></param>
 /// <returns></returns>
 public static IObservable <IEnumerable <KeyValuePair <TKey, TValue> > > Items <TKey, TValue>(this IObservableDictionary <TKey, TValue> source)
 {
     return(from _ in source.IsInitialised()
            from existingItems in source.TakeWhile(notification => notification.Type != DictionaryNotificationType.Initialised)
            .Select(dn => new KeyValuePair <TKey, TValue>(dn.Key, dn.Value))
            .ToList()
            select existingItems.AsEnumerable());
 }
 /// <summary>
 /// Gets a <see cref="IObservableDictionaryChange{TKey,TValue}" /> representing a <see cref="ObservableDictionaryChangeType.ItemValueReplaced" />,
 /// more particularly one for an item replacement inside the <see cref="IObservableDictionary{TKey,TValue}" />.
 /// </summary>
 /// <param name="dictionary">The dictionary that this change is for and notifying about.</param>
 /// <param name="key">The key.</param>
 /// <param name="newValue">The new value.</param>
 /// <param name="replacedOldValue">The replaced old value.</param>
 /// <returns></returns>
 /// <value>
 /// An <see cref="IObservableDictionaryChange{TKey,TValue}">instance</see> representing an item replacement <see cref="ObservableDictionaryChangeType.ItemValueReplaced" />.
 /// </value>
 public static IObservableDictionaryChange <TKey, TValue> ItemValueReplaced(IObservableDictionary <TKey, TValue> dictionary, TKey key, TValue newValue, TValue replacedOldValue)
 => new ObservableDictionaryChange <TKey, TValue>(dictionary, ObservableDictionaryChangeType.ItemValueReplaced, key, newValue, replacedOldValue);
 /// <summary>
 /// Gets a <see cref="IObservableDictionaryChange{TKey,TValue}" /> representing a <see cref="ObservableDictionaryChangeType.ItemValueChanged" />,
 /// meaning that a given <paramref name="value"/>, more precisely one of its properties, for one or more key(s) has changed.
 /// </summary>
 /// <param name="dictionary">The dictionary that this change is for and notifying about.</param>
 /// <param name="value">The value.</param>
 /// <param name="propertyName">Name of the property.</param>
 /// <returns></returns>
 /// <value>
 /// An <see cref="IObservableDictionaryChange{TKey,TValue}">instance</see> representing an item property changed <see cref="ObservableDictionaryChangeType.ItemValueChanged" />.
 /// </value>
 public static IObservableDictionaryChange <TKey, TValue> ItemValueChanged(IObservableDictionary <TKey, TValue> dictionary, TValue value, string propertyName)
 => new ObservableDictionaryChange <TKey, TValue>(dictionary, ObservableDictionaryChangeType.ItemValueChanged, value: value, changedPropertyName: propertyName);
 /// <summary>
 /// Gets a <see cref="IObservableDictionaryChange{TKey,TValue}" /> representing a <see cref="ObservableDictionaryChangeType.ItemRemoved" />
 /// for the given <paramref name="key" /> and <paramref name="value" />.
 /// </summary>
 /// <param name="dictionary">The dictionary that this change is for and notifying about.</param>
 /// <param name="key">The key.</param>
 /// <param name="value">The value.</param>
 /// <returns></returns>
 /// <value>
 /// An <see cref="IObservableDictionaryChange{TKey,TValue}">instance</see> representing a <see cref="ObservableDictionaryChangeType.ItemRemoved" />.
 /// </value>
 public static IObservableDictionaryChange <TKey, TValue> ItemRemoved(IObservableDictionary <TKey, TValue> dictionary, TKey key, TValue value)
 => new ObservableDictionaryChange <TKey, TValue>(dictionary, ObservableDictionaryChangeType.ItemRemoved, key, value);
 /// <summary>
 /// Gets a <see cref="IObservableDictionaryChange{TKey,TValue}" /> representing a <see cref="ObservableDictionaryChangeType.Reset" />.
 /// </summary>
 /// <param name="dictionary">The dictionary that this change is for and notifying about.</param>
 /// <returns></returns>
 /// <value>
 /// An <see cref="IObservableDictionaryChange{TKey,TValue}">instance</see> representing a <see cref="ObservableDictionaryChangeType.Reset" />.
 /// </value>
 public static IObservableDictionaryChange <TKey, TValue> Reset(IObservableDictionary <TKey, TValue> dictionary)
 => new ObservableDictionaryChange <TKey, TValue>(dictionary, ObservableDictionaryChangeType.Reset);