- 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

@@ -20,26 +20,21 @@ public partial class MainViewModel : ViewModelBase
{
private readonly List<ListItemTemplate> _templates =
[
new ListItemTemplate(typeof(AccountInfoViewModel), "user", "Account"),
new ListItemTemplate(typeof(ServerListViewModel), "file", "Servers"),
new ListItemTemplate(typeof(ContentBrowserViewModel), "folder", "Content")
new ListItemTemplate(typeof(AccountInfoViewModel), "user", "Account", null),
new ListItemTemplate(typeof(ServerListViewModel), "file", "Servers", false),
new ListItemTemplate(typeof(ServerListViewModel), "star", "Favorites", true),
new ListItemTemplate(typeof(ContentBrowserViewModel), "folder", "Content", null)
];
private readonly List<PopupViewModelBase> _viewQueue = new();
[ObservableProperty] private ViewModelBase _currentPage;
[ObservableProperty] private PopupViewModelBase? _currentPopup;
[ObservableProperty] private string _currentTitle = "Default";
[ObservableProperty] private bool _isEnabled = true;
[ObservableProperty] private bool _isPaneOpen;
[ObservableProperty] private bool _isPopupClosable = true;
[ObservableProperty] private bool _popup;
[ObservableProperty] private ListItemTemplate? _selectedListItem;
[GenerateProperty] private DebugService DebugService { get; } = default!;
@@ -57,22 +52,19 @@ public partial class MainViewModel : ViewModelBase
protected override void Initialise()
{
CurrentPage = ViewHelperService.GetViewModel<AccountInfoViewModel>();
Items = new ObservableCollection<ListItemTemplate>(_templates);
InitialiseInDesignMode();
PopupMessageService.OnPopupRequired += OnPopupRequired;
PopupMessageService.OnCloseRequired += OnPopupCloseRequired;
SelectedListItem = Items.First(vm => vm.ModelType == typeof(AccountInfoViewModel));
}
partial void OnSelectedListItemChanged(ListItemTemplate? value)
{
if (value is null) return;
if (!ViewHelperService.TryGetViewModel(value.ModelType, out var vmb)) return;
if (!ViewHelperService.TryGetViewModel(value.ModelType, out var vmb) || vmb is not IViewModelPage viewModelPage) return;
viewModelPage.OnPageOpen(value.args);
CurrentPage = vmb;
}

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)>

View File

@@ -0,0 +1,47 @@
using System;
using CommunityToolkit.Mvvm.ComponentModel;
using Nebula.Launcher.ViewModels.Pages;
using Nebula.Launcher.Views.Pages;
using Nebula.Shared.Services;
using Nebula.Shared.Utils;
using AddFavoriteView = Nebula.Launcher.Views.Popup.AddFavoriteView;
namespace Nebula.Launcher.ViewModels.Popup;
[ViewModelRegister(typeof(AddFavoriteView), false)]
[ConstructGenerator]
public partial class AddFavoriteViewModel : PopupViewModelBase
{
protected override void InitialiseInDesignMode()
{
}
protected override void Initialise()
{
}
[GenerateProperty]
public override PopupMessageService PopupMessageService { get; }
[GenerateProperty] private ServerListViewModel ServerListViewModel { get; }
[GenerateProperty] private DebugService DebugService { get; }
public override string Title => "Add to favorite";
public override bool IsClosable => true;
[ObservableProperty] private string _ipInput;
[ObservableProperty] private string _error = "";
public void OnEnter()
{
try
{
var uri = IpInput.ToRobustUrl();
ServerListViewModel.AddFavorite(uri);
Dispose();
}
catch (Exception e)
{
Error = e.Message;
DebugService.Error(e);
}
}
}

View File

@@ -38,6 +38,7 @@ public partial class ServerEntryModelView : ViewModelBase
[ObservableProperty] private string _description = "Fetching info...";
[ObservableProperty] private bool _expandInfo = false;
[ObservableProperty] private bool _tagDataVisible = false;
[ObservableProperty] private bool _isFavorite = false;
public ServerStatus Status { get; set; } =
new("", "", [], "", -1, -1, -1, false, DateTime.Now, -1);
@@ -51,10 +52,18 @@ public partial class ServerEntryModelView : ViewModelBase
{
if (_serverInfo == null)
{
var result =
await RestService.GetAsync<ServerInfo>(Address.InfoUri, CancellationService.Token);
if (result.Value == null) return null;
_serverInfo = result.Value;
try
{
var result =
await RestService.GetAsync<ServerInfo>(Address.InfoUri, CancellationService.Token);
if (result.Value == null) return null;
_serverInfo = result.Value;
}
catch (Exception e)
{
Description = e.Message;
DebugService.Error(e);
}
}
return _serverInfo;
@@ -248,7 +257,6 @@ public partial class ServerEntryModelView : ViewModelBase
var info = await GetServerInfo();
if (info == null)
{
Description = "Error think!";
return;
}