Skip to content

Commit

Permalink
Use StorageProvider API
Browse files Browse the repository at this point in the history
(instead of obselete APIs)
  • Loading branch information
Lauriethefish committed Oct 26, 2023
1 parent b96e380 commit 25c521b
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 68 deletions.
77 changes: 16 additions & 61 deletions QuestPatcher/BrowseImportManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using System.Threading.Tasks;
using Avalonia.Controls;
using Avalonia.Platform.Storage;
using QuestPatcher.Core;
using QuestPatcher.Core.Modding;
using QuestPatcher.Models;
Expand Down Expand Up @@ -33,7 +34,10 @@ private struct FileImportInfo
private readonly OperationLocker _locker;
private readonly QuestPatcherUiService _uiService;

private readonly FileDialogFilter _modsFilter = new();
private readonly FilePickerFileType _modsFilter = new("Quest Mods")
{
Patterns = new List<string>() { "*.qmod" }
};

private Queue<FileImportInfo>? _currentImportQueue;

Expand All @@ -45,56 +49,14 @@ public BrowseImportManager(OtherFilesManager otherFilesManager, ModManager modMa
_installManager = installManager;
_locker = locker;
_uiService = uiService;

_modsFilter.Name = "Quest Mods";
_modsFilter.Extensions.Add("qmod");
}

private static FileDialogFilter GetCosmeticFilter(FileCopyType copyType)
{
return new FileDialogFilter
{
Name = copyType.NamePlural,
Extensions = copyType.SupportedExtensions
};
}

private void AddAllCosmeticFilters(OpenFileDialog dialog)
{
foreach (var copyType in _otherFilesManager.CurrentDestinations)
{
dialog.Filters.Add(GetCosmeticFilter(copyType));
}
}

/// <summary>
/// Opens a browse dialog that has filters for all files supported by QuestPatcher.
/// This includes qmod and all other file copies.
/// </summary>
/// <returns>A task that completes when the dialog has closed and the files have been imported</returns>
public async Task ShowAllItemsBrowse()
private static FilePickerFileType GetCosmeticFilter(FileCopyType copyType)
{
var dialog = ConstructDialog();

// Add a filter for any file type that QuestPatcher supports
// This includes qmod and all cosmetic/file copy types.
FileDialogFilter allFiles = new()
return new FilePickerFileType(copyType.NamePlural)
{
Name = "All Allowed Files"
Patterns = copyType.SupportedExtensions.Select(extension => $"*.{extension}").ToList()
};

var allExtensions = allFiles.Extensions;
allExtensions.Add("qmod");
foreach (var copyType in _otherFilesManager.CurrentDestinations)
{
allExtensions.AddRange(copyType.SupportedExtensions);
}

dialog.Filters.Add(allFiles);
dialog.Filters.Add(_modsFilter);
AddAllCosmeticFilters(dialog);

await ShowDialogAndHandleResult(dialog);
}

/// <summary>
Expand All @@ -103,9 +65,7 @@ public async Task ShowAllItemsBrowse()
/// <returns>A task that completes when the dialog has closed and the files have been imported</returns>
public async Task ShowModsBrowse()
{
var dialog = ConstructDialog();
dialog.Filters.Add(_modsFilter);
await ShowDialogAndHandleResult(dialog);
await ShowDialogAndHandleResult(new() { _modsFilter });
}

/// <summary>
Expand All @@ -115,28 +75,23 @@ public async Task ShowModsBrowse()
/// <returns>A task that completes when the dialog has closed and the files have been imported</returns>
public async Task ShowFileCopyBrowse(FileCopyType cosmeticType)
{
var dialog = ConstructDialog();
dialog.Filters.Add(GetCosmeticFilter(cosmeticType));
await ShowDialogAndHandleResult(dialog, cosmeticType);
await ShowDialogAndHandleResult(new() { GetCosmeticFilter(cosmeticType) }, cosmeticType);
}

private static OpenFileDialog ConstructDialog()
private async Task ShowDialogAndHandleResult(List<FilePickerFileType> filters, FileCopyType? knownFileCopyType = null)
{
return new OpenFileDialog()
var files = await _mainWindow.StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions
{
AllowMultiple = true
};
}
AllowMultiple = true,
FileTypeFilter = filters
});

private async Task ShowDialogAndHandleResult(OpenFileDialog dialog, FileCopyType? knownFileCopyType = null)
{
string[] files = await dialog.ShowAsync(_mainWindow);
if (files == null)
{
return;
}

await AttemptImportFiles(files, knownFileCopyType);
await AttemptImportFiles(files.Select(file => file.Path.LocalPath).ToList(), knownFileCopyType);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion QuestPatcher/FileNameConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class FileNameConverter : IValueConverter
{
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if(value == null)
if (value == null)
{
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion QuestPatcher/ViewLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class ViewLocator : IDataTemplate

public Control? Build(object? data)
{
if(data == null)
if (data == null)
{
return null;
}
Expand Down
6 changes: 3 additions & 3 deletions QuestPatcher/ViewModels/LoadedViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,15 @@ public async void OnDragAndDrop(object? sender, DragEventArgs args)
// We need to handle this to avoid crashing QuestPatcher.
try
{
var fileNames = args.Data.GetFileNames();
if (fileNames == null) // Non-file items dragged
var files = args.Data.GetFiles();
if (files == null) // Non-file items dragged
{
Log.Debug("Drag and drop contained no file names");
return;
}

Log.Debug("Files found in drag and drop. Processing . . .");
await _browseManager.AttemptImportFiles(fileNames.ToList(), OtherItemsView.SelectedFileCopy);
await _browseManager.AttemptImportFiles(files.Select(file => file.Path.LocalPath).ToList(), OtherItemsView.SelectedFileCopy);
}
catch (COMException)
{
Expand Down
3 changes: 1 addition & 2 deletions QuestPatcher/Views/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using Avalonia;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Markup.Xaml;
Expand Down

0 comments on commit 25c521b

Please sign in to comment.