|
@@ -1,11 +1,17 @@
|
|
|
-using Comal.Classes;
|
|
|
+using Avalonia.Data;
|
|
|
+using Avalonia.Media;
|
|
|
+using Avalonia.Threading;
|
|
|
+using Comal.Classes;
|
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
|
using InABox.Avalonia;
|
|
|
+using InABox.Avalonia.Components;
|
|
|
using InABox.Clients;
|
|
|
using InABox.Core;
|
|
|
+using PRS.Avalonia.Dialogs;
|
|
|
using PRS.Avalonia.Modules;
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
+using System.ComponentModel;
|
|
|
using System.Linq;
|
|
|
using System.Text;
|
|
|
using System.Threading.Tasks;
|
|
@@ -34,13 +40,19 @@ public class DigitalFormCacheModel<TParent, TParentLink, TForm> : ISerializeBina
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-public interface IDigitalFormsHostViewModel
|
|
|
+public interface IDigitalFormsHostViewModel : INotifyPropertyChanged
|
|
|
{
|
|
|
Entity Parent { get; }
|
|
|
|
|
|
IDigitalFormInstance Form { get; }
|
|
|
|
|
|
DFLayout Layout { get; }
|
|
|
+
|
|
|
+ Action<DFLoadStorage> LoadValues { set; }
|
|
|
+
|
|
|
+ Func<DFSaveStorage> SaveValues { set; }
|
|
|
+
|
|
|
+ Func<List<string>?> Validate { set; }
|
|
|
}
|
|
|
|
|
|
public partial class DigitalFormsHostViewModel<TModel, TShell, TParent, TParentLink, TForm> : ModuleViewModel, IDigitalFormsHostViewModel
|
|
@@ -58,6 +70,9 @@ public partial class DigitalFormsHostViewModel<TModel, TShell, TParent, TParentL
|
|
|
[ObservableProperty]
|
|
|
private Guid _instanceID;
|
|
|
|
|
|
+ [ObservableProperty]
|
|
|
+ private DigitalForm _digitalForm;
|
|
|
+
|
|
|
[ObservableProperty]
|
|
|
private DigitalFormLayout _digitalFormLayout;
|
|
|
|
|
@@ -85,6 +100,18 @@ public partial class DigitalFormsHostViewModel<TModel, TShell, TParent, TParentL
|
|
|
[ObservableProperty]
|
|
|
private DFLayout _layout;
|
|
|
|
|
|
+ [ObservableProperty]
|
|
|
+ private DateTime _timeStarted = DateTime.MinValue;
|
|
|
+
|
|
|
+ [ObservableProperty]
|
|
|
+ private Action<DFLoadStorage> _loadValues = null!;
|
|
|
+
|
|
|
+ [ObservableProperty]
|
|
|
+ private Func<DFSaveStorage> _saveValues = null!;
|
|
|
+
|
|
|
+ [ObservableProperty]
|
|
|
+ private Func<List<string>?> _validate = null!;
|
|
|
+
|
|
|
public event Action? OnSaved;
|
|
|
|
|
|
private string CacheFileName => $"{typeof(TForm)}.{InstanceID}";
|
|
@@ -122,6 +149,7 @@ public partial class DigitalFormsHostViewModel<TModel, TShell, TParent, TParentL
|
|
|
{
|
|
|
await Model.RefreshAsync(DataAccess.Status == ConnectionStatus.Connected);
|
|
|
|
|
|
+ DigitalForm = Model.FirstOrDefault(x => x.ID == FormID)!.Entity;
|
|
|
Variables = Model.Variables.Where(x => x.FormID == FormID).Select(x => x.Entity).ToArray();
|
|
|
DigitalFormLayout = Model.Layouts.Where(x => x.FormID == FormID).First().Entity;
|
|
|
|
|
@@ -145,6 +173,7 @@ public partial class DigitalFormsHostViewModel<TModel, TShell, TParent, TParentL
|
|
|
x => x.FormCompleted,
|
|
|
x => x.FormCompletedBy.ID,
|
|
|
x => x.Created,
|
|
|
+ x => x.FormStarted,
|
|
|
x => x.FormOpen,
|
|
|
x => x.BlobData))
|
|
|
.ToObjects<TForm>()
|
|
@@ -167,18 +196,101 @@ public partial class DigitalFormsHostViewModel<TModel, TShell, TParent, TParentL
|
|
|
var layout = new DFLayout();
|
|
|
layout.LoadLayout(DigitalFormLayout.Layout);
|
|
|
layout.LoadVariables(Variables);
|
|
|
- Layout = layout;
|
|
|
+
|
|
|
+
|
|
|
+ Dispatcher.UIThread.Invoke(() =>
|
|
|
+ {
|
|
|
+ Layout = layout;
|
|
|
+ LoadValues(DigitalForm.DeserializeFormData(Form) ?? new());
|
|
|
+ });
|
|
|
|
|
|
NewForm = Form.FormData.IsNullOrWhiteSpace();
|
|
|
ReadOnly = Form.FormCompleted != DateTime.MinValue;
|
|
|
|
|
|
+ TimeStarted = DateTime.Now;
|
|
|
+
|
|
|
ProgressVisible = false;
|
|
|
return TimeSpan.Zero;
|
|
|
}
|
|
|
|
|
|
private async Task<bool> SaveForm()
|
|
|
{
|
|
|
- throw new NotImplementedException();
|
|
|
+ string[] options;
|
|
|
+ if(DigitalForm.AppliesTo.Equals(nameof(Kanban)) || DigitalForm.AppliesTo.Equals(nameof(Job)))
|
|
|
+ {
|
|
|
+ options = ["Save Progress", "Complete Form", "Complete and Duplicate", "Delete Form"];
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ options = ["Save Progress", "Complete Form", "Delete Form"];
|
|
|
+ }
|
|
|
+ var chosenOption = await OptionSelector.Execute("Select Option", options);
|
|
|
+ if(chosenOption is null)
|
|
|
+ {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ var completeDuplicate = chosenOption == "Complete and Duplicate";
|
|
|
+ var complete = chosenOption == "Complete Form" || completeDuplicate;
|
|
|
+ var delete = chosenOption == "Delete Form";
|
|
|
+
|
|
|
+ if(complete)
|
|
|
+ {
|
|
|
+ var errors = Dispatcher.UIThread.Invoke(Validate);
|
|
|
+ if(errors is not null)
|
|
|
+ {
|
|
|
+ await MessageDialog.ShowMessage($"Could not save form:\n - {string.Join("\n - ", errors)}\nPlease address these issues and try again.");
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ ProgressVisible = true;
|
|
|
+ DigitalForm.SerializeFormData(Form, Variables, SaveValues());
|
|
|
+
|
|
|
+ await Task.Run(() =>
|
|
|
+ {
|
|
|
+ if(Form.FormStarted == DateTime.MinValue)
|
|
|
+ {
|
|
|
+ Form.FormStarted = TimeStarted;
|
|
|
+ }
|
|
|
+ Form.FormOpen += (DateTime.Now - TimeStarted);
|
|
|
+
|
|
|
+ if(delete)
|
|
|
+ {
|
|
|
+ Form.FormCancelled = DateTime.Now;
|
|
|
+ }
|
|
|
+ else if (complete)
|
|
|
+ {
|
|
|
+ Form.FormCompleted = DateTime.Now;
|
|
|
+ Form.FormCompletedBy.ID = ClientFactory.UserGuid;
|
|
|
+ Form.FormCompletedBy.UserID = ClientFactory.UserID;
|
|
|
+ if(App.GPS is not null)
|
|
|
+ {
|
|
|
+ Form.Location.Longitude = App.GPS.Longitude;
|
|
|
+ Form.Location.Latitude = App.GPS.Latitude;
|
|
|
+ Form.Location.Timestamp = Form.FormCompleted;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ DigitalFormDataModel.Update(Form, Parent);
|
|
|
+ });
|
|
|
+ ProgressVisible = false;
|
|
|
+
|
|
|
+ if (completeDuplicate)
|
|
|
+ {
|
|
|
+ var formid = Form.Form.ID;
|
|
|
+ Form = new TForm();
|
|
|
+ Form.Parent.ID = Parent.ID;
|
|
|
+ Form.Form.ID = formid;
|
|
|
+ Dispatcher.UIThread.Invoke(() => LoadValues(new()));
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ Navigation.Back();
|
|
|
+ OnSaved?.Invoke();
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
}
|
|
|
|
|
|
public static void EditForm(TModel model, TShell shell, TParent parent)
|