- fix: memory leak from canalisation part 2
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Avalonia.Controls;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Nebula.Launcher.Models;
|
||||
@@ -19,7 +18,7 @@ namespace Nebula.Launcher.ViewModels;
|
||||
public sealed partial class ServerCompoundEntryViewModel :
|
||||
ViewModelBase, IFavoriteEntryModelView, IFilterConsumer, IListEntryModelView, IEntryNameHolder
|
||||
{
|
||||
[ObservableProperty] private ServerEntryModelView? _currentEntry;
|
||||
private ServerEntryModelView? _currentEntry;
|
||||
[ObservableProperty] private string _message = "Loading server entry...";
|
||||
[ObservableProperty] private bool _isFavorite;
|
||||
[ObservableProperty] private bool _loading = true;
|
||||
@@ -28,6 +27,28 @@ public sealed partial class ServerCompoundEntryViewModel :
|
||||
private RobustUrl? _url;
|
||||
private ServerFilter? _currentFilter;
|
||||
|
||||
public ServerEntryModelView? CurrentEntry
|
||||
{
|
||||
get => _currentEntry;
|
||||
set
|
||||
{
|
||||
if (value == _currentEntry) return;
|
||||
|
||||
_currentEntry = value;
|
||||
|
||||
if (_currentEntry != null)
|
||||
{
|
||||
_currentEntry.IsFavorite = IsFavorite;
|
||||
_currentEntry.Name = Name;
|
||||
_currentEntry.ProcessFilter(_currentFilter);
|
||||
}
|
||||
|
||||
Loading = _currentEntry == null;
|
||||
|
||||
OnPropertyChanged();
|
||||
}
|
||||
}
|
||||
|
||||
public string? Name
|
||||
{
|
||||
get => _name;
|
||||
@@ -54,31 +75,43 @@ public sealed partial class ServerCompoundEntryViewModel :
|
||||
{
|
||||
}
|
||||
|
||||
public ServerCompoundEntryViewModel LoadServerEntry(RobustUrl url,string? name, CancellationToken cancellationToken)
|
||||
public ServerCompoundEntryViewModel LoadWithEntry(ServerEntryModelView? entry)
|
||||
{
|
||||
Task.Run(async () =>
|
||||
{
|
||||
_url = url;
|
||||
try
|
||||
{
|
||||
Message = "Loading server entry...";
|
||||
var status = await RestService.GetAsync<ServerStatus>(_url.StatusUri, cancellationToken);
|
||||
|
||||
CurrentEntry = ServiceProvider.GetService<ServerEntryModelView>()!.WithData(_url, name, status);
|
||||
CurrentEntry.IsFavorite = IsFavorite;
|
||||
CurrentEntry.Loading = false;
|
||||
CurrentEntry.ProcessFilter(_currentFilter);
|
||||
Loading = false;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Message = e.Message;
|
||||
}
|
||||
}, cancellationToken);
|
||||
|
||||
CurrentEntry = entry;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ServerCompoundEntryViewModel LoadServerEntry(RobustUrl url, string? name, CancellationToken cancellationToken)
|
||||
{
|
||||
_url = url;
|
||||
_name = name;
|
||||
Task.Run(LoadServer, cancellationToken);
|
||||
return this;
|
||||
}
|
||||
|
||||
private async Task LoadServer()
|
||||
{
|
||||
if (_url is null)
|
||||
{
|
||||
Message = "Url is not set";
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
Message = "Loading server entry...";
|
||||
var status = await RestService.GetAsync<ServerStatus>(_url.StatusUri, CancellationToken.None);
|
||||
|
||||
CurrentEntry = ServiceProvider.GetService<ServerEntryModelView>()!.WithData(_url, null, status);
|
||||
|
||||
Loading = false;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Message = "Error while fetching data from " + _url + " : " + e.Message;
|
||||
}
|
||||
}
|
||||
|
||||
public void ToggleFavorites()
|
||||
{
|
||||
if (CurrentEntry is null && _url is not null)
|
||||
|
||||
Reference in New Issue
Block a user