- tweak: more right think

This commit is contained in:
2025-02-01 13:13:49 +03:00
parent 9eff84ddf8
commit 77956d35f3
3 changed files with 56 additions and 34 deletions

View File

@@ -18,7 +18,7 @@ public partial class ServerListViewModel
public ObservableCollection<ServerEntryModelView> FavoriteServers { get; } = new(); public ObservableCollection<ServerEntryModelView> FavoriteServers { get; } = new();
private async void UpdateFavoriteEntries() private void UpdateFavoriteEntries()
{ {
FavoriteServers.Clear(); FavoriteServers.Clear();
@@ -30,7 +30,7 @@ public partial class ServerListViewModel
foreach (var server in servers) foreach (var server in servers)
{ {
var s = await ServerViewContainer.Get(server.ToRobustUrl()); var s = ServerViewContainer.Get(server.ToRobustUrl());
s.IsFavorite = true; s.IsFavorite = true;
FavoriteServers.Add(s); FavoriteServers.Add(s);
} }

View File

@@ -60,12 +60,12 @@ public partial class ServerListViewModel : ViewModelBase, IViewModelPage
private void UpdateServerEntries() private void UpdateServerEntries()
{ {
Servers.Clear(); Servers.Clear();
Task.Run(async () => Task.Run(() =>
{ {
UnsortedServers.Sort(new ServerComparer()); UnsortedServers.Sort(new ServerComparer());
foreach (var info in UnsortedServers.Where(a => CheckServerThink(a.StatusData))) foreach (var info in UnsortedServers.Where(a => CheckServerThink(a.StatusData)))
{ {
var view = await ServerViewContainer.Get(info.Address.ToRobustUrl(), info.StatusData); var view = ServerViewContainer.Get(info.Address.ToRobustUrl(), info.StatusData);
Servers.Add(view); Servers.Add(view);
} }
}); });
@@ -147,45 +147,28 @@ public class ServerViewContainer(
_entries.Clear(); _entries.Clear();
} }
public async Task<ServerEntryModelView> Get(RobustUrl url, ServerStatus? serverStatus = null) public ServerEntryModelView Get(RobustUrl url, ServerStatus? serverStatus = null)
{ {
ServerEntryModelView? entry;
lock (_entries) lock (_entries)
{ {
if (_entries.TryGetValue(url.ToString(), out var entry1)) if (_entries.TryGetValue(url.ToString(), out entry))
{ {
return entry1; return entry;
} }
entry = viewHelperService.GetViewModel<ServerEntryModelView>().WithData(url, serverStatus);
_entries.Add(url.ToString(), entry);
} }
Console.WriteLine("Creating new instance... " + url.ToString() + _entries.Keys.ToList().Contains(url.ToString()));
try
{
serverStatus ??= await restService.GetAsync<ServerStatus>(url.StatusUri, cancellationService.Token);
}
catch (Exception e)
{
debugService.Error(e);
serverStatus = new ServerStatus("ErrorLand", $"ERROR: {e.Message}", [], "", -1, -1, -1, false, DateTime.Now,
-1);
}
var entry = viewHelperService.GetViewModel<ServerEntryModelView>().WithData(url, serverStatus);
entry.OnFavoriteToggle += () => entry.OnFavoriteToggle += () =>
{ {
if (entry.IsFavorite) serverListViewModel.RemoveFavorite(entry); if (entry.IsFavorite) serverListViewModel.RemoveFavorite(entry);
else serverListViewModel.AddFavorite(entry); else serverListViewModel.AddFavorite(entry);
}; };
lock (_entries)
{
if (_entries.TryGetValue(url.ToString(), out var entry1))
{
return entry1;
}
_entries.Add(url.ToString(), entry);
}
return entry; return entry;
} }
} }

View File

@@ -40,8 +40,18 @@ public partial class ServerEntryModelView : ViewModelBase
[ObservableProperty] private bool _tagDataVisible = false; [ObservableProperty] private bool _tagDataVisible = false;
[ObservableProperty] private bool _isFavorite = false; [ObservableProperty] private bool _isFavorite = false;
public ServerStatus Status { get; set; } = public ServerStatus Status { get; private set; } =
new("", "", [], "", -1, -1, -1, false, DateTime.Now, -1); new ServerStatus(
"Fetching data...",
$"Loading...", [],
"",
-1,
-1,
-1,
false,
DateTime.Now,
-1
);
public ObservableCollection<ServerLink> Links { get; } = new(); public ObservableCollection<ServerLink> Links { get; } = new();
public bool RunVisible => Process == null; public bool RunVisible => Process == null;
@@ -97,19 +107,48 @@ public partial class ServerEntryModelView : ViewModelBase
CurrLog = ViewHelperService.GetViewModel<LogPopupModelView>(); CurrLog = ViewHelperService.GetViewModel<LogPopupModelView>();
} }
public ServerEntryModelView WithData(RobustUrl url, ServerStatus serverStatus) public void SetStatus(ServerStatus serverStatus)
{ {
Status = serverStatus; Status = serverStatus;
Address = url;
Tags.Clear(); Tags.Clear();
foreach (var tag in Status.Tags) foreach (var tag in Status.Tags)
{ {
Tags.Add(tag); Tags.Add(tag);
} }
OnPropertyChanged(nameof(Status));
}
public ServerEntryModelView WithData(RobustUrl url, ServerStatus? serverStatus)
{
Address = url;
if (serverStatus is not null)
{
SetStatus(serverStatus);
}
else
{
FetchStatus();
}
return this; return this;
} }
private async void FetchStatus()
{
try
{
SetStatus(await RestService.GetAsync<ServerStatus>(Address.StatusUri, CancellationService.Token));
}
catch (Exception e)
{
DebugService.Error(e);
Status = new ServerStatus("ErrorLand", $"ERROR: {e.Message}", [], "", -1, -1, -1, false,
DateTime.Now,
-1);
}
}
public void ToggleFavorites() public void ToggleFavorites()
{ {
OnFavoriteToggle?.Invoke(); OnFavoriteToggle?.Invoke();