Move and delete files

This commit is contained in:
2026-02-16 13:08:23 +09:00
parent 0da7361c9f
commit 1d2e1b9f3e
5 changed files with 128 additions and 11 deletions
+34
View File
@@ -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<ReportFile>
{
// 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<ReportFile>;
return RunDropAction(dg, e, execute, sourceItem, targetItem, items?? []);
}
protected override ReportFile MakeCopy(ObservableCollection<ReportFile> parentCollection, ReportFile item)
{
// Return a clone of the item if you support Copy operations
return new ReportFile { };
}
}
+15
View File
@@ -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(); }
}
}
+24 -2
View File
@@ -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 result = await DialogHost.Show(new WarningDeleteItemModel(file));
if (result != null && (bool)result)
{
var idx = ReportFiles.IndexOf(file);
if (idx != -1)
{
ReportFiles.RemoveAt(idx);
UpdateMoveEnabled();
}
}
}
+31
View File
@@ -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);
}
}
}
+21 -6
View File
@@ -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"/>
</StackPanel>
<DataGrid x:Name="FilesGrid"
Classes="DragAndDrop ItemsDragAndDrop"
Grid.Row="2"
Grid.Column="0"
Grid.ColumnSpan="2"
@@ -50,6 +52,7 @@
VerticalScrollBarVisibility="Visible"
HorizontalScrollBarVisibility="Disabled"
ScrollViewer.AllowAutoHide="False"
HeadersVisibility="All"
BorderBrush="Gray">
<DataGrid.Styles>
<Style Selector="TextBlock">
@@ -91,19 +94,31 @@
Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Button Content="Up"
Command="{Binding $parent[DataGrid].((vm:MainViewModel)DataContext).MoveItemUp}"
<StackPanel Orientation="Horizontal"
Spacing="4">
<Button Command="{Binding $parent[DataGrid].((vm:MainViewModel)DataContext).MoveItemUp}"
CommandParameter="{Binding}"
IsEnabled="True"/>
IsEnabled="{Binding IsMoveUpEnabled}">
<Button.Content>
<Label Content="&#xf062;" FontFamily="{StaticResource FontAwesomeSolid}" />
</Button.Content>
</Button>
<Button Content="Down"
Command="{Binding $parent[DataGrid].((vm:MainViewModel)DataContext).MoveItemDown}"
CommandParameter="{Binding}"
IsEnabled="True"/>
IsEnabled="{Binding IsMoveDownEnabled}">
<Button.Content>
<Label Content="&#xf063;" FontFamily="{StaticResource FontAwesomeSolid}" />
</Button.Content>
</Button>
<Button Content="Byebye"
Command="{Binding $parent[DataGrid].((vm:MainViewModel)DataContext).RemoveFile}"
CommandParameter="{Binding}"
IsEnabled="True"/>
IsEnabled="True">
<Button.Content>
<Label Content="&#xf1f8;" FontFamily="{StaticResource FontAwesomeSolid}" />
</Button.Content>
</Button>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>