- add: Favorites - start

This commit is contained in:
2025-01-28 19:59:35 +03:00
parent e1657d9234
commit 730e2e5529
11 changed files with 216 additions and 91 deletions

View File

@@ -2,4 +2,5 @@
<SolidColorBrush x:Key="DefaultBackground">#222222</SolidColorBrush>
<SolidColorBrush x:Key="DefaultForeground">#121212</SolidColorBrush>
<SolidColorBrush x:Key="DefaultSelected">#D95F59</SolidColorBrush>
<BoxShadows x:Key="DefaultShadow">0 0 10 -1 #121212</BoxShadows>
</ResourceDictionary>

View File

@@ -0,0 +1,79 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading;
using CommunityToolkit.Mvvm.ComponentModel;
using Nebula.Shared;
using Nebula.Shared.Models;
using Nebula.Shared.Services;
using Nebula.Shared.Utils;
namespace Nebula.Launcher.ViewModels.Pages;
public partial class ServerListViewModel
{
[GenerateProperty] private ConfigurationService ConfigurationService { get; }
[GenerateProperty] private RestService RestService { get; }
[ObservableProperty] private bool _favoriteVisible = false;
public List<(RobustUrl,ServerStatus)> FavoriteServers = new();
public ObservableCollection<ServerEntryModelView> SortedFavoriteServers { get; } = new();
private void SortFavorite()
{
SortedFavoriteServers.Clear();
FavoriteServers.Sort(new ServerComparer());
foreach (var server in FavoriteServers.Where(a => CheckServerThink(a.Item2)))
SortedFavoriteServers.Add(GetServerEntryModelView(server));
}
private ServerEntryModelView GetServerEntryModelView((RobustUrl, ServerStatus) server)
{
var model = ViewHelperService.GetViewModel<ServerEntryModelView>().WithData(server.Item1, server.Item2);
model.OnFavoriteToggle += ()=> RemoveFavorite(model);
return model;
}
private async void FetchFavorite()
{
FavoriteServers.Clear();
var servers = ConfigurationService.GetConfigValue(CurrentConVar.Favorites);
if (servers is null || servers.Length == 0)
{
FavoriteVisible = false;
return;
}
FavoriteVisible = true;
foreach (var server in servers)
{
var uri = server.ToRobustUrl();
var serverInfo = await RestService.GetAsync<ServerStatus>(uri.StatusUri, CancellationToken.None);
if (serverInfo.Value is null)
{
continue;
}
FavoriteServers.Add((uri, serverInfo.Value));
}
SortFavorite();
}
public void AddFavorite(ServerEntryModelView entryModelView)
{
var servers = (ConfigurationService.GetConfigValue(CurrentConVar.Favorites) ?? []).ToList();
servers.Add(entryModelView.Address.ToString());
ConfigurationService.SetConfigValue(CurrentConVar.Favorites, servers.ToArray());
}
public void RemoveFavorite(ServerEntryModelView entryModelView)
{
var servers = (ConfigurationService.GetConfigValue(CurrentConVar.Favorites) ?? []).ToList();
servers.Remove(entryModelView.Address.ToString());
ConfigurationService.SetConfigValue(CurrentConVar.Favorites, servers.ToArray());
}
}

View File

