wabbajack/Wabbajack.App/Models/LoadingLock.cs

40 lines
1007 B
C#
Raw Normal View History

2021-10-23 16:39:48 +00:00
using System;
using System.Reactive.Disposables;
using Avalonia.Threading;
using ReactiveUI;
using ReactiveUI.Fody.Helpers;
namespace Wabbajack.App.Models;
public class LoadingLock : ReactiveObject, IDisposable
{
private readonly CompositeDisposable _disposable;
public LoadingLock()
{
_disposable = new CompositeDisposable();
this.WhenAnyValue(vm => vm.LoadLevel)
2021-10-23 16:51:17 +00:00
.Subscribe(v => IsLoading = v > 0)
2021-10-23 16:39:48 +00:00
.DisposeWith(_disposable);
}
2021-10-23 16:51:17 +00:00
[Reactive] public int LoadLevel { get; private set; }
[Reactive] public bool IsLoading { get; private set; }
2021-10-23 16:39:48 +00:00
public void Dispose()
{
GC.SuppressFinalize(this);
_disposable.Dispose();
}
2021-10-23 16:51:17 +00:00
public IDisposable WithLoading()
{
Dispatcher.UIThread.Post(() => { LoadLevel++; }, DispatcherPriority.Background);
return Disposable.Create(() =>
{
Dispatcher.UIThread.Post(() => { LoadLevel--; }, DispatcherPriority.Background);
});
}
2021-10-23 16:39:48 +00:00
}