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

Integration Features #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -56,21 +57,27 @@
e.Package.Opening = PackageOpening;
}

protected override bool BrowseOpenFile(out string path, out bool openReadOnly)
protected override async ValueTask<BrowseFileResult> BrowseOpenFile()

Check warning on line 60 in src/DtronixPackage.Tests/PackageManagerViewModelTests/TestPackageManagerViewModel.cs

View workflow job for this annotation

GitHub Actions / build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
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<BrowseFileResult> BrowseSaveFile()

Check warning on line 72 in src/DtronixPackage.Tests/PackageManagerViewModelTests/TestPackageManagerViewModel.cs

View workflow job for this annotation

GitHub Actions / build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
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<bool> SaveAsInternal(PackageManagerViewModelPackage package)
Expand Down
20 changes: 20 additions & 0 deletions src/DtronixPackage/ViewModel/BrowseFileResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace DtronixPackage.ViewModel;

public class BrowseFileResult
{
/// <summary>
/// True on successful open/selection. If false, cancel the process
/// </summary>
public bool Success { get; set; }

/// <summary>
/// The path to the file
/// </summary>
public string Path { get; set; }

Check warning on line 13 in src/DtronixPackage/ViewModel/BrowseFileResult.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Path' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

/// <summary>
/// Set to true to open the package in a read-only state; False to open normally.
/// Only used with BrowseOpenFile
/// </summary>
public bool OpenReadOnly { get; set; }
}
30 changes: 14 additions & 16 deletions src/DtronixPackage/ViewModel/PackageManagerViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,22 +117,18 @@ protected PackageManagerViewModel(string appName)

protected abstract void InvokeOnDispatcher(Action action);


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

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

protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
{
Expand Down Expand Up @@ -268,12 +264,12 @@ public virtual async Task<bool> 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<bool> Open(string path, bool forceReadOnly)
Expand Down Expand Up @@ -434,9 +430,11 @@ internal virtual async Task<bool> 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)
{
Expand Down Expand Up @@ -513,4 +511,4 @@ private void PackageOnMonitoredChanged(object? sender, EventArgs e)
SaveCommand.SetCanExecute(Package?.IsContentModified == true);
});
}
}
}
Loading