Searching for a solution to sort and / or filter ListBox

This commit is contained in:
Enrico Ludwig 2024-09-06 23:39:05 +02:00
parent 791cb531e2
commit 4806e62a00
2 changed files with 19 additions and 34 deletions

View File

@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17 # Visual Studio Version 17
VisualStudioVersion = 17.5.002.0 VisualStudioVersion = 17.5.002.0
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BetterRaid", "BetterRaid.csproj", "{C23C5237-3D18-424A-ACF2-62215BE5D557}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BetterRaid", "BetterRaid.csproj", "{5E0DA55A-6B6B-4906-ACB9-401AB203D537}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -11,10 +11,10 @@ Global
Release|Any CPU = Release|Any CPU Release|Any CPU = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution GlobalSection(ProjectConfigurationPlatforms) = postSolution
{C23C5237-3D18-424A-ACF2-62215BE5D557}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {5E0DA55A-6B6B-4906-ACB9-401AB203D537}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C23C5237-3D18-424A-ACF2-62215BE5D557}.Debug|Any CPU.Build.0 = Debug|Any CPU {5E0DA55A-6B6B-4906-ACB9-401AB203D537}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C23C5237-3D18-424A-ACF2-62215BE5D557}.Release|Any CPU.ActiveCfg = Release|Any CPU {5E0DA55A-6B6B-4906-ACB9-401AB203D537}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C23C5237-3D18-424A-ACF2-62215BE5D557}.Release|Any CPU.Build.0 = Release|Any CPU {5E0DA55A-6B6B-4906-ACB9-401AB203D537}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

View File

@ -1,6 +1,5 @@
using System; using System;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -10,14 +9,12 @@ using BetterRaid.Models;
using BetterRaid.Services; using BetterRaid.Services;
using BetterRaid.Views; using BetterRaid.Views;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using TwitchLib.PubSub.Events;
namespace BetterRaid.ViewModels; namespace BetterRaid.ViewModels;
public class MainWindowViewModel : ViewModelBase public class MainWindowViewModel : ViewModelBase
{ {
private ObservableCollection<TwitchChannel> _channels = []; private ObservableCollection<TwitchChannel> _channels = [];
private readonly ISynchronizaionService _synchronizationService; private readonly ISynchronizaionService _synchronizationService;
private readonly ILogger<MainWindowViewModel> _logger; private readonly ILogger<MainWindowViewModel> _logger;
private readonly IWebToolsService _webTools; private readonly IWebToolsService _webTools;
@ -35,18 +32,24 @@ public class MainWindowViewModel : ViewModelBase
set => SetProperty(ref _channels, value); set => SetProperty(ref _channels, value);
} }
public ObservableCollection<TwitchChannel> FilteredChannels => GetFilteredChannels();
public string? Filter public string? Filter
{ {
get => _filter; get => _filter;
set => SetProperty(ref _filter, value); set
{
SetProperty(ref _filter, value);
LoadChannelsFromDb();
}
} }
public bool OnlyOnline public bool OnlyOnline
{ {
get => _db.OnlyOnline; get => _db.OnlyOnline;
set => SetProperty(ref _onlyOnline, value); set
{
SetProperty(ref _onlyOnline, value);
LoadChannelsFromDb();
}
} }
public bool IsLoggedIn => _twitch.UserChannel != null; public bool IsLoggedIn => _twitch.UserChannel != null;
@ -110,7 +113,9 @@ public class MainWindowViewModel : ViewModelBase
var channels = _db.Database.Channels var channels = _db.Database.Channels
.ToList() .ToList()
.OrderBy(c => c.IsLive) .OrderByDescending(c => c.IsLive)
.Where(c => OnlyOnline && c.IsLive || !OnlyOnline)
.Where(c => string.IsNullOrWhiteSpace(Filter) || c.Name?.Contains(Filter, StringComparison.CurrentCultureIgnoreCase) == true)
.ToList(); .ToList();
foreach (var channel in channels) foreach (var channel in channels)
@ -125,26 +130,6 @@ public class MainWindowViewModel : ViewModelBase
} }
} }
private ObservableCollection<TwitchChannel> GetFilteredChannels()
{
var filteredChannels = Channels
.Where(channel => OnlyOnline == false || channel.IsLive)
.Where(channel => string.IsNullOrWhiteSpace(Filter) || channel.Name?.Contains(Filter, StringComparison.OrdinalIgnoreCase) == true)
.ToList();
return new ObservableCollection<TwitchChannel>(filteredChannels);
}
protected override void OnPropertyChanged(PropertyChangedEventArgs e)
{
base.OnPropertyChanged(e);
if (e.PropertyName == nameof(Filter))
{
OnPropertyChanged(nameof(FilteredChannels));
}
}
private void OnTwitchChannelUpdated(object? sender, TwitchChannel channel) private void OnTwitchChannelUpdated(object? sender, TwitchChannel channel)
{ {
LoadChannelsFromDb(); LoadChannelsFromDb();