Browse Source

Refactoring Digital Forms Stuff

Frank van den Bos 2 years ago
parent
commit
77c7921a56

+ 174 - 0
prs.mobile.new/PRS.Mobile/Components/DigitalForms/Editor/Views/DigitalFormEmbeddedMedia.cs

@@ -0,0 +1,174 @@
+using System.Linq;
+using InABox.Core;
+using InABox.Mobile;
+using Xamarin.Forms;
+
+namespace PRS.Mobile
+{
+    public abstract class DigitalFormEmbeddedMedia<T, TProperties> : MobileCard, IDigitalFormField<T,TProperties, byte[]> 
+        where T : DFLayoutField<TProperties>, new()
+        where TProperties : DFLayoutFieldProperties<byte[]>, new()
+    {
+
+        protected abstract bool IsVideo { get; }
+        
+        private readonly MobileButton _cameraButton;
+        private readonly Label _cameraLabel;
+        private readonly MobileButton _libraryButton;
+        private readonly Label _libraryLabel;
+
+        private T _definition;
+        public T Definition
+        {
+            get => _definition;
+            set
+            {
+                _definition = value;
+                Initialize(value ?? new T());
+            }
+        }
+        
+        private DigitalFormEmbeddedMediaValue _value;
+        public byte[] Value
+        {
+            get => _value.Data;
+            set => _value.Data = value;
+        }
+
+        public bool IsEmpty => Value?.Any() != true;
+
+        private bool _readOnly;
+        
+        public bool ReadOnly
+        {
+            get => _readOnly;
+            set
+            {
+                _readOnly = value;
+                UpdateStatus();
+            }
+        }
+        
+        public void Load(string value)
+        {
+            _value = new DigitalFormEmbeddedMediaValue(value);
+        }
+
+        public string Save()
+        {
+            return _value?.ToString() ?? "";
+        }
+
+        public event DigitalFormViewChangedHandler ValueChanged;
+
+        protected DigitalFormEmbeddedMedia()
+        {
+            Padding = new Thickness(10);
+            var grid = new Grid()
+            {
+                ColumnSpacing = 0,
+                RowSpacing = 0
+            };
+            grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
+            grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
+            grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto) });
+            grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto) });
+
+            _cameraButton = new MobileButton
+            {
+                Image = "camera",
+                ImageSize = new Size(50, 50),
+                Margin = 5,
+                BackgroundColor = Color.FromHex("#15C7C1"),
+                HeightRequest = 70,
+                WidthRequest = 70,
+                HorizontalOptions = LayoutOptions.Center,
+                VerticalOptions = LayoutOptions.Center,
+                Padding = 2
+            };
+            Grid.SetColumn(_cameraButton, 0);
+            Grid.SetRow(_cameraButton, 0);
+            _cameraButton.Clicked += CameraButton_Clicked;
+            grid.Children.Add(_cameraButton);
+
+            _cameraLabel = new Label
+            {
+                HorizontalOptions = LayoutOptions.Center,
+                HorizontalTextAlignment = TextAlignment.Center,
+                FontAttributes = FontAttributes.Bold,
+                TextColor = Color.Black
+            };
+            _cameraLabel.FontSize = Device.GetNamedSize(NamedSize.Small, _cameraLabel);
+            Grid.SetRow(_cameraLabel, 1);
+            Grid.SetColumn(_cameraLabel, 0);
+            grid.Children.Add(_cameraLabel);
+
+            _libraryButton = new MobileButton
+            {
+                Image = "gallery",
+                Margin = 5,
+                Padding = 2,
+                BackgroundColor = Color.FromHex("#15C7C1"),
+                HeightRequest = 70,
+                WidthRequest = 70,
+                HorizontalOptions = LayoutOptions.Center,
+                VerticalOptions = LayoutOptions.Center
+            };
+            Grid.SetRow(_libraryButton, 0);
+            Grid.SetColumn(_libraryButton, 1);
+            _libraryButton.Clicked += LibraryButton_Clicked;
+            grid.Children.Add(_libraryButton);
+
+            _libraryLabel = new()
+            {
+                Text = "Library",
+                HorizontalOptions = LayoutOptions.Center,
+                HorizontalTextAlignment = TextAlignment.Center,
+                FontAttributes = FontAttributes.Bold
+            };
+            _libraryLabel.FontSize = Device.GetNamedSize(NamedSize.Small, _libraryLabel);
+            Grid.SetRow(_libraryLabel, 1);
+            Grid.SetColumn(_libraryLabel, 1);
+            grid.Children.Add(_libraryLabel);
+            
+            Content = grid;
+        }
+
+        private void CameraButton_Clicked(object sender, MobileButtonClickEventArgs args)
+        {
+            // reset _id here
+        }
+
+        private void LibraryButton_Clicked(object sender, MobileButtonClickEventArgs args)
+        {
+            // reset _id here
+        }
+
+        private void Initialize(T definition)
+        {
+            UpdateStatus();
+        }
+
+        protected abstract bool DisableLibrary { get; }
+        protected abstract bool Secure { get; }
+        protected abstract bool Required { get; }
+        
+        private void UpdateStatus()
+        {
+            _cameraLabel.Text = IsVideo ? "Take Video" : "Camera";
+            _libraryButton.IsEnabled = !DisableLibrary;
+            
+            bool enabled = !_readOnly && !Secure;
+            _cameraButton.IsEnabled = enabled;
+            _libraryButton.IsEnabled = enabled && !DisableLibrary;
+            _libraryLabel.TextColor = enabled && !DisableLibrary
+                ? Color.Black
+                : Color.Gray;
+            
+            var colors = DigitalFormUtils.GetColors(!enabled, Required, false);
+            BackgroundColor = colors.Background;
+            BorderColor = colors.Border;
+        }
+        
+    }
+}

