Skip to content

Commit

Permalink
chore: get grid state from source
Browse files Browse the repository at this point in the history
  • Loading branch information
giard-alexandre committed Oct 16, 2024
1 parent 2c6b85b commit b85e506
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 64 deletions.
122 changes: 61 additions & 61 deletions samples/SampleApp/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,68 +17,68 @@
namespace SampleApp.ViewModels;

public class MainWindowViewModel : ReactiveObject {
private string? _filterText;

public MainWindowViewModel() {
//Set the randomizer seed to generate repeatable data sets.
Randomizer.Seed = new Random(8675309);
var data = new ObservableCollection<Person>(new Faker<Person>()
.RuleFor(person => person.Id, faker => faker.IndexFaker)
.RuleFor(person => person.Name, faker => faker.Name.FullName())
.RuleFor(person => person.DateOfBirth, faker => faker.Date.Past(80))
.RuleFor(person => person.Height, faker => faker.Random.Double())
.RuleFor(person => person.Gender, faker => faker.Person.Gender)
.RuleFor(person => person.Money, faker => faker.Finance.Amount(-1000M, 1000M, 5))
.RuleFor(person => person.IsChecked, faker => faker.Random.Bool())
.Generate(300)).ToObservableChangeSet(person => person.Id);


var searchFilter = this.WhenValueChanged(t => t.FilterText)
.Throttle(TimeSpan.FromMilliseconds(500))
.Select(BuildSearchFilter);

var filteredData = data.Filter(searchFilter);

DataSource = new DynamicFlatTreeDataGridSource<Person, int>(filteredData, RxApp.MainThreadScheduler) {
Columns = {
new DynamicTextColumn<Person, int>("Id", "Id", person => person.Id),
new DynamicTextColumn<Person, string>("Name", "Name", person => person.Name),
new DynamicTextColumn<Person, DateTime>("Date-of-Birth", "DoB", person => person.DateOfBirth),
new DynamicTemplateColumn<Person>("Height", "Height", "HeightCell"),
new DynamicTextColumn<Person, double>("Height-Raw", "Raw Height", person => person.Height),
new DynamicTextColumn<Person, Name.Gender>("Gender", "Gender", person => person.Gender), // To Template
new DynamicTextColumn<Person, decimal>("Money", "Money", person => person.Money),
new DynamicCheckBoxColumn<Person>("Checked", "Checked", person => person.IsChecked),
},
};
}

public DynamicFlatTreeDataGridSource<Person, int> DataSource { get; set; }

public string? FilterText {
get => _filterText;
set => this.RaiseAndSetIfChanged(ref _filterText, value);
}

public void PrintColumnStates() {
Console.WriteLine(JsonSerializer.Serialize(DataSource.Columns.GetColumnStates(),
new JsonSerializerOptions(JsonSerializerOptions.Default) { WriteIndented = true }));
}

private static Func<Person, bool> BuildSearchFilter(string? text) {
if (string.IsNullOrEmpty(text))
return _ => true;

return t => t.Name.Contains(text, StringComparison.OrdinalIgnoreCase);
}
private readonly JsonSerializerOptions _jsonOptions = new(JsonSerializerOptions.Default) { WriteIndented = true };
private string? _filterText;

public MainWindowViewModel() {
//Set the randomizer seed to generate repeatable data sets.
Randomizer.Seed = new Random(8675309);
var data = new ObservableCollection<Person>(new Faker<Person>()
.RuleFor(person => person.Id, faker => faker.IndexFaker)
.RuleFor(person => person.Name, faker => faker.Name.FullName())
.RuleFor(person => person.DateOfBirth, faker => faker.Date.Past(80))
.RuleFor(person => person.Height, faker => faker.Random.Double())
.RuleFor(person => person.Gender, faker => faker.Person.Gender)
.RuleFor(person => person.Money, faker => faker.Finance.Amount(-1000M, 1000M, 5))
.RuleFor(person => person.IsChecked, faker => faker.Random.Bool())
.Generate(300)).ToObservableChangeSet(person => person.Id);


var searchFilter = this.WhenValueChanged(t => t.FilterText)
.Throttle(TimeSpan.FromMilliseconds(500))
.Select(BuildSearchFilter);

var filteredData = data.Filter(searchFilter);

DataSource = new DynamicFlatTreeDataGridSource<Person, int>(filteredData, RxApp.MainThreadScheduler) {
Columns = {
new DynamicTextColumn<Person, int>("Id", "Id", person => person.Id),
new DynamicTextColumn<Person, string>("Name", "Name", person => person.Name),
new DynamicTextColumn<Person, DateTime>("Date-of-Birth", "DoB", person => person.DateOfBirth),
new DynamicTemplateColumn<Person>("Height", "Height", "HeightCell"),
new DynamicTextColumn<Person, double>("Height-Raw", "Raw Height", person => person.Height),
new DynamicTextColumn<Person, Name.Gender>("Gender", "Gender", person => person.Gender), // To Template
new DynamicTextColumn<Person, decimal>("Money", "Money", person => person.Money),
new DynamicCheckBoxColumn<Person>("Checked", "Checked", person => person.IsChecked),
},
};
}

public DynamicFlatTreeDataGridSource<Person, int> DataSource { get; set; }

public string? FilterText {
get => _filterText;
set => this.RaiseAndSetIfChanged(ref _filterText, value);
}

public void PrintColumnStates() {
Console.WriteLine(JsonSerializer.Serialize(DataSource.GetGridState(), _jsonOptions));
}

private static Func<Person, bool> BuildSearchFilter(string? text) {
if (string.IsNullOrEmpty(text))
return _ => true;

return t => t.Name.Contains(text, StringComparison.OrdinalIgnoreCase);
}
}

