Created
January 25, 2021 19:37
-
-
Save rdelrosario/98381ac7f8ff172e3a7ecd623779352f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace DynamicDataGroupingSample | |
{ | |
public class MainViewModel : ReactiveObject, IDisposable | |
{ | |
public MainViewModel() | |
{ | |
// Initial data | |
_sourceCache.AddOrUpdate(new List<Restaurant>() | |
{ | |
new Restaurant("Yao","Casual","Asian Fusion","Dominican Republic"), | |
new Restaurant("Chef Pepper","Casual","International","Dominican Republic"), | |
new Restaurant("Bottega Fratelli","Formal","International","Dominican Republic"), | |
new Restaurant("McDonalds","Fast Food","Burgers","United States"), | |
new Restaurant("Burger King","Fast Food","Burgers","United States"), | |
new Restaurant("Sushi Nation","Casual","Sushi","Venezuela"), | |
new Restaurant("Pollo Victorina","Fast Food","Chicken","Dominican Republic"), | |
new Restaurant("Pollo Rey","Fast Food","Chicken","Dominican Republic"), | |
new Restaurant("Asadero Argetino","Formal","Meat","Dominican Republic"), | |
new Restaurant("Hooters","Casual","Wings","United States"), | |
new Restaurant("Andres Carne","Casual","Meat","Colombia"), | |
new Restaurant("La Casita","Casual","Colombian Food","Colombia"), | |
new Restaurant("Cielo","Formal","International","Colombia"), | |
}); | |
//Search logic | |
Func<Restaurant, bool> restaurantFilter(string text) => restaurant => | |
{ | |
return string.IsNullOrEmpty(text) || restaurant.Name.ToLower().Contains(text.ToLower()) || restaurant.Type.ToLower().Contains(text.ToLower()); | |
}; | |
var filterPredicate = this.WhenAnyValue(x => x.SearchText) | |
.Throttle(TimeSpan.FromMilliseconds(250), RxApp.TaskpoolScheduler) | |
.DistinctUntilChanged() | |
.Select(restaurantFilter); | |
_cleanUp = _sourceCache.Connect() | |
.RefCount() | |
.Filter(filterPredicate) | |
.Sort(sortPredicate) | |
.Bind(out _restaurants) | |
.DisposeMany() | |
.Subscribe(); | |
} | |
public void Dispose() | |
{ | |
_cleanUp.Dispose(); | |
} | |
public ReadOnlyObservableCollection<Restaurant> Restaurants => _restaurants; | |
public string SearchText | |
{ | |
get => _searchText; | |
set => this.RaiseAndSetIfChanged(ref _searchText, value); | |
} | |
private SourceCache<Restaurant, string> _sourceCache = new SourceCache<Restaurant, string>(x => x.Id); | |
private readonly ReadOnlyObservableCollection<Restaurant> _restaurants; | |
private string _searchText; | |
private readonly IDisposable _cleanUp; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment