Browse Source

Refactoring Digital Forms Embedded Media and Lookup Values

Frank van den Bos 1 year ago
parent
commit
42f638c8ad

+ 1 - 1
InABox.Core/DigitalForms/Layouts/Fields/DFLayoutEmbeddedImage/DFLayoutEmbeddedImageProperties.cs

@@ -2,7 +2,7 @@
 
 namespace InABox.Core
 {
-    public class DFLayoutEmbeddedImageProperties : DFLayoutFieldProperties<byte[]>
+    public class DFLayoutEmbeddedImageProperties : DFLayoutFieldProperties<DFLayoutEmbeddedMediaValue>
     {
         [CheckBoxEditor]
         [EditorSequence(1)]

+ 47 - 0
InABox.Core/DigitalForms/Layouts/Fields/DFLayoutEmbeddedMediaValue.cs

@@ -0,0 +1,47 @@
+using System;
+
+namespace InABox.Core
+{
+    public class DFLayoutEmbeddedMediaValue
+    {
+        public Guid ID { get; set; }
+        public byte[]? Data { get; set; }
+        public byte[]? Thumbnail { get; set; }
+        
+        public void Load(string json)
+        {
+            if (String.IsNullOrWhiteSpace(json))
+                return;
+
+            try
+            {
+                var externaldata = Serialization.Deserialize<(Guid, byte[])>(json, true);
+                if (externaldata.Item1 != Guid.Empty)
+                {
+                    ID = externaldata.Item1;
+                    Thumbnail = externaldata.Item2;
+                }
+            }
+            catch
+            {
+                if (Guid.TryParse(json, out Guid id) && (id != Guid.Empty))
+                {
+                    ID = id;
+                    Thumbnail = null;
+                }
+                else
+                {
+                    ID = Guid.Empty;
+                    Data = Convert.FromBase64String(json);
+                    Thumbnail = null;
+                }
+            }
+
+        }
+
+        public override string ToString()
+        {
+            return Serialization.Serialize((ID, Thumbnail));
+        }
+    }
+}

+ 109 - 0
InABox.Core/DigitalForms/Layouts/Fields/DFLayoutEmbeddedMediaValues.cs

@@ -0,0 +1,109 @@
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using System.ComponentModel;
+using System.Linq;
+using System.Runtime.CompilerServices;
+using InABox.Core;
+
+namespace InABox.Core
+{
+    public class DFLayoutEmbeddedMediaValues : IEnumerable<DFLayoutEmbeddedMediaValue>, INotifyPropertyChanged
+    {
+        private readonly List<DFLayoutEmbeddedMediaValue> _values = new List<DFLayoutEmbeddedMediaValue>();
+        public DFLayoutEmbeddedMediaValue[] Values => _values.ToArray();
+
+        public List<byte[]> AsByteArrays
+        {
+            get => _values.Select(x => x?.Data).OfType<byte[]>().ToList();
+            set
+            {
+                _values.Clear();
+                foreach (var bytes in value)
+                    _values.Add(new DFLayoutEmbeddedMediaValue() { ID = Guid.Empty, Data = bytes});
+                OnPropertyChanged(nameof(Values));
+            }
+        }
+        
+        public DFLayoutEmbeddedMediaValues(String json = "")
+        {
+            if (String.IsNullOrWhiteSpace(json))
+                return;
+            _values.Clear();
+            List<string> values = Serialization.Deserialize<List<string>>(json) ?? new List<string>();
+            foreach (string s in values)
+            {
+                if (!String.IsNullOrWhiteSpace(s))
+                {
+                    if (Guid.TryParse(s, out Guid id) && (id != Guid.Empty))
+                    {
+                        // DigitalFormDocumentHandler.LoadDocument(id, data =>
+                        // {
+                        //     Add(new DigitalFormEmbeddedMediaValue()
+                        //     {
+                        //         ID = id,
+                        //         Data = data
+                        //     });
+                        // });
+                    }
+                    else if (s.IsBase64String())
+                    {
+                        Add(new DFLayoutEmbeddedMediaValue()
+                        {
+                            ID = Guid.Empty,
+                            Data = Convert.FromBase64String(s)
+                        });
+                    }
+                }
+            }
+        }
+
+        public override String ToString()
+        {
+            List<String> results = new List<string>();
+            foreach (var value in _values)
+                results.Add(value.ToString());
+            return Serialization.Serialize(results);
+        }
+        
+        public IEnumerator<DFLayoutEmbeddedMediaValue> GetEnumerator() => _values.GetEnumerator();
+
+        IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
+
+        public void Add(DFLayoutEmbeddedMediaValue value)
+        {
+            _values.Add(value);
+            OnPropertyChanged(nameof(Values));
+        }
+
+        public void Remove(DFLayoutEmbeddedMediaValue value)
+        {
+            if (_values.Contains(value))
+            {
+                _values.Remove(value);
+                OnPropertyChanged(nameof(Values));
+            }
+        }
+
+        public void Clear()
+        {
+            _values.Clear();
+            OnPropertyChanged(nameof(Values));
+        }
+
+        public event PropertyChangedEventHandler? PropertyChanged;
+
+        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = "")
+        {
+            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
+        }
+
+        protected bool SetField<T>(ref T field, T value, [CallerMemberName] string propertyName = "")
+        {
+            if (EqualityComparer<T>.Default.Equals(field, value)) return false;
+            field = value;
+            OnPropertyChanged(propertyName);
+            return true;
+        }
+    }
+}

+ 45 - 0
InABox.Core/DigitalForms/Layouts/Fields/DFLayoutLookupField/DFLayoutLookupValue.cs

@@ -0,0 +1,45 @@
+using System;
+using System.Collections.Generic;
+using InABox.Core;
+
+namespace PRS.Mobile
+{
+    public class DFLayoutLookupValue
+    {
+        public Guid ID { get; set; }
+        public String Text { get; set; }
+        public Dictionary<String,object> Values { get; set; }
+
+        public DFLayoutLookupValue()
+        {
+            ID = Guid.Empty;
+            Text = "";
+            Values = new Dictionary<string, object>();
+        }
+        
+        public DFLayoutLookupValue(String data = "") : this()
+        {
+            if (!String.IsNullOrWhiteSpace(data))
+            {
+                try
+                {
+                    var value = Serialization.Deserialize<DFLayoutLookupValue>(data, true);
+                    ID = value?.ID ?? Guid.Empty;
+                    Text = value?.Text ?? "";
+                    Values = value?.Values ?? new Dictionary<String, Object>();
+                }
+                catch (Exception e)
+                {
+                    ID = Guid.Empty;
+                    Text = data;
+                    Values = new Dictionary<String, Object>();
+                }
+            }
+        }
+
+        public override string ToString()
+        {
+            return Serialization.Serialize(this);
+        }
+    }
+}

+ 1 - 1
InABox.Core/DigitalForms/Layouts/Fields/DFLayoutMultiImage/DFLayoutMultiImageProperties.cs

@@ -5,7 +5,7 @@ using System.Linq;
 
 namespace InABox.Core
 {
-    public class DFLayoutMultiImageProperties : DFLayoutFieldProperties<List<byte[]>>
+    public class DFLayoutMultiImageProperties : DFLayoutFieldProperties<DFLayoutEmbeddedMediaValues>
     {
         [CheckBoxEditor]
         public bool DisableLibrary { get; set; } = false;

+ 1 - 1
InABox.Core/DigitalForms/Layouts/Fields/DFLayoutVideoField/DFLayoutVideoFieldProperties.cs

@@ -5,7 +5,7 @@ using System.Text;
 namespace InABox.Core
 {
 
-    public class DFLayoutVideoFieldProperties : DFLayoutFieldProperties<byte[]>
+    public class DFLayoutVideoFieldProperties : DFLayoutFieldProperties<DFLayoutEmbeddedMediaValue>
     {
         public enum VideoQuality
         {

+ 3 - 0
InABox.Core/InABox.Core.csproj

@@ -37,5 +37,8 @@
     <ItemGroup>
         <ProjectReference Include="..\inabox.logging.shared\InABox.Logging.Shared.csproj" />
     </ItemGroup>
+    <ItemGroup>
+      <Folder Include="DigitalForms\Layouts\Values\" />
+    </ItemGroup>
 
 </Project>