public record Person {
public int Id { get; set; }
public string Name { get; set; } = "";
public DateTime DateOfBirth { get; set; }
public double Height { get; set; }
public Name.Gender Gender { get; set; }
public decimal Money { get; set; }
public bool IsChecked { get; set; }
public int Id { get; set; }
public string Name { get; set; } = "";
public DateTime DateOfBirth { get; set; }
public double Height { get; set; }
public Name.Gender Gender { get; set; }
public decimal Money { get; set; }
public bool IsChecked { get; set; }
}
2 changes: 1 addition & 1 deletion src/DynamicTreeDataGrid/DynamicFlatTreeDataGridSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public DynamicFlatTreeDataGridSource(IObservable<IChangeSet<TModel, TModelKey>>
IDynamicColumns IDynamicTreeDataGridSource.Columns => Columns;
IColumns ITreeDataGridSource.Columns => Columns.DisplayedColumns;

public GridState GetCurrentGridState() => new() { ColumnStates = Columns.GetColumnStates() };
public GridState GetGridState() => new() { ColumnStates = Columns.GetColumnStates() };

public bool ApplyGridState(GridState state) => Columns.ApplyColumnStates(state.ColumnStates);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public DynamicHierarchicalTreeDataGridSource(IObservable<IChangeSet<TModel, TMod
IDynamicColumns IDynamicTreeDataGridSource.Columns => Columns;
IColumns ITreeDataGridSource.Columns => Columns.DisplayedColumns;

public GridState GetCurrentGridState() => throw new NotImplementedException();
public GridState GetGridState() => throw new NotImplementedException();
public bool ApplyGridState(GridState state) => throw new NotImplementedException();

#region Override base sorted logic with IChangeSet sorting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public interface IDynamicTreeDataGridSource : ITreeDataGridSource {
/// </summary>
public IObservable<int> TotalCount { get; }

GridState GetCurrentGridState();
GridState GetGridState();
bool ApplyGridState(GridState state);

// TODO: Add filter, state, maybe sort?
Expand Down

0 comments on commit b85e506

Please sign in to comment.