- tweak: server viewmodel cache

This commit is contained in:
2025-01-30 20:18:40 +03:00
parent 88402e7751
commit 329ec268cc
5 changed files with 85 additions and 94 deletions

View File

@@ -16,19 +16,11 @@ public partial class ServerListViewModel
[GenerateProperty] private ConfigurationService ConfigurationService { get; }
[GenerateProperty] private RestService RestService { get; }
public readonly List<(RobustUrl,ServerStatus)> RawFavoriteServers = new();
public ObservableCollection<ServerEntryModelView> FavoriteServers { get; } = new();
public ServerEntryModelView GetServerEntryModelView((RobustUrl, ServerStatus) server)
private async void UpdateFavoriteEntries()
{
var model = ViewHelperService.GetViewModel<ServerEntryModelView>().WithData(server.Item1, server.Item2);
model.OnFavoriteToggle += ()=> RemoveFavorite(model);
model.IsFavorite = true;
return model;
}
private async void FetchFavorite()
{
RawFavoriteServers.Clear();
FavoriteServers.Clear();
var servers = ConfigurationService.GetConfigValue(CurrentConVar.Favorites);
if (servers is null || servers.Length == 0)
@@ -38,21 +30,9 @@ public partial class ServerListViewModel
foreach (var server in servers)
{
var uri = server.ToRobustUrl();
try
{
var serverInfo = await RestService.GetAsync<ServerStatus>(uri.StatusUri, CancellationToken.None);
if (serverInfo.Value is null)
{
throw new Exception("Server info is null");
}
RawFavoriteServers.Add((uri, serverInfo.Value));
}
catch (Exception e)
{
RawFavoriteServers.Add((uri, new ServerStatus("ErrorLand",$"ERROR: {e.Message}",[],"",-1,-1,-1,false,DateTime.Now, -1)));
}
var s = await ServerViewContainer.Get(server.ToRobustUrl());
s.IsFavorite = true;
FavoriteServers.Add(s);
}
}
@@ -67,7 +47,7 @@ public partial class ServerListViewModel
var servers = (ConfigurationService.GetConfigValue(CurrentConVar.Favorites) ?? []).ToList();
servers.Add(robustUrl.ToString());
ConfigurationService.SetConfigValue(CurrentConVar.Favorites, servers.ToArray());
FetchFavorite();
UpdateFavoriteEntries();
}
public void RemoveFavorite(ServerEntryModelView entryModelView)
@@ -76,6 +56,6 @@ public partial class ServerListViewModel
servers.Remove(entryModelView.Address.ToString());
ConfigurationService.SetConfigValue(CurrentConVar.Favorites, servers.ToArray());
entryModelView.IsFavorite = false;
FetchFavorite();
UpdateFavoriteEntries();
}
}