Skip to content

Commit 741dbe1

Browse files
committed
Updated saving to use Tasks.
1 parent 12bbe54 commit 741dbe1

File tree

3 files changed

+46
-23
lines changed

3 files changed

+46
-23
lines changed

src/DtronixPackage.Tests/PackageManagerViewModelTests/TestPackageManagerViewModel.cs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Reflection.Metadata.Ecma335;
34
using System.Text;
45
using System.Threading.Tasks;
56
using DtronixPackage.ViewModel;
@@ -56,21 +57,27 @@ private void OnCreated(object sender, PackageEventArgs<PackageManagerViewModelPa
5657
e.Package.Opening = PackageOpening;
5758
}
5859

59-
protected override bool BrowseOpenFile(out string path, out bool openReadOnly)
60+
protected override async ValueTask<BrowseFileResult> BrowseOpenFile()
6061
{
6162
var args = new BrowseEventArgs();
6263
BrowsingOpen?.Invoke(args);
63-
path = args.Path;
64-
openReadOnly = args.ReadOnly;
65-
return args.Result;
64+
return new BrowseFileResult()
65+
{
66+
Success = args.Result,
67+
Path = args.Path,
68+
OpenReadOnly = args.ReadOnly
69+
};
6670
}
6771

68-
protected override bool BrowseSaveFile(out string path)
72+
protected override async ValueTask<BrowseFileResult> BrowseSaveFile()
6973
{
7074
var args = new BrowseEventArgs();
7175
BrowsingSave?.Invoke(args);
72-
path = args.Path;
73-
return args.Result;
76+
return new BrowseFileResult()
77+
{
78+
Success = args.Result,
79+
Path = args.Path
80+
};
7481
}
7582

7683
internal override async Task<bool> SaveAsInternal(PackageManagerViewModelPackage package)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace DtronixPackage.ViewModel;
2+
3+
public class BrowseFileResult
4+
{
5+
/// <summary>
6+
/// True on successful open/selection. If false, cancel the process
7+
/// </summary>
8+
public bool Success { get; set; }
9+
10+
/// <summary>
11+
/// The path to the file
12+
/// </summary>
13+
public string Path { get; set; }
14+
15+
/// <summary>
16+
/// Set to true to open the package in a read-only state; False to open normally.
17+
/// Only used with BrowseOpenFile
18+
/// </summary>
19+
public bool OpenReadOnly { get; set; }
20+
}

src/DtronixPackage/ViewModel/PackageManagerViewModel.cs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -117,22 +117,18 @@ protected PackageManagerViewModel(string appName)
117117

118118
protected abstract void InvokeOnDispatcher(Action action);
119119

120+
120121
/// <summary>
121-
/// Called when browsing for a package to open. Normally paired with OpenFileDialog
122+
/// Called when browsing for a package to open.
122123
/// </summary>
123-
/// <remarks>Normally paired with SaveFileDialog</remarks>
124-
/// <param name="path">Path of the package to open. Must exist.</param>
125-
/// <param name="openReadOnly">Set to true to open the package in a read-only state; False to open normally.</param>
126-
/// <returns>True on successful opening of package. False to cancel the opening process.</returns>
127-
protected abstract Task<bool> BrowseOpenFile(out string path, out bool openReadOnly);
124+
/// <returns>Result of the open process. Success will be set to false if the opening process should be canceled.</returns>
125+
protected abstract ValueTask<BrowseFileResult> BrowseOpenFile();
128126

129127
/// <summary>
130128
/// Called when browsing for destination to save a package.
131129
/// </summary>
132-
/// <remarks>Normally paired with SaveFileDialog</remarks>
133-
/// <param name="path">Destination path for the package to save.</param>
134-
/// <returns>True on successful selection of package destination. False to cancel the saving process.</returns>
135-
protected abstract Task<bool> BrowseSaveFile(out string path);
130+
/// <returns>Result of the open process. Success will be set to false if the saving process should be canceled.</returns>
131+
protected abstract ValueTask<BrowseFileResult> BrowseSaveFile();
136132

137133
protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
138134
{
@@ -268,12 +264,12 @@ public virtual async Task<bool> Open()
268264
if (!await TryClose())
269265
return false;
270266

271-
var result = await BrowseOpenFile(out var path, out var readOnly);
267+
var result = await BrowseOpenFile();
272268

273-
if (result != true)
269+
if (result.Success != true)
274270
return false;
275271

276-
return await Open(path, readOnly);
272+
return await Open(result.Path, result.OpenReadOnly);
277273
}
278274

279275
public virtual async Task<bool> Open(string path, bool forceReadOnly)
@@ -434,11 +430,11 @@ internal virtual async Task<bool> SaveAsInternal(TPackage? package)
434430
if (package == null)
435431
return false;
436432

437-
var browseResult = await BrowseSaveFile(out var path);
433+
var browseResult = await BrowseSaveFile();
438434

439-
if (browseResult)
435+
if (browseResult.Success)
440436
{
441-
var result = await package.Save(path);
437+
var result = await package.Save(browseResult.Path);
442438

443439
switch (result.SaveResult)
444440
{

0 commit comments

Comments
 (0)