Ver Fonte

Improved DigitalFormDocumentHandler infrastructure

Frank van den Bos há 1 ano atrás
pai
commit
ff2d4d2825
1 ficheiros alterados com 16 adições e 6 exclusões
  1. 16 6
      InABox.Core/DigitalForms/DigitalFormDocumentFactory.cs

+ 16 - 6
InABox.Core/DigitalForms/DigitalFormDocumentFactory.cs

@@ -13,7 +13,7 @@ namespace PRS.Mobile
     {
         void LoadDocument(Guid id, Action<byte[]> callback);
         Guid SaveDocument(byte[] data);
-        void Run(Action<bool> status);
+        void Run();
         void Stop();
     }
 
@@ -26,6 +26,8 @@ namespace PRS.Mobile
         
         protected abstract bool IsConnected { get; }
         
+        private static Action<bool>? _status;
+        
         public void LoadDocument(Guid id, Action<byte[]> callback)
         {
             var fullpath = Path.Combine(CachePath, FileName(id));
@@ -66,7 +68,12 @@ namespace PRS.Mobile
             }
         }
         
-        public void Run(Action<bool> status)
+        protected DigitalFormDocumentHandler(Action<bool> status)
+        {
+            _status = status;
+        }
+        
+        public void Run()
         {
             Stop();
             _cancel = new CancellationTokenSource();
@@ -74,7 +81,7 @@ namespace PRS.Mobile
                 () =>
                 {
                     bool? previouslyActive = null;
-                    while (true && !_cancel.IsCancellationRequested)
+                    while (!_cancel.IsCancellationRequested)
                     {
                         var file = Directory.EnumerateFiles(CachePath, "*.formdocument")
                             .FirstOrDefault();
@@ -83,7 +90,7 @@ namespace PRS.Mobile
                         if (isActive != previouslyActive)
                         {
                             previouslyActive = isActive;
-                            status(isActive);
+                            _status?.Invoke(isActive);
                         }
 
                         if (!String.IsNullOrWhiteSpace(file) && File.Exists(file) && IsConnected)
@@ -114,12 +121,15 @@ namespace PRS.Mobile
     {
 
         private static IDigitalFormDocumentHandler? _handler;
+        
 
-        public static void Run<THandler>(THandler handler, Action<bool> status) where THandler : IDigitalFormDocumentHandler
+        public static void Init<THandler>(THandler handler) where THandler : IDigitalFormDocumentHandler
         {
             _handler = handler;
-            _handler.Run(status);
         }
+
+        public static void Run() => _handler?.Run();
+        public static void Stop() => _handler?.Stop();
         
         public static void LoadDocument(Guid id, Action<byte[]> callback) => _handler?.LoadDocument(id, callback);