I'm trying to add a filter and sort to my app.
the sort and filter func code are stepped thru but does not work, I do not see any data in the checkListItem
I think the issue is the use of SourceCache ( I found sort/filer sample code and tried to add parts to my code , the sample uses hard coded items, but I need to load from json file)
( I have the Sample code also in the app as a test, and it works )
CheckListPageSortModel.cs
using MapleSugar.Models;
using MapleSugar.Services;
using MapleSugar.ViewModels.Buttons;
namespace MapleSugar.PageModels
{
public class CheckListPageSortModel : ReactiveObject, IDisposable
{
public CheckListPageSortModel()
{
//Search logic
Func<CheckListItem, bool> treeFilter(string text) => checklistItem =>
{
return string.IsNullOrEmpty(text) || checklistItem.TreeLocation.ToLower().Contains(text.ToLower()) || checklistItem.TreeNumber.ToString().Contains(text.ToLower());
};
var filterPredicate = this.WhenAnyValue(x => x.SearchText)
.Throttle(TimeSpan.FromMilliseconds(250), RxApp.TaskpoolScheduler)
.DistinctUntilChanged()
.Select(treeFilter);
//Filter logic
Func<CheckListItem, bool> sectorFilter(string sector) => checklistItem =>
{
return sector == "All" || sector == checklistItem.Sector.ToString();
};
var sectorPredicate = this.WhenAnyValue(x => x.SelectedSectorFilter)
.Select(sectorFilter);
//sort
var sortPredicate = this.WhenAnyValue(x => x.SortBy)
.Select(x => x == "SortField" ? SortExpressionComparer<CheckListItem>.Ascending(a => a.Sector) : SortExpressionComparer<CheckListItem>.Ascending(a => a.TreeNumber));
_cleanUp = _sourceCache.Connect()
.RefCount()
.Filter(sectorPredicate)
.Filter(filterPredicate)
.Sort(sortPredicate)
.Bind(out _myItems)
.DisposeMany()
.Subscribe();
//Set default values
SelectedSectorFilter = "All";
SortBy = "SortField";
// AddCommand = ReactiveCommand.CreateFromTask(async () => await ExecuteAdd());
// DeleteCommand = ReactiveCommand.Create<CheckListItem>(ExecuteRemove);
SortCommand = ReactiveCommand.CreateFromTask(ExecuteSort);
// WorkTreeLocationItems = new ObservableCollection<WorkTreeLocationItem>();
Load_Button_Clicked = new ButtonModel("Load Data", LoadCheckListAction);
// OnEditCommand = new Command(OnEditCommandAction);
}
ButtonModel _load_Button_Clicked;
public ButtonModel Load_Button_Clicked
{
get;
set;
}
private async Task ExecuteSort()
{
var sort = await App.Current.MainPage.DisplayActionSheet("Sort by", "Cancel", null, buttons: new string[] { "Name", "Type" });
if (sort != "Cancel")
{
SortBy = sort;
}
}
public void Dispose()
{
_cleanUp.Dispose();
}
public ReadOnlyObservableCollection<CheckListItem> CheckListItems => _myItems;
public string SearchText
{
get => _searchText;
set => this.RaiseAndSetIfChanged(ref _searchText, value);
}
public string SelectedSectorFilter
{
get => _selectedSectorFilter;
set => this.RaiseAndSetIfChanged(ref _selectedSectorFilter, value);
}
private string SortBy
{
get => _sortBy;
set => this.RaiseAndSetIfChanged(ref _sortBy, value);
}
private async void LoadCheckListAction()
{
try
{
var file = await FilePicker.PickAsync();
if (file != null)
{
var items = from Trees in CheckListService.GetSTrees(File.ReadAllText(file.FullPath))
orderby Trees.Sector, Trees.TreeNumber, Trees.SubTreeNumber, Trees.TreeSubLetter
group Trees by Trees.Sector.ToString().ToUpperInvariant() into sectorGroup
select new Grouping<string, CheckListItem>(sectorGroup.Key, sectorGroup);
foreach (var g in items)
_myItems.Add(g);
}
}
catch (Exception)
{
}
}
public ReactiveCommand<Unit, Unit> AddCommand { get; set; }
public ReactiveCommand<CheckListItem, Unit> DeleteCommand { get; }
public ReactiveCommand<Unit, Unit> SortCommand { get; }
private SourceCache<CheckListItem, string> _sourceCache = new SourceCache<CheckListItem, string>(x => x.Sector.ToString());
private readonly ReadOnlyObservableCollection<CheckListItem> _myItems;
private string _searchText;
private string _selectedSectorFilter;
private string _sortBy;
private readonly IDisposable _cleanUp;
}
}
TIA
Tim
here is part of the sample code I got the code from and why I think the sourceCache is the issue
MainViewModel.cs
using MapleSugar.Models;
namespace MapleSugar.PageModels
{
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);
//Filter logic
Func<Restaurant, bool> countryFilter(string country) => restaurant =>
{
return country == "All" || country == restaurant.Country;
};
var countryPredicate = this.WhenAnyValue(x => x.SelectedCountryFilter)
.Select(countryFilter);