diff --git a/app/.idea/.idea.DiscordHistoryTracker/.idea/avalonia.xml b/app/.idea/.idea.DiscordHistoryTracker/.idea/avalonia.xml index bc1f5bf..0a0dd66 100644 --- a/app/.idea/.idea.DiscordHistoryTracker/.idea/avalonia.xml +++ b/app/.idea/.idea.DiscordHistoryTracker/.idea/avalonia.xml @@ -9,15 +9,15 @@ - + - + @@ -25,4 +25,4 @@ - \ No newline at end of file + diff --git a/app/Desktop/Common/BytesValueConverter.cs b/app/Desktop/Common/BytesValueConverter.cs index 4769858..3a0e2ca 100644 --- a/app/Desktop/Common/BytesValueConverter.cs +++ b/app/Desktop/Common/BytesValueConverter.cs @@ -29,7 +29,7 @@ sealed class BytesValueConverter : IValueConverter { private const int Scale = 1000; - private static string Convert(ulong size) { + public static string Convert(ulong size) { int power = size == 0L ? 0 : (int) Math.Log(size, Scale); int unit = power >= Units.Length ? Units.Length - 1 : power; return Units[unit].Format(unit == 0 ? size : size / Math.Pow(Scale, unit)); diff --git a/app/Desktop/Desktop.csproj b/app/Desktop/Desktop.csproj index ec51f39..374bcec 100644 --- a/app/Desktop/Desktop.csproj +++ b/app/Desktop/Desktop.csproj @@ -32,10 +32,6 @@ - - CheckBoxDialog.axaml - Code - diff --git a/app/Desktop/Main/Controls/AttachmentFilterPanel.axaml b/app/Desktop/Main/Controls/DownloadItemFilterPanel.axaml similarity index 91% rename from app/Desktop/Main/Controls/AttachmentFilterPanel.axaml rename to app/Desktop/Main/Controls/DownloadItemFilterPanel.axaml index 9a9f489..9972608 100644 --- a/app/Desktop/Main/Controls/AttachmentFilterPanel.axaml +++ b/app/Desktop/Main/Controls/DownloadItemFilterPanel.axaml @@ -4,11 +4,11 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:controls="clr-namespace:DHT.Desktop.Main.Controls" mc:Ignorable="d" - x:Class="DHT.Desktop.Main.Controls.AttachmentFilterPanel" - x:DataType="controls:AttachmentFilterPanelModel"> + x:Class="DHT.Desktop.Main.Controls.DownloadItemFilterPanel" + x:DataType="controls:DownloadItemFilterPanelModel"> - + diff --git a/app/Desktop/Main/Controls/AttachmentFilterPanel.axaml.cs b/app/Desktop/Main/Controls/DownloadItemFilterPanel.axaml.cs similarity index 64% rename from app/Desktop/Main/Controls/AttachmentFilterPanel.axaml.cs rename to app/Desktop/Main/Controls/DownloadItemFilterPanel.axaml.cs index 4d577ca..f5c9281 100644 --- a/app/Desktop/Main/Controls/AttachmentFilterPanel.axaml.cs +++ b/app/Desktop/Main/Controls/DownloadItemFilterPanel.axaml.cs @@ -4,8 +4,8 @@ using Avalonia.Controls; namespace DHT.Desktop.Main.Controls; [SuppressMessage("ReSharper", "MemberCanBeInternal")] -public sealed partial class AttachmentFilterPanel : UserControl { - public AttachmentFilterPanel() { +public sealed partial class DownloadItemFilterPanel : UserControl { + public DownloadItemFilterPanel() { InitializeComponent(); } } diff --git a/app/Desktop/Main/Controls/AttachmentFilterPanelModel.cs b/app/Desktop/Main/Controls/DownloadItemFilterPanelModel.cs similarity index 54% rename from app/Desktop/Main/Controls/AttachmentFilterPanelModel.cs rename to app/Desktop/Main/Controls/DownloadItemFilterPanelModel.cs index 2d1928e..2707229 100644 --- a/app/Desktop/Main/Controls/AttachmentFilterPanelModel.cs +++ b/app/Desktop/Main/Controls/DownloadItemFilterPanelModel.cs @@ -12,7 +12,7 @@ using DHT.Utils.Tasks; namespace DHT.Desktop.Main.Controls; -sealed partial class AttachmentFilterPanelModel : ObservableObject, IDisposable { +sealed partial class DownloadItemFilterPanelModel : ObservableObject, IDisposable { public sealed record Unit(string Name, uint Scale); private static readonly Unit[] AllUnits = [ @@ -43,21 +43,21 @@ sealed partial class AttachmentFilterPanelModel : ObservableObject, IDisposable private readonly State state; private readonly string verb; - private readonly RestartableTask matchingAttachmentCountTask; - private long? matchingAttachmentCount; + private readonly RestartableTask downloadItemCountTask; + private long? matchingItemCount; - private readonly IDisposable attachmentCountSubscription; - private long? totalAttachmentCount; + private readonly IDisposable downloadItemCountSubscription; + private long? totalItemCount; [Obsolete("Designer")] - public AttachmentFilterPanelModel() : this(State.Dummy) {} + public DownloadItemFilterPanelModel() : this(State.Dummy) {} - public AttachmentFilterPanelModel(State state, string verb = "Matches") { + public DownloadItemFilterPanelModel(State state, string verb = "Matches") { this.state = state; this.verb = verb; - this.matchingAttachmentCountTask = new RestartableTask(SetAttachmentCounts, TaskScheduler.FromCurrentSynchronizationContext()); - this.attachmentCountSubscription = state.Db.Attachments.TotalCount.ObserveOn(AvaloniaScheduler.Instance).Subscribe(OnAttachmentCountChanged); + this.downloadItemCountTask = new RestartableTask(SetMatchingCount, TaskScheduler.FromCurrentSynchronizationContext()); + this.downloadItemCountSubscription = state.Db.Downloads.TotalCount.ObserveOn(AvaloniaScheduler.Instance).Subscribe(OnDownloadItemCountChanged); UpdateFilterStatistics(); @@ -65,7 +65,8 @@ sealed partial class AttachmentFilterPanelModel : ObservableObject, IDisposable } public void Dispose() { - attachmentCountSubscription.Dispose(); + downloadItemCountTask.Cancel(); + downloadItemCountSubscription.Dispose(); } private void OnPropertyChanged(object? sender, PropertyChangedEventArgs e) { @@ -74,8 +75,8 @@ sealed partial class AttachmentFilterPanelModel : ObservableObject, IDisposable } } - private void OnAttachmentCountChanged(long newAttachmentCount) { - totalAttachmentCount = newAttachmentCount; + private void OnDownloadItemCountChanged(long newItemCount) { + totalItemCount = newItemCount; UpdateFilterStatistics(); } @@ -83,32 +84,32 @@ sealed partial class AttachmentFilterPanelModel : ObservableObject, IDisposable private void UpdateFilterStatistics() { var filter = CreateFilter(); if (filter.IsEmpty) { - matchingAttachmentCountTask.Cancel(); - matchingAttachmentCount = totalAttachmentCount; + downloadItemCountTask.Cancel(); + matchingItemCount = totalItemCount; UpdateFilterStatisticsText(); } else { - matchingAttachmentCount = null; + matchingItemCount = null; UpdateFilterStatisticsText(); - matchingAttachmentCountTask.Restart(cancellationToken => state.Db.Attachments.Count(filter, cancellationToken)); + downloadItemCountTask.Restart(cancellationToken => state.Db.Downloads.Count(filter, cancellationToken)); } } - private void SetAttachmentCounts(long matchingAttachmentCount) { - this.matchingAttachmentCount = matchingAttachmentCount; + private void SetMatchingCount(long matchingAttachmentCount) { + this.matchingItemCount = matchingAttachmentCount; UpdateFilterStatisticsText(); } private void UpdateFilterStatisticsText() { - var matchingAttachmentCountStr = matchingAttachmentCount?.Format() ?? "(...)"; - var totalAttachmentCountStr = totalAttachmentCount?.Format() ?? "(...)"; + var matchingItemCountStr = matchingItemCount?.Format() ?? "(...)"; + var totalItemCountStr = totalItemCount?.Format() ?? "(...)"; - FilterStatisticsText = verb + " " + matchingAttachmentCountStr + " out of " + totalAttachmentCountStr + " attachment" + (totalAttachmentCount is null or 1 ? "." : "s."); + FilterStatisticsText = verb + " " + matchingItemCountStr + " out of " + totalItemCountStr + " file" + (totalItemCount is null or 1 ? "." : "s."); OnPropertyChanged(nameof(FilterStatisticsText)); } - public AttachmentFilter CreateFilter() { - AttachmentFilter filter = new (); + public DownloadItemFilter CreateFilter() { + DownloadItemFilter filter = new (); if (LimitSize) { try { diff --git a/app/Desktop/Main/Pages/AttachmentsPageModel.cs b/app/Desktop/Main/Pages/AttachmentsPageModel.cs deleted file mode 100644 index 9274c1e..0000000 --- a/app/Desktop/Main/Pages/AttachmentsPageModel.cs +++ /dev/null @@ -1,253 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Collections.ObjectModel; -using System.Reactive.Linq; -using System.Threading.Tasks; -using Avalonia.Controls; -using Avalonia.ReactiveUI; -using CommunityToolkit.Mvvm.ComponentModel; -using DHT.Desktop.Common; -using DHT.Desktop.Dialogs.Message; -using DHT.Desktop.Main.Controls; -using DHT.Server; -using DHT.Server.Data; -using DHT.Server.Data.Aggregations; -using DHT.Server.Data.Filters; -using DHT.Utils.Logging; -using DHT.Utils.Tasks; - -namespace DHT.Desktop.Main.Pages; - -sealed partial class AttachmentsPageModel : ObservableObject, IDisposable { - private static readonly Log Log = Log.ForType(); - - private static readonly DownloadItemFilter EnqueuedItemFilter = new () { - IncludeStatuses = new HashSet { - DownloadStatus.Enqueued, - DownloadStatus.Downloading - } - }; - - [ObservableProperty(Setter = Access.Private)] - private bool isToggleDownloadButtonEnabled = true; - - public string ToggleDownloadButtonText => IsDownloading ? "Stop Downloading" : "Start Downloading"; - - [ObservableProperty(Setter = Access.Private)] - [NotifyPropertyChangedFor(nameof(IsRetryFailedOnDownloadsButtonEnabled))] - private bool isRetryingFailedDownloads = false; - - [ObservableProperty(Setter = Access.Private)] - [NotifyPropertyChangedFor(nameof(IsRetryFailedOnDownloadsButtonEnabled))] - private bool hasFailedDownloads; - - public bool IsRetryFailedOnDownloadsButtonEnabled => !IsRetryingFailedDownloads && HasFailedDownloads; - - [ObservableProperty(Setter = Access.Private)] - private string downloadMessage = ""; - - public double DownloadProgress => totalItemsToDownloadCount is null or 0 ? 0.0 : 100.0 * doneItemsCount / totalItemsToDownloadCount.Value; - - public AttachmentFilterPanelModel FilterModel { get; } - - private readonly StatisticsRow statisticsEnqueued = new ("Enqueued"); - private readonly StatisticsRow statisticsDownloaded = new ("Downloaded"); - private readonly StatisticsRow statisticsFailed = new ("Failed"); - private readonly StatisticsRow statisticsSkipped = new ("Skipped"); - - public ObservableCollection StatisticsRows { get; } - - public bool IsDownloading => state.Downloader.IsDownloading; - - private readonly Window window; - private readonly State state; - private readonly ThrottledTask enqueueDownloadItemsTask; - private readonly ThrottledTask downloadStatisticsTask; - - private readonly IDisposable attachmentCountSubscription; - private readonly IDisposable downloadCountSubscription; - - private IDisposable? finishedItemsSubscription; - private int doneItemsCount; - private int totalEnqueuedItemCount; - private int? totalItemsToDownloadCount; - - public AttachmentsPageModel() : this(null!, State.Dummy) {} - - public AttachmentsPageModel(Window window, State state) { - this.window = window; - this.state = state; - - FilterModel = new AttachmentFilterPanelModel(state); - - StatisticsRows = [ - statisticsEnqueued, - statisticsDownloaded, - statisticsFailed, - statisticsSkipped - ]; - - enqueueDownloadItemsTask = new ThrottledTask(OnItemsEnqueued, TaskScheduler.FromCurrentSynchronizationContext()); - downloadStatisticsTask = new ThrottledTask(UpdateStatistics, TaskScheduler.FromCurrentSynchronizationContext()); - - attachmentCountSubscription = state.Db.Attachments.TotalCount.ObserveOn(AvaloniaScheduler.Instance).Subscribe(OnAttachmentCountChanged); - downloadCountSubscription = state.Db.Downloads.TotalCount.ObserveOn(AvaloniaScheduler.Instance).Subscribe(OnDownloadCountChanged); - - RecomputeDownloadStatistics(); - } - - public void Dispose() { - attachmentCountSubscription.Dispose(); - downloadCountSubscription.Dispose(); - finishedItemsSubscription?.Dispose(); - - enqueueDownloadItemsTask.Dispose(); - downloadStatisticsTask.Dispose(); - - FilterModel.Dispose(); - } - - private void OnAttachmentCountChanged(long newAttachmentCount) { - if (IsDownloading) { - EnqueueDownloadItemsLater(); - } - else { - RecomputeDownloadStatistics(); - } - } - - private void OnDownloadCountChanged(long newDownloadCount) { - RecomputeDownloadStatistics(); - } - - private async Task EnqueueDownloadItems() { - try { - OnItemsEnqueued(await state.Db.Downloads.EnqueueDownloadItems(CreateAttachmentFilter())); - } catch (Exception e) { - Log.Error(e); - await Dialog.ShowOk(window, "Download Error", "Failed to enqueue items for download."); - } - } - - private void EnqueueDownloadItemsLater() { - var filter = CreateAttachmentFilter(); - enqueueDownloadItemsTask.Post(cancellationToken => state.Db.Downloads.EnqueueDownloadItems(filter, cancellationToken)); - } - - private void OnItemsEnqueued(int itemCount) { - totalEnqueuedItemCount += itemCount; - totalItemsToDownloadCount = totalEnqueuedItemCount; - UpdateDownloadMessage(); - RecomputeDownloadStatistics(); - } - - private AttachmentFilter CreateAttachmentFilter() { - var filter = FilterModel.CreateFilter(); - filter.DownloadItemRule = AttachmentFilter.DownloadItemRules.OnlyNotPresent; - return filter; - } - - public async Task OnClickToggleDownload() { - IsToggleDownloadButtonEnabled = false; - - if (IsDownloading) { - await state.Downloader.Stop(); - - finishedItemsSubscription?.Dispose(); - finishedItemsSubscription = null; - - RecomputeDownloadStatistics(); - - await state.Db.Downloads.RemoveDownloadItems(EnqueuedItemFilter, FilterRemovalMode.RemoveMatching); - - doneItemsCount = 0; - totalEnqueuedItemCount = 0; - totalItemsToDownloadCount = null; - UpdateDownloadMessage(); - } - else { - var finishedItems = await state.Downloader.Start(); - - finishedItemsSubscription = finishedItems.Select(static _ => true) - .Buffer(TimeSpan.FromMilliseconds(100)) - .Select(static items => items.Count) - .Where(static items => items > 0) - .ObserveOn(AvaloniaScheduler.Instance) - .Subscribe(OnItemsFinished); - - await EnqueueDownloadItems(); - } - - OnPropertyChanged(nameof(ToggleDownloadButtonText)); - OnPropertyChanged(nameof(IsDownloading)); - IsToggleDownloadButtonEnabled = true; - } - - private void OnItemsFinished(int finishedItemCount) { - doneItemsCount += finishedItemCount; - UpdateDownloadMessage(); - } - - public async Task OnClickRetryFailedDownloads() { - IsRetryingFailedDownloads = true; - - try { - var allExceptFailedFilter = new DownloadItemFilter { - IncludeStatuses = new HashSet { - DownloadStatus.Enqueued, - DownloadStatus.Downloading, - DownloadStatus.Success - } - }; - - await state.Db.Downloads.RemoveDownloadItems(allExceptFailedFilter, FilterRemovalMode.KeepMatching); - - if (IsDownloading) { - await EnqueueDownloadItems(); - } - } catch (Exception e) { - Log.Error(e); - } finally { - IsRetryingFailedDownloads = false; - } - } - - private void RecomputeDownloadStatistics() { - downloadStatisticsTask.Post(state.Db.Downloads.GetStatistics); - } - - private void UpdateStatistics(DownloadStatusStatistics statusStatistics) { - statisticsEnqueued.Items = statusStatistics.EnqueuedCount; - statisticsEnqueued.Size = statusStatistics.EnqueuedSize; - - statisticsDownloaded.Items = statusStatistics.SuccessfulCount; - statisticsDownloaded.Size = statusStatistics.SuccessfulSize; - - statisticsFailed.Items = statusStatistics.FailedCount; - statisticsFailed.Size = statusStatistics.FailedSize; - - statisticsSkipped.Items = statusStatistics.SkippedCount; - statisticsSkipped.Size = statusStatistics.SkippedSize; - - HasFailedDownloads = statusStatistics.FailedCount > 0; - - UpdateDownloadMessage(); - } - - private void UpdateDownloadMessage() { - DownloadMessage = IsDownloading ? doneItemsCount.Format() + " / " + (totalItemsToDownloadCount?.Format() ?? "?") : ""; - - OnPropertyChanged(nameof(DownloadProgress)); - } - - [ObservableObject] - public sealed partial class StatisticsRow(string state) { - public string State { get; } = state; - - [ObservableProperty] - private int items; - - [ObservableProperty] - private ulong? size; - } -} diff --git a/app/Desktop/Main/Pages/AttachmentsPage.axaml b/app/Desktop/Main/Pages/DownloadsPage.axaml similarity index 64% rename from app/Desktop/Main/Pages/AttachmentsPage.axaml rename to app/Desktop/Main/Pages/DownloadsPage.axaml index cb4449f..56fb04d 100644 --- a/app/Desktop/Main/Pages/AttachmentsPage.axaml +++ b/app/Desktop/Main/Pages/DownloadsPage.axaml @@ -5,11 +5,11 @@ xmlns:pages="clr-namespace:DHT.Desktop.Main.Pages" xmlns:controls="clr-namespace:DHT.Desktop.Main.Controls" mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" - x:Class="DHT.Desktop.Main.Pages.AttachmentsPage" - x:DataType="pages:AttachmentsPageModel"> + x:Class="DHT.Desktop.Main.Pages.DownloadsPage" + x:DataType="pages:DownloadsPageModel"> - + @@ -31,19 +31,15 @@ - -