Skip to content

Commit

Permalink
Move DialogStorageFile/Folder into shared library
Browse files Browse the repository at this point in the history
  • Loading branch information
mysteryx93 committed Feb 16, 2023
1 parent 9523d97 commit faa9f86
Show file tree
Hide file tree
Showing 13 changed files with 44 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,24 @@
using HanumanInstitute.MvvmDialogs.Avalonia;
using Ookii.Dialogs.WinForms;
using Avalonia.Controls;
using System.Collections.Generic;
using HanumanInstitute.MvvmDialogs.FileSystem;
using HanumanInstitute.MvvmDialogs.PathInfo;

namespace Demo.Avalonia.CustomOpenFolderDialog;

public class CustomDialogFactory : DialogFactoryBase
{
private readonly IPathInfoFactory _infoFactory;

/// <summary>
/// Initializes a new instance of a FrameworkDialog.
/// </summary>
/// <param name="chain">If the dialog is not handled by this class, calls this other handler next.</param>
public CustomDialogFactory(IDialogFactory? chain = null)
public CustomDialogFactory(IPathInfoFactory infoFactory, IDialogFactory? chain = null)
: base(chain)
{
_infoFactory = infoFactory;
}

/// <inheritdoc />
Expand All @@ -28,7 +34,7 @@ public CustomDialogFactory(IDialogFactory? chain = null)
_ => base.ShowDialogAsync(owner, settings, appSettings)
};

private async Task<string?> ShowOpenFolderDialogAsync(IView? owner, OpenFolderDialogSettings settings, AppDialogSettingsBase appSettings)
private async Task<IReadOnlyList<IDialogStorageFolder>> ShowOpenFolderDialogAsync(IView? owner, OpenFolderDialogSettings settings, AppDialogSettingsBase appSettings)
{
if (owner == null) throw new ArgumentNullException(nameof(owner));

Expand All @@ -48,6 +54,7 @@ public CustomDialogFactory(IDialogFactory? chain = null)
};
var result = await UiExtensions.RunUiAsync(() => dialog.ShowDialog(handle));

return result == true ? dialog.SelectedPath : null;
return result == true ? new IDialogStorageFolder[] { new DialogStorageFolder(_infoFactory.GetDirectoryInfo(dialog.SelectedPath!)) } :
Array.Empty<IDialogStorageFolder>();
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@

using HanumanInstitute.MvvmDialogs;
using HanumanInstitute.MvvmDialogs.PathInfo;

namespace Demo.Avalonia.CustomOpenFolderDialog
{
public static class DialogFactoryExtensions
{
public static IDialogFactory AddCustomOpenFolder(this IDialogFactory factory) => new CustomDialogFactory(factory);
public static IDialogFactory AddCustomOpenFolder(this IDialogFactory factory) => new CustomDialogFactory(new PathInfoFactory(), factory);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ private async Task OpenFolderAsync()
};

var result = await _dialogService.ShowOpenFolderDialogAsync(this, settings);
Path = result?.Path?.ToString();
Path = result?.LocalPath;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Demo.Wpf.CustomOpenFolderDialog;

public class CustomDialogFactory : DialogFactoryBase
{
private IPathInfoFactory _infoFactory;
private readonly IPathInfoFactory _infoFactory;

/// <summary>
/// Initializes a new instance of a FrameworkDialog.
Expand Down
6 changes: 3 additions & 3 deletions src/MvvmDialogs.Avalonia/Api/FrameworkDialogsApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ public async Task<IReadOnlyList<IDialogStorageFile>> ShowOpenFileDialogAsync(Con
{
if (owner == null) { throw new ArgumentNullException(nameof(owner)); }
var result = await GetStorage(owner).OpenFilePickerAsync(options).ConfigureAwait(true);
return result.Select(x => new DialogStorageFile(x)).ToList();
return result.Select(x => new AvaloniaDialogStorageFile(x)).ToList();
}

public async Task<IDialogStorageFile?> ShowSaveFileDialogAsync(ContentControl? owner, FilePickerSaveOptions options)
{
if (owner == null) { throw new ArgumentNullException(nameof(owner)); }
var result = await GetStorage(owner).SaveFilePickerAsync(options).ConfigureAwait(true);
return result != null ? new DialogStorageFile(result) : null;
return result != null ? new AvaloniaDialogStorageFile(result) : null;
}

public async Task<IReadOnlyList<IDialogStorageFolder>> ShowOpenFolderDialogAsync(ContentControl? owner, FolderPickerOpenOptions options)
{
if (owner == null) { throw new ArgumentNullException(nameof(owner)); }
var result = await GetStorage(owner).OpenFolderPickerAsync(options).ConfigureAwait(true);
return result.Select(x => new DialogStorageFolder(x)).ToList();
return result.Select(x => new AvaloniaDialogStorageFolder(x)).ToList();
}

private static IStorageProvider GetStorage(ContentControl owner) => TopLevel.GetTopLevel(owner)?.StorageProvider ??
Expand Down
10 changes: 5 additions & 5 deletions src/MvvmDialogs.Avalonia/BookmarkFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,23 @@ public class BookmarkFileSystem : IBookmarkFileSystem
if (storage != null)
{
var result = await storage.OpenFileBookmarkAsync(bookmark).ConfigureAwait(false);
return result != null ? new DialogStorageFile(result) : null;
return result != null ? new AvaloniaDialogStorageFile(result) : null;
}
return null;
}

/// <inheritdoc />
public async Task<IDialogStorageFolder?> OpenFolderBookmarkAsync(string bookmark)
{
var storage = GetStorageProvider();
if (storage != null)
{
var result = await storage.OpenFolderBookmarkAsync(bookmark).ConfigureAwait(false);
return result != null ? new DialogStorageFolder(result) : null;
return result != null ? new AvaloniaDialogStorageFolder(result) : null;
}
return null;
}

/// <inheritdoc />
public async Task ReleaseFileBookmarkAsync(string bookmark)
{
Expand All @@ -55,7 +55,7 @@ public async Task ReleaseFileBookmarkAsync(string bookmark)
}
}
}

