- tweak: more right think
This commit is contained in:
@@ -18,7 +18,7 @@ public partial class ServerListViewModel
|
||||
|
||||
public ObservableCollection<ServerEntryModelView> FavoriteServers { get; } = new();
|
||||
|
||||
private async void UpdateFavoriteEntries()
|
||||
private void UpdateFavoriteEntries()
|
||||
{
|
||||
FavoriteServers.Clear();
|
||||
|
||||
@@ -30,7 +30,7 @@ public partial class ServerListViewModel
|
||||
|
||||
foreach (var server in servers)
|
||||
{
|
||||
var s = await ServerViewContainer.Get(server.ToRobustUrl());
|
||||
var s = ServerViewContainer.Get(server.ToRobustUrl());
|
||||
s.IsFavorite = true;
|
||||
FavoriteServers.Add(s);
|
||||
}
|
||||
|
||||
@@ -60,12 +60,12 @@ public partial class ServerListViewModel : ViewModelBase, IViewModelPage
|
||||
private void UpdateServerEntries()
|
||||
{
|
||||
Servers.Clear();
|
||||
Task.Run(async () =>
|
||||
Task.Run(() =>
|
||||
{
|
||||
UnsortedServers.Sort(new ServerComparer());
|
||||
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);
|
||||
}
|
||||
});
|
||||
@@ -147,45 +147,28 @@ public class ServerViewContainer(
|
||||
_entries.Clear();
|
||||
}
|
||||
|
||||
public async Task<ServerEntryModelView> Get(RobustUrl url, ServerStatus? serverStatus = null)
|
||||
public ServerEntryModelView Get(RobustUrl url, ServerStatus? serverStatus = null)
|
||||
{
|
||||
ServerEntryModelView? entry;
|
||||
|
||||
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 += () =>
|
||||
{
|
||||
if (entry.IsFavorite) serverListViewModel.RemoveFavorite(entry);
|
||||
else serverListViewModel.AddFavorite(entry);
|
||||
};
|
||||
|
||||
lock (_entries)
|
||||
{
|
||||
if (_entries.TryGetValue(url.ToString(), out var entry1))
|
||||
{
|
||||
return entry1;
|
||||
}
|
||||
_entries.Add(url.ToString(), entry);
|
||||
}
|
||||
|
||||
return entry;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,8 +40,18 @@ public partial class ServerEntryModelView : ViewModelBase
|
||||
[ObservableProperty] private bool _tagDataVisible = false;
|
||||
[ObservableProperty] private bool _isFavorite = false;
|
||||
|
||||
public ServerStatus Status { get; set; } =
|
||||
new("", "", [], "", -1, -1, -1, false, DateTime.Now, -1);
|
||||
public ServerStatus Status { get; private set; } =
|
||||
new ServerStatus(
|
||||
"Fetching data...",
|
||||
$"Loading...", [],
|
||||
"",
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
false,
|
||||
DateTime.Now,
|
||||
-1
|
||||
);
|
||||
|
||||
public ObservableCollection<ServerLink> Links { get; } = new();
|
||||
public bool RunVisible => Process == null;
|
||||
@@ -97,18 +107,47 @@ public partial class ServerEntryModelView : ViewModelBase
|
||||
CurrLog = ViewHelperService.GetViewModel<LogPopupModelView>();
|
||||
}
|
||||
|
||||
public ServerEntryModelView WithData(RobustUrl url, ServerStatus serverStatus)
|
||||
public void SetStatus(ServerStatus serverStatus)
|
||||
{
|
||||
Status = serverStatus;
|
||||
Address = url;
|
||||
Tags.Clear();
|
||||
foreach (var tag in Status.Tags)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
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()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user