@@ -1,4 +1,5 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
@@ -8,6 +9,7 @@ using Nebula.Launcher.Services;
using Nebula.Launcher.Views.Pages;
using Nebula.Shared.Models;
using Nebula.Shared.Services;
using Nebula.Shared.Utils;
namespace Nebula.Launcher.ViewModels.Pages;
@@ -26,6 +28,9 @@ public partial class ServerListViewModel : ViewModelBase
//Design think
protected override void InitialiseInDesignMode()
{
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",
new ServerStatus("Nebula", "TestCraft", ["16+", "RU"], "super", 12, 55, 1, false, DateTime.Now, 20), [])));
ServerInfos.Add(CreateServerView(new ServerHubInfo("ss14://localhost",
@@ -44,12 +49,14 @@ public partial class ServerListViewModel : ViewModelBase
OnSearchChange += OnChangeSearch;
if (!HubService.IsUpdating) SortServers();
FetchFavorite();
}
private void OnChangeSearch()
{
if(string.IsNullOrEmpty(SearchText)) return;
SortServers();
SortFavorite();
}
private void HubServerChangedEventArgs(HubServerChangedEventArgs obj)
@@ -69,20 +76,20 @@ public partial class ServerListViewModel : ViewModelBase
{
ServerInfos.Clear();
UnsortedServers.Sort(new ServerComparer());
foreach (var server in UnsortedServers.Where(CheckServerThink)) ServerInfos.Add(CreateServerView(server));
foreach (var server in UnsortedServers.Where(a => CheckServerThink(a.StatusData))) ServerInfos.Add(CreateServerView(server));
});
}
private bool CheckServerThink(ServerHubInfo hubInfo)
private bool CheckServerThink(ServerStatus hubInfo)
{
if (string.IsNullOrEmpty(SearchText)) return true;
return hubInfo.StatusData.Name.ToLower().Contains(SearchText.ToLower());
return hubInfo.Name.ToLower().Contains(SearchText.ToLower());
}
private ServerEntryModelView CreateServerView(ServerHubInfo serverHubInfo)
{
var svn = ViewHelperService.GetViewModel<ServerEntryModelView>();
svn.ServerHubInfo = serverHubInfo;
var svn = ViewHelperService.GetViewModel<ServerEntryModelView>().WithData(serverHubInfo);
svn.OnFavoriteToggle += () => AddFavorite(svn);
return svn;
}
@@ -96,7 +103,7 @@ public partial class ServerListViewModel : ViewModelBase
}
}
public class ServerComparer : IComparer<ServerHubInfo>
public class ServerComparer : IComparer<ServerHubInfo>, IComparer<ServerStatus>, IComparer<(RobustUrl,ServerStatus)>
{
public int Compare(ServerHubInfo? x, ServerHubInfo? y)
{
@@ -107,6 +114,30 @@ public class ServerComparer : IComparer<ServerHubInfo>
if (ReferenceEquals(null, x))
return -1;
return y.StatusData.Players.CompareTo(x.StatusData.Players);
return Compare(x.StatusData, y.StatusData);
}
public int Compare(ServerStatus? x, ServerStatus? y)
{
if (ReferenceEquals(x, y))
return 0;
if (ReferenceEquals(null, y))
return 1;
if (ReferenceEquals(null, x))
return -1;
return y.Players.CompareTo(x.Players);
}
public int Compare((RobustUrl, ServerStatus) x, (RobustUrl, ServerStatus) y)
{
if (ReferenceEquals(x, y))
return 0;
if (ReferenceEquals(null, y))
return 1;
if (ReferenceEquals(null, x))
return -1;
return Compare(x.Item2, y.Item2);
}
}

View File

