- add: filter implementation

This commit is contained in:
2025-03-14 19:42:55 +03:00
parent 7e1e7bb9ad
commit 2d71cf8f38
3 changed files with 65 additions and 4 deletions

View File

@@ -23,6 +23,8 @@ public partial class ServerListViewModel : ViewModelBase, IViewModelPage
[ObservableProperty] private bool _isFavoriteMode; [ObservableProperty] private bool _isFavoriteMode;
[ObservableProperty] private bool _isFilterVisible;
public ObservableCollection<ServerEntryModelView> Servers { get; }= new(); public ObservableCollection<ServerEntryModelView> Servers { get; }= new();
public ObservableCollection<Exception> HubErrors { get; } = new(); public ObservableCollection<Exception> HubErrors { get; } = new();
@@ -36,6 +38,8 @@ public partial class ServerListViewModel : ViewModelBase, IViewModelPage
private ServerViewContainer ServerViewContainer { get; set; } private ServerViewContainer ServerViewContainer { get; set; }
private List<ServerHubInfo> UnsortedServers { get; } = new(); private List<ServerHubInfo> UnsortedServers { get; } = new();
private List<string> _filters = new();
//Design think //Design think
protected override void InitialiseInDesignMode() protected override void InitialiseInDesignMode()
@@ -60,6 +64,24 @@ public partial class ServerListViewModel : ViewModelBase, IViewModelPage
UpdateFavoriteEntries(); UpdateFavoriteEntries();
} }
public void OnFilterChanged(string name, bool active)
{
DebugService.Debug($"OnFilterChanged: {name} {active}");
if(active)
_filters.Add(name);
else
_filters.Remove(name);
if(IsFavoriteMode)
{
UpdateFavoriteEntries();
}
else
{
UpdateServerEntries();
}
}
private void HubServerLoadingError(Exception obj) private void HubServerLoadingError(Exception obj)
{ {
HubErrors.Add(obj); HubErrors.Add(obj);
@@ -74,7 +96,7 @@ public partial class ServerListViewModel : ViewModelBase, IViewModelPage
Task.Run(() => Task.Run(() =>
{ {
UnsortedServers.Sort(new ServerComparer()); UnsortedServers.Sort(new ServerComparer());
foreach (var info in UnsortedServers.Where(a => CheckServerThink(a.StatusData))) foreach (var info in UnsortedServers.Where(CheckServerThink))
{ {
var view = ServerViewContainer.Get(info.Address.ToRobustUrl(), info.StatusData); var view = ServerViewContainer.Get(info.Address.ToRobustUrl(), info.StatusData);
Servers.Add(view); Servers.Add(view);
@@ -112,14 +134,21 @@ public partial class ServerListViewModel : ViewModelBase, IViewModelPage
} }
} }
private bool CheckServerThink(ServerStatus hubInfo) private bool CheckServerThink(ServerHubInfo hubInfo)
{ {
if (string.IsNullOrEmpty(SearchText)) return true; var isNameEqual = string.IsNullOrEmpty(SearchText) || hubInfo.StatusData.Name.ToLower().Contains(SearchText.ToLower());
return hubInfo.Name.ToLower().Contains(SearchText.ToLower());
if (_filters.Count == 0) return isNameEqual;
if(_filters.Select(t=>t.Replace('_',':').Replace("ERPYes","18+")).Any(t=>hubInfo.StatusData.Tags.Contains(t) || hubInfo.InferredTags.Contains(t)))
return isNameEqual;
return false;
} }
public void FilterRequired() public void FilterRequired()
{ {
IsFilterVisible = !IsFilterVisible;
} }
public void AddFavoriteRequired() public void AddFavoriteRequired()

View File

@@ -34,6 +34,28 @@
Padding="0" /> Padding="0" />
</StackPanel> </StackPanel>
</ScrollViewer> </ScrollViewer>
<Border Grid.Row="0"
Background="{StaticResource DefaultGrad}"
Margin="0,0,0,0" CornerRadius="20,20,0,0"
VerticalAlignment="Bottom"
IsVisible="{Binding IsFilterVisible}">
<StackPanel Orientation="Vertical" Spacing="2" Margin="15">
<StackPanel Orientation="Horizontal" Spacing="15">
<TextBlock Text="Roleplay:" VerticalAlignment="Center" Margin="0,0,15,0"/>
<CheckBox Click="Button_OnClick" Name="rp_none"><TextBlock Text="NonRP"/></CheckBox>
<CheckBox Click="Button_OnClick" Name="rp_low"><TextBlock Text="LowRP"/></CheckBox>
<CheckBox Click="Button_OnClick" Name="rp_med"><TextBlock Text="MediumRP"/></CheckBox>
<CheckBox Click="Button_OnClick" Name="rp_high"><TextBlock Text="HardRP"/></CheckBox>
<CheckBox Click="Button_OnClick" Name="ERPYes"><TextBlock Text="18+"/></CheckBox>
</StackPanel>
<StackPanel Orientation="Horizontal" Spacing="15">
<TextBlock Text="Language:" VerticalAlignment="Center" Margin="0,0,4,0"/>
<CheckBox Click="Button_OnClick" Name="lang_ru"><TextBlock Text="RU"/></CheckBox>
<CheckBox Click="Button_OnClick" Name="lang_en"><TextBlock Text="EN"/></CheckBox>
</StackPanel>
</StackPanel>
</Border>
<Border <Border
Background="{StaticResource DefaultGrad}" Background="{StaticResource DefaultGrad}"

View File

@@ -1,4 +1,7 @@
using System;
using System.Collections.Generic;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Interactivity;
using ServerListViewModel = Nebula.Launcher.ViewModels.Pages.ServerListViewModel; using ServerListViewModel = Nebula.Launcher.ViewModels.Pages.ServerListViewModel;
namespace Nebula.Launcher.Views.Pages; namespace Nebula.Launcher.Views.Pages;
@@ -23,4 +26,11 @@ public partial class ServerListView : UserControl
var context = (ServerListViewModel?)DataContext; var context = (ServerListViewModel?)DataContext;
context?.OnSearchChange?.Invoke(); context?.OnSearchChange?.Invoke();
} }
private void Button_OnClick(object? sender, RoutedEventArgs e)
{
var send = sender as CheckBox;
var context = (ServerListViewModel?)DataContext;
context?.OnFilterChanged(send.Name, send.IsChecked.Value);
}
} }