- tweak: Search think

This commit is contained in:
2024-12-27 08:22:17 +03:00
parent 516801840c
commit ba687e51d6
19 changed files with 372 additions and 245 deletions

View File

@@ -1,9 +1,11 @@
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Input;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Nebula.Launcher.Services;
using Nebula.Launcher.Utils;
using Nebula.Launcher.ViewHelper;
using Nebula.Launcher.Views.Pages;
@@ -12,6 +14,8 @@ namespace Nebula.Launcher.ViewModels;
[ViewRegister(typeof(AccountInfoView))]
public partial class AccountInfoViewModel : ViewModelBase
{
private readonly PopupMessageService _popupMessageService;
private readonly ConfigurationService _configurationService;
private readonly AuthService _authService;
public ObservableCollection<AuthLoginPasswordModel> Accounts { get; } = new();
@@ -25,20 +29,39 @@ public partial class AccountInfoViewModel : ViewModelBase
[ObservableProperty]
private string _currentAuthServer = String.Empty;
public ObservableCollection<string> AuthUrls { get; } = new();
[ObservableProperty] private bool _pageEnabled = true;
public AuthLoginPassword CurrentALP => new AuthLoginPassword(CurrentLogin, CurrentPassword, CurrentAuthServer);
private AuthLoginPassword CurrentAlp
{
get => new(CurrentLogin, CurrentPassword, CurrentAuthServer);
set
{
CurrentLogin = value.Login;
CurrentPassword = value.Password;
CurrentAuthServer = value.AuthServer;
}
}
//Design think
public AccountInfoViewModel()
{
AddAccount(new AuthLoginPassword("Binka","12341",""));
AuthUrls.Add("https://cinka.ru");
AuthUrls.Add("https://cinka.ru");
}
//Real think
public AccountInfoViewModel(IServiceProvider serviceProvider, AuthService authService) : base(serviceProvider)
public AccountInfoViewModel(IServiceProvider serviceProvider, PopupMessageService popupMessageService,
ConfigurationService configurationService, AuthService authService) : base(serviceProvider)
{
//_popupMessageService = mainViewModel;
_popupMessageService = popupMessageService;
_configurationService = configurationService;
_authService = authService;
ReadAuthConfig();
}
public void AuthByALP(AuthLoginPassword authLoginPassword)
@@ -52,12 +75,18 @@ public partial class AccountInfoViewModel : ViewModelBase
public async void DoAuth()
{
PageEnabled = false;
if(await _authService.Auth(CurrentALP))
Console.WriteLine("Hello, " + _authService.SelectedAuth!.Username);
_popupMessageService.PopupInfo("Auth think, please wait...");
if(await _authService.Auth(CurrentAlp))
{
_popupMessageService.ClosePopup();
_popupMessageService.PopupInfo("Hello, " + _authService.SelectedAuth!.Username);
}
else
Console.WriteLine("Shit!");
PageEnabled = true;
{
_popupMessageService.ClosePopup();
_popupMessageService.PopupInfo("Well, shit is happened");
}
}
private void AddAccount(AuthLoginPassword authLoginPassword)
@@ -78,39 +107,41 @@ public partial class AccountInfoViewModel : ViewModelBase
Accounts.Add(alpm);
}
private void ReadAuthConfig()
{
foreach (var profile in
_configurationService.GetConfigValue<AuthLoginPassword[]>(CurrentConVar.AuthProfiles)!)
{
AddAccount(profile);
}
var currProfile = _configurationService.GetConfigValue<AuthLoginPassword>(CurrentConVar.AuthProfiles);
if (currProfile != null)
{
CurrentAlp = currProfile;
DoAuth();
}
AuthUrls.Clear();
var authUrls = _configurationService.GetConfigValue<string[]>(CurrentConVar.AuthServers)!;
foreach (var url in authUrls)
{
AuthUrls.Add(url);
}
}
[RelayCommand]
public void OnSaveProfile()
{
AddAccount(CurrentALP);
AddAccount(CurrentAlp);
_configurationService.SetConfigValue(CurrentConVar.AuthProfiles, Accounts.Select(a => (AuthLoginPassword) a).ToArray());
}
}
public class Ref<T>
{
public T Value = default!;
}
public class DelegateCommand<T> : ICommand
{
private readonly Action<T> _func;
public readonly Ref<T> TRef = new();
public DelegateCommand(Action<T> func)
public string AuthItemSelect
{
_func = func;
set => CurrentAuthServer = value;
}
public bool CanExecute(object? parameter)
{
return true;
}
public void Execute(object? parameter)
{
_func(TRef.Value);
}
public event EventHandler? CanExecuteChanged;
}
public record AuthLoginPasswordModel(string Login, string Password, string AuthServer, ICommand OnSelect = default!, ICommand OnDelete = default!)

