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 System.Collections.ObjectModel;
|
||||||
using ReactiveUI;
|
using ReactiveUI;
|
||||||
using System.Reactive;
|
using System.Reactive;
|
||||||
|
using System.Reactive.Linq;
|
||||||
using Avalonia.Media;
|
using Avalonia.Media;
|
||||||
using pgLabII.Views;
|
using pgLabII.Views;
|
||||||
using ReactiveUI.SourceGenerators;
|
using ReactiveUI.SourceGenerators;
|
||||||
|
|
@ -9,30 +10,37 @@ namespace pgLabII.ViewModels;
|
||||||
|
|
||||||
public partial class ServerListViewModel : ViewModelBase
|
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; }
|
[Reactive] public partial ServerConfigurationViewModel? SelectedServerConfiguration { get; set; }
|
||||||
|
|
||||||
public ObservableCollection<ServerConfigurationViewModel> ServerConfigurations { get; } =
|
//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 ReactiveCommand<ServerConfigurationViewModel?, Unit> ExploreServerCommand { get; }
|
public ReactiveCommand<ServerConfigurationViewModel?, Unit> ExploreServerCommand { get; }
|
||||||
public ReactiveCommand<ServerConfigurationViewModel?, Unit> EditServerCommand { get; }
|
public ReactiveCommand<ServerConfigurationViewModel?, Unit> EditServerCommand { get; }
|
||||||
|
|
@ -52,14 +60,18 @@ public partial class ServerListViewModel : ViewModelBase
|
||||||
return Unit.Default;
|
return Unit.Default;
|
||||||
});
|
});
|
||||||
|
|
||||||
EditServerCommand = ReactiveCommand.Create<ServerConfigurationViewModel?, Unit>((sc) =>
|
EditServerCommand = ReactiveCommand.CreateFromTask<ServerConfigurationViewModel?, Unit>(async (sc) =>
|
||||||
{
|
{
|
||||||
var conf = sc ?? SelectedServerConfiguration;
|
var conf = sc ?? SelectedServerConfiguration;
|
||||||
if (conf is null)
|
if (conf is null)
|
||||||
return Unit.Default;
|
return Unit.Default;
|
||||||
|
|
||||||
EditServerConfigurationWindow window = new(new(conf));
|
EditServerConfigurationWindow window = new(new(conf));
|
||||||
window.Show();
|
bool result = await window.ShowDialog<bool>( );
|
||||||
|
if (result)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
return Unit.Default;
|
return Unit.Default;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -78,5 +90,27 @@ public partial class ServerListViewModel : ViewModelBase
|
||||||
EditServerConfigurationWindow window = new(new());
|
EditServerConfigurationWindow window = new(new());
|
||||||
window.Show();
|
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}"/>
|
Command="{Binding ExploreServerCommand}"/>
|
||||||
</StackPanel>
|
</StackPanel>
|
||||||
|
|
||||||
|
<TextBox Watermark="Filter servers..."
|
||||||
|
Text="{Binding FilterText, Mode=TwoWay}"
|
||||||
|
Margin="0,5"/>
|
||||||
|
|
||||||
<DataGrid ItemsSource="{Binding ServerConfigurations}"
|
<DataGrid ItemsSource="{Binding ServerConfigurations}"
|
||||||
SelectedItem="{Binding SelectedServerConfiguration, Mode=TwoWay}"
|
SelectedItem="{Binding SelectedServerConfiguration, Mode=TwoWay}"
|
||||||
HorizontalAlignment="Stretch"
|
HorizontalAlignment="Stretch"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue