- tweak: Server list refactor

This commit is contained in:
2025-01-30 10:11:54 +03:00
parent 52beaaa5e2
commit 88402e7751
3 changed files with 67 additions and 58 deletions

View File

@@ -16,20 +16,9 @@ public partial class ServerListViewModel
[GenerateProperty] private ConfigurationService ConfigurationService { get; } [GenerateProperty] private ConfigurationService ConfigurationService { get; }
[GenerateProperty] private RestService RestService { get; } [GenerateProperty] private RestService RestService { get; }
[ObservableProperty] private bool _favoriteVisible = false; public readonly List<(RobustUrl,ServerStatus)> RawFavoriteServers = new();
public List<(RobustUrl,ServerStatus)> FavoriteServers = new(); public ServerEntryModelView GetServerEntryModelView((RobustUrl, ServerStatus) server)
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); var model = ViewHelperService.GetViewModel<ServerEntryModelView>().WithData(server.Item1, server.Item2);
model.OnFavoriteToggle += ()=> RemoveFavorite(model); model.OnFavoriteToggle += ()=> RemoveFavorite(model);
@@ -39,17 +28,14 @@ public partial class ServerListViewModel
private async void FetchFavorite() private async void FetchFavorite()
{ {
FavoriteServers.Clear(); RawFavoriteServers.Clear();
var servers = ConfigurationService.GetConfigValue(CurrentConVar.Favorites); var servers = ConfigurationService.GetConfigValue(CurrentConVar.Favorites);
if (servers is null || servers.Length == 0) if (servers is null || servers.Length == 0)
{ {
FavoriteVisible = false;
return; return;
} }
FavoriteVisible = true;
foreach (var server in servers) foreach (var server in servers)
{ {
var uri = server.ToRobustUrl(); var uri = server.ToRobustUrl();
@@ -61,15 +47,13 @@ public partial class ServerListViewModel
throw new Exception("Server info is null"); throw new Exception("Server info is null");
} }
FavoriteServers.Add((uri, serverInfo.Value)); RawFavoriteServers.Add((uri, serverInfo.Value));
} }
catch (Exception e) catch (Exception e)
{ {
FavoriteServers.Add((uri, new ServerStatus("ErrorLand",$"ERROR: {e.Message}",[],"",-1,-1,-1,false,DateTime.Now, -1))); RawFavoriteServers.Add((uri, new ServerStatus("ErrorLand",$"ERROR: {e.Message}",[],"",-1,-1,-1,false,DateTime.Now, -1)));
} }
} }
SortFavorite();
} }
public void AddFavorite(ServerEntryModelView entryModelView) public void AddFavorite(ServerEntryModelView entryModelView)

View File