/// <inheritdoc />
public async Task ReleaseFolderBookmarkAsync(string bookmark)
{
Expand Down
6 changes: 3 additions & 3 deletions src/MvvmDialogs.Avalonia/DialogStorageFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
namespace HanumanInstitute.MvvmDialogs.Avalonia;

/// <inheritdoc cref="IDialogStorageFile"/>
public class DialogStorageFile : DialogStorageItem, IDialogStorageFile
public class AvaloniaDialogStorageFile : AvaloniaDialogStorageItem, IDialogStorageFile
{
private readonly IStorageFile _item;

/// <summary>
/// Initializes a new instance of DialogStorageFile as a bridge to specified Avalonia IStorageFile.
/// </summary>
/// <param name="item">An Avalonia IStorageFile from which to get the values.</param>
public DialogStorageFile(IStorageFile item) : base(item)
public AvaloniaDialogStorageFile(IStorageFile item) : base(item)
{
_item = item;
}
Expand Down
9 changes: 5 additions & 4 deletions src/MvvmDialogs.Avalonia/DialogStorageFolder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@
using System.Linq;
using Avalonia.Platform.Storage;
using HanumanInstitute.MvvmDialogs.FileSystem;
using HanumanInstitute.MvvmDialogs.PathInfo;

namespace HanumanInstitute.MvvmDialogs.Avalonia;

/// <inheritdoc cref="IDialogStorageFolder"/>
public class DialogStorageFolder : DialogStorageItem, IDialogStorageFolder
public class AvaloniaDialogStorageFolder : AvaloniaDialogStorageItem, IDialogStorageFolder
{
private readonly IStorageFolder _item;

/// <summary>
/// Initializes a new instance of DialogStorageFolder as a bridge to specified Avalonia IStorageFolder.
/// </summary>
/// <param name="item">An Avalonia IStorageFolder from which to get the values.</param>
public DialogStorageFolder(IStorageFolder item) : base(item)
public AvaloniaDialogStorageFolder(IStorageFolder item) : base(item)
{
_item = item;
}
Expand All @@ -23,6 +24,6 @@ public DialogStorageFolder(IStorageFolder item) : base(item)
public async Task<IEnumerable<IDialogStorageItem>> GetItemsAsync()
{
var list = await _item.GetItemsAsync();
return list.Select(x => x is IStorageFile f ? (IDialogStorageItem)new DialogStorageFile(f) : new DialogStorageFolder((IStorageFolder)x));
return list.Select(x => x is IStorageFile f ? (IDialogStorageItem)new AvaloniaDialogStorageFile(f) : new AvaloniaDialogStorageFolder((IStorageFolder)x));
}
}
6 changes: 3 additions & 3 deletions src/MvvmDialogs.Avalonia/DialogStorageItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
namespace HanumanInstitute.MvvmDialogs.Avalonia;

/// <inheritdoc />
public class DialogStorageItem : IDialogStorageItem
public class AvaloniaDialogStorageItem : IDialogStorageItem
{
private readonly IStorageItem _item;

/// <summary>
/// Initializes a new instance of DialogStorageItem as a bridge to specified Avalonia IStorageItem.
/// </summary>
/// <param name="item">An Avalonia IStorageItem from which to get the values.</param>
public DialogStorageItem(IStorageItem item)
public AvaloniaDialogStorageItem(IStorageItem item)
{
_item = item;
}
Expand Down Expand Up @@ -43,7 +43,7 @@ public async Task<DialogStorageItemProperties> GetBasicPropertiesAsync()
public async Task<IDialogStorageFolder?> GetParentAsync()
{
var result = await _item.GetParentAsync().ConfigureAwait(true);
return result != null ? new DialogStorageFolder(result) : null;
return result != null ? new AvaloniaDialogStorageFolder(result) : null;
}

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
using HanumanInstitute.MvvmDialogs.FileSystem;
using HanumanInstitute.MvvmDialogs.PathInfo;

namespace HanumanInstitute.MvvmDialogs.Wpf;
namespace HanumanInstitute.MvvmDialogs.FileSystem;

/// <inheritdoc cref="IDialogStorageFile"/>
public class DialogStorageFile : DialogStorageItem, IDialogStorageFile
{
private readonly IFileInfo _info;

/// <summary>
/// Initializes a new instance of DialogStorageFile to expose a path.
/// </summary>
Expand All @@ -17,7 +17,7 @@ public DialogStorageFile(IFileInfo info) : base(info)
{
_info = (IFileInfo)InfoBase;
}

/// <inheritdoc />
public Task<Stream> OpenReadAsync() => Task.Run<Stream>(() => File.OpenRead(_info.FullName));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using System.IO;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using HanumanInstitute.MvvmDialogs.FileSystem;
using HanumanInstitute.MvvmDialogs.PathInfo;

namespace HanumanInstitute.MvvmDialogs.Wpf;
namespace HanumanInstitute.MvvmDialogs.FileSystem;

/// <inheritdoc cref="IDialogStorageFolder"/>
public class DialogStorageFolder : DialogStorageItem, IDialogStorageFolder
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using HanumanInstitute.MvvmDialogs.FileSystem;
using HanumanInstitute.MvvmDialogs.PathInfo;

namespace HanumanInstitute.MvvmDialogs.Wpf;
namespace HanumanInstitute.MvvmDialogs.FileSystem;

/// <inheritdoc />
public abstract class DialogStorageItem : IDialogStorageItem
Expand Down
8 changes: 4 additions & 4 deletions src/MvvmDialogs/PathInfo/DirectoryInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ public DirectoryInfo(string path) =>
/// <param name="pathInfo">The System.IO.DirectoryInfo to wrap onto.</param>
public DirectoryInfo(System.IO.DirectoryInfo pathInfo) =>
_info = pathInfo;

/// <inheritdoc />
public string Name => _info.Name;

/// <inheritdoc />
public string FullName => _info.FullName;

/// <inheritdoc />
public IDirectoryInfo? Parent => _info.Parent != null ? new DirectoryInfo(_info.Parent) : null;

/// <inheritdoc />
public bool Exists => _info.Exists;

Expand All @@ -46,7 +46,7 @@ public DirectoryInfo(System.IO.DirectoryInfo pathInfo) =>

/// <inheritdoc />
public IEnumerable<IPathInfo> EnumeratePathInfos(string searchPattern) => EnumeratePathInfos(searchPattern, SearchOption.TopDirectoryOnly);

/// <inheritdoc />
public IEnumerable<IPathInfo> EnumeratePathInfos(string searchPattern, SearchOption searchOption) => _info.EnumerateFileSystemInfos(searchPattern, searchOption)
.Select<FileSystemInfo, IPathInfo>(x => x is System.IO.FileInfo f ? new FileInfo(f) : new DirectoryInfo((System.IO.DirectoryInfo)x));
Expand Down

0 comments on commit faa9f86

Please sign in to comment.