-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
179 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using Microsoft.UI.Xaml; | ||
using Microsoft.UI.Xaml.Data; | ||
using System; | ||
using System.Linq; | ||
|
||
namespace SimpleList.Converters | ||
{ | ||
public class FileNameToCanConvertCommandVisible : IValueConverter | ||
{ | ||
public static readonly FileNameToCanConvertCommandVisible Instance = new(); | ||
public object Convert(object value, Type targetType, object parameter, string language) | ||
{ | ||
string[] allowedExtensions = { ".csv", ".doc", ".docx", ".odp", ".ods", ".odt", ".pot", ".potm", ".potx", ".pps", ".ppsx", ".ppsxm", ".ppt", ".pptm", ".pptx", ".rtf", ".xls", ".xlsx" }; | ||
if (value is string fileName) | ||
{ | ||
string fileExtension = System.IO.Path.GetExtension(fileName).ToLower(); | ||
if (allowedExtensions.Contains(fileExtension)) | ||
{ | ||
return Visibility.Visible; | ||
} | ||
} | ||
return Visibility.Collapsed; | ||
} | ||
|
||
|
||
public object ConvertBack(object value, Type targetType, object parameter, string language) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using CommunityToolkit.Mvvm.ComponentModel; | ||
using CommunityToolkit.Mvvm.DependencyInjection; | ||
using SimpleList.Services; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Windows.Storage.Pickers; | ||
using Windows.Storage; | ||
using WinRT.Interop; | ||
using Microsoft.UI.Xaml; | ||
using CommunityToolkit.Mvvm.Input; | ||
using System.IO; | ||
using System.Linq; | ||
|
||
namespace SimpleList.ViewModels | ||
{ | ||
public class ConvertFileFormatViewModel : ObservableObject | ||
{ | ||
public ConvertFileFormatViewModel(FileViewModel file) | ||
{ | ||
_file = file; | ||
ConvertFileCommand = new AsyncRelayCommand(ConvertFileFormat); | ||
} | ||
|
||
public async Task ConvertFileFormat() | ||
{ | ||
Window _downloadPathSelectWindow = new(); | ||
IntPtr hwnd = WindowNative.GetWindowHandle(_downloadPathSelectWindow); | ||
FileSavePicker savePicker = new() | ||
{ | ||
SuggestedStartLocation = PickerLocationId.Downloads | ||
}; | ||
savePicker.FileTypeChoices.Add("PDF Document", new List<string>() { ".pdf" }); | ||
savePicker.SuggestedFileName = Path.GetFileNameWithoutExtension(_file.Name); | ||
InitializeWithWindow.Initialize(savePicker, hwnd); | ||
StorageFile file = await savePicker.PickSaveFileAsync(); | ||
SavedFilePath = file?.Path; | ||
|
||
string fileExtension = Path.GetExtension(_file.Name).ToLower(); | ||
|
||
if (allowedExtensions.Contains(fileExtension)) | ||
{ | ||
await drive.ConvertFileFormat(_file.Id, file); | ||
} | ||
} | ||
|
||
private readonly FileViewModel _file; | ||
private readonly OneDrive drive = Ioc.Default.GetService<OneDrive>(); | ||
private static readonly string[] allowedExtensions = { ".csv", ".doc", ".docx", ".odp", ".ods", ".odt", ".pot", ".potm", ".potx", ".pps", ".ppsx", ".ppsxm", ".ppt", ".pptm", ".pptx", ".rtf", ".xls", ".xlsx" }; | ||
private string _selectedFormat = "pdf"; | ||
private string _savedFilePath; | ||
|
||
public IEnumerable<string> TargetFormats => new List<string> { "pdf" }; | ||
public string SelectedFormat { get => _selectedFormat; set => SetProperty(ref _selectedFormat, value); } | ||
public string SavedFilePath { get => _savedFilePath; set => SetProperty(ref _savedFilePath, value); } | ||
public string FormattedExtensions => string.Join(", ", allowedExtensions.Select(ext => ext.TrimStart('.'))); | ||
public AsyncRelayCommand ConvertFileCommand { get; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<ContentDialog | ||
x:Class="SimpleList.Views.ConvertFileFormatView" | ||
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:ConvertFileFormatViewModel}" | ||
Title="Convert File Format" | ||
PrimaryButtonText="Convert" | ||
DefaultButton="Primary" | ||
CloseButtonText="Cancel"> | ||
|
||
<Grid> | ||
<Grid.RowDefinitions> | ||
<RowDefinition /> | ||
<RowDefinition /> | ||
<RowDefinition /> | ||
</Grid.RowDefinitions> | ||
|
||
<TextBlock Margin="0 0 0 10" TextWrapping="Wrap"> | ||
<Run Text="The currently supported file formats include " /> | ||
<Run Text="{Binding FormattedExtensions}" /> | ||
</TextBlock> | ||
|
||
<RelativePanel Grid.Row="1"> | ||
<TextBlock Text="Target Format" FontWeight="Bold" RelativePanel.AlignLeftWithPanel="True" /> | ||
<ComboBox ItemsSource="{Binding TargetFormats}" SelectedIndex="0" SelectedItem="{Binding SelectedFormat, Mode=TwoWay}" RelativePanel.AlignRightWithPanel="True" /> | ||
</RelativePanel> | ||
|
||
<RelativePanel Grid.Row="2"> | ||
<TextBlock Text="Save Path" FontWeight="Bold" RelativePanel.AlignLeftWithPanel="True" /> | ||
<StackPanel Orientation="Horizontal" RelativePanel.AlignRightWithPanel="True"> | ||
<TextBox Text="{Binding SavedFilePath}" IsEnabled="False" /> | ||
<Button Content="Browser" Command="{Binding ConvertFileCommand}" /> | ||
</StackPanel> | ||
</RelativePanel> | ||
</Grid> | ||
</ContentDialog> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ConvertFileFormatView : ContentDialog | ||
{ | ||
public ConvertFileFormatView() | ||
{ | ||
InitializeComponent(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters