Quellcode durchsuchen

avalonia: Fixed problem with digital forms being reset when looking at in image and then back again

Kenric Nugteren vor 2 Monaten
Ursprung
Commit
fa73142e1c

+ 1 - 7
PRS.Avalonia/PRS.Avalonia/Components/FormsEditor/DigitalFormsHostView.axaml

@@ -11,13 +11,7 @@
                 Classes.HideHeader="{Binding !HasDocuments}"
                 SelectedIndex="{Binding SelectedTab}">
         <TabItem Header="Form">
-            <ScrollViewer>
-                <forms:DigitalFormViewer Name="Viewer"
-                                         Entity="{Binding Parent}"
-                                         Form="{Binding Form}"
-                                         FieldChanged="FormViewer_FieldChanged"
-                                         ReadOnly="{Binding ReadOnly}"/>
-            </ScrollViewer>
+            <ScrollViewer Content="{Binding Viewer}"/>
         </TabItem>
         <TabItem Header="Documents">
             <prs:DocumentList Repository="{Binding Documents}"/>

+ 0 - 20
PRS.Avalonia/PRS.Avalonia/Components/FormsEditor/DigitalFormsHostView.axaml.cs

@@ -23,26 +23,6 @@ public partial class DigitalFormsHostView : UserControl
         var model = DataContext as IDigitalFormsHostViewModel;
         if (model is null) return;
 
-        model.WhenAnyValue(x => x.Layout).Subscribe(value =>
-        {
-            if(value is not null)
-            {
-                Viewer.Layout = value;
-            }
-        });
-        model.SaveValues = Viewer.SaveValues;
-        model.LoadValues = Viewer.LoadValues;
-        model.Validate = () =>
-        {
-            if (Viewer.Validate(out var errors))
-            {
-                return null;
-            }
-            else
-            {
-                return errors;
-            }
-        };
     }
 
     private void FormViewer_FieldChanged(string fieldName)

+ 37 - 2
PRS.Avalonia/PRS.Avalonia/Components/FormsEditor/DigitalFormsHostViewModel.cs

@@ -11,6 +11,9 @@ using System.ComponentModel;
 using System.Linq;
 using System.Threading.Tasks;
 using InABox.Avalonia.Dialogs;
+using Avalonia;
+using DynamicData.Binding;
+using ReactiveUI;
 
 namespace PRS.Avalonia.DigitalForms;
 
@@ -50,6 +53,8 @@ public interface IDigitalFormsHostViewModel : INotifyPropertyChanged
 
     bool HasDocuments { get; }
 
+    DigitalFormViewer Viewer { get; }
+
     DigitalFormDocumentModel Documents { get; }
 
     Action<DFLoadStorage> LoadValues { set; }
@@ -136,6 +141,11 @@ public partial class DigitalFormsHostViewModel<TModel, TShell, TParent, TParentL
 
     private bool _isChanged = false;
 
+    private bool _loaded = false;
+
+    [ObservableProperty]
+    private DigitalFormViewer _viewer;
+
     public DigitalFormsHostViewModel()
     {
         PrimaryMenu.Add(new(Images.save, SaveForm));
@@ -189,6 +199,8 @@ public partial class DigitalFormsHostViewModel<TModel, TShell, TParent, TParentL
 
     protected override async Task<TimeSpan> OnRefresh()
     {
+        if (_loaded) return TimeSpan.Zero;
+
         await Task.WhenAll(
             Model.RefreshAsync(DataAccess.Status == ConnectionStatus.Connected),
             Documents.RefreshAsync(DataAccess.Status == ConnectionStatus.Connected));
@@ -259,18 +271,41 @@ public partial class DigitalFormsHostViewModel<TModel, TShell, TParent, TParentL
         layout.LoadLayout(DigitalFormLayout.Layout);
         layout.LoadVariables(Variables);
 
-
         Dispatcher.UIThread.Invoke(() =>
         {
-            Layout = layout;
+            Viewer = new DigitalFormViewer
+            {
+                ReadOnly = ReadOnly
+            };
+            Viewer.Bind(DigitalFormViewer.EntityProperty, this.WhenChanged(x => x.Parent, (x, v) => v));
+            Viewer.Bind(DigitalFormViewer.FormProperty, this.WhenChanged(x => x.Form, (x, v) => v));
+            Viewer.FieldChanged += FieldChanged;
+            Viewer.Layout = layout;
+            SaveValues = Viewer.SaveValues;
+            LoadValues = Viewer.LoadValues;
+            Validate = () =>
+            {
+                if (Viewer.Validate(out var errors))
+                {
+                    return null;
+                }
+                else
+                {
+                    return errors;
+                }
+            };
+        
+
             LoadValues(DigitalForm.DeserializeFormData(Form) ?? new());
         });
 
         _isChanged = NewForm;
+        _loaded = true;
 
         TimeStarted = DateTime.Now;
 
         ProgressVisible = false;
+
         return TimeSpan.Zero;
     }
 

+ 1 - 1
PRS.Avalonia/PRS.Avalonia/Components/FormsEditor/Fields/DFEmbeddedMediaFieldControl.cs

@@ -188,7 +188,7 @@ abstract partial class DFEmbeddedMediaFieldControl<TField, TProperties, TValue>
     public override void SetSerializedValue(DFLayoutEmbeddedMediaValue value)
     {
         _value = value;
-        _value.Thumbnail = (_value.Data is not null && _value.Data.Length > 0) ? CreateThumbnail(_value.Data) : null;
+        _value.Thumbnail = (_value.Data is not null && _value.Data.Length > 0) ? CreateThumbnail(_value.Data) : _value.Thumbnail;
 
         UpdateUI();
     }