Skip to content

Commit 24f31eb

Browse files
authored
GUI: Add save button, #109 (#110)
1 parent 500334a commit 24f31eb

File tree

4 files changed

+83
-10
lines changed

4 files changed

+83
-10
lines changed

JavaToCSharpGui/Infrastructure/HostStorageProvider.cs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,44 @@
33
namespace JavaToCSharpGui.Infrastructure;
44

55
/// <inheritdoc cref="IHostStorageProvider" />
6-
public class HostStorageProvider : IHostStorageProvider
6+
public class HostStorageProvider(IStorageProvider storageProvider) : IHostStorageProvider
77
{
8-
private readonly IStorageProvider _storageProvider;
9-
10-
public HostStorageProvider(IStorageProvider storageProvider) => _storageProvider = storageProvider;
8+
/// <inheritdoc />
9+
public bool CanPickFolder => storageProvider.CanPickFolder;
1110

1211
/// <inheritdoc />
13-
public bool CanPickFolder => _storageProvider.CanPickFolder;
12+
public bool CanOpen => storageProvider.CanOpen;
1413

1514
/// <inheritdoc />
16-
public bool CanOpen => _storageProvider.CanOpen;
15+
public bool CanSave => storageProvider.CanSave;
1716

1817
/// <inheritdoc />
1918
public async Task<IReadOnlyList<IStorageFile>> OpenFilePickerAsync(FilePickerOpenOptions options)
2019
{
21-
return await _storageProvider.OpenFilePickerAsync(options);
20+
return await storageProvider.OpenFilePickerAsync(options);
2221
}
2322

2423
/// <inheritdoc />
2524
public async Task<IReadOnlyList<IStorageFolder>> OpenFolderPickerAsync(FolderPickerOpenOptions options)
2625
{
27-
return await _storageProvider.OpenFolderPickerAsync(options);
26+
return await storageProvider.OpenFolderPickerAsync(options);
2827
}
29-
28+
3029
/// <inheritdoc />
3130
public async Task<IStorageFolder?> TryGetWellKnownFolderAsync(WellKnownFolder wellKnownFolder)
3231
{
33-
return await _storageProvider.TryGetWellKnownFolderAsync(wellKnownFolder);
32+
return await storageProvider.TryGetWellKnownFolderAsync(wellKnownFolder);
33+
}
34+
35+
/// <inheritdoc />
36+
public async Task<IStorageFile?> OpenSaveFileDialogAsync(FilePickerSaveOptions options)
37+
{
38+
return await storageProvider.SaveFilePickerAsync(options);
39+
}
40+
41+
/// <inheritdoc />
42+
public async Task<IStorageFolder?> TryGetFolderFromPathAsync(string path)
43+
{
44+
return await storageProvider.TryGetFolderFromPathAsync(path);
3445
}
3546
}

JavaToCSharpGui/Infrastructure/IHostStorageProvider.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ public interface IHostStorageProvider
1717
/// </summary>
1818
bool CanOpen { get; }
1919

20+
/// <summary>
21+
/// Can the save file picker be opened on the current platform.
22+
/// </summary>
23+
bool CanSave { get; }
24+
2025
/// <summary>
2126
/// Gets the path to a well known folder.
2227
/// </summary>
@@ -37,4 +42,18 @@ public interface IHostStorageProvider
3742
/// <param name="options">The file picker configuration.</param>
3843
/// <returns>A list of selected files.</returns>
3944
Task<IReadOnlyList<IStorageFile>> OpenFilePickerAsync(FilePickerOpenOptions options);
45+
46+
/// <summary>
47+
/// Opens the save file picker dialog.
48+
/// </summary>
49+
/// <param name="options">The file picker configuration.</param>
50+
/// <returns>The selected file.</returns>
51+
Task<IStorageFile?> OpenSaveFileDialogAsync(FilePickerSaveOptions options);
52+
53+
/// <summary>
54+
/// Tries to get a folder from a path.
55+
/// </summary>
56+
/// <param name="path">The path to the folder.</param>
57+
/// <returns>The folder, or <c>null</c> if not found.</returns>
58+
Task<IStorageFolder?> TryGetFolderFromPathAsync(string path);
4059
}

JavaToCSharpGui/ViewModels/MainWindowViewModel.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,42 @@ private async Task CopyOutput()
324324
await _dispatcher.InvokeAsync(() => { ConversionStateLabel = ""; }, DispatcherPriority.Background);
325325
}
326326

327+
[RelayCommand]
328+
private async Task SaveOutput()
329+
{
330+
if (_storageProvider?.CanSave is true)
331+
{
332+
IStorageFolder? startLocation = null;
333+
334+
if (Path.GetDirectoryName(OpenPath) is string dir)
335+
{
336+
startLocation = await _storageProvider.TryGetFolderFromPathAsync(dir);
337+
}
338+
339+
startLocation ??= await _storageProvider.TryGetWellKnownFolderAsync(WellKnownFolder.Documents);
340+
341+
var filePickerSaveOptions = new FilePickerSaveOptions
342+
{
343+
SuggestedFileName = Path.GetFileNameWithoutExtension(OpenPath) + ".cs",
344+
SuggestedStartLocation = startLocation,
345+
Title = "Save C# File"
346+
};
347+
348+
var result = await _storageProvider.OpenSaveFileDialogAsync(filePickerSaveOptions);
349+
350+
if (result is not null)
351+
{
352+
await File.WriteAllTextAsync(result.Path.LocalPath, CSharpText);
353+
354+
ConversionStateLabel = "Saved C# code to file!";
355+
356+
await Task.Delay(2000);
357+
358+
await _dispatcher.InvokeAsync(() => { ConversionStateLabel = ""; }, DispatcherPriority.Background);
359+
}
360+
}
361+
}
362+
327363
[RelayCommand]
328364
private static void ForkMeOnGitHub() => Process.Start(new ProcessStartInfo
329365
{

JavaToCSharpGui/Views/MainWindow.axaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,20 @@
9393
<StackPanel Grid.Row="1"
9494
Orientation="Horizontal"
9595
HorizontalAlignment="Right"
96+
Spacing="10"
9697
Margin="5">
9798
<Button Name="CopyOutput"
9899
ToolTip.Tip="Copy to Clipboard"
99100
AutomationProperties.Name="Copy to Clipboard"
100101
Command="{CompiledBinding CopyOutputCommand}">
101102
<i:Icon Value="fa-copy" />
102103
</Button>
104+
<Button Name="SaveOutput"
105+
ToolTip.Tip="Save to File"
106+
AutomationProperties.Name="Save to File"
107+
Command="{CompiledBinding SaveOutputCommand}">
108+
<i:Icon Value="fa-save" />
109+
</Button>
103110
</StackPanel>
104111
<TextBox Name="CSharpText"
105112
Text="{CompiledBinding CSharpText}"

0 commit comments

Comments
 (0)