From 12bbe540bbbbd1ddd3438e321a6efe8ad8f2ac00 Mon Sep 17 00:00:00 2001 From: djgosnell Date: Fri, 28 Jun 2024 11:42:57 -0400 Subject: [PATCH 1/2] Changed ViewModel to utilize asynchronous FileDialog calls. --- .../ViewModel/PackageManagerViewModel.cs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/DtronixPackage/ViewModel/PackageManagerViewModel.cs b/src/DtronixPackage/ViewModel/PackageManagerViewModel.cs index b217e7b..e340d08 100644 --- a/src/DtronixPackage/ViewModel/PackageManagerViewModel.cs +++ b/src/DtronixPackage/ViewModel/PackageManagerViewModel.cs @@ -124,7 +124,7 @@ protected PackageManagerViewModel(string appName) /// Path of the package to open. Must exist. /// Set to true to open the package in a read-only state; False to open normally. /// True on successful opening of package. False to cancel the opening process. - protected abstract bool BrowseOpenFile(out string path, out bool openReadOnly); + protected abstract Task BrowseOpenFile(out string path, out bool openReadOnly); /// /// Called when browsing for destination to save a package. @@ -132,7 +132,7 @@ protected PackageManagerViewModel(string appName) /// Normally paired with SaveFileDialog /// Destination path for the package to save. /// True on successful selection of package destination. False to cancel the saving process. - protected abstract bool BrowseSaveFile(out string path); + protected abstract Task BrowseSaveFile(out string path); protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null) { @@ -268,7 +268,7 @@ public virtual async Task Open() if (!await TryClose()) return false; - var result = BrowseOpenFile(out var path, out var readOnly); + var result = await BrowseOpenFile(out var path, out var readOnly); if (result != true) return false; @@ -434,7 +434,9 @@ internal virtual async Task SaveAsInternal(TPackage? package) if (package == null) return false; - if (BrowseSaveFile(out var path)) + var browseResult = await BrowseSaveFile(out var path); + + if (browseResult) { var result = await package.Save(path); @@ -513,4 +515,4 @@ private void PackageOnMonitoredChanged(object? sender, EventArgs e) SaveCommand.SetCanExecute(Package?.IsContentModified == true); }); } -} \ No newline at end of file +} From 741dbe116293351e957bb085958a43e7470cfdeb Mon Sep 17 00:00:00 2001 From: djgosnell Date: Fri, 28 Jun 2024 11:54:13 -0400 Subject: [PATCH 2/2] Updated saving to use Tasks. --- .../TestPackageManagerViewModel.cs | 21 +++++++++----- .../ViewModel/BrowseFileResult.cs | 20 +++++++++++++ .../ViewModel/PackageManagerViewModel.cs | 28 ++++++++----------- 3 files changed, 46 insertions(+), 23 deletions(-) create mode 100644 src/DtronixPackage/ViewModel/BrowseFileResult.cs diff --git a/src/DtronixPackage.Tests/PackageManagerViewModelTests/TestPackageManagerViewModel.cs b/src/DtronixPackage.Tests/PackageManagerViewModelTests/TestPackageManagerViewModel.cs index 5e8ec02..359ef61 100644 --- a/src/DtronixPackage.Tests/PackageManagerViewModelTests/TestPackageManagerViewModel.cs +++ b/src/DtronixPackage.Tests/PackageManagerViewModelTests/TestPackageManagerViewModel.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Reflection.Metadata.Ecma335; using System.Text; using System.Threading.Tasks; using DtronixPackage.ViewModel; @@ -56,21 +57,27 @@ private void OnCreated(object sender, PackageEventArgs BrowseOpenFile() { var args = new BrowseEventArgs(); BrowsingOpen?.Invoke(args); - path = args.Path; - openReadOnly = args.ReadOnly; - return args.Result; + return new BrowseFileResult() + { + Success = args.Result, + Path = args.Path, + OpenReadOnly = args.ReadOnly + }; } - protected override bool BrowseSaveFile(out string path) + protected override async ValueTask BrowseSaveFile() { var args = new BrowseEventArgs(); BrowsingSave?.Invoke(args); - path = args.Path; - return args.Result; + return new BrowseFileResult() + { + Success = args.Result, + Path = args.Path + }; } internal override async Task SaveAsInternal(PackageManagerViewModelPackage package) diff --git a/src/DtronixPackage/ViewModel/BrowseFileResult.cs b/src/DtronixPackage/ViewModel/BrowseFileResult.cs new file mode 100644 index 0000000..2e3ed44 --- /dev/null +++ b/src/DtronixPackage/ViewModel/BrowseFileResult.cs @@ -0,0 +1,20 @@ +namespace DtronixPackage.ViewModel; + +public class BrowseFileResult +{ + /// + /// True on successful open/selection. If false, cancel the process + /// + public bool Success { get; set; } + + /// + /// The path to the file + /// + public string Path { get; set; } + + /// + /// Set to true to open the package in a read-only state; False to open normally. + /// Only used with BrowseOpenFile + /// + public bool OpenReadOnly { get; set; } +} diff --git a/src/DtronixPackage/ViewModel/PackageManagerViewModel.cs b/src/DtronixPackage/ViewModel/PackageManagerViewModel.cs index e340d08..01eada1 100644 --- a/src/DtronixPackage/ViewModel/PackageManagerViewModel.cs +++ b/src/DtronixPackage/ViewModel/PackageManagerViewModel.cs @@ -117,22 +117,18 @@ protected PackageManagerViewModel(string appName) protected abstract void InvokeOnDispatcher(Action action); + /// - /// Called when browsing for a package to open. Normally paired with OpenFileDialog + /// Called when browsing for a package to open. /// - /// Normally paired with SaveFileDialog - /// Path of the package to open. Must exist. - /// Set to true to open the package in a read-only state; False to open normally. - /// True on successful opening of package. False to cancel the opening process. - protected abstract Task BrowseOpenFile(out string path, out bool openReadOnly); + /// Result of the open process. Success will be set to false if the opening process should be canceled. + protected abstract ValueTask BrowseOpenFile(); /// /// Called when browsing for destination to save a package. /// - /// Normally paired with SaveFileDialog - /// Destination path for the package to save. - /// True on successful selection of package destination. False to cancel the saving process. - protected abstract Task BrowseSaveFile(out string path); + /// Result of the open process. Success will be set to false if the saving process should be canceled. + protected abstract ValueTask BrowseSaveFile(); protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null) { @@ -268,12 +264,12 @@ public virtual async Task Open() if (!await TryClose()) return false; - var result = await BrowseOpenFile(out var path, out var readOnly); + var result = await BrowseOpenFile(); - if (result != true) + if (result.Success != true) return false; - return await Open(path, readOnly); + return await Open(result.Path, result.OpenReadOnly); } public virtual async Task Open(string path, bool forceReadOnly) @@ -434,11 +430,11 @@ internal virtual async Task SaveAsInternal(TPackage? package) if (package == null) return false; - var browseResult = await BrowseSaveFile(out var path); + var browseResult = await BrowseSaveFile(); - if (browseResult) + if (browseResult.Success) { - var result = await package.Save(path); + var result = await package.Save(browseResult.Path); switch (result.SaveResult) {