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; 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 Current { get; } = current;
public long Total { get; } = total; public long Total { get; } = total;
public string? Message { get; } = message; 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()); _ = html.AppendLine(await streamReader.ReadLineAsync());
progress.Report(new ProcessingProgress(length, contentStream.Position)); progress.Report(new ProcessingProgress(contentStream.Position, length));
} }
HtmlDocument htmlDoc = new HtmlDocument(); 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 WindowState windowState;
private bool showSidePanel; private bool showSidePanel;
private bool isBusy; 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 ObservableCollection<TabItem> Tabs { get; } = [new TabItem() { Header = "New Tab" }];
public TabItem CurrentTab public TabItem CurrentTab
@@ -108,7 +110,7 @@ public partial class MainWindowViewModel : ViewModelBase
set => this.RaiseAndSetIfChanged(ref isBusy, value); set => this.RaiseAndSetIfChanged(ref isBusy, value);
} }
public int Progress public double Progress
{ {
get => progress; get => progress;
set 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 SettingsViewModel SettingsViewModel => new SettingsViewModel();
public bool CloseTabEnabled => Tabs.Count > 1; public bool CloseTabEnabled => Tabs.Count > 1;
public bool BackEnabled => CurrentTabState.BackHistory.Count > 0; public bool BackEnabled => CurrentTabState.BackHistory.Count > 0;
@@ -205,6 +219,7 @@ public partial class MainWindowViewModel : ViewModelBase
contentProcessorManager.RegisterProcessor(new HtmlProcessor()); contentProcessorManager.RegisterProcessor(new HtmlProcessor());
contentProcessorManager.RegisterProcessor(new CommonImageProcessor()); contentProcessorManager.RegisterProcessor(new CommonImageProcessor());
contentProcessorManager.RegisterProcessor(new DownloadProcessor());
GlobalState.ThemeChanged += (s, e) => GlobalState.ThemeChanged += (s, e) =>
{ {
@@ -276,7 +291,7 @@ public partial class MainWindowViewModel : ViewModelBase
} }
else else
{ {
HttpResponseMessage httpResponseMessage = await httpClient.GetAsync(Url); HttpResponseMessage httpResponseMessage = await httpClient.GetAsync(Url, HttpCompletionOption.ResponseHeadersRead);
string? newUrl = httpResponseMessage.RequestMessage?.RequestUri?.ToString(); string? newUrl = httpResponseMessage.RequestMessage?.RequestUri?.ToString();
string oldUrl = Url; string oldUrl = Url;
@@ -300,14 +315,17 @@ public partial class MainWindowViewModel : ViewModelBase
return; return;
} }
Content = await contentProcessorManager.ProcessContent(httpResponseMessage, new Progress<ProcessingProgress>(p => Progress = p.Percentage)); Content = await contentProcessorManager.ProcessContent(httpResponseMessage, new Progress<ProcessingProgress>(UpdateProgress));
if (oldUrl != Url) if (Content.Cacheable)
{ {
cacheService.Set(oldUrl, Content); if (oldUrl != Url)
} {
cacheService.Set(oldUrl, Content);
}
cacheService.Set(Url, Content); cacheService.Set(Url, Content);
}
} }
Dispatcher.UIThread.Post(() => CurrentTab.Header = Content.Title); 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) private void UrlChanged(object? sender, EventArgs e)
{ {
this.RaisePropertyChanged(nameof(ForwardEnabled)); this.RaisePropertyChanged(nameof(ForwardEnabled));
+1 -1
View File
@@ -114,7 +114,7 @@
</TextBox.Styles> </TextBox.Styles>
</TextBox> </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> </Grid>
<Button Grid.Column="5" Command="{Binding ToggleSidePanel}" HorizontalAlignment="Right" i:Attached.Icon="fa-solid fa-table-columns" /> <Button Grid.Column="5" Command="{Binding ToggleSidePanel}" HorizontalAlignment="Right" i:Attached.Icon="fa-solid fa-table-columns" />