Files
NebulaLauncher/Nebula.Launcher/ViewModels/Pages/ServerListViewModel.Favorite.cs

60 lines
1.9 KiB
C#
Raw Normal View History

2025-01-28 19:59:35 +03:00
using System.Collections.ObjectModel;
using System.Linq;
using Nebula.Shared.Models;
using Nebula.Shared.Services;
using Nebula.Shared.Utils;
namespace Nebula.Launcher.ViewModels.Pages;
public partial class ServerListViewModel
{
[GenerateProperty] private ConfigurationService ConfigurationService { get; }
[GenerateProperty] private RestService RestService { get; }
public ObservableCollection<ServerEntryModelView> FavoriteServers { get; } = [];
2025-01-30 10:11:54 +03:00
2025-02-01 13:13:49 +03:00
private void UpdateFavoriteEntries()
2025-01-28 19:59:35 +03:00
{
foreach(var fav in FavoriteServers.ToList()){
FavoriteServers.Remove(fav);
}
2025-01-28 19:59:35 +03:00
2025-02-01 18:19:18 +03:00
var servers = ConfigurationService.GetConfigValue(LauncherConVar.Favorites);
2025-01-28 19:59:35 +03:00
if (servers is null || servers.Length == 0)
{
return;
}
foreach (var server in servers)
{
2025-02-01 13:13:49 +03:00
var s = ServerViewContainer.Get(server.ToRobustUrl());
2025-01-30 20:18:40 +03:00
s.IsFavorite = true;
FavoriteServers.Add(s);
2025-01-28 19:59:35 +03:00
}
ApplyFilter();
2025-01-28 19:59:35 +03:00
}
public void AddFavorite(ServerEntryModelView entryModelView)
2025-01-29 12:32:42 +03:00
{
entryModelView.IsFavorite = true;
AddFavorite(entryModelView.Address);
}
public void AddFavorite(RobustUrl robustUrl)
2025-01-28 19:59:35 +03:00
{
2025-02-01 18:19:18 +03:00
var servers = (ConfigurationService.GetConfigValue(LauncherConVar.Favorites) ?? []).ToList();
2025-01-29 12:32:42 +03:00
servers.Add(robustUrl.ToString());
2025-02-01 18:19:18 +03:00
ConfigurationService.SetConfigValue(LauncherConVar.Favorites, servers.ToArray());
2025-01-30 20:18:40 +03:00
UpdateFavoriteEntries();
2025-01-28 19:59:35 +03:00
}
public void RemoveFavorite(ServerEntryModelView entryModelView)
{
2025-02-01 18:19:18 +03:00
var servers = (ConfigurationService.GetConfigValue(LauncherConVar.Favorites) ?? []).ToList();
2025-01-28 19:59:35 +03:00
servers.Remove(entryModelView.Address.ToString());
2025-02-01 18:19:18 +03:00
ConfigurationService.SetConfigValue(LauncherConVar.Favorites, servers.ToArray());
2025-01-29 12:32:42 +03:00
entryModelView.IsFavorite = false;
2025-01-30 20:18:40 +03:00
UpdateFavoriteEntries();
2025-01-28 19:59:35 +03:00
}
}