Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GUI: Add save button, #109 #110

Merged
merged 1 commit into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions JavaToCSharpGui/Infrastructure/HostStorageProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,44 @@
namespace JavaToCSharpGui.Infrastructure;

/// <inheritdoc cref="IHostStorageProvider" />
public class HostStorageProvider : IHostStorageProvider
public class HostStorageProvider(IStorageProvider storageProvider) : IHostStorageProvider
{
private readonly IStorageProvider _storageProvider;

public HostStorageProvider(IStorageProvider storageProvider) => _storageProvider = storageProvider;
/// <inheritdoc />
public bool CanPickFolder => storageProvider.CanPickFolder;

/// <inheritdoc />
public bool CanPickFolder => _storageProvider.CanPickFolder;
public bool CanOpen => storageProvider.CanOpen;

/// <inheritdoc />
public bool CanOpen => _storageProvider.CanOpen;
public bool CanSave => storageProvider.CanSave;

/// <inheritdoc />
public async Task<IReadOnlyList<IStorageFile>> OpenFilePickerAsync(FilePickerOpenOptions options)
{
return await _storageProvider.OpenFilePickerAsync(options);
return await storageProvider.OpenFilePickerAsync(options);
}

/// <inheritdoc />
public async Task<IReadOnlyList<IStorageFolder>> OpenFolderPickerAsync(FolderPickerOpenOptions options)
{
return await _storageProvider.OpenFolderPickerAsync(options);
return await storageProvider.OpenFolderPickerAsync(options);
}

/// <inheritdoc />
public async Task<IStorageFolder?> TryGetWellKnownFolderAsync(WellKnownFolder wellKnownFolder)
{
return await _storageProvider.TryGetWellKnownFolderAsync(wellKnownFolder);
return await storageProvider.TryGetWellKnownFolderAsync(wellKnownFolder);
}

/// <inheritdoc />
public async Task<IStorageFile?> OpenSaveFileDialogAsync(FilePickerSaveOptions options)
{
return await storageProvider.SaveFilePickerAsync(options);
}

/// <inheritdoc />
public async Task<IStorageFolder?> TryGetFolderFromPathAsync(string path)
{
return await storageProvider.TryGetFolderFromPathAsync(path);
}
}
19 changes: 19 additions & 0 deletions JavaToCSharpGui/Infrastructure/IHostStorageProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public interface IHostStorageProvider
/// </summary>
bool CanOpen { get; }

/// <summary>
/// Can the save file picker be opened on the current platform.
/// </summary>
bool CanSave { get; }

/// <summary>
/// Gets the path to a well known folder.
/// </summary>
Expand All @@ -37,4 +42,18 @@ public interface IHostStorageProvider
/// <param name="options">The file picker configuration.</param>
/// <returns>A list of selected files.</returns>
Task<IReadOnlyList<IStorageFile>> OpenFilePickerAsync(FilePickerOpenOptions options);

/// <summary>
/// Opens the save file picker dialog.
/// </summary>
/// <param name="options">The file picker configuration.</param>
/// <returns>The selected file.</returns>
Task<IStorageFile?> OpenSaveFileDialogAsync(FilePickerSaveOptions options);

/// <summary>
/// Tries to get a folder from a path.
/// </summary>
/// <param name="path">The path to the folder.</param>
/// <returns>The folder, or <c>null</c> if not found.</returns>
Task<IStorageFolder?> TryGetFolderFromPathAsync(string path);
}
36 changes: 36 additions & 0 deletions JavaToCSharpGui/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,42 @@ private async Task CopyOutput()
await _dispatcher.InvokeAsync(() => { ConversionStateLabel = ""; }, DispatcherPriority.Background);
}

[RelayCommand]
private async Task SaveOutput()
{
if (_storageProvider?.CanSave is true)
{
IStorageFolder? startLocation = null;

if (Path.GetDirectoryName(OpenPath) is string dir)
{
startLocation = await _storageProvider.TryGetFolderFromPathAsync(dir);
}

startLocation ??= await _storageProvider.TryGetWellKnownFolderAsync(WellKnownFolder.Documents);

var filePickerSaveOptions = new FilePickerSaveOptions
{
SuggestedFileName = Path.GetFileNameWithoutExtension(OpenPath) + ".cs",
SuggestedStartLocation = startLocation,
Title = "Save C# File"
};

var result = await _storageProvider.OpenSaveFileDialogAsync(filePickerSaveOptions);

if (result is not null)
{
await File.WriteAllTextAsync(result.Path.LocalPath, CSharpText);

ConversionStateLabel = "Saved C# code to file!";

await Task.Delay(2000);

await _dispatcher.InvokeAsync(() => { ConversionStateLabel = ""; }, DispatcherPriority.Background);
}
}
}

[RelayCommand]
private static void ForkMeOnGitHub() => Process.Start(new ProcessStartInfo
{
Expand Down
7 changes: 7 additions & 0 deletions JavaToCSharpGui/Views/MainWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,20 @@
<StackPanel Grid.Row="1"
Orientation="Horizontal"
HorizontalAlignment="Right"
Spacing="10"
Margin="5">
<Button Name="CopyOutput"
ToolTip.Tip="Copy to Clipboard"
AutomationProperties.Name="Copy to Clipboard"
Command="{CompiledBinding CopyOutputCommand}">
<i:Icon Value="fa-copy" />
</Button>
<Button Name="SaveOutput"
ToolTip.Tip="Save to File"
AutomationProperties.Name="Save to File"
Command="{CompiledBinding SaveOutputCommand}">
<i:Icon Value="fa-save" />
</Button>
</StackPanel>
<TextBox Name="CSharpText"
Text="{CompiledBinding CSharpText}"
Expand Down
Loading