Skip to content

Commit

Permalink
Feat: add searching functionality on the 'RecommendedFood' page
Browse files Browse the repository at this point in the history
  • Loading branch information
sulaimanfawwazak committed Nov 25, 2024
1 parent 9e715be commit 64e9db2
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 6 deletions.
5 changes: 3 additions & 2 deletions Appview/Views/RecommendedFood.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<!-- Search Bar -->
<Border BorderBrush="White" BorderThickness="2" CornerRadius="5" Padding="2" Margin="10">
<TextBox Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" Height="35" FontSize="18" Background="#FB6501" Foreground="White" local:Placeholder.Placeholder="Cari Dengan Keywords ..."/>
<TextBox Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" Height="35" FontSize="18" Background="#FB6501" Foreground="White" local:Placeholder.Placeholder="Search Products..." VerticalContentAlignment="Center" Text="{Binding SearchQuery, UpdateSourceTrigger=PropertyChanged}"/>
</Border>


Expand All @@ -30,7 +30,8 @@

<!-- Recommended Food Items with Vertical Scroll -->
<ScrollViewer HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto" Margin="10" MaxHeight="660">
<ItemsControl ItemsSource="{Binding ProductViewModel.Products}">
<!--<ItemsControl ItemsSource="{Binding ProductViewModel.Products}">-->
<ItemsControl ItemsSource="{Binding FilteredProducts}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal" />
Expand Down
65 changes: 61 additions & 4 deletions Appview/Views/RecommendedFood.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using Appview.ViewModel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
Expand All @@ -18,9 +20,6 @@

namespace Appview.Views
{
/// <summary>
/// Interaction logic for RecommendedFood.xaml
/// </summary>
public partial class RecommendedFood : UserControl
{
public RecommendedFood()
Expand All @@ -33,6 +32,8 @@ public RecommendedFood()
ProductViewModel = new GetProductFromDB(),
OrderViewModel = new GetOrdersFromDB(userId)
};

viewModel.InitializeFilteredProducts();
//DataContext = new GetProductFromDB();
DataContext = viewModel;
}
Expand Down Expand Up @@ -83,9 +84,65 @@ private void Border_MouseDown(object sender, MouseButtonEventArgs e)
}

}
public class CompositeViewModelRecommendedFood
public class CompositeViewModelRecommendedFood : INotifyPropertyChanged
{
private string _searchQuery;
public string SearchQuery
{
get => _searchQuery;
set
{
_searchQuery = value;
OnPropertyChanged();
FilterProducts();
}
}
public GetProductFromDB ProductViewModel { get; set; }
public GetOrdersFromDB OrderViewModel { get; set; }

private ObservableCollection<Product> _filteredProducts;
public ObservableCollection<Product> FilteredProducts
{
get => _filteredProducts;
private set
{
_filteredProducts = value;
OnPropertyChanged();
}
}
public CompositeViewModelRecommendedFood()
{
//FilteredProducts = new ObservableCollection<Product>(ProductViewModel.Products);
}

public void InitializeFilteredProducts()
{
if (ProductViewModel?.Products != null)
{
FilteredProducts = new ObservableCollection<Product>(ProductViewModel.Products);
}
}

private void FilterProducts()
{
if (string.IsNullOrWhiteSpace(SearchQuery))
{
FilteredProducts = new ObservableCollection<Product>(ProductViewModel.Products);
}
else
{
var filtered = ProductViewModel.Products
.Where(p => p.ProductName.Contains(SearchQuery, StringComparison.OrdinalIgnoreCase))
.ToList();

FilteredProducts = new ObservableCollection<Product>(filtered);
}
}

public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([System.Runtime.CompilerServices.CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}

0 comments on commit 64e9db2

Please sign in to comment.