+ 40 - 0
prs.mobile.new/PRS.Mobile/Components/DigitalForms/Editor/Views/DigitalFormEmbeddedMediaValue.cs

@@ -0,0 +1,40 @@
+using System;
+using System.Linq;
+using InABox.Core;
+
+namespace PRS.Mobile
+{
+    public class DigitalFormEmbeddedMediaValue
+    {
+        public Guid ID { get; set; }
+        public byte[] Data { get; set; }
+
+        public DigitalFormEmbeddedMediaValue(String json = "")
+        {
+            if (String.IsNullOrWhiteSpace(json))
+                return;
+            if (Guid.TryParse(json, out Guid id) && (id != Guid.Empty))
+            {
+                DigitalFormDocumentHandler.LoadDocument(id, 
+                    data =>
+                    {
+                        ID = id;
+                        Data = data;
+                    }
+                );
+            }
+            else if (json.IsBase64String())
+            {
+                ID = Guid.Empty;
+                Data = Convert.FromBase64String(json);
+            }
+        }
+
+        public override string ToString()
+        {
+            if ((ID == Guid.Empty) && (Data.Any() == true))
+                ID = DigitalFormDocumentHandler.SaveDocument(Data);
+            return ID.ToString();
+        }
+    }
+}

+ 54 - 0
prs.mobile.new/PRS.Mobile/Components/DigitalForms/Editor/Views/DigitalFormEmbeddedMediaValues.cs

@@ -0,0 +1,54 @@
+using System.Collections;
+using System.Collections.Generic;
+using System.ComponentModel;
+using System.Runtime.CompilerServices;
+
+namespace PRS.Mobile
+{
+    public class DigitalFormEmbeddedMediaValues : IEnumerable<DigitalFormEmbeddedMediaValue>, INotifyPropertyChanged
+    {
+        private readonly List<DigitalFormEmbeddedMediaValue> _values = new();
+
+        public DigitalFormEmbeddedMediaValue[] Values => _values.ToArray();
+        
+        public IEnumerator<DigitalFormEmbeddedMediaValue> GetEnumerator() => _values.GetEnumerator();
+
+        IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
+
+        public void Add(DigitalFormEmbeddedMediaValue value)
+        {
+            _values.Add(value);
+            OnPropertyChanged(nameof(Values));
+        }
+
+        public void Remove(DigitalFormEmbeddedMediaValue 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 = null)
+        {
+            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
+        }
+
+        protected bool SetField<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
+        {
+            if (EqualityComparer<T>.Default.Equals(field, value)) return false;
+            field = value;
+            OnPropertyChanged(propertyName);
+            return true;
+        }
+    }
+}

+ 12 - 0
prs.mobile.new/PRS.Mobile/Components/DigitalForms/Editor/Views/DigitalFormEmbeddedVideo.cs

@@ -0,0 +1,12 @@
+using InABox.Core;
+
+namespace PRS.Mobile
+{
+    public class DigitalFormEmbeddedVideo : DigitalFormEmbeddedMedia<DFLayoutVideoField, DFLayoutVideoFieldProperties>
+    {
+        protected override bool IsVideo => true;
+        protected override bool DisableLibrary => Definition.Properties.DisableLibrary;
+        protected override bool Secure => Definition.Properties.Secure;
+        protected override bool Required => Definition.Properties.Required;
+    }
+}

+ 81 - 0
prs.mobile.new/PRS.Mobile/Components/DigitalForms/Editor/Views/DigitalFormLookupButton.cs

