diff --git a/Helpers/DataGridDropHandler.cs b/Helpers/DataGridDropHandler.cs new file mode 100644 index 0000000..d0d0efc --- /dev/null +++ b/Helpers/DataGridDropHandler.cs @@ -0,0 +1,34 @@ +using System.Collections.ObjectModel; +using Avalonia.Controls; +using Avalonia.Input; +using Avalonia.VisualTree; +using Avalonia.Xaml.Interactions.DragAndDrop; +using ReceiptPDFBuilder.Models; +using ReceiptPDFBuilder.ViewModels; + +namespace ReceiptPDFBuilder.Helpers; + +class DataGridDropHandler : BaseDataGridDropHandler +{ + // https://wieslawsoltes.github.io/Xaml.Behaviors/articles/drag-and-drop-datagrid/datagrid-drag-and-drop-overview.html + // ...that page's code basically doesn't work in the way you'd expect. so just use the source code sample's code. + protected override bool Validate(DataGrid dg, DragEventArgs e, object? sourceContext, object? targetContext, bool execute) + { + if (sourceContext is not ReportFile sourceItem + || targetContext is not MainViewModel vm + || dg.GetVisualAt(e.GetPosition(dg)) is not Control targetControl + || targetControl.DataContext is not ReportFile targetItem) + { + return false; + } + + var items = dg.ItemsSource as ObservableCollection; + return RunDropAction(dg, e, execute, sourceItem, targetItem, items?? []); + } + + protected override ReportFile MakeCopy(ObservableCollection parentCollection, ReportFile item) + { + // Return a clone of the item if you support Copy operations + return new ReportFile { }; + } +} \ No newline at end of file diff --git a/Models/ReportFile.cs b/Models/ReportFile.cs index 93b1932..d46949b 100644 --- a/Models/ReportFile.cs +++ b/Models/ReportFile.cs @@ -6,6 +6,9 @@ namespace ReceiptPDFBuilder.Models; class ReportFile : ChangeNotifier { + private bool _isMoveUpEnabled = false; + private bool _isMoveDownEnabled = false; + private string _title; private DateOnly _date; private DateTime _dateTime; @@ -75,4 +78,16 @@ class ReportFile : ChangeNotifier { get => Path.GetFileName(_filePath); } + + public bool IsMoveUpEnabled + { + get => _isMoveUpEnabled; + set { _isMoveUpEnabled = value; NotifyPropertyChanged(); } + } + + public bool IsMoveDownEnabled + { + get => _isMoveDownEnabled; + set { _isMoveDownEnabled = value; NotifyPropertyChanged(); } + } } \ No newline at end of file diff --git a/ViewModels/MainViewModel.cs b/ViewModels/MainViewModel.cs index f2fe4d0..a5c22b2 100644 --- a/ViewModels/MainViewModel.cs +++ b/ViewModels/MainViewModel.cs @@ -4,8 +4,11 @@ using System; using System.Collections.ObjectModel; using System.Globalization; using System.IO; +using System.Linq; using System.Threading.Tasks; using Avalonia.Platform.Storage; +using Avalonia.Themes.Fluent; +using DialogHostAvalonia; using ImageMagick; using MigraDoc.DocumentObjectModel; using MigraDoc.Rendering; @@ -91,15 +94,27 @@ class MainViewModel : BaseViewModel, IFontResolver Title = Path.GetFileName(filePath), Date = DateOnly.FromDateTime(File.GetCreationTime(filePath)), Notes = "", - FilePath = filePath + FilePath = filePath, + IsMoveDownEnabled = true, + IsMoveUpEnabled = true, }); } } + UpdateMoveEnabled(); } } } } + private void UpdateMoveEnabled() + { + for (var i = 0; i < ReportFiles.Count; i++) + { + ReportFiles[i].IsMoveUpEnabled = i != 0; + ReportFiles[i].IsMoveDownEnabled = i != ReportFiles.Count - 1; + } + } + public void MoveItemUp(ReportFile file) { var idx = ReportFiles.IndexOf(file); @@ -112,6 +127,7 @@ class MainViewModel : BaseViewModel, IFontResolver // So, remove and insert. ReportFiles.RemoveAt(idx); ReportFiles.Insert(idx - 1, file); + UpdateMoveEnabled(); } } @@ -122,15 +138,21 @@ class MainViewModel : BaseViewModel, IFontResolver { ReportFiles.RemoveAt(idx); ReportFiles.Insert(idx + 1, file); + UpdateMoveEnabled(); } } - public void RemoveFile(ReportFile file) + public async void RemoveFile(ReportFile file) { - var idx = ReportFiles.IndexOf(file); - if (idx != -1) + var result = await DialogHost.Show(new WarningDeleteItemModel(file)); + if (result != null && (bool)result) { - ReportFiles.RemoveAt(idx); + var idx = ReportFiles.IndexOf(file); + if (idx != -1) + { + ReportFiles.RemoveAt(idx); + UpdateMoveEnabled(); + } } } diff --git a/ViewModels/WarningDeleteItemModel.cs b/ViewModels/WarningDeleteItemModel.cs new file mode 100644 index 0000000..cf45700 --- /dev/null +++ b/ViewModels/WarningDeleteItemModel.cs @@ -0,0 +1,31 @@ +using DialogHostAvalonia; +using ReceiptPDFBuilder.Helpers; +using ReceiptPDFBuilder.Models; + +namespace ReceiptPDFBuilder.ViewModels +{ + class WarningDeleteItemModel : ChangeNotifier + { + ReportFile _file; + + public WarningDeleteItemModel(ReportFile file) + { + _file = file; + } + + public ReportFile File + { + get => _file; + } + + public void KeepItem() + { + DialogHost.Close("DialogHost", false); + } + + public void RemoveItem() + { + DialogHost.Close("DialogHost", true); + } + } +} diff --git a/Views/MainView.axaml b/Views/MainView.axaml index 73842eb..2a1bd9e 100644 --- a/Views/MainView.axaml +++ b/Views/MainView.axaml @@ -4,6 +4,7 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" x:Class="ReceiptPDFBuilder.Views.MainView" + xmlns:helpers="clr-namespace:ReceiptPDFBuilder.Helpers" xmlns:models="clr-namespace:ReceiptPDFBuilder.Models" xmlns:vm="clr-namespace:ReceiptPDFBuilder.ViewModels" xmlns:progRing="clr-namespace:AvaloniaProgressRing;assembly=AvaloniaProgressRing" @@ -35,6 +36,7 @@ Margin="10,20,0,0"/>