Show progress message on progress bar

This commit is contained in:
Stone_Red
2024-06-04 16:08:56 +02:00
parent 2d75a1070b
commit 7bb7a174ed
5 changed files with 97 additions and 12 deletions
@@ -1,10 +1,12 @@
namespace Markdowser.Processing;
public readonly struct ProcessingProgress(long current, long total, string? message = null)
public readonly struct ProcessingProgress(long current, long total, string? message = null, bool isBytes = false)
{
public long Current { get; } = current;
public long Total { get; } = total;
public string? Message { get; } = message;
public int Percentage => (int)((double)Total / Current * 100);
public bool IsBytes { get; } = isBytes;
public int Percentage => (int)((double)Current / Total * 100);
}
@@ -49,7 +49,7 @@ internal partial class HtmlProcessor : IContentProcessor
{
_ = html.AppendLine(await streamReader.ReadLineAsync());
progress.Report(new ProcessingProgress(length, contentStream.Position));
progress.Report(new ProcessingProgress(contentStream.Position, length));
}
HtmlDocument htmlDoc = new HtmlDocument();
+50
View File
@@ -0,0 +1,50 @@
using System;
using System.Globalization;
namespace Markdowser.Utilities;
internal static class BytesToString
{
public static string Convert(long value)
{
string suffix;
double readable;
switch (Math.Abs(value))
{
case >= 0x1000000000000000:
suffix = "EiB";
readable = value >> 50;
break;
case >= 0x4000000000000:
suffix = "PiB";
readable = value >> 40;
break;
case >= 0x10000000000:
suffix = "TiB";
readable = value >> 30;
break;
case >= 0x40000000:
suffix = "GiB";
readable = value >> 20;
break;
case >= 0x100000:
suffix = "MiB";
readable = value >> 10;
break;
case >= 0x400:
suffix = "KiB";
readable = value;
break;
default:
return value.ToString("0 B");
}
return (readable / 1024).ToString("0.00 ", CultureInfo.InvariantCulture) + suffix;
}
}
@@ -31,7 +31,9 @@ public partial class MainWindowViewModel : ViewModelBase
private WindowState windowState;
private bool showSidePanel;
private bool isBusy;
private int progress;
private double progress;
private double progressMax;
private string progressText;
public ObservableCollection<TabItem> Tabs { get; } = [new TabItem() { Header = "New Tab" }];
public TabItem CurrentTab
@@ -108,7 +110,7 @@ public partial class MainWindowViewModel : ViewModelBase
set => this.RaiseAndSetIfChanged(ref isBusy, value);
}
public int Progress
public double Progress
{
get => progress;
set
@@ -118,6 +120,18 @@ public partial class MainWindowViewModel : ViewModelBase
}
}
public double ProgressMax
{
get => progressMax;
set => _ = this.RaiseAndSetIfChanged(ref progressMax, value);
}
public string ProgressText
{
get => progressText;
set => this.RaiseAndSetIfChanged(ref progressText, value);
}
public SettingsViewModel SettingsViewModel => new SettingsViewModel();
public bool CloseTabEnabled => Tabs.Count > 1;
public bool BackEnabled => CurrentTabState.BackHistory.Count > 0;
@@ -205,6 +219,7 @@ public partial class MainWindowViewModel : ViewModelBase
contentProcessorManager.RegisterProcessor(new HtmlProcessor());
contentProcessorManager.RegisterProcessor(new CommonImageProcessor());
contentProcessorManager.RegisterProcessor(new DownloadProcessor());
GlobalState.ThemeChanged += (s, e) =>
{
@@ -276,7 +291,7 @@ public partial class MainWindowViewModel : ViewModelBase
}
else
{
HttpResponseMessage httpResponseMessage = await httpClient.GetAsync(Url);
HttpResponseMessage httpResponseMessage = await httpClient.GetAsync(Url, HttpCompletionOption.ResponseHeadersRead);
string? newUrl = httpResponseMessage.RequestMessage?.RequestUri?.ToString();
string oldUrl = Url;
@@ -300,8 +315,10 @@ public partial class MainWindowViewModel : ViewModelBase
return;
}
Content = await contentProcessorManager.ProcessContent(httpResponseMessage, new Progress<ProcessingProgress>(p => Progress = p.Percentage));
Content = await contentProcessorManager.ProcessContent(httpResponseMessage, new Progress<ProcessingProgress>(UpdateProgress));
if (Content.Cacheable)
{
if (oldUrl != Url)
{
cacheService.Set(oldUrl, Content);
@@ -309,6 +326,7 @@ public partial class MainWindowViewModel : ViewModelBase
cacheService.Set(Url, Content);
}
}
Dispatcher.UIThread.Post(() => CurrentTab.Header = Content.Title);
@@ -354,6 +372,21 @@ public partial class MainWindowViewModel : ViewModelBase
});
}
private void UpdateProgress(ProcessingProgress progress)
{
Progress = progress.Current;
ProgressMax = progress.Total;
if (progress.IsBytes)
{
ProgressText = $"{progress.Message} ({{1:0}}%) {BytesToString.Convert((long)Progress)}/{BytesToString.Convert((long)ProgressMax)}";
}
else
{
ProgressText = $"{progress.Message} ({{1:0}}%) {{0}}/{{3}}";
}
}
private void UrlChanged(object? sender, EventArgs e)
{
this.RaisePropertyChanged(nameof(ForwardEnabled));
+1 -1
View File
@@ -114,7 +114,7 @@
</TextBox.Styles>
</TextBox>
<ProgressBar IsVisible="{Binding IsBusy}" IsIndeterminate="{Binding ProgressIndeterminate}" ShowProgressText="{Binding !ProgressIndeterminate}" Maximum="100" Height="32" Grid.Row="2" Value="{Binding Progress}" />
<ProgressBar IsVisible="{Binding IsBusy}" IsIndeterminate="{Binding ProgressIndeterminate}" ShowProgressText="{Binding !ProgressIndeterminate}" ProgressTextFormat="{Binding ProgressText}" Height="32" Grid.Row="2" Value="{Binding Progress}" Maximum="{Binding ProgressMax}" />
</Grid>
<Button Grid.Column="5" Command="{Binding ToggleSidePanel}" HorizontalAlignment="Right" i:Attached.Icon="fa-solid fa-table-columns" />