@@ -1,18 +1,13 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Input;
using Avalonia;
using Avalonia.Media;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Nebula.Launcher.Services;
using Nebula.Launcher.ViewModels.Popup;
using Nebula.Launcher.Views;
@@ -27,8 +22,9 @@ namespace Nebula.Launcher.ViewModels;
public partial class ServerEntryModelView : ViewModelBase
{
private Process? _p;
public RobustUrl Address { get; private set; }
public Action? OnFavoriteToggle;
public LogPopupModelView CurrLog;
[GenerateProperty] private AuthService AuthService { get; } = default!;
[GenerateProperty] private ContentService ContentService { get; } = default!;
@@ -40,8 +36,13 @@ public partial class ServerEntryModelView : ViewModelBase
[GenerateProperty] private RestService RestService { get; } = default!;
[ObservableProperty] private string _description = "Fetching info...";
public ObservableCollection<ServerLink> Links { get; } = new();
[ObservableProperty] private bool _expandInfo = false;
[ObservableProperty] private bool _tagDataVisible = false;
public ServerStatus Status { get; set; } =
new("", "", [], "", -1, -1, -1, false, DateTime.Now, -1);
public ObservableCollection<ServerLink> Links { get; } = new();
public bool RunVisible => Process == null;
private ServerInfo? _serverInfo = null;
@@ -51,7 +52,7 @@ public partial class ServerEntryModelView : ViewModelBase
if (_serverInfo == null)
{
var result =
await RestService.GetAsync<ServerInfo>(ServerHubInfo.Address.ToRobustUrl().InfoUri, CancellationService.Token);
await RestService.GetAsync<ServerInfo>(Address.InfoUri, CancellationService.Token);
if (result.Value == null) return null;
_serverInfo = result.Value;
}
@@ -59,27 +60,6 @@ public partial class ServerEntryModelView : ViewModelBase
return _serverInfo;
}
private ServerHubInfo _serverHubInfo = default!;
public ServerHubInfo ServerHubInfo
{
get => _serverHubInfo;
set
{
Tags.Clear();
foreach (var tag in value.StatusData.Tags)
{
Tags.Add(tag);
}
foreach (var tag in value.InferredTags)
{
Tags.Add(tag);
}
_serverHubInfo = value;
OnPropertyChanged(nameof(ServerHubInfo));
}
}
public ObservableCollection<string> Tags { get; } = [];
public ICommand OnLinkGo { get; }= new LinkGoCommand();
@@ -98,13 +78,12 @@ public partial class ServerEntryModelView : ViewModelBase
{
Description = "Server of meow girls! Nya~ \nNyaMeow\nOOOINK!!";
Links.Add(new ServerLink("Discord","discord","https://cinka.ru"));
ServerHubInfo = new ServerHubInfo("ss14://localhost",
new ServerStatus("Ameba",
"Locala meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow ",
["rp:hrp", "18+"],
"Antag", 15, 5, 1, false
, DateTime.Now, 100),
["meow:rp"]);
Status = new ServerStatus("Ameba",
"Locala meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow ",
["rp:hrp", "18+"],
"Antag", 15, 5, 1, false
, DateTime.Now, 100);
Address = "ss14://localhost".ToRobustUrl();
}
protected override void Initialise()
@@ -112,6 +91,41 @@ public partial class ServerEntryModelView : ViewModelBase
CurrLog = ViewHelperService.GetViewModel<LogPopupModelView>();
}
public ServerEntryModelView WithData(ServerHubInfo value)
{
Status = value.StatusData;
Address = value.Address.ToRobustUrl();
Tags.Clear();
foreach (var tag in Status.Tags)
{
Tags.Add(tag);
}
foreach (var tag in value.InferredTags)
{
Tags.Add(tag);
}
return this;
}
public ServerEntryModelView WithData(RobustUrl url, ServerStatus serverStatus)
{
Status = serverStatus;
Address = url;
Tags.Clear();
foreach (var tag in Status.Tags)
{
Tags.Add(tag);
}
return this;
}
public void ToggleFavorites()
{
OnFavoriteToggle?.Invoke();
}
public void RunInstance()
{
Task.Run(RunAsync);
@@ -124,7 +138,7 @@ public partial class ServerEntryModelView : ViewModelBase
var authProv = AuthService.SelectedAuth;
var buildInfo =
await ContentService.GetBuildInfo(new RobustUrl(ServerHubInfo.Address), CancellationService.Token);
await ContentService.GetBuildInfo(Address, CancellationService.Token);
using (var loadingContext = ViewHelperService.GetViewModel<LoadingContextViewModel>())
{
@@ -145,7 +159,7 @@ public partial class ServerEntryModelView : ViewModelBase
{ "ROBUST_AUTH_TOKEN", authProv?.Token.Token },
{ "ROBUST_AUTH_SERVER", authProv?.AuthLoginPassword.AuthServer },
{ "ROBUST_AUTH_PUBKEY", buildInfo.BuildInfo.Auth.PublicKey },
{ "GAME_URL", ServerHubInfo.Address },
{ "GAME_URL", Address.ToString() },
{ "AUTH_LOGIN", authProv?.AuthLoginPassword.Login }
},
CreateNoWindow = true,

View File

@@ -1,5 +1,5 @@
<UserControl
Background="{StaticResource DefaultBackground}"
Background="#1a1a1a"
d:DesignHeight="450"
d:DesignWidth="800"
mc:Ignorable="d"
@@ -39,7 +39,7 @@
<SplitView.Pane>
<Border
Background="{StaticResource DefaultBackground}"
BoxShadow="0 0 5 0 #121212"
BoxShadow="0 0 15 -2 #121212"
CornerRadius="0,8,8,0"
Grid.Column="0"
Grid.Row="0"
@@ -79,7 +79,7 @@
<Border
Background="{StaticResource DefaultBackground}"
BoxShadow="0 0 5 0 #121212"
BoxShadow="{StaticResource DefaultShadow}"
CornerRadius="0,0,0,0"
Grid.Column="0"
Grid.ColumnSpan="2"
@@ -103,7 +103,7 @@
https://cinka.ru/nebula-launcher/
</TextBlock>
</Button>
<TextBlock HorizontalAlignment="Right" VerticalAlignment="Center">prototype-product-v0.01</TextBlock>
<TextBlock HorizontalAlignment="Right" VerticalAlignment="Center">v0.02-a</TextBlock>
</Panel>
</Label>
</Border>
@@ -118,7 +118,7 @@
<Grid RowDefinitions="35,*,20">
<Border
BorderBrush="{StaticResource DefaultForeground}"
BorderThickness="0,0,0,2"
BoxShadow="0 2 15 -2 #121212"
CornerRadius="10,10,0,0"
Grid.Row="0">
<Panel Margin="12,0,0,0" VerticalAlignment="Center">
@@ -141,7 +141,7 @@
<Border
BorderBrush="{StaticResource DefaultForeground}"
BorderThickness="0,2,0,0"
BoxShadow="0 -2 15 -2 #121212"
CornerRadius="0,0,10,10"
Grid.Row="2">
<Panel Margin="12,0,12,0" VerticalAlignment="Center">

View File

@@ -13,12 +13,13 @@
<pages:AccountInfoViewModel />
</Design.DataContext>
<Grid
ColumnDefinitions="*,1.5*"
ColumnDefinitions="*,1*"
Margin="15"
RowDefinitions="*">
<StackPanel Grid.Column="1" Grid.Row="0">
<Border
BoxShadow="0 -1 15 0 #121212"
Background="{StaticResource DefaultBackground}"
BoxShadow="0 -1 15 -2 #121212"
CornerRadius="10,10,0,0"
Margin="5,5,5,0"
Padding="5">
@@ -32,7 +33,8 @@
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type pages:AuthLoginPasswordModel}">
<Border
BoxShadow="0 1 15 0 #121212"
Background="{StaticResource DefaultBackground}"
BoxShadow="0 1 15 -2 #121212"
CornerRadius="0,10,0,10"
Margin="5,5,5,5"
VerticalAlignment="Center">
@@ -74,7 +76,8 @@
Grid.ColumnSpan="{Binding AuthViewSpan}"
Grid.Row="0">
<Border
BoxShadow="0 0 15 0 #121212"
Background="{StaticResource DefaultBackground}"
BoxShadow="{StaticResource DefaultShadow}"
CornerRadius="10"
Margin="5"
Padding="15">
@@ -109,7 +112,7 @@
</StackPanel>
<Border
BoxShadow="0 0 5 0 #121212"
BoxShadow="{StaticResource DefaultShadow}"
CornerRadius="10"
IsVisible="{Binding AuthUrlConfigExpand}">
<ScrollViewer Height="80">
@@ -129,7 +132,7 @@
</ListBox>
</ScrollViewer>
</Border>
<Border BoxShadow="0 0 5 0 #121212">
<Border Background="{StaticResource DefaultSelected}" BoxShadow="{StaticResource DefaultShadow}">
<Button
Command="{Binding DoAuth}"
HorizontalAlignment="Stretch"
@@ -160,12 +163,12 @@
Margin="5,20,5,5"
Orientation="Horizontal"
Spacing="5">
<Border BoxShadow="0 0 5 0 #121212">
<Border BoxShadow="{StaticResource DefaultShadow}">
<Button Command="{Binding Logout}">
<Label>Logout</Label>
</Button>
</Border>
<Border BoxShadow="0 0 5 0 #121212">
<Border BoxShadow="{StaticResource DefaultShadow}">
<Button Command="{Binding SaveProfileCommand}">
<Label>Save profile</Label>
</Button>

