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 b217e7b..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 bool 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 bool 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 = 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,9 +430,11 @@ internal virtual async Task SaveAsInternal(TPackage? package) if (package == null) return false; - if (BrowseSaveFile(out var path)) + var browseResult = await BrowseSaveFile(); + + if (browseResult.Success) { - var result = await package.Save(path); + var result = await package.Save(browseResult.Path); switch (result.SaveResult) { @@ -513,4 +511,4 @@ private void PackageOnMonitoredChanged(object? sender, EventArgs e) SaveCommand.SetCanExecute(Package?.IsContentModified == true); }); } -} \ No newline at end of file +}