Files
NebulaLauncher/Nebula.Launcher/ViewModels/Popup/LoadingContextViewModel.cs

132 lines
3.8 KiB
C#
Raw Normal View History

using System;
using System.Collections.ObjectModel;
2025-01-08 18:00:06 +03:00
using CommunityToolkit.Mvvm.ComponentModel;
2025-06-23 16:39:30 +03:00
using Nebula.Launcher.Services;
2025-01-08 18:00:06 +03:00
using Nebula.Launcher.Views.Popup;
using Nebula.Shared.Models;
2025-01-14 22:10:16 +03:00
using Nebula.Shared.Services;
2025-07-02 21:32:51 +03:00
using Nebula.Shared.ViewHelper;
2025-01-08 18:00:06 +03:00
2025-01-14 22:10:16 +03:00
namespace Nebula.Launcher.ViewModels.Popup;
2025-01-08 18:00:06 +03:00
[ViewModelRegister(typeof(LoadingContextView), false)]
2025-01-14 22:10:16 +03:00
[ConstructGenerator]
public sealed partial class LoadingContextViewModel : PopupViewModelBase, ILoadingHandlerFactory, IConnectionSpeedHandler
2025-01-08 18:00:06 +03:00
{
public ObservableCollection<LoadingContext> LoadingContexts { get; } = [];
public ObservableCollection<double> Values { get; } = [];
[ObservableProperty] private string _speedText = "";
[ObservableProperty] private bool _showSpeed;
[ObservableProperty] private int _loadingColumnSize = 2;
2025-01-14 22:10:16 +03:00
[GenerateProperty] public override PopupMessageService PopupMessageService { get; }
2025-03-12 14:51:47 +03:00
[GenerateProperty] public CancellationService CancellationService { get; }
2025-01-14 22:10:16 +03:00
public string LoadingName { get; set; } = LocalizationService.GetString("popup-loading");
2025-03-14 18:32:03 +03:00
public bool IsCancellable { get; set; } = true;
2025-01-12 15:15:01 +03:00
public override bool IsClosable => false;
2025-01-08 18:00:06 +03:00
public override string Title => LocalizationService.GetString("popup-loading");
2025-01-14 22:10:16 +03:00
public void Cancel()
2025-01-08 18:00:06 +03:00
{
if (!IsCancellable) return;
CancellationService.Cancel();
Dispose();
2025-01-08 18:00:06 +03:00
}
public void PasteSpeed(int speed)
2025-01-08 18:00:06 +03:00
{
if (Values.Count == 0)
{
ShowSpeed = true;
LoadingColumnSize = 1;
}
SpeedText = FileLoadingFormater.FormatBytes(speed) + " / s";
Values.Add(speed);
if(Values.Count > 10) Values.RemoveAt(0);
2025-01-08 18:00:06 +03:00
}
public ILoadingHandler CreateLoadingContext(ILoadingFormater? loadingFormater = null)
2025-01-08 18:00:06 +03:00
{
var instance = new LoadingContext(this, loadingFormater ?? DefaultLoadingFormater.Instance);
LoadingContexts.Add(instance);
return instance;
2025-01-08 18:00:06 +03:00
}
public void RemoveContextInstance(LoadingContext loadingContext)
2025-01-08 18:00:06 +03:00
{
LoadingContexts.Remove(loadingContext);
2025-01-08 18:00:06 +03:00
}
2025-01-14 22:10:16 +03:00
protected override void Initialise()
2025-07-10 15:22:15 +03:00
{
}
protected override void InitialiseInDesignMode()
{
var context = CreateLoadingContext();
context.SetJobsCount(5);
context.SetResolvedJobsCount(2);
context.SetLoadingMessage("message");
var ctx1 = CreateLoadingContext(new FileLoadingFormater());
ctx1.SetJobsCount(1020120);
ctx1.SetResolvedJobsCount(12331);
ctx1.SetLoadingMessage("File data");
for (var i = 0; i < 14; i++)
{
PasteSpeed(Random.Shared.Next(10000000));
}
2025-03-12 14:51:47 +03:00
}
}
2025-03-12 14:51:47 +03:00
public sealed partial class LoadingContext : ObservableObject, ILoadingHandler
{
private readonly LoadingContextViewModel _master;
private readonly ILoadingFormater _loadingFormater;
public string LoadingText => _loadingFormater.Format(this);
[ObservableProperty] private string _message = string.Empty;
[ObservableProperty] private long _currJobs;
[ObservableProperty] private long _resolvedJobs;
public LoadingContext(LoadingContextViewModel master, ILoadingFormater loadingFormater)
2025-01-14 22:10:16 +03:00
{
_master = master;
_loadingFormater = loadingFormater;
2025-01-14 22:10:16 +03:00
}
public void SetJobsCount(long count)
2025-01-14 22:10:16 +03:00
{
CurrJobs = count;
OnPropertyChanged(nameof(LoadingText));
}
public long GetJobsCount()
{
return CurrJobs;
}
public void SetResolvedJobsCount(long count)
{
ResolvedJobs = count;
OnPropertyChanged(nameof(LoadingText));
}
public long GetResolvedJobsCount()
{
return ResolvedJobs;
}
public void SetLoadingMessage(string message)
{
Message = message;
}
public void Dispose()
{
_master.RemoveContextInstance(this);
2025-01-14 22:10:16 +03:00
}
}