@@ -0,0 +1,81 @@
+using System;
+using InABox.Core;
+using InABox.Mobile;
+
+namespace PRS.Mobile
+{
+    public class DigitalFormLookupButton : MobileButton, IDigitalFormField<DFLayoutLookupField, string>
+    {
+        
+        private DFLayoutLookupField _definition;
+        public DFLayoutLookupField Definition
+        {
+            get => _definition;
+            set
+            {
+                _definition = value;
+                Initialize(value ??  new DFLayoutLookupField());
+            } 
+        }
+
+        private String _value;
+        public String Value
+        {
+            get => _value;
+            set => _value = value;
+        }
+
+        private bool _readOnly;
+        public bool ReadOnly
+        {
+            get => _readOnly;
+            set
+            {
+                _readOnly = value;
+                UpdateStatus();
+            }
+        }
+
+        public event DigitalFormViewChangedHandler<DFLayoutLookupField, string> ValueChanged;
+
+        public DigitalFormLookupButton()
+        {
+            MinimumHeightRequest = 45;
+            Text = "Select Value";
+            Clicked += DoLookup;
+        }
+
+        private void DoLookup(object sender, MobileButtonClickEventArgs args)
+        {
+            var entityType = CoreUtils.GetEntity(Definition.Properties.LookupType);
+            var modeltype = typeof(SelectionViewModel<>).MakeGenericType(entityType);
+            var model = (ISelectionViewModel)Activator.CreateInstance(modeltype);
+            foreach (var additionalcolumn in Definition.Properties.AdditionalPropertiesList)
+                model.VisibleColumns.Add(additionalcolumn);
+            GenericSelectionPage _selector = new GenericSelectionPage("Select Value", model);
+            _selector.OnItemSelected += (o,e) =>
+            {
+                Value = e.Row.Get<String>("ID");
+                Text = e.Formatted;
+                ValueChanged?.Invoke(_definition, new DigitalFormViewChangedArgs<String>(Value));
+            };
+            Navigation.PushAsync(_selector);
+        }
+
+        private void Initialize(DFLayoutLookupField definition)
+        {
+            UpdateStatus();
+        }
+
+        private void UpdateStatus()
+        {
+            IsEnabled = !_readOnly || Definition.Properties.Secure;
+            
+            var labelcolors = DigitalFormUtils.GetColors(!IsEnabled, Definition.Properties.Required, true);
+            BackgroundColor = labelcolors.Background;
+            BorderColor = labelcolors.Border;
+            TextColor = labelcolors.Foreground;
+        }
+    
+    }
+}

+ 34 - 0
prs.mobile.new/PRS.Mobile/Components/DigitalForms/Editor/Views/DigitalFormLookupValue.cs

@@ -0,0 +1,34 @@
+using System;
+using InABox.Core;
+
+namespace PRS.Mobile
+{
+    public class DigitalFormLookupValue
+    {
+        public Guid ID { get; set; }
+        public String Text { get; set; }
+        
+        public DigitalFormLookupValue(String data = "")
+        {
+            if (!String.IsNullOrWhiteSpace(data))
+            {
+                try
+                {
+                    var value = Serialization.Deserialize<DigitalFormLookupValue>(data, true);
+                    ID = value.ID;
+                    Text = value.Text;
+                }
+                catch (Exception e)
+                {
+                    ID = Guid.Empty;
+                    Text = data;
+                }
+            }
+        }
+
+        public override string ToString()
+        {
+            return Serialization.Serialize(this);
+        }
+    }
+}

+ 89 - 0
prs.mobile.new/PRS.Mobile/Components/DigitalForms/Editor/Views/DigitalFormLookupView.cs

@@ -0,0 +1,89 @@
+using System;
+using InABox.Core;
+using InABox.Mobile;
+using Xamarin.Forms;
+
+namespace PRS.Mobile
+{
+    public abstract class DigitalFormLookupView : ContentView,
+        IDigitalFormField<DFLayoutLookupField, DFLayoutLookupFieldProperties, Guid>
+    {
+        private DFLayoutLookupField _definition;
+        public DFLayoutLookupField Definition
+        {
+            get => _definition;
+            set
+            {
+                _definition = value;
+                Initialize(value ??  new DFLayoutLookupField());
+            } 
+        }
+
+        protected DigitalFormLookupValue _value;
+        public Guid Value
+        {
+            get => _value.ID;
+            set =>_value.ID = value;
+        }
+
+        public bool IsEmpty => Value == Guid.Empty;
+
+        protected abstract void UpdateUI();
+        
+        private bool _readOnly;
+        public bool ReadOnly
+        {
+            get => _readOnly;
+            set
+            {
+                _readOnly = value;
+                UpdateStatus();
+            }
+        }
+        
+        public void Load(string value)
+        {
+            _value = new DigitalFormLookupValue(value);
+            UpdateUI();
+        }
+
+        public string Save()
+        {
+            return _value?.ToString() ?? "";
+        }
+
+        public event DigitalFormViewChangedHandler ValueChanged;
+
+        protected DigitalFormLookupView()
+        {
+            _value = new DigitalFormLookupValue();
+        }
+        
+        protected void DoLookup(object sender, MobileButtonClickEventArgs args)
+        {
+            var entityType = CoreUtils.GetEntity(Definition.Properties.LookupType);
+            var modeltype = typeof(SelectionViewModel<>).MakeGenericType(entityType);
+            var model = (ISelectionViewModel)Activator.CreateInstance(modeltype);
+            foreach (var additionalcolumn in Definition.Properties.AdditionalPropertiesList)
+                model.VisibleColumns.Add(additionalcolumn);
+            GenericSelectionPage _selector = new GenericSelectionPage("Select Value", model);
+            _selector.OnItemSelected += (o,e) =>
+            {
+                _value.ID = e.Row.Get<Guid>("ID");
+                _value.Text = e.Formatted;
+                _value.Values = e.Row.ToDictionary();
+                UpdateUI();
+                ValueChanged?.Invoke(this, new DigitalFormViewChangedArgs(Definition,Value));
+            };
+            Navigation.PushAsync(_selector);
+        }
+
+        private void Initialize(DFLayoutLookupField definition)
+        {
+            UpdateStatus();
+        }
+
+        protected abstract void UpdateStatus();
+
+    }
+}

