- tweak: Favorite button

This commit is contained in:
2025-01-29 12:32:42 +03:00
parent 730e2e5529
commit 37ff25ba70
18 changed files with 233 additions and 75 deletions

View File

@@ -16,7 +16,7 @@ namespace Nebula.Launcher.ViewModels.Pages;
[ViewModelRegister(typeof(AccountInfoView))]
[ConstructGenerator]
public partial class AccountInfoViewModel : ViewModelBase
public partial class AccountInfoViewModel : ViewModelBase, IViewModelPage
{
[ObservableProperty] private bool _authMenuExpand;
@@ -190,6 +190,11 @@ public partial class AccountInfoViewModel : ViewModelBase
ConfigurationService.SetConfigValue(CurrentConVar.AuthProfiles,
Accounts.Select(a => (AuthLoginPassword)a).ToArray());
}
public void OnPageOpen(object? args)
{
}
}
public record AuthLoginPasswordModel(

View File

@@ -22,7 +22,7 @@ namespace Nebula.Launcher.ViewModels.Pages;
[ViewModelRegister(typeof(ContentBrowserView))]
[ConstructGenerator]
public sealed partial class ContentBrowserViewModel : ViewModelBase
public sealed partial class ContentBrowserViewModel : ViewModelBase , IViewModelPage
{
private readonly List<ContentEntry> _root = new();
@@ -204,6 +204,10 @@ public sealed partial class ContentBrowserViewModel : ViewModelBase
_history.RemoveAt(0);
return h;
}
public void OnPageOpen(object? args)
{
}
}
public class ContentEntry

View File

@@ -1,17 +0,0 @@
using System.Collections.ObjectModel;
using Nebula.Shared.Models;
namespace Nebula.Launcher.ViewModels.Pages;
public class FavoriteServerListViewModel : ViewModelBase
{
public ObservableCollection<ServerHubInfo> Servers = new();
protected override void Initialise()
{
}
protected override void InitialiseInDesignMode()
{
}
}

View File

@@ -0,0 +1,6 @@
namespace Nebula.Launcher.ViewModels.Pages;
public interface IViewModelPage
{
public void OnPageOpen(object? args);
}

View File

@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
@@ -32,6 +33,7 @@ public partial class ServerListViewModel
{
var model = ViewHelperService.GetViewModel<ServerEntryModelView>().WithData(server.Item1, server.Item2);
model.OnFavoriteToggle += ()=> RemoveFavorite(model);
model.IsFavorite = true;
return model;
}
@@ -51,23 +53,37 @@ public partial class ServerListViewModel
foreach (var server in servers)
{
var uri = server.ToRobustUrl();
var serverInfo = await RestService.GetAsync<ServerStatus>(uri.StatusUri, CancellationToken.None);
if (serverInfo.Value is null)
try
{
continue;
}
var serverInfo = await RestService.GetAsync<ServerStatus>(uri.StatusUri, CancellationToken.None);
if (serverInfo.Value is null)
{
throw new Exception("Server info is null");
}
FavoriteServers.Add((uri, serverInfo.Value));
FavoriteServers.Add((uri, serverInfo.Value));
}
catch (Exception e)
{
FavoriteServers.Add((uri, new ServerStatus("ErrorLand",$"ERROR: {e.Message}",[],"",-1,-1,-1,false,DateTime.Now, -1)));
}
}
SortFavorite();
}
public void AddFavorite(ServerEntryModelView entryModelView)
{
entryModelView.IsFavorite = true;
AddFavorite(entryModelView.Address);
}
public void AddFavorite(RobustUrl robustUrl)
{
var servers = (ConfigurationService.GetConfigValue(CurrentConVar.Favorites) ?? []).ToList();
servers.Add(entryModelView.Address.ToString());
servers.Add(robustUrl.ToString());
ConfigurationService.SetConfigValue(CurrentConVar.Favorites, servers.ToArray());
FetchFavorite();
}
public void RemoveFavorite(ServerEntryModelView entryModelView)
@@ -75,5 +91,7 @@ public partial class ServerListViewModel
var servers = (ConfigurationService.GetConfigValue(CurrentConVar.Favorites) ?? []).ToList();
servers.Remove(entryModelView.Address.ToString());
ConfigurationService.SetConfigValue(CurrentConVar.Favorites, servers.ToArray());
entryModelView.IsFavorite = false;
FetchFavorite();
}
}

