using Lantean.QBitTorrentClient; using Lantean.QBTMudBlade.Components.Dialogs; using Lantean.QBTMudBlade.Filter; using Lantean.QBTMudBlade.Models; using MudBlazor; namespace Lantean.QBTMudBlade { public static class DialogHelper { public static readonly DialogOptions FormDialogOptions = new() { CloseButton = true, MaxWidth = MaxWidth.Medium, ClassBackground = "background-blur" }; private static readonly DialogOptions _confirmDialogOptions = new() { ClassBackground = "background-blur" }; public static async Task InvokeAddTorrentFileDialog(this IDialogService dialogService, IApiClient apiClient) { var result = await dialogService.ShowAsync("Upload local torrent", FormDialogOptions); var dialogResult = await result.Result; if (dialogResult.Canceled) { return; } var options = (AddTorrentFileOptions)dialogResult.Data; var streams = new List(); var files = new Dictionary(); foreach (var file in options.Files) { var stream = file.OpenReadStream(); streams.Add(stream); files.Add(file.Name, stream); } await apiClient.AddTorrent( urls: null, files, options.SavePath, options.Cookie, options.Category, tags: null, options.SkipHashCheck, !options.StartTorrent, options.ContentLayout, options.RenameTorrent, options.UploadLimit, options.DownloadLimit, ratioLimit: null, seedingTimeLimit: null, options.TorrentManagementMode, options.DownloadInSequentialOrder, options.DownloadFirstAndLastPiecesFirst); foreach (var stream in streams) { await stream.DisposeAsync(); } } public static async Task InvokeAddTorrentLinkDialog(this IDialogService dialogService, IApiClient apiClient) { var result = await dialogService.ShowAsync("Download from URLs", FormDialogOptions); var dialogResult = await result.Result; if (dialogResult.Canceled) { return; } var options = (AddTorrentLinkOptions)dialogResult.Data; await apiClient.AddTorrent( urls: options.Urls, torrents: null, options.SavePath, options.Cookie, options.Category, tags: null, options.SkipHashCheck, !options.StartTorrent, options.ContentLayout, options.RenameTorrent, options.UploadLimit, options.DownloadLimit, ratioLimit: null, seedingTimeLimit: null, options.TorrentManagementMode, options.DownloadInSequentialOrder, options.DownloadFirstAndLastPiecesFirst); } public static async Task InvokeDeleteTorrentDialog(this IDialogService dialogService, IApiClient apiClient, params string[] hashes) { var reference = await dialogService.ShowAsync($"Remove torrent{(hashes.Length == 1 ? "" : "s")}?"); var result = await reference.Result; if (result.Canceled) { return; } await apiClient.DeleteTorrents(hashes, (bool)result.Data); } public static async Task InvokeRenameFilesDialog(this IDialogService dialogService, IApiClient apiClient, string hash) { await Task.Delay(0); } public static async Task InvokeAddCategoryDialog(this IDialogService dialogService, IApiClient apiClient, IEnumerable? hashes = null) { var reference = await dialogService.ShowAsync("New Category"); var result = await reference.Result; if (result.Canceled) { return; } var category = (Category)result.Data; await apiClient.AddCategory(category.Name, category.SavePath); if (hashes is not null) { await apiClient.SetTorrentCategory(category.Name, null, hashes.ToArray()); } } public static async Task ShowConfirmDialog(this IDialogService dialogService, string title, string content, Func onSuccess) { var parameters = new DialogParameters { { nameof(ConfirmDialog.Content), content } }; var result = await dialogService.ShowAsync(title, parameters, _confirmDialogOptions); var dialogResult = await result.Result; if (dialogResult.Canceled) { return; } await onSuccess(); } public static async Task ShowConfirmDialog(this IDialogService dialogService, string title, string content, Action onSuccess) { await ShowConfirmDialog(dialogService, title, content, () => { onSuccess(); return Task.CompletedTask; }); } public static async Task ShowSingleFieldDialog(this IDialogService dialogService, string title, string label, T? value, Func onSuccess) { var parameters = new DialogParameters { { nameof(SingleFieldDialog.Label), label }, { nameof(SingleFieldDialog.Value), value } }; var result = await dialogService.ShowAsync>(title, parameters, _confirmDialogOptions); var dialogResult = await result.Result; if (dialogResult.Canceled) { return; } await onSuccess((T)dialogResult.Data); } public static async Task InvokeUploadRateDialog(this IDialogService dialogService, IApiClient apiClient, long rate, IEnumerable hashes) { var parameters = new DialogParameters { { nameof(SliderFieldDialog.Value), rate }, { nameof(SliderFieldDialog.Min), 0 }, { nameof(SliderFieldDialog.Max), 100 }, }; var result = await dialogService.ShowAsync>("Upload Rate", parameters, FormDialogOptions); var dialogResult = await result.Result; if (dialogResult.Canceled) { return; } await apiClient.SetTorrentUploadLimit((long)dialogResult.Data, null, hashes.ToArray()); } public static async Task InvokeShareRatioDialog(this IDialogService dialogService, IApiClient apiClient, float ratio, IEnumerable hashes) { var parameters = new DialogParameters { { nameof(SliderFieldDialog.Value), ratio }, { nameof(SliderFieldDialog.Min), 0 }, { nameof(SliderFieldDialog.Max), 100 }, }; var result = await dialogService.ShowAsync>("Upload Rate", parameters, FormDialogOptions); var dialogResult = await result.Result; if (dialogResult.Canceled) { return; } await apiClient.SetTorrentShareLimit((float)dialogResult.Data, 0, null, hashes.ToArray()); } public static async Task>?> ShowFilterOptionsDialog(this IDialogService dialogService, List>? propertyFilterDefinitions) { var parameters = new DialogParameters { { nameof(FilterOptionsDialog.FilterDefinitions), propertyFilterDefinitions }, }; var result = await dialogService.ShowAsync>("Filters", parameters, FormDialogOptions); var dialogResult = await result.Result; if (dialogResult.Canceled) { return null; } return (List>?)dialogResult.Data; } } }