diff --git a/pgLabII/ViewModels/ServerListViewModel.cs b/pgLabII/ViewModels/ServerListViewModel.cs index a8cbf0d..e72bc8d 100644 --- a/pgLabII/ViewModels/ServerListViewModel.cs +++ b/pgLabII/ViewModels/ServerListViewModel.cs @@ -1,6 +1,7 @@ using System.Collections.ObjectModel; using ReactiveUI; using System.Reactive; +using System.Reactive.Linq; using Avalonia.Media; using pgLabII.Views; using ReactiveUI.SourceGenerators; @@ -9,30 +10,37 @@ namespace pgLabII.ViewModels; public partial class ServerListViewModel : ViewModelBase { + private readonly ObservableCollection _allServers = + [ + new (new() + { + Name = "pg18", + ColorEnabled = true, + Host = "localhost", + Port = 5418, + InitialDatabase = "postgres", + UserName = "postgres", + Password = "admin" + }) + { + Color = Colors.Aquamarine, + }, + new (new () + { + Name = "Bar", + ColorEnabled = false, + Host = "db.host.nl" + }), + ]; + + [Reactive] private string _filterText = ""; + + [ObservableAsProperty] + private ObservableCollection _serverConfigurations; + [Reactive] public partial ServerConfigurationViewModel? SelectedServerConfiguration { get; set; } - public ObservableCollection ServerConfigurations { get; } = - [ - new (new() - { - Name = "pg18", - ColorEnabled = true, - Host = "localhost", - Port = 5418, - InitialDatabase = "postgres", - UserName = "postgres", - Password = "admin" - }) - { - Color = Colors.Aquamarine, - }, - new (new () - { - Name = "Bar", - ColorEnabled = false, - Host = "db.host.nl" - }), - ]; + //public ObservableCollection ServerConfigurations { get; } public ReactiveCommand ExploreServerCommand { get; } public ReactiveCommand EditServerCommand { get; } @@ -52,14 +60,18 @@ public partial class ServerListViewModel : ViewModelBase return Unit.Default; }); - EditServerCommand = ReactiveCommand.Create((sc) => + EditServerCommand = ReactiveCommand.CreateFromTask(async (sc) => { var conf = sc ?? SelectedServerConfiguration; if (conf is null) return Unit.Default; EditServerConfigurationWindow window = new(new(conf)); - window.Show(); + bool result = await window.ShowDialog( ); + if (result) + { + + } return Unit.Default; }); @@ -78,5 +90,27 @@ public partial class ServerListViewModel : ViewModelBase EditServerConfigurationWindow window = new(new()); window.Show(); }); + + _serverConfigurationsHelper = this.WhenAnyValue(x => x.FilterText) + .Throttle(TimeSpan.FromMilliseconds(200)) + .Select(FilterServers) + .ToProperty(this, x => x.ServerConfigurations); } + + private ObservableCollection FilterServers(string? filterText) + { + if (string.IsNullOrWhiteSpace(filterText)) + { + return new ObservableCollection(_allServers); + } + + var filtered = _allServers.Where(s => + s.Name?.Contains(filterText, StringComparison.OrdinalIgnoreCase) is true || + s.Host?.Contains(filterText, StringComparison.OrdinalIgnoreCase) is true || + s.InitialDatabase?.Contains(filterText, StringComparison.OrdinalIgnoreCase) is true || + s.UserName?.Contains(filterText, StringComparison.OrdinalIgnoreCase) is true + ).ToList(); + + return new ObservableCollection(filtered); + } } diff --git a/pgLabII/Views/ServerListView.axaml b/pgLabII/Views/ServerListView.axaml index c7ba44d..2fe02b3 100644 --- a/pgLabII/Views/ServerListView.axaml +++ b/pgLabII/Views/ServerListView.axaml @@ -26,6 +26,10 @@ Command="{Binding ExploreServerCommand}"/> + +