wabbajack/Wabbajack.App/Controls/ResourceViewModel.cs

66 lines
1.7 KiB
C#
Raw Normal View History

2021-10-21 12:57:02 +00:00
using System;
using System.Reactive.Disposables;
using System.Reactive.Linq;
2021-10-23 16:51:17 +00:00
using System.Timers;
using Avalonia.Threading;
2021-10-23 16:51:17 +00:00
using ReactiveUI;
2021-10-21 12:57:02 +00:00
using ReactiveUI.Fody.Helpers;
using Wabbajack.App.ViewModels;
using Wabbajack.Common;
2021-10-21 12:57:02 +00:00
using Wabbajack.RateLimiter;
namespace Wabbajack.App.Controls;
public class ResourceViewModel : ViewModelBase, IActivatableViewModel, IDisposable
{
private readonly IResource _resource;
private readonly Timer _timer;
public ResourceViewModel(IResource resource)
{
Activator = new ViewModelActivator();
_resource = resource;
_timer = new Timer(250);
2021-10-21 12:57:02 +00:00
Name = resource.Name;
2021-10-23 16:51:17 +00:00
2021-10-21 12:57:02 +00:00
this.WhenActivated(disposables =>
{
2021-10-23 16:51:17 +00:00
_timer.Elapsed += TimerElapsed;
2021-10-21 12:57:02 +00:00
_timer.Start();
2021-10-23 16:51:17 +00:00
2021-10-21 12:57:02 +00:00
Disposable.Create(() =>
{
_timer.Stop();
_timer.Elapsed -= TimerElapsed;
}).DisposeWith(disposables);
MaxTasks = _resource.MaxTasks;
MaxThroughput = _resource.MaxThroughput;
2021-10-21 12:57:02 +00:00
});
}
2021-10-23 16:51:17 +00:00
[Reactive] public int MaxTasks { get; set; }
[Reactive] public long MaxThroughput { get; set; }
[Reactive] public long CurrentThroughput { get; set; }
[Reactive] public string Name { get; set; }
[Reactive] public string ThroughputHumanFriendly { get; set; }
2021-10-21 12:57:02 +00:00
public void Dispose()
{
_timer.Dispose();
}
2021-10-23 16:51:17 +00:00
private void TimerElapsed(object? sender, ElapsedEventArgs e)
{
Dispatcher.UIThread.Post(() => {
CurrentThroughput = _resource.StatusReport.Transferred;
ThroughputHumanFriendly = _resource.StatusReport.Transferred.ToFileSizeString();
});
2021-10-23 16:51:17 +00:00
}
2021-10-21 12:57:02 +00:00
}