Explorar el Código

Remembering old OriginalValues when restoring snapshot of BaseObjec.t

Kenric Nugteren hace 1 año
padre
commit
901cbbfd70
Se han modificado 1 ficheros con 33 adiciones y 20 borrados
  1. 33 20
      InABox.Core/BaseObject.cs

+ 33 - 20
InABox.Core/BaseObject.cs

@@ -154,23 +154,6 @@ namespace InABox.Core
             return false;
         }
 
-        private bool HasChanged(object? before, object? after)
-        {
-            if ((before == null || before.Equals("")) && (after == null || after.Equals("")))
-                return false;
-
-            if (before == null != (after == null))
-                return true;
-
-            if (!before!.GetType().Equals(after!.GetType()))
-                return true;
-
-            if (before is string[] && after is string[])
-                return !(before as string[]).SequenceEqual(after as string[]);
-
-            return !before.Equals(after);
-        }
-
         public void OnPropertyChanged(string name, object? before, object? after)
         {
             if (!IsObserving())
@@ -187,7 +170,7 @@ namespace InABox.Core
 
             LoadedColumns.Add(name);
 
-            if (!HasChanged(before, after))
+            if (!BaseObjectExtensions.HasChanged(before, after))
                 return;
 
             CheckOriginalValues();
@@ -372,9 +355,9 @@ namespace InABox.Core
     public class BaseObjectSnapshot<T>
         where T : BaseObject
     {
-        private List<(IProperty, object?)> Values;
+        private readonly List<(IProperty, object?)> Values;
 
-        private T Object;
+        private readonly T Object;
 
         public BaseObjectSnapshot(T obj)
         {
@@ -393,13 +376,43 @@ namespace InABox.Core
             var bObs = Object.IsObserving();
             Object.SetObserving(false);
             foreach(var (prop, value) in Values)
+            {
+                var oldValue = prop.Getter()(Object);
+
                 prop.Setter()(Object, value);
+                if(BaseObjectExtensions.HasChanged(oldValue, value))
+                {
+                    var parent = prop.Parent != null ? prop.Parent.Getter()(Object) as BaseObject : Object;
+                    if(parent != null)
+                    {
+                        var localPropName = prop is StandardProperty stdProp ? stdProp.Property.Name : prop.Name;
+                        parent.OriginalValues[localPropName] = oldValue;
+                    }
+                }
+            }
             Object.SetObserving(bObs);
         }
     }
 
     public static class BaseObjectExtensions
     {
+        public static bool HasChanged(object? before, object? after)
+        {
+            if ((before == null || before.Equals("")) && (after == null || after.Equals("")))
+                return false;
+
+            if (before == null != (after == null))
+                return true;
+
+            if (!before!.GetType().Equals(after!.GetType()))
+                return true;
+
+            if (before is string[] && after is string[])
+                return !(before as string[]).SequenceEqual(after as string[]);
+
+            return !before.Equals(after);
+        }
+
         public static bool HasColumn<T>(this T sender, string column)
             where T : BaseObject
         {