2025-06-22 10:40:42 +03:00
|
|
|
using System;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2025-07-02 21:32:51 +03:00
|
|
|
using Nebula.Launcher.Models;
|
2025-06-22 10:40:42 +03:00
|
|
|
using Nebula.Launcher.ServerListProviders;
|
|
|
|
|
using Nebula.Launcher.ViewModels.Pages;
|
|
|
|
|
using Nebula.Launcher.Views;
|
|
|
|
|
using Nebula.Shared.Models;
|
|
|
|
|
using Nebula.Shared.Services;
|
2025-07-02 21:32:51 +03:00
|
|
|
using Nebula.Shared.ViewHelper;
|
2025-06-22 10:40:42 +03:00
|
|
|
|
|
|
|
|
namespace Nebula.Launcher.ViewModels;
|
|
|
|
|
|
|
|
|
|
[ViewModelRegister(typeof(ServerCompoundEntryView), false)]
|
|
|
|
|
[ConstructGenerator]
|
|
|
|
|
public sealed partial class ServerCompoundEntryViewModel :
|
2025-06-22 22:11:27 +03:00
|
|
|
ViewModelBase, IFavoriteEntryModelView, IFilterConsumer, IListEntryModelView, IEntryNameHolder
|
2025-06-22 10:40:42 +03:00
|
|
|
{
|
2025-12-12 22:23:45 +03:00
|
|
|
private ServerEntryModelView? _currentEntry;
|
2025-06-22 22:11:27 +03:00
|
|
|
[ObservableProperty] private string _message = "Loading server entry...";
|
2025-06-22 10:40:42 +03:00
|
|
|
[ObservableProperty] private bool _isFavorite;
|
|
|
|
|
[ObservableProperty] private bool _loading = true;
|
|
|
|
|
|
2025-06-22 22:11:27 +03:00
|
|
|
private string? _name;
|
2025-06-27 21:31:38 +03:00
|
|
|
private RobustUrl? _url;
|
|
|
|
|
private ServerFilter? _currentFilter;
|
2025-06-22 22:11:27 +03:00
|
|
|
|
2025-12-12 22:23:45 +03:00
|
|
|
public ServerEntryModelView? CurrentEntry
|
|
|
|
|
{
|
|
|
|
|
get => _currentEntry;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (value == _currentEntry) return;
|
|
|
|
|
|
|
|
|
|
_currentEntry = value;
|
|
|
|
|
|
|
|
|
|
if (_currentEntry != null)
|
|
|
|
|
{
|
|
|
|
|
_currentEntry.IsFavorite = IsFavorite;
|
|
|
|
|
_currentEntry.Name = Name;
|
|
|
|
|
_currentEntry.ProcessFilter(_currentFilter);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Loading = _currentEntry == null;
|
|
|
|
|
|
|
|
|
|
OnPropertyChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-22 22:11:27 +03:00
|
|
|
public string? Name
|
|
|
|
|
{
|
|
|
|
|
get => _name;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_name = value;
|
|
|
|
|
OnPropertyChanged();
|
|
|
|
|
|
|
|
|
|
if (CurrentEntry != null)
|
|
|
|
|
CurrentEntry.Name = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-22 10:40:42 +03:00
|
|
|
[GenerateProperty] private RestService RestService { get; }
|
|
|
|
|
[GenerateProperty] private IServiceProvider ServiceProvider{ get; }
|
|
|
|
|
[GenerateProperty] private FavoriteServerListProvider FavoriteServerListProvider { get; }
|
|
|
|
|
|
|
|
|
|
protected override void InitialiseInDesignMode()
|
|
|
|
|
{
|
2025-06-22 22:11:27 +03:00
|
|
|
Name = "TEST.TEST";
|
2025-06-22 10:40:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Initialise()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-12 22:23:45 +03:00
|
|
|
public ServerCompoundEntryViewModel LoadWithEntry(ServerEntryModelView? entry)
|
2025-06-22 10:40:42 +03:00
|
|
|
{
|
2025-12-12 22:23:45 +03:00
|
|
|
CurrentEntry = entry;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
2025-06-22 10:40:42 +03:00
|
|
|
|
2025-12-12 22:23:45 +03:00
|
|
|
public ServerCompoundEntryViewModel LoadServerEntry(RobustUrl url, string? name, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
_url = url;
|
|
|
|
|
_name = name;
|
|
|
|
|
Task.Run(LoadServer, cancellationToken);
|
2025-06-22 10:40:42 +03:00
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-12 22:23:45 +03:00
|
|
|
private async Task LoadServer()
|
|
|
|
|
{
|
|
|
|
|
if (_url is null)
|
|
|
|
|
{
|
|
|
|
|
Message = "Url is not set";
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Message = "Loading server entry...";
|
|
|
|
|
var status = await RestService.GetAsync<ServerStatus>(_url.StatusUri, CancellationToken.None);
|
|
|
|
|
|
|
|
|
|
CurrentEntry = ServiceProvider.GetService<ServerEntryModelView>()!.WithData(_url, null, status);
|
|
|
|
|
|
|
|
|
|
Loading = false;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Message = "Error while fetching data from " + _url + " : " + e.Message;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-22 10:40:42 +03:00
|
|
|
public void ToggleFavorites()
|
|
|
|
|
{
|
2025-06-27 21:31:38 +03:00
|
|
|
if (CurrentEntry is null && _url is not null)
|
|
|
|
|
{
|
|
|
|
|
IsFavorite = !IsFavorite;
|
|
|
|
|
if(IsFavorite) FavoriteServerListProvider.AddFavorite(_url);
|
|
|
|
|
else FavoriteServerListProvider.RemoveFavorite(_url);
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-22 22:11:27 +03:00
|
|
|
CurrentEntry?.ToggleFavorites();
|
2025-06-22 10:40:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void ProcessFilter(ServerFilter? serverFilter)
|
|
|
|
|
{
|
2025-06-27 21:31:38 +03:00
|
|
|
_currentFilter = serverFilter;
|
2025-06-22 10:40:42 +03:00
|
|
|
if(CurrentEntry is IFilterConsumer filterConsumer)
|
|
|
|
|
filterConsumer.ProcessFilter(serverFilter);
|
|
|
|
|
}
|
2025-12-11 21:47:54 +03:00
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
CurrentEntry?.Dispose();
|
|
|
|
|
}
|
2025-06-22 10:40:42 +03:00
|
|
|
}
|