|
|
@@ -3,6 +3,12 @@ using Avalonia.Controls;
|
|
|
using Avalonia.Markup.Xaml;
|
|
|
using CommunityToolkit.Mvvm.Input;
|
|
|
using InABox.Avalonia;
|
|
|
+using InABox.Avalonia.Dialogs;
|
|
|
+using InABox.Avalonia.Platform;
|
|
|
+using System;
|
|
|
+using System.IO;
|
|
|
+using System.Linq;
|
|
|
+using System.Threading.Tasks;
|
|
|
|
|
|
namespace PRS.Avalonia.Components;
|
|
|
|
|
|
@@ -33,4 +39,47 @@ public partial class DocumentList : UserControl
|
|
|
x.DocumentID = shell.DocumentID;
|
|
|
});
|
|
|
}
|
|
|
+
|
|
|
+ public async Task<bool> AddImage<T, TOptions, TShell>(TOptions options, Func<TShell, Task<bool>>? customiseShell = null)
|
|
|
+ where T : MobileDocumentSource
|
|
|
+ where TOptions : MobileImageOptions<T>
|
|
|
+ where TShell : class, IEntityDocumentShell
|
|
|
+ {
|
|
|
+ if(Repository is null)
|
|
|
+ {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ MobileDocument? file = null;
|
|
|
+ try
|
|
|
+ {
|
|
|
+ file = await MobileDocument.From(App.TopLevel, options);
|
|
|
+ }
|
|
|
+ catch(Exception e)
|
|
|
+ {
|
|
|
+ MobileLogging.LogExceptionMessage(e);
|
|
|
+ await MessageDialog.ShowError(e);
|
|
|
+ }
|
|
|
+
|
|
|
+ if(file?.Data is not null && file.Data.Length > 0)
|
|
|
+ {
|
|
|
+ var ext = Path.GetExtension(file.FileName);
|
|
|
+ file.FileName = Path.ChangeExtension(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), ext);
|
|
|
+ var shell = Repository.AddItem() as TShell
|
|
|
+ ?? throw new Exception("Repository is not of correct type");
|
|
|
+
|
|
|
+ var confirm = customiseShell is null || await customiseShell.Invoke(shell);
|
|
|
+ if (confirm)
|
|
|
+ {
|
|
|
+ await ProgressDialog.Execute("Saving Image", () =>
|
|
|
+ {
|
|
|
+ shell.Thumbnail = PlatformTools.ImageTools.CreateThumbnail(file.Data, 256, 256);
|
|
|
+ shell.FileName = file.FileName;
|
|
|
+
|
|
|
+ var documentShell = EntityDocumentUtils.SaveDocument<TShell>(file, () => shell, "Created on Mobile Device");
|
|
|
+ });
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
}
|