View File

@@ -6,6 +6,7 @@ using System.Linq;
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.ComponentModel;
using Nebula.Launcher.Services;
using Nebula.Launcher.ViewModels.Popup;
using Nebula.Launcher.Views.Pages;
using Nebula.Shared.Models;
using Nebula.Shared.Services;
@@ -15,14 +16,17 @@ namespace Nebula.Launcher.ViewModels.Pages;
[ViewModelRegister(typeof(ServerListView))]
[ConstructGenerator]
public partial class ServerListViewModel : ViewModelBase
public partial class ServerListViewModel : ViewModelBase, IViewModelPage
{
[ObservableProperty] private string _searchText = string.Empty;
[ObservableProperty] private bool _isFavoriteMode;
public Action? OnSearchChange;
[GenerateProperty] private HubService HubService { get; } = default!;
[GenerateProperty] private PopupMessageService PopupMessageService { get; }
[GenerateProperty, DesignConstruct] private ViewHelperService ViewHelperService { get; } = default!;
public ObservableCollection<ServerEntryModelView> ServerInfos { get; } = new();
public ObservableCollection<ServerEntryModelView> SortedServers { get; } = new();
private List<ServerHubInfo> UnsortedServers { get; } = new();
//Design think
@@ -31,11 +35,11 @@ public partial class ServerListViewModel : ViewModelBase
FavoriteVisible = true;
SortedFavoriteServers.Add(GetServerEntryModelView(("ss14://localhost".ToRobustUrl(),
new ServerStatus("Nebula", "TestCraft", ["16+", "RU"], "super", 12, 55, 1, false, DateTime.Now, 20))));
ServerInfos.Add(CreateServerView(new ServerHubInfo("ss14://localhost",
SortedServers.Add(CreateServerView(new ServerHubInfo("ss14://localhost",
new ServerStatus("Nebula", "TestCraft", ["16+", "RU"], "super", 12, 55, 1, false, DateTime.Now, 20), [])));
ServerInfos.Add(CreateServerView(new ServerHubInfo("ss14://localhost",
SortedServers.Add(CreateServerView(new ServerHubInfo("ss14://localhost",
new ServerStatus("Nebula", "TestCraft", ["16+", "RU"], "super", 12, 55, 1, false, DateTime.Now, 20), [])));
ServerInfos.Add(CreateServerView(new ServerHubInfo("ss14://localhost",
SortedServers.Add(CreateServerView(new ServerHubInfo("ss14://localhost",
new ServerStatus("Nebula", "TestCraft", ["16+", "RU"], "super", 12, 55, 1, false, DateTime.Now, 20), [])));
}
@@ -74,9 +78,9 @@ public partial class ServerListViewModel : ViewModelBase
{
Task.Run(() =>
{
ServerInfos.Clear();
SortedServers.Clear();
UnsortedServers.Sort(new ServerComparer());
foreach (var server in UnsortedServers.Where(a => CheckServerThink(a.StatusData))) ServerInfos.Add(CreateServerView(server));
foreach (var server in UnsortedServers.Where(a => CheckServerThink(a.StatusData))) SortedServers.Add(CreateServerView(server));
});
}
@@ -97,10 +101,24 @@ public partial class ServerListViewModel : ViewModelBase
{
}
public void AddFavoriteRequired()
{
var p = ViewHelperService.GetViewModel<AddFavoriteViewModel>();
PopupMessageService.Popup(p);
}
public void UpdateRequired()
{
Task.Run(HubService.UpdateHub);
}
public void OnPageOpen(object? args)
{
if (args is bool fav)
{
IsFavoriteMode = fav;
}
}
}
public class ServerComparer : IComparer<ServerHubInfo>, IComparer<ServerStatus>, IComparer<(RobustUrl,ServerStatus)>