Skip to content

Commit

Permalink
Add permanent delete.
Browse files Browse the repository at this point in the history
  • Loading branch information
aiguoli committed Aug 31, 2023
1 parent 18245a9 commit aa5ea40
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 10 deletions.
5 changes: 5 additions & 0 deletions Services/OneDrive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ public async Task DeleteItem(string itemId)
await graphClient.Drives[driveId].Items[itemId].DeleteAsync();
}

public async Task PermanentDeleteItem(string itemId)
{
await graphClient.Drives[driveId].Items[itemId].PermanentDelete.PostAsync();
}

private class TokenProvider : IAccessTokenProvider
{
private readonly Func<string[], Task<string>> getTokenDelegate;
Expand Down
4 changes: 4 additions & 0 deletions SimpleList.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
<None Remove="Pages\TaskManagerPage.xaml" />
<None Remove="Views\ConvertFileFormatView.xaml" />
<None Remove="Views\CreateFolderView.xaml" />
<None Remove="Views\DeleteFileView.xaml" />
<None Remove="Views\FileView.xaml" />
<None Remove="Views\PropertyView.xaml" />
<None Remove="Views\RenameFileView.xaml" />
Expand Down Expand Up @@ -102,6 +103,9 @@
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
<Page Update="Views\DeleteFileView.xaml">
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Update="Views\ConvertFileFormatView.xaml">
<Generator>MSBuild:Compile</Generator>
</Page>
Expand Down
40 changes: 40 additions & 0 deletions ViewModels/DeleteFileViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.DependencyInjection;
using CommunityToolkit.Mvvm.Input;
using SimpleList.Services;
using System.Threading.Tasks;

namespace SimpleList.ViewModels
{
public class DeleteFileViewModel : ObservableObject
{
public DeleteFileViewModel(FileViewModel file)
{
File = file;
DeleteFileCommand = new AsyncRelayCommand(DeleteFile);
}

public async Task DeleteFile()
{
OneDrive drive = Ioc.Default.GetService<OneDrive>();
if (PermanentDelete)
{
await drive.PermanentDeleteItem(File.Id);
} else
{
await drive.DeleteItem(File.Id);
}
await File.Cloud.Refresh();
}

private bool _permanentDelete;

public FileViewModel File;
public AsyncRelayCommand DeleteFileCommand { get; }
public bool PermanentDelete
{
get => _permanentDelete;
set => SetProperty(ref _permanentDelete, value);
}
}
}
9 changes: 0 additions & 9 deletions ViewModels/FileViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public FileViewModel(CloudViewModel cloud, DriveItem file)
_file = file;
ItemType = IsFile ? "File" : "Folder";
DownloadFileCommand = new RelayCommand<string>(DownloadFile);
DeleteFileCommand = new RelayCommand<string>(DeleteFile);
}

private async void DownloadFile(string itemId)
Expand Down Expand Up @@ -50,13 +49,6 @@ private async void DownloadFile(string itemId)
await manager.AddDownloadTask(itemId, file);
}
}
private async void DeleteFile(string itemId)
{
string parrentId = Cloud.ParentItemId;
OneDrive drive = Ioc.Default.GetService<OneDrive>();
await drive.DeleteItem(itemId);
await Cloud.GetFiles(parrentId);
}

private readonly DriveItem _file;

Expand All @@ -70,6 +62,5 @@ private async void DeleteFile(string itemId)
public CloudViewModel Cloud { get; }
public string ItemType { get; }
public RelayCommand<string> DownloadFileCommand { get; }
public RelayCommand<string> DeleteFileCommand { get; }
}
}
30 changes: 30 additions & 0 deletions Views/DeleteFileView.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<ContentDialog
x:Class="SimpleList.Views.DeleteFileView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="using:SimpleList.ViewModels"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DataContext="{x:Bind vm:DeleteFileViewModel}"
Title="Delete item"
PrimaryButtonText="Delete"
CloseButtonText="Cancel"
PrimaryButtonCommand="{Binding DeleteFileCommand}"
DefaultButton="Primary">

<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>

<TextBlock>
<Run Text="Are you sure to delete" />
<Run Text="{Binding File.Name}" />
</TextBlock>

<CheckBox Grid.Row="1" Content="Permanent Delete" IsChecked="{Binding PermanentDelete, Mode=TwoWay}" />
</Grid>
</ContentDialog>
12 changes: 12 additions & 0 deletions Views/DeleteFileView.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Microsoft.UI.Xaml.Controls;

namespace SimpleList.Views
{
public sealed partial class DeleteFileView : ContentDialog
{
public DeleteFileView()
{
InitializeComponent();
}
}
}
2 changes: 1 addition & 1 deletion Views/FileView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<MenuFlyout>
<MenuFlyoutItem Text="Open" Command="{Binding Cloud.OpenFolderCommand}" CommandParameter="{Binding}" IsEnabled="{Binding IsFolder}" />
<MenuFlyoutItem Text="Download" Command="{Binding DownloadFileCommand}" CommandParameter="{Binding Id}" IsEnabled="{Binding IsFile}" />
<MenuFlyoutItem Text="Delete" Command="{Binding DeleteFileCommand}" CommandParameter="{Binding Id}" />
<MenuFlyoutItem Text="Delete" Click="ShowDeleteFileDialogAsync" />
<MenuFlyoutItem Text="Convert" Click="ShowConverFiletDialogAsync" Visibility="{Binding Name, Converter={StaticResource FileNameToCanConvertCommandVisible}}" />
<MenuFlyoutItem Text="Share" Click="ShowShareFileDialogAsync" />
<MenuFlyoutItem Text="Rename" Click="ShowRenameFileDialogAsync" />
Expand Down
11 changes: 11 additions & 0 deletions Views/FileView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,16 @@ private async void ShowConverFiletDialogAsync(object sender, RoutedEventArgs e)
await dialog.ShowAsync();
}
}

private async void ShowDeleteFileDialogAsync(object sender, RoutedEventArgs e)
{
FileViewModel viewModel = DataContext as FileViewModel;
DeleteFileView dialog = new()
{
XamlRoot = XamlRoot,
DataContext = new DeleteFileViewModel(viewModel)
};
await dialog.ShowAsync();
}
}
}

0 comments on commit aa5ea40

Please sign in to comment.