Kaynağa Gözat

wpf: Fixed problems with DFVideoControl

Kenric Nugteren 2 ay önce
ebeveyn
işleme
3a3953e89f

+ 5 - 1
InABox.Core/Client/Client.cs

@@ -206,7 +206,11 @@ namespace InABox.Clients
         public static Task<CoreTable> QueryAsync<TEntity>(Filter<TEntity>? filter = null, Columns<TEntity>? columns = null, SortOrder<TEntity>? orderby = null, CoreRange? range = null)
             where TEntity : Entity, IRemotable, new()
         {
-            return Task.Run(() => new Client<TEntity>().Query(filter, columns, orderby, range));
+            return Task.Run(() =>
+            {
+                var data = new Client<TEntity>().Query(filter, columns, orderby, range);
+                return data;
+            });
         }
 
         public static void Query<TEntity>(Filter<TEntity>? filter, Columns<TEntity>? columns, SortOrder<TEntity>? orderby, CoreRange? range, Action<CoreTable?, Exception?> callback)

+ 21 - 23
InABox.Core/DigitalForms/DigitalFormDocumentFactory.cs

@@ -10,7 +10,7 @@ namespace InABox.Core
 
     public interface IDigitalFormDocumentHandler
     {
-        void LoadDocument(Guid id, Action<byte[]> callback);
+        Task<byte[]> LoadDocumentAsync(Guid id);
         byte[] LoadDocument(Guid id);
         Guid SaveDocument(byte[] data);
         void Run();
@@ -34,33 +34,27 @@ namespace InABox.Core
             if (File.Exists(fullpath))
                 return File.ReadAllBytes(fullpath);
 
-                var result = new Client<Document>().Query(
-                    new Filter<Document>(x => x.FileName).IsEqualTo(FileName(id)),
-                    null,
-                    null
-                ).Rows.FirstOrDefault()?.Get<Document,byte[]>(c=>c.Data);
-                return result;
+            var result = new Client<Document>().Query(
+                new Filter<Document>(x => x.FileName).IsEqualTo(FileName(id)),
+                null,
+                null
+            ).Rows.FirstOrDefault()?.Get<Document,byte[]>(c=>c.Data);
+            return result;
         }
         
-        public void LoadDocument(Guid id, Action<byte[]> callback)
+        public async Task<byte[]> LoadDocumentAsync(Guid id)
         {
             var fullpath = Path.Combine(CachePath, FileName(id));
             if (File.Exists(fullpath))
-                callback(File.ReadAllBytes(fullpath));
+                return File.ReadAllBytes(fullpath);
             else
             {
-                new Client<Document>().Query(
+                var data = await Client.QueryAsync(
                     new Filter<Document>(x => x.FileName).IsEqualTo(FileName(id)),
+                    Columns.None<Document>().Add(x => x.Data),
                     null,
-                    null,
-                    CoreRange.All,
-                    (o, e) =>
-                    {
-                        var row = o?.Rows.FirstOrDefault();
-                        if (row != null)
-                            callback(row.Get<Document, byte[]>(c => c.Data));
-                    }
-                );
+                    CoreRange.All);
+                return data.Rows.FirstOrDefault().Get<Document, byte[]>(x => x.Data);
             }
         }
 
@@ -143,12 +137,16 @@ namespace InABox.Core
             _handler = handler;
         }
 
+        public static IDigitalFormDocumentHandler Handler => _handler ?? throw new Exception("Digital form document handler uninitialised.");
+
         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);
-        public static byte[]? LoadDocument(Guid id) => _handler?.LoadDocument(id);
-        public static Guid SaveDocument(byte[] data) => _handler?.SaveDocument(data) ?? Guid.Empty;
+
+        public static void LoadDocument(Guid id, Action<byte[]> callback) =>
+            LoadDocumentAsync(id).ContinueWith(task => callback(task.Result));
+        public static Task<byte[]> LoadDocumentAsync(Guid id) => Handler.LoadDocumentAsync(id);
+        public static byte[]? LoadDocument(Guid id) => Handler.LoadDocument(id);
+        public static Guid SaveDocument(byte[] data) => Handler.SaveDocument(data);
         
     }
 }

+ 18 - 23
inabox.wpf/DigitalForms/Designer/Controls/Fields/DFVideoControl.cs

@@ -8,6 +8,9 @@ using System.Windows;
 using System.Windows.Controls;
 using System.Windows.Media;
 using Microsoft.Xaml.Behaviors.Core;
+using System.IO;
+using System.Threading.Tasks;
+using InABox.Clients;
 
 namespace InABox.DynamicGrid
 {
@@ -28,8 +31,6 @@ namespace InABox.DynamicGrid
         bool firstPlay = true;*/
         private string _tempfilename = null!;
 
-        byte[]? Data;
-
         protected override FrameworkElement Create()
         {
             _tempfilename = System.IO.Path.ChangeExtension(System.IO.Path.GetTempFileName(),".mp4");
@@ -160,28 +161,20 @@ namespace InABox.DynamicGrid
             _player.Pause();
         }
 
-        private void PlayButton_Click()
-        {  
-
-            if (System.IO.File.Exists(_tempfilename))
+        private async Task LoadVideo()
+        {
+            if (!File.Exists(_tempfilename))
             {
-                _player.Play();
-            }
-            else
-            {
-                DigitalFormDocumentFactory.LoadDocument(_value.ID, (data) =>
-                {
-                    Dispatcher.BeginInvoke(() =>
-                    {
-                        System.IO.File.WriteAllBytes(_tempfilename, data);
-
-                        _player.Position = new TimeSpan(0);
-                        _player.Play();
-                    });
-                }); 
+                _value.Data ??= await DigitalFormDocumentFactory.LoadDocumentAsync(_value.ID);
+                File.WriteAllBytes(_tempfilename, _value.Data);
             }
         }
 
+        private void PlayButton_Click()
+        {
+            LoadVideo().ContinueWith(task => _player.Play(), TaskScheduler.FromCurrentSynchronizationContext());
+        }
+
         public override DFLayoutEmbeddedMediaValue GetSerializedValue()
         {
             if ((_value.Data?.Any() == true) && (_value.ID == Guid.Empty))
@@ -197,14 +190,16 @@ namespace InABox.DynamicGrid
 
         public override byte[] GetValue()
         {
-            return Data ?? Array.Empty<byte>();
+            Task.Run(async () => await LoadVideo().ConfigureAwait(false))
+                .Wait();
+            return _value.Data ?? Array.Empty<byte>();
         }
 
         public override void SetValue(byte[]? value)
         {
-            Data = value;
+            SetSerializedValue(new() { Data = value });
         }
 
-        protected override bool IsEmpty() => Data is null || Data.Length == 0;
+        protected override bool IsEmpty() => _value.Data is null || _value.Data.Length == 0;
     }
 }