@@ -7,6 +7,7 @@ using System.Threading.Tasks;
using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.ComponentModel;
using Nebula.Launcher.Services; using Nebula.Launcher.Services;
using Nebula.Launcher.ViewModels.Popup; using Nebula.Launcher.ViewModels.Popup;
using Nebula.Launcher.Views;
using Nebula.Launcher.Views.Pages; using Nebula.Launcher.Views.Pages;
using Nebula.Shared.Models; using Nebula.Shared.Models;
using Nebula.Shared.Services; using Nebula.Shared.Services;
@@ -21,46 +22,69 @@ public partial class ServerListViewModel : ViewModelBase, IViewModelPage
[ObservableProperty] private string _searchText = string.Empty; [ObservableProperty] private string _searchText = string.Empty;
[ObservableProperty] private bool _isFavoriteMode; [ObservableProperty] private bool _isFavoriteMode;
public SortedList<float, ServerEntryModelView> Servers { get; } = new(Comparer<float>.Create((x,y)=>y.CompareTo(x)));
private ServerViewContainer ServerViewContainer;
public Action? OnSearchChange; public Action? OnSearchChange;
[GenerateProperty] private HubService HubService { get; } = default!; [GenerateProperty] private HubService HubService { get; } = default!;
[GenerateProperty] private PopupMessageService PopupMessageService { get; } [GenerateProperty] private PopupMessageService PopupMessageService { get; }
[GenerateProperty, DesignConstruct] private ViewHelperService ViewHelperService { get; } = default!; [GenerateProperty, DesignConstruct] private ViewHelperService ViewHelperService { get; } = default!;
public ObservableCollection<ServerEntryModelView> SortedServers { get; } = new();
private List<ServerHubInfo> UnsortedServers { get; } = new(); private List<ServerHubInfo> UnsortedServers { get; } = new();
//Design think //Design think
protected override void InitialiseInDesignMode() protected override void InitialiseInDesignMode()
{ {
FavoriteVisible = true; ServerViewContainer = new ServerViewContainer(this);
SortedFavoriteServers.Add(GetServerEntryModelView(("ss14://localhost".ToRobustUrl(),
new ServerStatus("Nebula", "TestCraft", ["16+", "RU"], "super", 12, 55, 1, false, DateTime.Now, 20))));
SortedServers.Add(CreateServerView(new ServerHubInfo("ss14://localhost",
new ServerStatus("Nebula", "TestCraft", ["16+", "RU"], "super", 12, 55, 1, false, DateTime.Now, 20), [])));
SortedServers.Add(CreateServerView(new ServerHubInfo("ss14://localhost",
new ServerStatus("Nebula", "TestCraft", ["16+", "RU"], "super", 12, 55, 1, false, DateTime.Now, 20), [])));
SortedServers.Add(CreateServerView(new ServerHubInfo("ss14://localhost",
new ServerStatus("Nebula", "TestCraft", ["16+", "RU"], "super", 12, 55, 1, false, DateTime.Now, 20), [])));
} }
//real think //real think
protected override void Initialise() protected override void Initialise()
{ {
ServerViewContainer = new ServerViewContainer(this);
foreach (var info in HubService.ServerList) UnsortedServers.Add(info); foreach (var info in HubService.ServerList) UnsortedServers.Add(info);
HubService.HubServerChangedEventArgs += HubServerChangedEventArgs; HubService.HubServerChangedEventArgs += HubServerChangedEventArgs;
HubService.HubServerLoaded += SortServers; HubService.HubServerLoaded += UpdateServerEntries;
OnSearchChange += OnChangeSearch; OnSearchChange += OnChangeSearch;
if (!HubService.IsUpdating) SortServers(); if (!HubService.IsUpdating) UpdateServerEntries();
FetchFavorite(); //FetchFavorite();
}
private void UpdateServerEntries()
{
Servers.Clear();
OnPropertyChanged(nameof(Servers));
foreach (var info in UnsortedServers.Where(a=>CheckServerThink(a.StatusData)))
{
var view = ServerViewContainer.Get(info.Address.ToRobustUrl(), info.StatusData);
float players = info.StatusData.Players;
while (true)
{
try
{
Servers.Add(players,view);
OnPropertyChanged(nameof(Servers));
break;
}
catch (Exception e)
{
Console.WriteLine(e);
players += 0.01f;
}
}
}
} }
private void OnChangeSearch() private void OnChangeSearch()
{ {
if(string.IsNullOrEmpty(SearchText)) return; if(string.IsNullOrEmpty(SearchText)) return;
SortServers();
SortFavorite(); UpdateServerEntries();
} }
private void HubServerChangedEventArgs(HubServerChangedEventArgs obj) private void HubServerChangedEventArgs(HubServerChangedEventArgs obj)
@@ -74,29 +98,12 @@ public partial class ServerListViewModel : ViewModelBase, IViewModelPage
if (obj.Action == HubServerChangeAction.Clear) UnsortedServers.Clear(); if (obj.Action == HubServerChangeAction.Clear) UnsortedServers.Clear();
} }
private void SortServers()
{
Task.Run(() =>
{
SortedServers.Clear();
UnsortedServers.Sort(new ServerComparer());
foreach (var server in UnsortedServers.Where(a => CheckServerThink(a.StatusData))) SortedServers.Add(CreateServerView(server));
});
}
private bool CheckServerThink(ServerStatus hubInfo) private bool CheckServerThink(ServerStatus hubInfo)
{ {
if (string.IsNullOrEmpty(SearchText)) return true; if (string.IsNullOrEmpty(SearchText)) return true;
return hubInfo.Name.ToLower().Contains(SearchText.ToLower()); return hubInfo.Name.ToLower().Contains(SearchText.ToLower());
} }
private ServerEntryModelView CreateServerView(ServerHubInfo serverHubInfo)
{
var svn = ViewHelperService.GetViewModel<ServerEntryModelView>().WithData(serverHubInfo);
svn.OnFavoriteToggle += () => AddFavorite(svn);
return svn;
}
public void FilterRequired() public void FilterRequired()
{ {
} }
@@ -121,6 +128,24 @@ public partial class ServerListViewModel : ViewModelBase, IViewModelPage
} }
} }
public class ServerViewContainer(ServerListViewModel serverListViewModel)
{
private readonly Dictionary<RobustUrl, ServerEntryModelView> _entries = new();
public ServerEntryModelView Get(RobustUrl url, ServerStatus serverStatus)
{
if (_entries.TryGetValue(url, out var entry))
{
return entry;
}
entry = serverListViewModel.GetServerEntryModelView((url, serverStatus));
_entries.Add(url, entry);
return entry;
}
}
public class ServerComparer : IComparer<ServerHubInfo>, IComparer<ServerStatus>, IComparer<(RobustUrl,ServerStatus)> public class ServerComparer : IComparer<ServerHubInfo>, IComparer<ServerStatus>, IComparer<(RobustUrl,ServerStatus)>
{ {
public int Compare(ServerHubInfo? x, ServerHubInfo? y) public int Compare(ServerHubInfo? x, ServerHubInfo? y)

View File

@@ -25,11 +25,11 @@
<Panel> <Panel>
<ItemsControl <ItemsControl
IsVisible="{Binding IsFavoriteMode}" IsVisible="{Binding IsFavoriteMode}"
ItemsSource="{Binding SortedFavoriteServers}" ItemsSource="{Binding Servers.Values}"
Padding="0" /> Padding="0" />
<ItemsControl <ItemsControl
IsVisible="{Binding !IsFavoriteMode}" IsVisible="{Binding !IsFavoriteMode}"
ItemsSource="{Binding SortedServers}" ItemsSource="{Binding Servers.Values}"
Padding="0" /> Padding="0" />
</Panel> </Panel>
</ScrollViewer> </ScrollViewer>