Move and delete files
This commit is contained in:
@@ -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 { };
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,6 +6,9 @@ namespace ReceiptPDFBuilder.Models;
|
|||||||
|
|
||||||
class ReportFile : ChangeNotifier
|
class ReportFile : ChangeNotifier
|
||||||
{
|
{
|
||||||
|
private bool _isMoveUpEnabled = false;
|
||||||
|
private bool _isMoveDownEnabled = false;
|
||||||
|
|
||||||
private string _title;
|
private string _title;
|
||||||
private DateOnly _date;
|
private DateOnly _date;
|
||||||
private DateTime _dateTime;
|
private DateTime _dateTime;
|
||||||
@@ -75,4 +78,16 @@ class ReportFile : ChangeNotifier
|
|||||||
{
|
{
|
||||||
get => Path.GetFileName(_filePath);
|
get => Path.GetFileName(_filePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bool IsMoveUpEnabled
|
||||||
|
{
|
||||||
|
get => _isMoveUpEnabled;
|
||||||
|
set { _isMoveUpEnabled = value; NotifyPropertyChanged(); }
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool IsMoveDownEnabled
|
||||||
|
{
|
||||||
|
get => _isMoveDownEnabled;
|
||||||
|
set { _isMoveDownEnabled = value; NotifyPropertyChanged(); }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -4,8 +4,11 @@ using System;
|
|||||||
using System.Collections.ObjectModel;
|
using System.Collections.ObjectModel;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Avalonia.Platform.Storage;
|
using Avalonia.Platform.Storage;
|
||||||
|
using Avalonia.Themes.Fluent;
|
||||||
|
using DialogHostAvalonia;
|
||||||
using ImageMagick;
|
using ImageMagick;
|
||||||
using MigraDoc.DocumentObjectModel;
|
using MigraDoc.DocumentObjectModel;
|
||||||
using MigraDoc.Rendering;
|
using MigraDoc.Rendering;
|
||||||
@@ -91,15 +94,27 @@ class MainViewModel : BaseViewModel, IFontResolver
|
|||||||
Title = Path.GetFileName(filePath),
|
Title = Path.GetFileName(filePath),
|
||||||
Date = DateOnly.FromDateTime(File.GetCreationTime(filePath)),
|
Date = DateOnly.FromDateTime(File.GetCreationTime(filePath)),
|
||||||
Notes = "",
|
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)
|
public void MoveItemUp(ReportFile file)
|
||||||
{
|
{
|
||||||
var idx = ReportFiles.IndexOf(file);
|
var idx = ReportFiles.IndexOf(file);
|
||||||
@@ -112,6 +127,7 @@ class MainViewModel : BaseViewModel, IFontResolver
|
|||||||
// So, remove and insert.
|
// So, remove and insert.
|
||||||
ReportFiles.RemoveAt(idx);
|
ReportFiles.RemoveAt(idx);
|
||||||
ReportFiles.Insert(idx - 1, file);
|
ReportFiles.Insert(idx - 1, file);
|
||||||
|
UpdateMoveEnabled();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -122,15 +138,21 @@ class MainViewModel : BaseViewModel, IFontResolver
|
|||||||
{
|
{
|
||||||
ReportFiles.RemoveAt(idx);
|
ReportFiles.RemoveAt(idx);
|
||||||
ReportFiles.Insert(idx + 1, file);
|
ReportFiles.Insert(idx + 1, file);
|
||||||
|
UpdateMoveEnabled();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RemoveFile(ReportFile file)
|
public async void RemoveFile(ReportFile file)
|
||||||
{
|
{
|
||||||
var idx = ReportFiles.IndexOf(file);
|
var result = await DialogHost.Show(new WarningDeleteItemModel(file));
|
||||||
if (idx != -1)
|
if (result != null && (bool)result)
|
||||||
{
|
{
|
||||||
ReportFiles.RemoveAt(idx);
|
var idx = ReportFiles.IndexOf(file);
|
||||||
|
if (idx != -1)
|
||||||
|
{
|
||||||
|
ReportFiles.RemoveAt(idx);
|
||||||
|
UpdateMoveEnabled();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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
@@ -4,6 +4,7 @@
|
|||||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||||
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
|
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
|
||||||
x:Class="ReceiptPDFBuilder.Views.MainView"
|
x:Class="ReceiptPDFBuilder.Views.MainView"
|
||||||
|
xmlns:helpers="clr-namespace:ReceiptPDFBuilder.Helpers"
|
||||||
xmlns:models="clr-namespace:ReceiptPDFBuilder.Models"
|
xmlns:models="clr-namespace:ReceiptPDFBuilder.Models"
|
||||||
xmlns:vm="clr-namespace:ReceiptPDFBuilder.ViewModels"
|
xmlns:vm="clr-namespace:ReceiptPDFBuilder.ViewModels"
|
||||||
xmlns:progRing="clr-namespace:AvaloniaProgressRing;assembly=AvaloniaProgressRing"
|
xmlns:progRing="clr-namespace:AvaloniaProgressRing;assembly=AvaloniaProgressRing"
|
||||||
@@ -35,6 +36,7 @@
|
|||||||
Margin="10,20,0,0"/>
|
Margin="10,20,0,0"/>
|
||||||
</StackPanel>
|
</StackPanel>
|
||||||
<DataGrid x:Name="FilesGrid"
|
<DataGrid x:Name="FilesGrid"
|
||||||
|
Classes="DragAndDrop ItemsDragAndDrop"
|
||||||
Grid.Row="2"
|
Grid.Row="2"
|
||||||
Grid.Column="0"
|
Grid.Column="0"
|
||||||
Grid.ColumnSpan="2"
|
Grid.ColumnSpan="2"
|
||||||
@@ -50,6 +52,7 @@
|
|||||||
VerticalScrollBarVisibility="Visible"
|
VerticalScrollBarVisibility="Visible"
|
||||||
HorizontalScrollBarVisibility="Disabled"
|
HorizontalScrollBarVisibility="Disabled"
|
||||||
ScrollViewer.AllowAutoHide="False"
|
ScrollViewer.AllowAutoHide="False"
|
||||||
|
HeadersVisibility="All"
|
||||||
BorderBrush="Gray">
|
BorderBrush="Gray">
|
||||||
<DataGrid.Styles>
|
<DataGrid.Styles>
|
||||||
<Style Selector="TextBlock">
|
<Style Selector="TextBlock">
|
||||||
@@ -91,19 +94,31 @@
|
|||||||
Width="*">
|
Width="*">
|
||||||
<DataGridTemplateColumn.CellTemplate>
|
<DataGridTemplateColumn.CellTemplate>
|
||||||
<DataTemplate>
|
<DataTemplate>
|
||||||
<StackPanel Orientation="Horizontal">
|
<StackPanel Orientation="Horizontal"
|
||||||
<Button Content="Up"
|
Spacing="4">
|
||||||
Command="{Binding $parent[DataGrid].((vm:MainViewModel)DataContext).MoveItemUp}"
|
<Button Command="{Binding $parent[DataGrid].((vm:MainViewModel)DataContext).MoveItemUp}"
|
||||||
CommandParameter="{Binding}"
|
CommandParameter="{Binding}"
|
||||||
IsEnabled="True"/>
|
IsEnabled="{Binding IsMoveUpEnabled}">
|
||||||
|
<Button.Content>
|
||||||
|
<Label Content="" FontFamily="{StaticResource FontAwesomeSolid}" />
|
||||||
|
</Button.Content>
|
||||||
|
</Button>
|
||||||
<Button Content="Down"
|
<Button Content="Down"
|
||||||
Command="{Binding $parent[DataGrid].((vm:MainViewModel)DataContext).MoveItemDown}"
|
Command="{Binding $parent[DataGrid].((vm:MainViewModel)DataContext).MoveItemDown}"
|
||||||
CommandParameter="{Binding}"
|
CommandParameter="{Binding}"
|
||||||
IsEnabled="True"/>
|
IsEnabled="{Binding IsMoveDownEnabled}">
|
||||||
|
<Button.Content>
|
||||||
|
<Label Content="" FontFamily="{StaticResource FontAwesomeSolid}" />
|
||||||
|
</Button.Content>
|
||||||
|
</Button>
|
||||||
<Button Content="Byebye"
|
<Button Content="Byebye"
|
||||||
Command="{Binding $parent[DataGrid].((vm:MainViewModel)DataContext).RemoveFile}"
|
Command="{Binding $parent[DataGrid].((vm:MainViewModel)DataContext).RemoveFile}"
|
||||||
CommandParameter="{Binding}"
|
CommandParameter="{Binding}"
|
||||||
IsEnabled="True"/>
|
IsEnabled="True">
|
||||||
|
<Button.Content>
|
||||||
|
<Label Content="" FontFamily="{StaticResource FontAwesomeSolid}" />
|
||||||
|
</Button.Content>
|
||||||
|
</Button>
|
||||||
</StackPanel>
|
</StackPanel>
|
||||||
</DataTemplate>
|
</DataTemplate>
|
||||||
</DataGridTemplateColumn.CellTemplate>
|
</DataGridTemplateColumn.CellTemplate>
|
||||||
|
|||||||
Reference in New Issue
Block a user