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 ReactiveUI;
2021-10-21 12:57:02 +00:00
using ReactiveUI.Fody.Helpers;
using Wabbajack.App.ViewModels;
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(1.0);
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);
this.WhenAnyValue(vm => vm.MaxThroughput)
.Skip(1)
2021-10-23 16:51:17 +00:00
.Subscribe(v => { _resource.MaxThroughput = MaxThroughput; }).DisposeWith(disposables);
2021-10-21 12:57:02 +00:00
this.WhenAnyValue(vm => vm.MaxTasks)
.Skip(1)
2021-10-23 16:51:17 +00:00
.Subscribe(v => { _resource.MaxTasks = MaxTasks; }).DisposeWith(disposables);
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; }
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)
{
MaxTasks = _resource.MaxTasks;
MaxThroughput = _resource.MaxThroughput;
CurrentThroughput = _resource.StatusReport.Transferred;
}
2021-10-21 12:57:02 +00:00
}