View File

@@ -15,6 +15,7 @@ public partial class InfoPopupViewModel : PopupViewModelBase
public InfoPopupViewModel(IServiceProvider serviceProvider) : base(serviceProvider)
{
}
public override string Title => "Info";
[ObservableProperty]

View File

@@ -7,7 +7,9 @@ using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.DependencyInjection;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using Microsoft.Extensions.DependencyInjection;
using Nebula.Launcher.Models;
using Nebula.Launcher.Services;
using Nebula.Launcher.ViewHelper;
using Nebula.Launcher.Views;
using Nebula.Launcher.Views.Pages;
@@ -21,39 +23,26 @@ public partial class MainViewModel : ViewModelBase
{
TryGetViewModel(typeof(AccountInfoViewModel), out var model);
_currentPage = model!;
TryGetViewModel(typeof(MessagePopupViewModel), out var viewModelBase);
_messagePopupViewModel = (MessagePopupViewModel)viewModelBase!;
Items = new ObservableCollection<ListItemTemplate>(_templates);
SelectedListItem = Items.First(vm => vm.ModelType == typeof(AccountInfoViewModel));
}
public MainViewModel(AccountInfoViewModel accountInfoViewModel, MessagePopupViewModel messagePopupViewModel,
public MainViewModel(AccountInfoViewModel accountInfoViewModel, PopupMessageService popupMessageService,
IServiceProvider serviceProvider): base(serviceProvider)
{
_currentPage = accountInfoViewModel;
_messagePopupViewModel = messagePopupViewModel;
_popupMessageService = popupMessageService;
Items = new ObservableCollection<ListItemTemplate>(_templates);
_messagePopupViewModel.OnOpenRequired += () => OnOpenRequired();
_messagePopupViewModel.OnCloseRequired += () => OnCloseRequired();
_popupMessageService.OnPopupRequired += OnPopupRequired;
SelectedListItem = Items.First(vm => vm.ModelType == typeof(AccountInfoViewModel));
}
private void OnCloseRequired()
{
IsEnabled = true;
Popup = false;
}
private void OnOpenRequired()
{
IsEnabled = false;
Popup = true;
}
private readonly Queue<PopupViewModelBase> _viewQueue = new();
private readonly List<ListItemTemplate> _templates =
[
new ListItemTemplate(typeof(AccountInfoViewModel), "Account", "Account"),
@@ -66,10 +55,15 @@ public partial class MainViewModel : ViewModelBase
[ObservableProperty]
private ViewModelBase _currentPage;
private readonly PopupMessageService _popupMessageService;
[ObservableProperty] private bool _isEnabled = true;
[ObservableProperty] private bool _popup;
private readonly MessagePopupViewModel _messagePopupViewModel;
[ObservableProperty]
private PopupViewModelBase? _currentPopup;
[ObservableProperty]
private string _currentTitle = "Default";
[ObservableProperty]
private ListItemTemplate? _selectedListItem;
@@ -84,20 +78,63 @@ public partial class MainViewModel : ViewModelBase
}
CurrentPage = vmb;
var model = GetViewModel<InfoPopupViewModel>();
model.InfoText = "Переключили прикол!";
_messagePopupViewModel.PopupMessage(model);
}
public ObservableCollection<ListItemTemplate> Items { get; }
public void PopupMessage(PopupViewModelBase viewModelBase)
{
if (CurrentPopup == null)
{
CurrentPopup = viewModelBase;
CurrentTitle = viewModelBase.Title;
OnOpenRequired();
}
else
{
_viewQueue.Enqueue(viewModelBase);
}
}
private void OnCloseRequired()
{
IsEnabled = true;
Popup = false;
}
private void OnOpenRequired()
{
IsEnabled = false;
Popup = true;
}
private void OnPopupRequired(PopupViewModelBase? viewModelBase)
{
if (viewModelBase is null)
{
ClosePopup();
}
else
{
PopupMessage(viewModelBase);
}
}
[RelayCommand]
private void TriggerPane()
{
IsPaneOpen = !IsPaneOpen;
}
[RelayCommand]
public void ClosePopup()
{
if (!_viewQueue.TryDequeue(out var viewModelBase))
OnCloseRequired();
else
CurrentTitle = viewModelBase.Title;
CurrentPopup = viewModelBase;
}
}

