|
| 1 | +Imports System.Collections.ObjectModel |
| 2 | +Imports System.ComponentModel |
| 3 | + |
| 4 | +Imports CommunityToolkit.Mvvm.ComponentModel |
| 5 | +Imports CommunityToolkit.Mvvm.Input |
| 6 | + |
| 7 | +Public Class DatabaseViewModel : Inherits ObservableObject |
| 8 | + |
| 9 | + Public Property DatabaseResults As ObservableCollection(Of DatabaseCompressionResult) |
| 10 | + Public ReadOnly Property FilteredResults As ICollectionView |
| 11 | + |
| 12 | + Public ReadOnly Property DatabaseGamesCount As Integer |
| 13 | + Get |
| 14 | + Return DatabaseResults.Count |
| 15 | + End Get |
| 16 | + End Property |
| 17 | + |
| 18 | + Public ReadOnly Property DatabaseSubmissionsCount As Integer |
| 19 | + Get |
| 20 | + Return DatabaseResults.Sum(Function(result) _ |
| 21 | + (If(result.Result_X4K?.TotalResults, 0)) + |
| 22 | + (If(result.Result_X8K?.TotalResults, 0)) + |
| 23 | + (If(result.Result_X16K?.TotalResults, 0)) + |
| 24 | + (If(result.Result_LZX?.TotalResults, 0)) |
| 25 | + ) |
| 26 | + End Get |
| 27 | + End Property |
| 28 | + |
| 29 | + |
| 30 | + Public ReadOnly Property LastUpdatedDatabase As DateTime |
| 31 | + Get |
| 32 | + Return SettingsHandler.AppSettings.ResultsDBLastUpdated |
| 33 | + End Get |
| 34 | + End Property |
| 35 | + |
| 36 | + |
| 37 | + Private _searchText As String |
| 38 | + Public Property SearchText As String |
| 39 | + Get |
| 40 | + Return _searchText |
| 41 | + End Get |
| 42 | + Set(value As String) |
| 43 | + SetProperty(_searchText, value) |
| 44 | + FilteredResults.Refresh() |
| 45 | + End Set |
| 46 | + End Property |
| 47 | + |
| 48 | + Public Sub New() |
| 49 | + DatabaseResults = New ObservableCollection(Of DatabaseCompressionResult)(Application.GetService(Of IWikiService).GetAllDatabaseCompressionResultsAsync().GetAwaiter.GetResult) |
| 50 | + |
| 51 | + FilteredResults = CollectionViewSource.GetDefaultView(DatabaseResults) |
| 52 | + FilteredResults.Filter = AddressOf FilterResults |
| 53 | + End Sub |
| 54 | + |
| 55 | + |
| 56 | + Private Function NormalizeString(input As String) As String |
| 57 | + If String.IsNullOrEmpty(input) Then Return String.Empty |
| 58 | + Return New String(input.Where(Function(c) Char.IsLetterOrDigit(c) OrElse Char.IsWhiteSpace(c)).ToArray()).ToLowerInvariant() |
| 59 | + End Function |
| 60 | + |
| 61 | + Private Function FilterResults(obj As Object) As Boolean |
| 62 | + If String.IsNullOrWhiteSpace(SearchText) Then Return True |
| 63 | + Dim item = TryCast(obj, DatabaseCompressionResult) |
| 64 | + If item Is Nothing OrElse item.GameName Is Nothing Then Return False |
| 65 | + |
| 66 | + ' Normalize GameName for punctuation-insensitive search |
| 67 | + Dim normalizedGameName = NormalizeString(item.GameName) |
| 68 | + |
| 69 | + Return (item.GameName.IndexOf(SearchText, StringComparison.OrdinalIgnoreCase) >= 0) OrElse |
| 70 | + (normalizedGameName.Contains(SearchText)) OrElse |
| 71 | + (item.SteamID.ToString().Contains(SearchText)) |
| 72 | + End Function |
| 73 | + |
| 74 | + Public ReadOnly Property SortCommand As ICommand = New RelayCommand(Of Object)(Sub(f) SortResults(f)) |
| 75 | + |
| 76 | + |
| 77 | + Private Sub SortResults(param As Object) |
| 78 | + Dim sortOption = param?.ToString() |
| 79 | + FilteredResults.SortDescriptions.Clear() |
| 80 | + |
| 81 | + Select Case sortOption |
| 82 | + Case "GameNameAsc" |
| 83 | + FilteredResults.SortDescriptions.Add(New SortDescription("GameName", ListSortDirection.Ascending)) |
| 84 | + Case "GameNameDesc" |
| 85 | + FilteredResults.SortDescriptions.Add(New SortDescription("GameName", ListSortDirection.Descending)) |
| 86 | + Case "SteamIDAsc" |
| 87 | + FilteredResults.SortDescriptions.Add(New SortDescription("SteamID", ListSortDirection.Ascending)) |
| 88 | + Case "SteamIDDesc" |
| 89 | + FilteredResults.SortDescriptions.Add(New SortDescription("SteamID", ListSortDirection.Descending)) |
| 90 | + Case "MaxSavingsAsc" |
| 91 | + FilteredResults.SortDescriptions.Add(New SortDescription("MaxSavings", ListSortDirection.Ascending)) |
| 92 | + Case "MaxSavingsDesc" |
| 93 | + FilteredResults.SortDescriptions.Add(New SortDescription("MaxSavings", ListSortDirection.Descending)) |
| 94 | + End Select |
| 95 | + End Sub |
| 96 | + |
| 97 | + |
| 98 | +End Class |
0 commit comments