using Microsoft.UI.Xaml; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using VDownload.Services.Common; using Windows.Storage; using Windows.Storage.Pickers; using WinRT.Interop; namespace VDownload.Services.UI.StoragePicker { public interface IStoragePickerService : IInitializableService { Task OpenDirectory(); Task OpenDirectory(StoragePickerStartLocation startLocation); Task> OpenMultipleFiles(); Task> OpenMultipleFiles(StoragePickerStartLocation startLocation); Task> OpenMultipleFiles(string[] fileTypes); Task> OpenMultipleFiles(string[] fileTypes, StoragePickerStartLocation startLocation); Task OpenSingleFile(); Task OpenSingleFile(StoragePickerStartLocation startLocation); Task OpenSingleFile(string[] fileTypes); Task OpenSingleFile(string[] fileTypes, StoragePickerStartLocation startLocation); Task SaveFile(FileSavePickerFileTypeChoice[] fileTypes, string defaultFileType); Task SaveFile(FileSavePickerFileTypeChoice[] fileTypes, string defaultFileType, StoragePickerStartLocation startLocation); } public class StoragePickerService : IStoragePickerService { #region FIELDS protected Window _root; #endregion #region PUBLIC METHODS public async Task Initialize(Window arg) => await Task.Run(() => _root = arg); public async Task OpenDirectory() => await OpenDirectory(StoragePickerStartLocation.Unspecified); public async Task OpenDirectory(StoragePickerStartLocation startLocation) { FolderPicker picker = new FolderPicker(); InitializePicker(picker); ConfigureFolderPicker(picker, startLocation); StorageFolder directory = await picker.PickSingleFolderAsync(); return directory?.Path; } public async Task OpenSingleFile() => await OpenSingleFile(["*"], StoragePickerStartLocation.Unspecified); public async Task OpenSingleFile(string[] fileTypes) => await OpenSingleFile(fileTypes, StoragePickerStartLocation.Unspecified); public async Task OpenSingleFile(StoragePickerStartLocation startLocation) => await OpenSingleFile(["*"], startLocation); public async Task OpenSingleFile(string[] fileTypes, StoragePickerStartLocation startLocation) { FileOpenPicker picker = new FileOpenPicker(); InitializePicker(picker); ConfigureFileOpenPicker(picker, fileTypes, startLocation); StorageFile storageFile = await picker.PickSingleFileAsync(); return storageFile?.Path; } public async Task> OpenMultipleFiles() => await OpenMultipleFiles(["*"], StoragePickerStartLocation.Unspecified); public async Task> OpenMultipleFiles(string[] fileTypes) => await OpenMultipleFiles(fileTypes, StoragePickerStartLocation.Unspecified); public async Task> OpenMultipleFiles(StoragePickerStartLocation startLocation) => await OpenMultipleFiles(["*"], startLocation); public async Task> OpenMultipleFiles(string[] fileTypes, StoragePickerStartLocation startLocation) { FileOpenPicker picker = new FileOpenPicker(); InitializePicker(picker); ConfigureFileOpenPicker(picker, fileTypes, startLocation); IEnumerable list = await picker.PickMultipleFilesAsync(); return list.Select(x => x.Path); } public async Task SaveFile(FileSavePickerFileTypeChoice[] fileTypes, string defaultFileType) => await SaveFile(fileTypes, defaultFileType, StoragePickerStartLocation.Unspecified); public async Task SaveFile(FileSavePickerFileTypeChoice[] fileTypes, string defaultFileType, StoragePickerStartLocation startLocation) { FileSavePicker picker = new FileSavePicker(); InitializePicker(picker); ConfigureFileSavePicker(picker, fileTypes, defaultFileType, startLocation); StorageFile file = await picker.PickSaveFileAsync(); return file?.Path; } #endregion #region PRIVATE METHODS protected void InitializePicker(object picker) { var hwnd = WindowNative.GetWindowHandle(_root); InitializeWithWindow.Initialize(picker, hwnd); } protected void ConfigureFolderPicker(FolderPicker picker, StoragePickerStartLocation startLocation) { if (startLocation != StoragePickerStartLocation.Unspecified) { picker.SuggestedStartLocation = (PickerLocationId)startLocation; } } protected void ConfigureFileOpenPicker(FileOpenPicker picker, string[] fileTypes, StoragePickerStartLocation startLocation) { foreach (string fileType in fileTypes) { picker.FileTypeFilter.Add(fileType); } if (startLocation != StoragePickerStartLocation.Unspecified) { picker.SuggestedStartLocation = (PickerLocationId)startLocation; } } protected void ConfigureFileSavePicker(FileSavePicker picker, FileSavePickerFileTypeChoice[] fileTypes, string defaultFileType, StoragePickerStartLocation startLocation) { if (startLocation != StoragePickerStartLocation.Unspecified) { picker.SuggestedStartLocation = (PickerLocationId)startLocation; } foreach (FileSavePickerFileTypeChoice fileType in fileTypes) { picker.FileTypeChoices.Add(fileType.Description, fileType.Extensions.ToList()); } if (!defaultFileType.StartsWith('.')) { defaultFileType = $".{defaultFileType}"; } if (!fileTypes.Any(x => x.Extensions.Contains(defaultFileType))) { picker.FileTypeChoices.Add("Default", [defaultFileType]); } picker.DefaultFileExtension = defaultFileType; } #endregion } }