Przeglądaj źródła

Implemented WpfDigitalFormDocumentHandler

frogsoftware 1 rok temu
rodzic
commit
0e7dba5113

+ 22 - 7
InABox.Core/DigitalForms/DigitalFormDocumentFactory.cs

@@ -14,6 +14,7 @@ namespace PRS.Mobile
         void LoadDocument(Guid id, Action<byte[]> callback);
         Guid SaveDocument(byte[] data);
         void Run(Action<bool> status);
+        void Stop();
     }
 
     public abstract class DigitalFormDocumentHandler : IDigitalFormDocumentHandler
@@ -53,14 +54,27 @@ namespace PRS.Mobile
             File.WriteAllBytes(filename,data);
             return result;
         }
+
+        private CancellationTokenSource? _cancel;
+
+        public void Stop()
+        {
+            if (_cancel != null)
+            {
+                _cancel.Cancel();
+                _cancel = null;
+            }
+        }
         
         public void Run(Action<bool> status)
         {
+            Stop();
+            _cancel = new CancellationTokenSource();
             Task.Run(
                 () =>
                 {
                     bool? previouslyActive = null;
-                    while (true)
+                    while (true && !_cancel.IsCancellationRequested)
                     {
                         var file = Directory.EnumerateFiles(CachePath, "*.formdocument")
                             .FirstOrDefault();
@@ -90,7 +104,8 @@ namespace PRS.Mobile
 
                         Thread.Sleep(1000);
                     }
-                }
+                },
+                _cancel.Token
             );
         }
     }
@@ -98,17 +113,17 @@ namespace PRS.Mobile
     public static class DigitalFormDocumentFactory
     {
 
-        private static IDigitalFormDocumentHandler _handler;
+        private static IDigitalFormDocumentHandler? _handler;
 
-        public static void Run<THandler>(Action<bool> status) where THandler : IDigitalFormDocumentHandler, new()
+        public static void Run<THandler>(THandler handler, Action<bool> status) where THandler : IDigitalFormDocumentHandler
         {
-            _handler = new THandler();
+            _handler = handler;
             _handler.Run(status);
         }
         
-        public static void LoadDocument(Guid id, Action<byte[]> callback) => _handler.LoadDocument(id, callback);
+        public static void LoadDocument(Guid id, Action<byte[]> callback) => _handler?.LoadDocument(id, callback);
 
-        public static Guid SaveDocument(byte[] data) => _handler.SaveDocument(data);
+        public static Guid SaveDocument(byte[] data) => _handler?.SaveDocument(data) ?? Guid.Empty;
         
     }
 }

+ 19 - 0
inabox.wpf/DigitalForms/WpfDigitalFormDocumentHandler.cs

@@ -0,0 +1,19 @@
+using System;
+using InABox.Clients;
+using InABox.Core;
+using PRS.Mobile;
+
+namespace InABox.DynamicGrid;
+
+public class WpfDigitalFormDocumentHandler : DigitalFormDocumentHandler
+{
+    protected override string CachePath => CoreUtils.GetPath();
+
+    private readonly Func<bool> _connected;
+    protected override bool IsConnected => _connected?.Invoke() ?? true;
+
+    public WpfDigitalFormDocumentHandler(Func<bool> connected)
+    {
+        _connected = connected;
+    }
+}