瀏覽代碼

avalonia: Added AddForm button to SiteITPForms

Kenric Nugteren 4 月之前
父節點
當前提交
3b89f5a74d

+ 31 - 0
PRS.Avalonia/PRS.Avalonia/Components/FormsEditor/DigitalFormsHostViewModel.cs

@@ -1,4 +1,5 @@
 using CommunityToolkit.Mvvm.ComponentModel;
+using InABox.Avalonia;
 using InABox.Core;
 using PRS.Avalonia.Modules;
 using System;
@@ -32,4 +33,34 @@ public partial class DigitalFormsHostViewModel : ModuleViewModel
 
         // TODO: LoadItems; show loading screen or progress ring while loading.
     }
+
+    /// <summary>
+    /// Edit a form.
+    /// </summary>
+    /// <typeparam name="TParent"></typeparam>
+    /// <typeparam name="TParentLink"></typeparam>
+    /// <typeparam name="TForm"></typeparam>
+    /// <param name="shell"></param>
+    /// <param name="parent"></param>
+    /// <param name="model">A model to refresh on saving, or <see langword="null"/>.</param>
+    public static void EditForm<TParent, TParentLink, TForm>(IDigitalFormInstanceShell shell, TParent parent, ICoreRepository? model = null)
+        where TParent : Entity, IRemotable, IPersistent, new()
+        where TParentLink : EntityLink<TParent>, new()
+        where TForm : Entity, IRemotable, IPersistent, IDigitalFormInstance<TParentLink>, new()
+    {
+        Navigation.Navigate<DigitalFormsHostViewModel>(x =>
+        {
+            x.Configure(parent, shell.FormID, shell.ID);
+            x.OnSaved += () =>
+            {
+                model?.RefreshAsync(true).ContinueWith(task =>
+                {
+                    if(task.Exception is not null)
+                    {
+                        MobileLogging.Log(task.Exception);
+                    }
+                });
+            };
+        });
+    }
 }

+ 0 - 21
PRS.Avalonia/PRS.Avalonia/Components/FormsList/FormsList.axaml.cs

@@ -202,25 +202,4 @@ public partial class FormsList : UserControl
         await Model.RefreshAsync(true);
         return true;
     }
-
-    public void EditForm<TParent, TParentLink, TForm>(IDigitalFormInstanceShell shell, TParent parent)
-        where TParent : Entity, IRemotable, IPersistent, new()
-        where TParentLink : EntityLink<TParent>, new()
-        where TForm : Entity, IRemotable, IPersistent, IDigitalFormInstance<TParentLink>, new()
-    {
-        Navigation.Navigate<DigitalFormsHostViewModel>(x =>
-        {
-            x.Configure(parent, shell.FormID, shell.ID);
-            x.OnSaved += () =>
-            {
-                Model?.RefreshAsync(true).ContinueWith(task =>
-                {
-                    if(task.Exception is not null)
-                    {
-                        MobileLogging.Log(task.Exception);
-                    }
-                });
-            };
-        });
-    }
 }

+ 1 - 1
PRS.Avalonia/PRS.Avalonia/MainView.axaml

@@ -41,7 +41,7 @@
 				<Button
 					Grid.Row="0"
 					Grid.Column="0"
-					Margin="5,0,0,0"
+					Margin="7,7,0,7"
 					Classes="Transparent"
 					HorizontalAlignment="Center"
 					VerticalAlignment="Center"

+ 29 - 2
PRS.Avalonia/PRS.Avalonia/Modules/Site/SiteItps/SiteITPFormsViewModel.cs

@@ -1,5 +1,8 @@
-using CommunityToolkit.Mvvm.ComponentModel;
+using Comal.Classes;
+using CommunityToolkit.Mvvm.ComponentModel;
 using CommunityToolkit.Mvvm.Input;
+using InABox.Avalonia.Components;
+using PRS.Avalonia.DigitalForms;
 using System;
 using System.Collections.Generic;
 using System.Linq;
@@ -10,7 +13,7 @@ namespace PRS.Avalonia.Modules;
 
 public partial class SiteITPFormsViewModel : ModuleViewModel
 {
-    public override string Title => ITPs.FirstOrDefault(x => x.ID == ItpID)?.Description ?? "No ITP Selected";
+    public override string Title => ITP?.Description ?? "No ITP Selected";
 
     [ObservableProperty]
     private Guid _itpID;
@@ -21,6 +24,28 @@ public partial class SiteITPFormsViewModel : ModuleViewModel
     [ObservableProperty]
     private JobITPFormModel _forms;
 
+    public JobITPShell? ITP => ITPs.FirstOrDefault(x => x.ID == ItpID);
+
+    public SiteITPFormsViewModel()
+    {
+        PrimaryMenu.Add(new AvaloniaMenuItem(Images.plus, AddForm));
+    }
+
+    private void AddForm()
+    {
+        var itp = ITP;
+        if (itp is null) return;
+
+        var newForm = Forms.AddItem();
+        newForm.ParentID = itp.ID;
+        newForm.FormID = itp.FormID;
+        newForm.FormCode = itp.FormCode;
+        newForm.FormDescription = itp.FormName;
+        newForm.Save("Created on Mobile Device");
+
+        Forms.Search();
+    }
+
     protected override async Task<TimeSpan> OnRefresh()
     {
         await Task.WhenAll([
@@ -33,6 +58,8 @@ public partial class SiteITPFormsViewModel : ModuleViewModel
     [RelayCommand]
     private void FormClicked(JobITPFormShell shell)
     {
+        if (ITP is not JobITPShell itp) return;
 
+        DigitalFormsHostViewModel.EditForm<JobITP, JobITPLink, JobITPForm>(shell, itp.Entity);
     }
 }