- add: Favorites - start
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using Nebula.Shared;
|
||||
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; }
|
||||
|
||||
[ObservableProperty] private bool _favoriteVisible = false;
|
||||
|
||||
public List<(RobustUrl,ServerStatus)> FavoriteServers = new();
|
||||
public ObservableCollection<ServerEntryModelView> SortedFavoriteServers { get; } = new();
|
||||
|
||||
private void SortFavorite()
|
||||
{
|
||||
SortedFavoriteServers.Clear();
|
||||
FavoriteServers.Sort(new ServerComparer());
|
||||
foreach (var server in FavoriteServers.Where(a => CheckServerThink(a.Item2)))
|
||||
SortedFavoriteServers.Add(GetServerEntryModelView(server));
|
||||
}
|
||||
|
||||
private ServerEntryModelView GetServerEntryModelView((RobustUrl, ServerStatus) server)
|
||||
{
|
||||
var model = ViewHelperService.GetViewModel<ServerEntryModelView>().WithData(server.Item1, server.Item2);
|
||||
model.OnFavoriteToggle += ()=> RemoveFavorite(model);
|
||||
return model;
|
||||
}
|
||||
|
||||
private async void FetchFavorite()
|
||||
{
|
||||
FavoriteServers.Clear();
|
||||
|
||||
var servers = ConfigurationService.GetConfigValue(CurrentConVar.Favorites);
|
||||
if (servers is null || servers.Length == 0)
|
||||
{
|
||||
FavoriteVisible = false;
|
||||
return;
|
||||
}
|
||||
|
||||
FavoriteVisible = true;
|
||||
|
||||
foreach (var server in servers)
|
||||
{
|
||||
var uri = server.ToRobustUrl();
|
||||
var serverInfo = await RestService.GetAsync<ServerStatus>(uri.StatusUri, CancellationToken.None);
|
||||
if (serverInfo.Value is null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
FavoriteServers.Add((uri, serverInfo.Value));
|
||||
}
|
||||
|
||||
SortFavorite();
|
||||
}
|
||||
|
||||
public void AddFavorite(ServerEntryModelView entryModelView)
|
||||
{
|
||||
var servers = (ConfigurationService.GetConfigValue(CurrentConVar.Favorites) ?? []).ToList();
|
||||
servers.Add(entryModelView.Address.ToString());
|
||||
ConfigurationService.SetConfigValue(CurrentConVar.Favorites, servers.ToArray());
|
||||
}
|
||||
|
||||
public void RemoveFavorite(ServerEntryModelView entryModelView)
|
||||
{
|
||||
var servers = (ConfigurationService.GetConfigValue(CurrentConVar.Favorites) ?? []).ToList();
|
||||
servers.Remove(entryModelView.Address.ToString());
|
||||
ConfigurationService.SetConfigValue(CurrentConVar.Favorites, servers.ToArray());
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
@@ -8,6 +9,7 @@ using Nebula.Launcher.Services;
|
||||
using Nebula.Launcher.Views.Pages;
|
||||
using Nebula.Shared.Models;
|
||||
using Nebula.Shared.Services;
|
||||
using Nebula.Shared.Utils;
|
||||
|
||||
namespace Nebula.Launcher.ViewModels.Pages;
|
||||
|
||||
@@ -26,6 +28,9 @@ public partial class ServerListViewModel : ViewModelBase
|
||||
//Design think
|
||||
protected override void InitialiseInDesignMode()
|
||||
{
|
||||
FavoriteVisible = true;
|
||||
SortedFavoriteServers.Add(GetServerEntryModelView(("ss14://localhost".ToRobustUrl(),
|
||||
new ServerStatus("Nebula", "TestCraft", ["16+", "RU"], "super", 12, 55, 1, false, DateTime.Now, 20))));
|
||||
ServerInfos.Add(CreateServerView(new ServerHubInfo("ss14://localhost",
|
||||
new ServerStatus("Nebula", "TestCraft", ["16+", "RU"], "super", 12, 55, 1, false, DateTime.Now, 20), [])));
|
||||
ServerInfos.Add(CreateServerView(new ServerHubInfo("ss14://localhost",
|
||||
@@ -44,12 +49,14 @@ public partial class ServerListViewModel : ViewModelBase
|
||||
OnSearchChange += OnChangeSearch;
|
||||
|
||||
if (!HubService.IsUpdating) SortServers();
|
||||
FetchFavorite();
|
||||
}
|
||||
|
||||
private void OnChangeSearch()
|
||||
{
|
||||
if(string.IsNullOrEmpty(SearchText)) return;
|
||||
SortServers();
|
||||
SortFavorite();
|
||||
}
|
||||
|
||||
private void HubServerChangedEventArgs(HubServerChangedEventArgs obj)
|
||||
@@ -69,20 +76,20 @@ public partial class ServerListViewModel : ViewModelBase
|
||||
{
|
||||
ServerInfos.Clear();
|
||||
UnsortedServers.Sort(new ServerComparer());
|
||||
foreach (var server in UnsortedServers.Where(CheckServerThink)) ServerInfos.Add(CreateServerView(server));
|
||||
foreach (var server in UnsortedServers.Where(a => CheckServerThink(a.StatusData))) ServerInfos.Add(CreateServerView(server));
|
||||
});
|
||||
}
|
||||
|
||||
private bool CheckServerThink(ServerHubInfo hubInfo)
|
||||
private bool CheckServerThink(ServerStatus hubInfo)
|
||||
{
|
||||
if (string.IsNullOrEmpty(SearchText)) return true;
|
||||
return hubInfo.StatusData.Name.ToLower().Contains(SearchText.ToLower());
|
||||
return hubInfo.Name.ToLower().Contains(SearchText.ToLower());
|
||||
}
|
||||
|
||||
private ServerEntryModelView CreateServerView(ServerHubInfo serverHubInfo)
|
||||
{
|
||||
var svn = ViewHelperService.GetViewModel<ServerEntryModelView>();
|
||||
svn.ServerHubInfo = serverHubInfo;
|
||||
var svn = ViewHelperService.GetViewModel<ServerEntryModelView>().WithData(serverHubInfo);
|
||||
svn.OnFavoriteToggle += () => AddFavorite(svn);
|
||||
return svn;
|
||||
}
|
||||
|
||||
@@ -96,7 +103,7 @@ public partial class ServerListViewModel : ViewModelBase
|
||||
}
|
||||
}
|
||||
|
||||
public class ServerComparer : IComparer<ServerHubInfo>
|
||||
public class ServerComparer : IComparer<ServerHubInfo>, IComparer<ServerStatus>, IComparer<(RobustUrl,ServerStatus)>
|
||||
{
|
||||
public int Compare(ServerHubInfo? x, ServerHubInfo? y)
|
||||
{
|
||||
@@ -107,6 +114,30 @@ public class ServerComparer : IComparer<ServerHubInfo>
|
||||
if (ReferenceEquals(null, x))
|
||||
return -1;
|
||||
|
||||
return y.StatusData.Players.CompareTo(x.StatusData.Players);
|
||||
return Compare(x.StatusData, y.StatusData);
|
||||
}
|
||||
|
||||
public int Compare(ServerStatus? x, ServerStatus? y)
|
||||
{
|
||||
if (ReferenceEquals(x, y))
|
||||
return 0;
|
||||
if (ReferenceEquals(null, y))
|
||||
return 1;
|
||||
if (ReferenceEquals(null, x))
|
||||
return -1;
|
||||
|
||||
return y.Players.CompareTo(x.Players);
|
||||
}
|
||||
|
||||
public int Compare((RobustUrl, ServerStatus) x, (RobustUrl, ServerStatus) y)
|
||||
{
|
||||
if (ReferenceEquals(x, y))
|
||||
return 0;
|
||||
if (ReferenceEquals(null, y))
|
||||
return 1;
|
||||
if (ReferenceEquals(null, x))
|
||||
return -1;
|
||||
|
||||
return Compare(x.Item2, y.Item2);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user