Esempio n. 1
0
        /// <summary>
        /// Merges a list.
        /// </summary>
        /// <typeparam name="TKey">Type of key.</typeparam>
        /// <typeparam name="TSource">State type.</typeparam>
        /// <typeparam name="TTarget">Target type.</typeparam>
        /// <param name="source">Redux state type.</param>
        /// <param name="target">Target type linked to <paramref name="source"/>.</param>
        /// <param name="creator">Creator function that creates new instances of target types.</param>
        /// <returns>Merge result.</returns>
        public static MergeResult MergeList <TKey, TSource, TTarget>(IEnumerable <TSource> source, IList <TTarget> target, Func <TSource, TTarget> creator)
            where TSource : IKeyedItem <TKey>
            where TTarget : IBoundViewModel <TSource>
        {
            MergeResult    result  = new MergeResult();
            List <TSource> current = new List <TSource>(source);

            for (int i = target.Count - 1; i >= 0; i--)
            {
                TKey targetKey  = target[i].State.Key;
                var  findResult = Find(targetKey, source);
                if (!findResult.Found)
                {
                    target.RemoveAt(i);
                    result.Removed++;
                }
                else
                {
                    current.Remove(findResult.Match);
                    if (!ReferenceEquals(findResult.Match, target[i].State))
                    {
                        target[i].Update(findResult.Match);
                        result.Updated++;
                    }
                }
            }
            foreach (TSource item in current)
            {
                target.Add(creator(item));
                result.Added++;
            }
            return(result);
        }
Esempio n. 2
0
        /// <summary>
        /// Merges a dictionary.
        /// </summary>
        /// <typeparam name="TKey">Type of key.</typeparam>
        /// <typeparam name="TValue">Type of dictionary value.</typeparam>
        /// <typeparam name="TTargetValue">Type of target value.</typeparam>
        /// <param name="source">Redux state type.</param>
        /// <param name="target">Target type linked to <paramref name="source"/>.</param>
        /// <param name="creator">Creator function that creates new instances of target types.</param>
        /// <returns>Merge result.</returns>
        public static MergeResult MergeDictionary <TKey, TValue, TTargetValue>(
            IDictionary <TKey, TValue> source,
            IList <KeyValuePair <TKey, TTargetValue> > target,
            Func <TValue, TTargetValue> creator)
            where TTargetValue : IBoundViewModel <TValue>
        {
            MergeResult result  = new MergeResult();
            var         current = new Dictionary <TKey, TValue>(source);

            for (int i = target.Count - 1; i >= 0; i--)
            {
                TKey targetKey = target[i].Key;
                if (!source.TryGetValue(targetKey, out var match))
                {
                    target.RemoveAt(i);
                    result.Removed++;
                }
                else
                {
                    current.Remove(targetKey);
                    if (!ReferenceEquals(match, target[i].Value.State))
                    {
                        target[i].Value.Update(match);
                        result.Updated++;
                    }
                }
            }
            foreach (var pair in current)
            {
                var value = creator(pair.Value);
                target.Add(new KeyValuePair <TKey, TTargetValue>(pair.Key, value));
                result.Added++;
            }
            return(result);
        }