View File

@@ -1,11 +0,0 @@
<UserControl
d:DesignHeight="450"
d:DesignWidth="800"
mc:Ignorable="d"
x:Class="Nebula.Launcher.Views.Pages.FavoriteServerListView"
xmlns="https://github.com/avaloniaui"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
Welcome to Avalonia!
</UserControl>

View File

@@ -1,11 +0,0 @@
using Avalonia.Controls;
namespace Nebula.Launcher.Views.Pages;
public partial class FavoriteServerListView : UserControl
{
public FavoriteServerListView()
{
InitializeComponent();
}
}

View File

@@ -22,7 +22,24 @@
Grid.RowSpan="2"
Margin="0,0,0,10"
Padding="0,0,10,0">
<ItemsControl ItemsSource="{Binding ServerInfos}" Padding="0" />
<StackPanel Spacing="5">
<Border
Background="{StaticResource DefaultSelected}"
BoxShadow="{StaticResource DefaultShadow}"
IsVisible="{Binding FavoriteVisible}"
Margin="5">
<StackPanel Margin="0,10,0,10" Spacing="5">
<Label
HorizontalAlignment="Center"
Margin="5"
VerticalAlignment="Center">
Favorites
</Label>
<ItemsControl ItemsSource="{Binding SortedFavoriteServers}" Padding="0" />
</StackPanel>
</Border>
<ItemsControl ItemsSource="{Binding ServerInfos}" Padding="0" />
</StackPanel>
</ScrollViewer>
<Border