View File

@@ -1,68 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Input;
using Avalonia.Logging;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Nebula.Launcher.ViewHelper;
using Nebula.Launcher.Views.Popup;
namespace Nebula.Launcher.ViewModels;
[ViewRegister(typeof(MessagePopupView))]
public partial class MessagePopupViewModel : ViewModelBase
{
public MessagePopupViewModel() : base()
{
}
public MessagePopupViewModel(IServiceProvider serviceProvider) : base(serviceProvider)
{
}
public Action? OnCloseRequired;
public Action? OnOpenRequired;
public Queue<PopupViewModelBase> ViewQueue = new();
[ObservableProperty]
private PopupViewModelBase? _currentPopup;
[ObservableProperty]
private string _currentTitle = "Default";
public void PopupMessage(PopupViewModelBase viewModelBase)
{
Console.WriteLine(viewModelBase.Title);
if (CurrentPopup == null)
{
CurrentPopup = viewModelBase;
CurrentTitle = viewModelBase.Title;
OnOpenRequired?.Invoke();
}
else
{
ViewQueue.Enqueue(viewModelBase);
}
}
[RelayCommand]
private void TriggerClose()
{
ClosePopup();
}
[RelayCommand]
private void ClosePopup()
{
Console.WriteLine("Gadeem");
if (!ViewQueue.TryDequeue(out var viewModelBase))
OnCloseRequired?.Invoke();
else
CurrentTitle = viewModelBase.Title;
CurrentPopup = viewModelBase;
}
}

View File

@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using CommunityToolkit.Mvvm.ComponentModel;
using Nebula.Launcher.Models;
using Nebula.Launcher.Services;
@@ -12,7 +13,11 @@ namespace Nebula.Launcher.ViewModels;
[ViewRegister(typeof(ServerListView))]
public partial class ServerListViewModel : ViewModelBase
{
public ObservableCollection<ServerHubInfo> ServerInfos { get; } = new ObservableCollection<ServerHubInfo>();
public ObservableCollection<ServerHubInfo> ServerInfos { get; } = new();
public Action? OnSearchChange;
[ObservableProperty] private string _searchText;
[ObservableProperty]
private ServerHubInfo? _selectedListItem;
@@ -29,6 +34,12 @@ public partial class ServerListViewModel : ViewModelBase
public ServerListViewModel(IServiceProvider serviceProvider, HubService hubService) : base(serviceProvider)
{
hubService.HubServerChangedEventArgs += HubServerChangedEventArgs;
OnSearchChange += OnChangeSearch;
}
private void OnChangeSearch()
{
SortServers();
}
private void HubServerChangedEventArgs(HubServerChangedEventArgs obj)
@@ -48,13 +59,24 @@ public partial class ServerListViewModel : ViewModelBase
}
}
SortServers();
}
private void SortServers()
{
ServerInfos.Clear();
UnsortedServers.Sort(new ServerComparer());
foreach (var VARIABLE in UnsortedServers)
foreach (var server in UnsortedServers.Where(CheckServerThink))
{
ServerInfos.Add(VARIABLE);
ServerInfos.Add(server);
}
}
private bool CheckServerThink(ServerHubInfo hubInfo)
{
if (string.IsNullOrEmpty(SearchText)) return true;
return hubInfo.StatusData.Name.ToLower().Contains(SearchText.ToLower());
}
}
public class ServerComparer : IComparer<ServerHubInfo>