Ejemplo n.º 1
0
        /// <summary>
        /// Update a shadow given current and next values.
        /// </summary>
        /// <typeparam name="TItem"></typeparam>
        /// <param name="context"></param>
        /// <param name="current"></param>
        /// <param name="next"></param>
        /// <param name="shadow"></param>
        /// <param name="added"></param>
        /// <param name="removed"></param>
        public static void Update <TItem>(IShadowContext context, IPrimitive current,
                                          IPrimitive next,
                                          IShadow <TItem> shadow,
                                          Action <IShadow <TItem> > added   = default,
                                          Action <IShadow <TItem> > removed = default)
        {
            // current == null and next != null implies -> new
            // current != null && next != null implies -> update/change
            // current != null && next == null, implies -> remove

            try
            {
                // now, we do have a problem in that the shadow may not exist...//
                // if so then its a create
                if (shadow != null)
                {
                    // the shadow could well change...
                    // in this case, we need to ...

                    var sameElementType = current.AreSameType(next);

                    if (!sameElementType)
                    {
                        if (current != null)
                        {
                            // this isn't right, since the layout hasn't been retained
                            context.MapperFactory.Remove(context, shadow, current);
                            // we could want to 'drop' the content view here, how would we do that ?
                            removed?.Invoke(shadow);
                            shadow = default;
                        }
                    }
                    else
                    {
                        context.MapperFactory.Update(context, shadow, current, next);
                    }
                }

                if (shadow == null)
                {
                    var baseShadow = context.MapperFactory.Create(context, next);

                    shadow = (IShadow <TItem>)baseShadow;

                    added?.Invoke(shadow);
                }
            }
            catch (Exception exception)
            {
                Logging.Instance.LogError(exception, "Shadow:Update Current:{current} Next:{next} ShadowType:{shadowType}", current, next, shadow?.GetType().FriendlyName());
                throw;
            }
        }