View File

@@ -19,8 +19,8 @@
</Design.DataContext>
<Border
Background="Transparent"
BoxShadow="-2 0 18 -1 #121212"
Background="{StaticResource DefaultBackground}"
BoxShadow="-2 0 5 -1 #121212"
CornerRadius="10"
Margin="5">
<Grid ColumnDefinitions="*,80,50" RowDefinitions="35,*,*">
@@ -46,7 +46,7 @@
VerticalScrollBarVisibility="Disabled"
x:Name="AutoScrollViewer">
<Label VerticalAlignment="Center">
<TextBlock Text="{Binding ServerHubInfo.StatusData.Name}" />
<TextBlock Text="{Binding Status.Name}" />
</Label>
</ScrollViewer>
</Button>
@@ -58,15 +58,14 @@
Orientation="Horizontal"
VerticalAlignment="Center">
<Label>
<TextBlock Text="{Binding ServerHubInfo.StatusData.Players}" />
<TextBlock Text="{Binding Status.Players}" />
</Label>
<Label>/</Label>
<Label>
<TextBlock Text="{Binding ServerHubInfo.StatusData.SoftMaxPlayers}" />
<TextBlock Text="{Binding Status.SoftMaxPlayers}" />
</Label>
</StackPanel>
<Panel
Grid.Column="2"
Grid.Row="0"
@@ -152,7 +151,7 @@
Margin="5"
VerticalAlignment="Center">
<Label FontSize="10" Margin="5,0,5,0">
<TextBlock Text="{Binding ServerHubInfo.StatusData.Map}" />
<TextBlock Text="{Binding Status.Map}" />
</Label>
</Border>
</StackPanel>
@@ -167,7 +166,7 @@
Margin="5"
VerticalAlignment="Center">
<Label FontSize="10" Margin="5,0,5,0">
<TextBlock Text="{Binding ServerHubInfo.StatusData.Preset}" />
<TextBlock Text="{Binding Status.Preset}" />
</Label>
</Border>
</StackPanel>

View File

@@ -29,4 +29,7 @@ public static class CurrentConVar
public static readonly ConVar<AuthLoginPassword> AuthCurrent =
ConVarBuilder.Build<AuthLoginPassword>("auth.current");
public static readonly ConVar<string[]> Favorites =
ConVarBuilder.Build<string[]>("server.favorites", []);
}