Forráskód Böngészése

avalonia: Document Scanner now uses new ImageEditor

Kenric Nugteren 1 hete
szülő
commit
2e7aa6b6b7

+ 1 - 1
PRS.Avalonia/PRS.Avalonia/MainViewModel.cs

@@ -53,7 +53,7 @@ public partial class MainViewModel : ViewModelBase
         PropertyChanged += MainViewModel_PropertyChanged;
 
         // change to HomeView 
-        Navigation.Reset<TestViewModel>();
+        Navigation.Reset<LoginViewModel>();
     }
 
     public override bool OnBackButtonPressed()

+ 4 - 1
PRS.Avalonia/PRS.Avalonia/Modules/DocumentScanner/DocumentScannerEditorView.axaml

@@ -3,6 +3,7 @@
              xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
              xmlns:modules="using:PRS.Avalonia.Modules"
+			 xmlns:components="using:InABox.Avalonia.Components"
 			 xmlns:converters="using:InABox.Avalonia.Converters"
              mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
              x:Class="PRS.Avalonia.Modules.DocumentScannerEditorView"
@@ -12,7 +13,9 @@
 			<RowDefinition Height="*"/>
 			<RowDefinition Height="150"/>
 		</Grid.RowDefinitions>
-		<Image Grid.Row="0" Source="{Binding Document.Data,Converter={x:Static converters:ByteArrayToImageSourceConverter.Instance}}"/>
+		<components:ImageEditor Name="Editor" Grid.Row="0"
+								Source="{Binding Document.Data,Converter={x:Static converters:ByteArrayToImageSourceConverter.Instance}}"
+								Changed="ImageEditor_Changed"/>
 		<TextBox Grid.Row="1"
 				 Watermark="Enter Note Here"
 				 Text="{Binding DataEntryDocument.Note}"

+ 29 - 0
PRS.Avalonia/PRS.Avalonia/Modules/DocumentScanner/DocumentScannerEditorView.axaml.cs

@@ -1,13 +1,42 @@
 using Avalonia;
 using Avalonia.Controls;
 using Avalonia.Markup.Xaml;
+using System;
+using System.IO;
+using System.Threading.Tasks;
 
 namespace PRS.Avalonia.Modules;
 
 public partial class DocumentScannerEditorView : UserControl
 {
+    private DocumentScannerEditorViewModel? ViewModel;
+
     public DocumentScannerEditorView()
     {
         InitializeComponent();
     }
+
+    protected override void OnDataContextChanged(EventArgs e)
+    {
+        base.OnDataContextChanged(e);
+
+        ViewModel = DataContext as DocumentScannerEditorViewModel;
+        if(ViewModel != null)
+        {
+            ViewModel.GetImage = GetImage;
+        }
+    }
+
+    private byte[] GetImage()
+    {
+        return Editor.SaveImage();
+    }
+
+    private void ImageEditor_Changed(object? sender, EventArgs e)
+    {
+        if(ViewModel != null)
+        {
+            ViewModel.ImageChanged = true;
+        }
+    }
 }

+ 18 - 0
PRS.Avalonia/PRS.Avalonia/Modules/DocumentScanner/DocumentScannerEditorViewModel.cs

@@ -1,6 +1,7 @@
 using CommunityToolkit.Mvvm.ComponentModel;
 using InABox.Avalonia;
 using InABox.Avalonia.Components;
+using InABox.Avalonia.Platform;
 using InABox.Core;
 using System;
 using System.Collections.Generic;
@@ -23,6 +24,12 @@ internal partial class DocumentScannerEditorViewModel : ModuleViewModel
     [ObservableProperty]
     private DocumentModel? _documentModel;
 
+    [ObservableProperty]
+    private Func<byte[]>? _getImage;
+
+    [ObservableProperty]
+    private bool _imageChanged;
+
     public DocumentScannerEditorViewModel()
     {
         PrimaryMenu.Add(new AvaloniaMenuItem(Images.save, Save));
@@ -55,6 +62,17 @@ internal partial class DocumentScannerEditorViewModel : ModuleViewModel
     {
         ProgressVisible = true;
 
+        if (ImageChanged && GetImage is not null && Document is not null)
+        {
+            var imgData = GetImage();
+            Document.Data = imgData;
+            Document.CRC = CoreUtils.CalculateCRC(imgData);
+            Document.TimeStamp = DateTime.Now;
+            await Document.SaveAsync("Updated from Mobile Device");
+
+            DataEntryDocument.Thumbnail = PlatformTools.ImageTools.CreateThumbnail(imgData, 256, 256);
+        }
+
         await DataEntryDocument.SaveAsync("Updated from Mobile Device");
 
         ProgressVisible = false;