Преглед изворни кода

Refactored DigitalFormDocumentFactory

Frank van den Bos пре 1 година
родитељ
комит
1e1d1d15f6

+ 7 - 4
prs.mobile.new/PRS.Mobile/Components/DigitalForms/Editor/Views/DigitalFormEmbeddedMedia.cs

@@ -81,7 +81,7 @@ namespace PRS.Mobile
             if (_value == null)
                 return new DFLayoutEmbeddedMediaValue().ToString();
             if ((_value.Data?.Any() == true) && (_value.ID == Guid.Empty))
-                _value.ID = DigitalFormDocumentHandler.SaveDocument(_value.Data);
+                _value.ID = DigitalFormDocumentFactory.SaveDocument(_value.Data);
             return _value?.ToString() ?? "";
         }
 
@@ -172,12 +172,15 @@ namespace PRS.Mobile
             {
                 if (_value.ID != Guid.Empty)
                 {
-                    DigitalFormDocumentHandler.LoadDocument(
+                    DigitalFormDocumentFactory.LoadDocument(
                         _value.ID,
                         data =>
                         {
-                            _value.Data = data;
-                            Navigation.PushAsync(new ImageViewerPage(data));
+                            Device.BeginInvokeOnMainThread(() =>
+                            {
+                                _value.Data = data;
+                                Navigation.PushAsync(new ImageViewerPage(data)); 
+                            });
                         }
                     );
                 }

+ 3 - 2
prs.mobile.new/PRS.Mobile/Components/DigitalForms/Editor/Views/DigitalFormSignature.cs

@@ -62,7 +62,8 @@ namespace PRS.Mobile
             if (String.IsNullOrWhiteSpace(serialized))
                 return;
             if (Guid.TryParse(serialized, out _id) && (_id != Guid.Empty))
-                DigitalFormDocumentHandler.LoadDocument(_id, v => Value = v);
+                DigitalFormDocumentFactory.LoadDocument(_id,
+                    v => Device.BeginInvokeOnMainThread(() => Value = v));
             else if (serialized.IsBase64String())
                 Value = Convert.FromBase64String(serialized);
         }
@@ -70,7 +71,7 @@ namespace PRS.Mobile
         public string Serialize()
         {
             if ((_id == Guid.Empty) && Value?.Any() == true)
-                _id = DigitalFormDocumentHandler.SaveDocument(Value);
+                _id = DigitalFormDocumentFactory.SaveDocument(Value);
             return _id.ToString();
         }
 

+ 0 - 88
prs.mobile.new/PRS.Mobile/Components/DigitalForms/Editor/Views/DigitalFormUtils.cs

@@ -1,11 +1,3 @@
-using System;
-using System.IO;
-using System.Linq;
-using System.Threading;
-using System.Threading.Tasks;
-using InABox.Clients;
-using InABox.Core;
-using InABox.Mobile;
 using Xamarin.Forms;
 
 namespace PRS.Mobile
@@ -60,84 +52,4 @@ namespace PRS.Mobile
         }
         
     }
-
-    public static class DigitalFormDocumentHandler
-    {
-
-        private static String FileName(Guid id) => $"{id}.formdocument";
-        
-        private static String CacheFileName(Guid id) => CoreRepository.CacheFileName(FileName(id));
-        
-        public static void LoadDocument(Guid id, Action<byte[]> callback)
-        {
-            var filename = CacheFileName(id);
-            if (CoreRepository.IsCached(filename))
-                callback(File.ReadAllBytes(filename));
-            else
-            {
-                new Client<Document>().Query(
-                    new Filter<Document>(x => x.FileName).IsEqualTo(FileName(id)),
-                    null,
-                    null,
-                    (o, e) =>
-                    {
-                        var row = o?.Rows.FirstOrDefault();
-                        if (row != null)
-                        {
-                            Device.BeginInvokeOnMainThread(() =>
-                                callback(row.Get<Document, byte[]>(c => c.Data))
-                            );
-                        }
-                    }
-                );
-            }
-        }
-
-        public static Guid SaveDocument(byte[] data)
-        {
-            Guid result = Guid.NewGuid();
-            File.WriteAllBytes(CacheFileName(result),data);
-            return result;
-        }
-
-        public static void Run(Action<bool> status)
-        {
-            Task.Run(
-                () =>
-                {
-                    bool? previouslyActive = null;
-                    while (true)
-                    {
-                        var file = Directory.EnumerateFiles(CoreRepository.CacheFolder(), "*.formdocument")
-                            .FirstOrDefault();
-
-                        var isActive = !String.IsNullOrWhiteSpace(file);
-                        if (isActive != previouslyActive)
-                        {
-                            previouslyActive = isActive;
-                            status(isActive);
-                        }
-
-                        if (!String.IsNullOrWhiteSpace(file) && File.Exists(file) && App.Data.IsConnected())
-                        {
-                            var data = File.ReadAllBytes(file);
-                            var document = new Document()
-                            {
-                                FileName = Path.GetFileName(file),
-                                Data = data,
-                                CRC = CoreUtils.CalculateCRC(data),
-                                TimeStamp = DateTime.Now,
-                                Created = DateTime.Now,
-                                CreatedBy = App.Data.Me.Code
-                            };
-                            new Client<Document>().Save(document, "Uploaded from Mobile Device");
-                            File.Delete(file);
-                        }
-
-                        Thread.Sleep(1000);
-                    }
-                }
-            );
-        }
-    }
 }

+ 10 - 0
prs.mobile.new/PRS.Mobile/Components/DigitalForms/Editor/Views/MobileDigitalFormDocumentHandler.cs

@@ -0,0 +1,10 @@
+using InABox.Mobile;
+
+namespace PRS.Mobile
+{
+    public class MobileDigitalFormDocumentHandler : DigitalFormDocumentHandler
+    {
+        protected override string CachePath => CoreRepository.CacheFolder();
+        protected override bool IsConnected => App.Data.IsConnected();
+    }
+}

+ 1 - 1
prs.mobile.new/PRS.Mobile/Components/DigitalForms/ExistingForms/ExistingForms.xaml.cs

@@ -263,7 +263,7 @@ namespace PRS.Mobile
             var form = new Client<TForm>()
                 .Query(new Filter<TForm>(x => x.ID).IsEqualTo(shell.ID))
                 .Rows
-                .Select(x => x.ToObject<AssignmentForm>())
+                .Select(x => x.ToObject<TForm>())
                 .FirstOrDefault();
             
             var model = new DigitalFormHostModel<TParent, TParentLink, TForm>();

+ 1 - 1
prs.mobile.new/PRS.Mobile/Main/DataModel.cs

@@ -362,7 +362,7 @@ namespace PRS.Mobile
             App.Transport.OnClose += OnTransportDisconnected;
             
 
-            DigitalFormDocumentHandler.Run(
+            DigitalFormDocumentFactory.Run<MobileDigitalFormDocumentHandler>(
                 b =>
                 {
                     IsBackgroundUpdateStatusActive = b;