Filter serverlist
This commit is contained in:
parent
4b8a346cfb
commit
954a7eaefe
2 changed files with 62 additions and 24 deletions
|
|
@ -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<ServerConfigurationViewModel> _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<ServerConfigurationViewModel> _serverConfigurations;
|
||||
|
||||
[Reactive] public partial ServerConfigurationViewModel? SelectedServerConfiguration { get; set; }
|
||||
|
||||
public ObservableCollection<ServerConfigurationViewModel> 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<ServerConfigurationViewModel> ServerConfigurations { get; }
|
||||
|
||||
public ReactiveCommand<ServerConfigurationViewModel?, Unit> ExploreServerCommand { get; }
|
||||
public ReactiveCommand<ServerConfigurationViewModel?, Unit> EditServerCommand { get; }
|
||||
|
|
@ -52,14 +60,18 @@ public partial class ServerListViewModel : ViewModelBase
|
|||
return Unit.Default;
|
||||
});
|
||||
|
||||
EditServerCommand = ReactiveCommand.Create<ServerConfigurationViewModel?, Unit>((sc) =>
|
||||
EditServerCommand = ReactiveCommand.CreateFromTask<ServerConfigurationViewModel?, Unit>(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<bool>( );
|
||||
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<ServerConfigurationViewModel> FilterServers(string? filterText)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(filterText))
|
||||
{
|
||||
return new ObservableCollection<ServerConfigurationViewModel>(_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<ServerConfigurationViewModel>(filtered);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,6 +26,10 @@
|
|||
Command="{Binding ExploreServerCommand}"/>
|
||||
</StackPanel>
|
||||
|
||||
<TextBox Watermark="Filter servers..."
|
||||
Text="{Binding FilterText, Mode=TwoWay}"
|
||||
Margin="0,5"/>
|
||||
|
||||
<DataGrid ItemsSource="{Binding ServerConfigurations}"
|
||||
SelectedItem="{Binding SelectedServerConfiguration, Mode=TwoWay}"
|
||||
HorizontalAlignment="Stretch"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue