Files
NebulaLauncher/Nebula.Launcher/ServerListProviders/FavoriteServerListProvider.cs

131 lines
3.9 KiB
C#
Raw Permalink Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
2025-06-15 17:13:31 +03:00
using Avalonia;
using Avalonia.Controls;
using Avalonia.Layout;
using Avalonia.Media;
using Microsoft.Extensions.DependencyInjection;
using Nebula.Launcher.ViewModels;
using Nebula.Launcher.ViewModels.Pages;
2025-06-15 17:13:31 +03:00
using Nebula.Launcher.ViewModels.Popup;
using Nebula.Shared;
using Nebula.Shared.Models;
using Nebula.Shared.Services;
using Nebula.Shared.Utils;
namespace Nebula.Launcher.ServerListProviders;
2025-06-17 21:07:32 +03:00
[ServiceRegister, ConstructGenerator]
public sealed partial class FavoriteServerListProvider : IServerListProvider, IServerListDirtyInvoker
{
[GenerateProperty] private ConfigurationService ConfigurationService { get; }
2025-06-15 17:13:31 +03:00
[GenerateProperty] private IServiceProvider ServiceProvider { get; }
[GenerateProperty] private ServerViewContainer ServerViewContainer { get; }
2025-06-22 10:40:42 +03:00
private List<IListEntryModelView> _serverLists = [];
2025-06-19 21:12:42 +03:00
private string[] rawServerLists = [];
public bool IsLoaded { get; private set; }
public Action? OnLoaded { get; set; }
public Action? Dirty { get; set; }
2025-06-22 10:40:42 +03:00
public IEnumerable<IListEntryModelView> GetServers()
{
return _serverLists;
}
public IEnumerable<Exception> GetErrors()
{
return [];
}
public void LoadServerList()
{
IsLoaded = false;
_serverLists.Clear();
var servers = GetFavoriteEntries();
2025-06-22 10:40:42 +03:00
var serverEntries = servers.Select(s =>
ServerViewContainer.Get(s.ToRobustUrl())
);
2025-06-15 17:13:31 +03:00
2025-06-22 10:40:42 +03:00
_serverLists.AddRange(serverEntries);
2025-06-15 17:13:31 +03:00
_serverLists.Add(new AddFavoriteButton(ServiceProvider));
IsLoaded = true;
OnLoaded?.Invoke();
}
public void AddFavorite(ServerEntryModelView entryModelView)
{
AddFavorite(entryModelView.Address);
}
public void AddFavorite(RobustUrl robustUrl)
{
var servers = GetFavoriteEntries();
servers.Add(robustUrl.ToString());
ConfigurationService.SetConfigValue(LauncherConVar.Favorites, servers.ToArray());
2025-06-22 10:40:42 +03:00
if(ServerViewContainer.Get(robustUrl) is IFavoriteEntryModelView favoriteView) favoriteView.IsFavorite = true;
}
public void RemoveFavorite(ServerEntryModelView entryModelView)
{
var servers = GetFavoriteEntries();
servers.Remove(entryModelView.Address.ToString());
ConfigurationService.SetConfigValue(LauncherConVar.Favorites, servers.ToArray());
}
2025-06-22 10:40:42 +03:00
public void RemoveFavorite(RobustUrl url)
{
var servers = GetFavoriteEntries();
servers.Remove(url.ToString());
ConfigurationService.SetConfigValue(LauncherConVar.Favorites, servers.ToArray());
}
private List<string> GetFavoriteEntries()
{
2025-06-19 21:12:42 +03:00
return rawServerLists.ToList();
}
2025-06-19 21:12:42 +03:00
private void Initialise()
{
ConfigurationService.SubscribeVarChanged(LauncherConVar.Favorites, OnVarChanged, true);
}
private void OnVarChanged(string[]? value)
{
if (value == null)
{
rawServerLists = [];
Dirty?.Invoke();
return;
}
rawServerLists = value;
Dirty?.Invoke();
}
private void InitialiseInDesignMode(){}
2025-06-15 17:13:31 +03:00
}
2025-06-22 10:40:42 +03:00
public class AddFavoriteButton: Border, IListEntryModelView{
2025-06-15 17:13:31 +03:00
private Button _addFavoriteButton = new Button();
public AddFavoriteButton(IServiceProvider serviceProvider)
{
Margin = new Thickness(5, 5, 5, 20);
Background = new SolidColorBrush(Color.Parse("#222222"));
CornerRadius = new CornerRadius(20f);
_addFavoriteButton.HorizontalAlignment = HorizontalAlignment.Center;
_addFavoriteButton.Click += (sender, args) =>
{
serviceProvider.GetService<PopupMessageService>()!.Popup(
serviceProvider.GetService<AddFavoriteViewModel>()!);
};
_addFavoriteButton.Content = "Add Favorite";
Child = _addFavoriteButton;
}
2025-06-22 10:40:42 +03:00
public bool IsFavorite { get; set; }
}