NetworkTaskFactory

This commit is contained in:
2024-09-29 08:21:48 +05:00
parent 45c3f90da0
commit 4704f1217a
24 changed files with 817 additions and 287 deletions

View File

@@ -0,0 +1,20 @@
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="Млаумчерб.Клиент.видимое.DownloadTaskView"
Padding="4" MaxHeight="60" MinWidth="200"
VerticalAlignment="Top"
HorizontalAlignment="Stretch"
BorderThickness="1" BorderBrush="#999999">
<Grid RowDefinitions="30 30" ColumnDefinitions="* 30">
<TextBlock Grid.Row="0" Grid.Column="0" Name="NameText"/>
<Button Grid.Row="0" Grid.Column="1"
Classes="button_no_border"
Background="Transparent"
Foreground="#FF4040"
FontSize="12"
Click="RemoveFromList">
[X]
</Button>
<TextBlock Grid.Row="1" Grid.Column="0" Name="DownloadedProgressText"/>
</Grid>
</UserControl>

View File

@@ -0,0 +1,42 @@
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Threading;
using Млаумчерб.Клиент.сеть;
namespace Млаумчерб.Клиент.видимое;
public partial class DownloadTaskView : UserControl
{
private readonly NetworkTask _task;
private readonly Action<DownloadTaskView> _removeFromList;
public DownloadTaskView()
{
throw new NotImplementedException();
}
public DownloadTaskView(NetworkTask task, Action<DownloadTaskView> removeFromList)
{
_task = task;
_removeFromList = removeFromList;
InitializeComponent();
NameText.Text = task.Name;
task.OnProgress += ReportProgress;
}
void ReportProgress(DownloadProgress progress)
{
Dispatcher.UIThread.Invoke(() =>
{
DownloadedProgressText.Text = progress.ToString();
});
}
private void RemoveFromList(object? sender, RoutedEventArgs e)
{
_task.Cancel();
Dispatcher.UIThread.Invoke(() => _removeFromList.Invoke(this));
}
}

View File

@@ -20,7 +20,7 @@ public partial class VersionItemView : ListBoxItem
Props = props;
InitializeComponent();
text.Text = props.Name;
props.DownloadCompleted += UpdateBackground;
props.OnDownloadCompleted += UpdateBackground;
UpdateBackground();
}

View File

@@ -33,8 +33,9 @@
VerticalScrollBarVisibility="Visible"
Background="Transparent">
<TextBox Name="LogTextBox"
FontSize="12"
FontSize="14"
IsReadOnly="True" TextWrapping="Wrap"
VerticalAlignment="Top"
Background="Transparent" BorderThickness="0"/>
</ScrollViewer>
</Grid>
@@ -92,10 +93,13 @@
Загрузки
</TextBlock>
</Border>
<ScrollViewer Name="DownloadsScrollViewer" Grid.Row="1"
<ScrollViewer Grid.Row="1"
HorizontalScrollBarVisibility="Disabled"
VerticalScrollBarVisibility="Visible"
Background="Transparent"/>
Background="Transparent"
Padding="1">
<StackPanel Name="DownloadsPanel" VerticalAlignment="Top"/>
</ScrollViewer>
</Grid>
</Border>
</Grid>

View File

@@ -1,11 +1,9 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Presenters;
using Avalonia.Data;
using Avalonia.Interactivity;
using Avalonia.Platform.Storage;
using Avalonia.Threading;
using Avalonia.VisualTree;
using Млаумчерб.Клиент.классы;
namespace Млаумчерб.Клиент.видимое;
@@ -59,19 +57,25 @@ public partial class Окне : Window
{
Приложение.Логгер.OnLogMessage += (context, severity, message, format) =>
{
if(severity == LogSeverity.Debug)
return;
StringBuilder b = new();
b.Append(DateTime.Now.ToString("[HH:mm:ss]["));
b.Append(severity);
b.Append("]: ");
b.Append(message);
b.Append('\n');
double offsetFromBottom = LogScrollViewer.Extent.Height
- LogScrollViewer.Offset.Y
- LogScrollViewer.Viewport.Height;
bool is_scrolled_to_end = offsetFromBottom < 20.0; // scrolled less then one line up
LogTextBox.Text += b.ToString();
if(is_scrolled_to_end)
LogScrollViewer.ScrollToEnd();
Dispatcher.UIThread.Invoke(() =>
{
double offsetFromBottom = LogScrollViewer.Extent.Height
- LogScrollViewer.Offset.Y
- LogScrollViewer.Viewport.Height;
bool is_scrolled_to_end = offsetFromBottom < 20.0; // scrolled less then one line up
LogTextBox.Text += b.ToString();
if (is_scrolled_to_end)
LogScrollViewer.ScrollToEnd();
});
};
Username = Приложение.Настройки.имя_пользователя;
@@ -81,7 +85,7 @@ public partial class Окне : Window
Directory.Create(Пути.GetVersionDescriptorDir());
VersionComboBox.SelectedIndex = 0;
VersionComboBox.IsEnabled = false;
var versions = await GameVersionDescriptor.GetAllVersionsAsync();
var versions = await GameVersion.GetAllVersionsAsync();
Dispatcher.UIThread.Invoke(() =>
{
foreach (var p in versions)
@@ -114,8 +118,18 @@ public partial class Окне : Window
if (selectedVersion == null)
return;
var v = await GameVersionDescriptor.CreateFromPropsAsync(selectedVersion);
v.BeginUpdate(CheckGameFiles);
var v = await GameVersion.CreateFromPropsAsync(selectedVersion);
var updateTasks = await v.CreateUpdateTasksAsync(CheckGameFiles);
foreach (var t in updateTasks)
{
var updateTask = t.StartAsync();
Dispatcher.UIThread.Invoke(() =>
{
var view = new DownloadTaskView(t, view => DownloadsPanel.Children.Remove(view));
DownloadsPanel.Children.Add(view);
});
await updateTask;
}
Dispatcher.UIThread.Invoke(() => CheckGameFiles = false);
}
catch (Exception ex)