+ 7 - 0
prs.mobile.new/PRS.Mobile/Components/DigitalForms/Editor/Views/DigitalFormMultiImage.cs

@@ -0,0 +1,7 @@
+namespace PRS.Mobile
+{
+    public class DigitalFormMultiImage
+    {
+        
+    }
+}

+ 0 - 0
prs.mobile.new/PRS.Mobile/Images/gallery.svg → prs.mobile.new/PRS.Mobile/Images/photolibrary.svg


+ 1 - 0
prs.mobile.new/PRS.Mobile/Images/upload.svg

@@ -0,0 +1 @@
+<svg height="512" viewBox="0 0 32 32" width="512" xmlns="http://www.w3.org/2000/svg"><g id="Ikon"><path d="m29 16.2446a3.9694 3.9694 0 0 1 -4.15 3.7554h-17.61c-2.2695 0-4.8546-2.0654-3.9887-4.7939.8864-2.7933 4.6615-4.0442 6.7487-2.6084-2.0541-4.5869 2.5129-7.8258 6.4158-7.5851 3.7077.2286 8.4891 3.2855 6.5842 7.5851 2.288-1.5965 6 .1049 6 3.6469z" fill="#2196f3"/><circle cx="16" cy="21" fill="#eee" r="6"/></g><g id="Line"><path d="m17.293 20.707-.293-.2929v3.6127a1 1 0 0 1 -2 0v-3.6127l-.293.2929c-.9034.9309-2.3452-.51-1.414-1.414l2-2a.9994.9994 0 0 1 1.414 0l2 2c.9309.9034-.5101 2.3452-1.414 1.414zm9.9458-9.1435a4.8809 4.8809 0 0 0 -2.8169-.5381 5.1458 5.1458 0 0 0 -.7719-3.0733c-1.5166-2.4951-4.7813-3.79-7.1729-3.9375a8.1091 8.1091 0 0 0 -6.981 3.1866 5.5054 5.5054 0 0 0 -.9644 3.833 5.9979 5.9979 0 0 0 -6.2334 3.8691c-1.0944 3.1126 1.8711 6.1439 4.9417 6.0967h1.76c.1638 9.3326 13.8349 9.334 14 0h1.85a4.97 4.97 0 0 0 5.15-4.7559 5.1593 5.1593 0 0 0 -2.7612-4.6806zm-16.2388 9.4365c.0338-6.6326 9.9652-6.6332 10 0-.0338 6.6324-9.9652 6.6329-10 0zm13.85-2h-2.1286c-1.7738-6.6572-11.6681-6.658-13.4425 0h-2.0389c-1.752.0113-3.6925-1.6309-3.0356-3.4912.536-1.8962 3.4647-3.2388 5.2285-2.0869a1.006 1.006 0 0 0 1.4795-1.2334 3.7162 3.7162 0 0 1 .2276-3.8485 6.0816 6.0816 0 0 1 5.2144-2.3291c1.9492.12 4.4941 1.1827 5.5869 2.98a3.291 3.291 0 0 1 .144 3.2012 1.0059 1.0059 0 0 0 1.4869 1.2256 2.6429 2.6429 0 0 1 2.7417-.0811 3.1935 3.1935 0 0 1 1.6861 2.9075 2.9762 2.9762 0 0 1 -3.15 2.7559z"/></g></svg>

File diff suppressed because it is too large
+ 0 - 0
prs.mobile.new/PRS.Mobile/Images/videolibrary.svg


Some files were not shown because too many files changed in this diff