From 0790d6aad5117fc4c8e6254cb404b0a91eafebf9 Mon Sep 17 00:00:00 2001 From: Harshit Mishra Date: Fri, 4 Oct 2024 16:09:36 +0530 Subject: [PATCH 1/2] Raw searchbox implementation --- .../WPFGallery/MainWindow.xaml | 12 +++++ .../WPFGallery/MainWindow.xaml.cs | 29 +++++++++++ .../ViewModels/MainWindowViewModel.cs | 50 +++++++++++++++++++ 3 files changed, 91 insertions(+) diff --git a/Sample Applications/WPFGallery/MainWindow.xaml b/Sample Applications/WPFGallery/MainWindow.xaml index 5d0cf364..51d9c8ef 100644 --- a/Sample Applications/WPFGallery/MainWindow.xaml +++ b/Sample Applications/WPFGallery/MainWindow.xaml @@ -210,6 +210,18 @@ KeyUp="SearchBox_KeyUp" LostFocus="SearchBox_LostFocus" />--> + + _controls; + + [ObservableProperty] + private ICollection _allPages; + + public ObservableCollection FilteredSearchItems { get; set; } + + private string _searchBoxText; + + public string SearchBoxText + { + get => _searchBoxText; + set + { + _searchBoxText = value; + OnPropertyChanged(nameof(SearchBoxText)); + + FilterItems(); + } + } + [ObservableProperty] private ControlInfoDataItem? _selectedControl; private readonly INavigationService _navigationService; @@ -51,6 +71,20 @@ public void Forward() public MainWindowViewModel(INavigationService navigationService) { _controls = ControlsInfoDataSource.Instance.ControlsInfo; + + _allPages = new ObservableCollection(); + foreach (ControlInfoDataItem groupControl in _controls) + { + _allPages.Add(groupControl); + + foreach(ControlInfoDataItem control in groupControl.Items) + { + _allPages.Add(control); + } + } + + FilteredSearchItems = new ObservableCollection(AllPages); + _navigationService = navigationService; _timer = new DispatcherTimer(); @@ -65,6 +99,22 @@ public void UpdateSearchText(string searchText) _timer.Start(); } + private void FilterItems() + { + if(string.IsNullOrEmpty(SearchBoxText)) + { + FilteredSearchItems = new ObservableCollection(AllPages); + } + else + { + FilteredSearchItems = new ObservableCollection( + AllPages.Where(item => item.Title.ToLower().Contains(SearchBoxText.ToLower())) + ); + } + + OnPropertyChanged(nameof(FilteredSearchItems)); + } + private void PerformSearchNavigation(object? sender, EventArgs e) { _timer.Stop(); From f8acd85a91d8044568ad55fb18e621cd5b764ae8 Mon Sep 17 00:00:00 2001 From: Harshit Mishra Date: Fri, 4 Oct 2024 16:25:01 +0530 Subject: [PATCH 2/2] Improving seperation of code logic and clean-up --- .../Models/ControlsInfoDataSource.cs | 37 +++++++++++++++++++ .../ViewModels/MainWindowViewModel.cs | 11 +----- 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/Sample Applications/WPFGallery/Models/ControlsInfoDataSource.cs b/Sample Applications/WPFGallery/Models/ControlsInfoDataSource.cs index 116b08bc..74abb5bf 100644 --- a/Sample Applications/WPFGallery/Models/ControlsInfoDataSource.cs +++ b/Sample Applications/WPFGallery/Models/ControlsInfoDataSource.cs @@ -53,6 +53,8 @@ public sealed class ControlsInfoDataSource private static readonly ControlsInfoDataSource _instance; + private ICollection _allPages; + public static ControlsInfoDataSource Instance { get @@ -70,6 +72,8 @@ private ControlsInfoDataSource() { var jsonText = ReadControlsData(); ControlsInfo = JsonSerializer.Deserialize>(jsonText); + + GetAllPages(); } #endregion @@ -111,6 +115,39 @@ public ICollection GetAllControlsInfo() return allControls; } + private void GetAllPages() + { + _allPages = new ObservableCollection(); + + foreach (ControlInfoDataItem groupPage in ControlsInfo) + { + _allPages.Add(groupPage); + + foreach (ControlInfoDataItem controlPage in groupPage.Items) + { + _allPages.Add(controlPage); + } + } + } + + public ObservableCollection FilterItems(string filterText) + { + ObservableCollection filteredItems = new(); + + if (string.IsNullOrEmpty(filterText)) + { + filteredItems = new ObservableCollection(_allPages); + } + else + { + filteredItems = new ObservableCollection( + _allPages.Where(item => item.Title.ToLower().Contains(filterText.ToLower())) + ); + } + + return filteredItems; + } + public ICollection GetGroupedControlsInfo() { return ControlsInfo.Where(x => x.IsGroup == true && x.UniqueId != "Design Guidance" && x.UniqueId != "Samples").ToList(); diff --git a/Sample Applications/WPFGallery/ViewModels/MainWindowViewModel.cs b/Sample Applications/WPFGallery/ViewModels/MainWindowViewModel.cs index 3049d142..66c847f2 100644 --- a/Sample Applications/WPFGallery/ViewModels/MainWindowViewModel.cs +++ b/Sample Applications/WPFGallery/ViewModels/MainWindowViewModel.cs @@ -101,16 +101,7 @@ public void UpdateSearchText(string searchText) private void FilterItems() { - if(string.IsNullOrEmpty(SearchBoxText)) - { - FilteredSearchItems = new ObservableCollection(AllPages); - } - else - { - FilteredSearchItems = new ObservableCollection( - AllPages.Where(item => item.Title.ToLower().Contains(SearchBoxText.ToLower())) - ); - } + FilteredSearchItems = ControlsInfoDataSource.Instance.FilterItems(SearchBoxText); OnPropertyChanged(nameof(FilteredSearchItems)); }