Skip to content

Commit

Permalink
Update DataGrid Examples in Demo app
Browse files Browse the repository at this point in the history
  • Loading branch information
enisn committed Feb 23, 2024
1 parent b08e158 commit 92fdfd9
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 117 deletions.
28 changes: 14 additions & 14 deletions demo/UraniumApp/Pages/DataGrids/CustomDataGridPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,19 @@
xmlns:m="clr-namespace:UraniumUI.Icons.MaterialSymbols;assembly=UraniumUI.Icons.MaterialSymbols"
xmlns:uranium="http://schemas.enisn-projects.io/dotnet/maui/uraniumui"
xmlns:material="http://schemas.enisn-projects.io/dotnet/maui/uraniumui/material"
xmlns:plain="clr-namespace:Plainer.Maui.Controls;assembly=Plainer.Maui"
BindingContext="{uranium:Inject {Type local:CustomDataGridPageViewModel}}"
x:DataType="local:CustomDataGridPageViewModel"
x:Name="page">
<ContentPage.BindingContext>
<local:CustomDataGridPageViewModel />
</ContentPage.BindingContext>
<ContentPage.Content>
<ScrollView>
<StackLayout>

<Grid ColumnDefinitions="80,*,Auto" Padding="20">
<Entry Text="{Binding Row}" Placeholder="RowIndex" Keyboard="Numeric" />
<Entry Grid.Column="1" Text="{Binding Name}" Placeholder="Student Name" Keyboard="Plain" />
<Button Grid.Column="2" Command="{Binding AddNewCommand}" Text="Add" BackgroundColor="{StaticResource Primary}" TextColor="{StaticResource OnPrimary}"/>
</Grid>

<Label Text="Fully Defined Columns:" />
<Label Text="Custom CellItemTemplate for each column" FontSize="Title" HorizontalOptions="Center" />
<BoxView StyleClass="Divider" Margin="8,20" />
<HorizontalStackLayout HorizontalOptions="Center">
<Button StyleClass="FilledButton" Text="Add data" Command="{Binding AddNewCommand}" />
</HorizontalStackLayout>

<material:DataGrid ItemsSource="{Binding Items}" HorizontalOptions="Center" Margin="30">

Expand All @@ -43,22 +41,24 @@
<material:DataGridColumn Title="Student Name">
<material:DataGridColumn.CellItemTemplate>
<DataTemplate>
<Label Text="{Binding Name}" TextColor="Red" HorizontalOptions="Center" VerticalOptions="Center" Margin="0" />
<Label Text="{Binding Name}" FontAttributes="Bold" HorizontalOptions="Center" VerticalOptions="Center" Margin="0" />
</DataTemplate>
</material:DataGridColumn.CellItemTemplate>
</material:DataGridColumn>

<material:DataGridColumn Title="Student Age">
<material:DataGridColumn.CellItemTemplate>
<DataTemplate>
<Border BackgroundColor="Green" uranium:DynamicTint.BackgroundColorOpacity="0.5" Padding="5">
<Label Text="{Binding Age}" TextColor="Blue" />
<Border Padding="6" HorizontalOptions="Center" VerticalOptions="Center"
StrokeShape="RoundRectangle 6"
BackgroundColor="{AppThemeBinding Light={StaticResource Tertiary}, Dark={StaticResource TertiaryDark}}">
<plain:EntryView Text="{Binding Age}" TextColor="{AppThemeBinding Light={StaticResource OnTertiary}, Dark={StaticResource OnTertiaryDark}}" HorizontalOptions="Center"/>
</Border>
</DataTemplate>
</material:DataGridColumn.CellItemTemplate>
</material:DataGridColumn>

<material:DataGridColumn Binding="{Binding Id}" Title="Identity" />
<material:DataGridColumn ValueBinding="{Binding Id}" Title="Identity" />

<material:DataGridColumn Title="Actions" >
<material:DataGridColumn.CellItemTemplate>
Expand Down
79 changes: 41 additions & 38 deletions demo/UraniumApp/Pages/DataGrids/CustomDataGridPageViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
using System.Collections.ObjectModel;
using Bogus;
using DotNurse.Injector.Attributes;
using ReactiveUI;
using System.Collections.ObjectModel;
using System.Windows.Input;
using UraniumUI.Dialogs;

namespace UraniumApp.Pages.DataGrids;

[RegisterAs(typeof(CustomDataGridPageViewModel))]
public class CustomDataGridPageViewModel : BindableObject
{
static Random random = new();

private ObservableCollection<Student> items;
public ObservableCollection<Student> Items { get => items; protected set { items = value; OnPropertyChanged(); } }
private bool isBusy;
Expand All @@ -15,20 +18,18 @@ public class CustomDataGridPageViewModel : BindableObject
public ICommand AddNewCommand { get; set; }
public ICommand RemoveItemCommand { get; set; }
public int Row { get; set; }
public string Name { get; set; } = "Student Custom 1";

public CustomDataGridPageViewModel()
public CustomDataGridPageViewModel(IDialogService dialogService)
{
Initialize();

AddNewCommand = new Command(() =>
AddNewCommand = new Command(async () =>
{
Items.Insert(Row, new Student
{
Age = random.Next(10, 99),
Id = random.Next(0, int.MaxValue),
Name = Name
});
var newStudent = StudentDataStore.faker.Generate();

newStudent.Name = await dialogService.DisplayTextPromptAsync("New Student", "Enter the name of the new student", "Add", "Cancel", initialValue: newStudent.Name);

Items.Add(newStudent);
});

RemoveItemCommand = new Command((item) =>
Expand All @@ -46,38 +47,40 @@ protected async virtual void Initialize()
Items = new ObservableCollection<Student>(await DataStore.GetListAsync());
IsBusy = false;
}
}

public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public Guid SecurityStamp { get; set; } = Guid.NewGuid();
public DateTime RegistrationDate { get; set; } = DateTime.UtcNow.AddDays(-1 * random.Next(1, 2100));
}
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public Guid SecurityStamp { get; set; } = Guid.NewGuid();
public DateTime RegistrationDate { get; set; }
}

public class StudentDataStore
public class StudentDataStore
{
internal static Faker<Student> faker = new Faker<Student>()
.RuleFor(x => x.Id, f => f.IndexFaker)
.RuleFor(x => x.Name, f => f.Person.FullName)
.RuleFor(x => x.Age, f => f.Random.Number(14, 85))
.RuleFor(x => x.SecurityStamp, f => f.Random.Guid())
.RuleFor(x => x.RegistrationDate, f => f.Date.Past(1));

public async Task<List<Student>> GetListAsync(bool simulateNetwork = true)
{
public async Task<List<Student>> GetListAsync(bool simulateNetwork = true)
if (simulateNetwork)
{
if (simulateNetwork)
{
await Task.Delay(random.Next(500, 2000));
}
await Task.Delay(Random.Shared.Next(500, 2000));
}

var list = new List<Student>();
var list = new List<Student>();

for (int i = 0; i < 18; i++)
{
list.Add(new Student
{
Id = i,
Name = "Student " + i,
Age = random.Next(14, 85),
});
}

return list;
for (int i = 0; i < 12; i++)
{
list.Add(faker.Generate());
}

return list;
}
}
2 changes: 1 addition & 1 deletion demo/UraniumApp/Pages/DataGrids/EditorDataGridPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
x:Class="UraniumApp.Pages.DataGrids.EditorDataGridPage"
Title="EditorDataGridPage">
<ContentPage.BindingContext>
<local:SimpleDataGridPageViewModel />
<local:EditorDataGridPageViewModel />
</ContentPage.BindingContext>
<ScrollView>

Expand Down
35 changes: 35 additions & 0 deletions demo/UraniumApp/Pages/DataGrids/EditorDataGridPageViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Bogus;
using ReactiveUI.Fody.Helpers;
using System.ComponentModel;

namespace UraniumApp.Pages.DataGrids;
public class EditorDataGridPageViewModel : BindableObject
{
public List<ReactiveStudent> Items { get; } = new();
public EditorDataGridPageViewModel()
{
for (int i = 0; i < 10; i++)
{
Items.Add(GenerateStudent());
}
}

private static Faker<ReactiveStudent> studentFaker = new Faker<ReactiveStudent>()
.RuleFor(x => x.Id, f => f.IndexFaker)
.RuleFor(x => x.Name, f => f.Person.FullName)
.RuleFor(x => x.Age, f => f.Random.Number(14, 85));

public static ReactiveStudent GenerateStudent()
{
return studentFaker.Generate();
}
}

public class ReactiveStudent : ReactiveUI.ReactiveObject
{
[Reactive] public int Id { get; set; }
[Reactive] public string Name { get; set; }
[Reactive] public int Age { get; set; }


}
11 changes: 2 additions & 9 deletions demo/UraniumApp/Pages/DataGrids/SelectableDataGridPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
xmlns:m="clr-namespace:UraniumUI.Icons.MaterialSymbols;assembly=UraniumUI.Icons.MaterialSymbols"
xmlns:plain="clr-namespace:Plainer.Maui.Controls;assembly=Plainer.Maui"
xmlns:local="clr-namespace:UraniumApp.Pages.DataGrids"
xmlns:uranium="http://schemas.enisn-projects.io/dotnet/maui/uraniumui"
xmlns:material="http://schemas.enisn-projects.io/dotnet/maui/uraniumui/material"
BindingContext="{uranium:Inject {Type local:SelectableDataGridPageViewModel}}"
Title="DataGrid Selection">
<ContentPage.BindingContext>
<local:SelectableDataGridPageViewModel />
</ContentPage.BindingContext>
<ContentPage.Content>
<ScrollView>
<StackLayout>
Expand All @@ -26,12 +25,6 @@
<Label Text="No data available..." HorizontalOptions="Center"/>
</VerticalStackLayout>
</material:DataGrid.EmptyView>

<material:DataGrid.TitleTemplate>
<DataTemplate>
<Label Text="{Binding Title}" />
</DataTemplate>
</material:DataGrid.TitleTemplate>

<material:DataGrid.Columns>
<material:DataGridSelectionColumn />
Expand Down
24 changes: 15 additions & 9 deletions demo/UraniumApp/Pages/DataGrids/SelectableDataGridPageViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
using System;
using System.Collections.Generic;
using DotNurse.Injector.Attributes;
using ReactiveUI.Fody.Helpers;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using UraniumUI.Dialogs;

namespace UraniumApp.Pages.DataGrids;
public class SelectableDataGridPageViewModel : CustomDataGridPageViewModel

[RegisterAs(typeof(SelectableDataGridPageViewModel))]
public class SelectableDataGridPageViewModel : BindableObject
{
[Reactive] public ObservableCollection<Student> Items { get; private set; } = new();
public ObservableCollection<Student> SelectedItems { get; set; } = new ObservableCollection<Student>();

public ICommand RemoveSelectedCommand { get; set; }

public SelectableDataGridPageViewModel() : base()
protected StudentDataStore DataStore { get; } = new StudentDataStore();

public SelectableDataGridPageViewModel()
{
Initialize();

RemoveSelectedCommand = new Command(() =>
{
for (int i = 0; i < SelectedItems.Count; i++)
Expand All @@ -24,9 +29,10 @@ public SelectableDataGridPageViewModel() : base()
});
}

protected override async void Initialize()
protected async void Initialize()
{
Items = new ObservableCollection<Student>(await DataStore.GetListAsync(simulateNetwork: false));
Items = new ObservableCollection<Student>(
await DataStore.GetListAsync(simulateNetwork: false));

SelectedItems.Add(Items[0]);
SelectedItems.Add(Items[2]);
Expand Down
2 changes: 1 addition & 1 deletion demo/UraniumApp/Pages/DataGrids/SimpleDataGridPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<material:DataGrid ItemsSource="{Binding Items}" UseAutoColumns="True" HorizontalOptions="Center" Margin="30">
<material:DataGrid.TitleTemplate>
<DataTemplate>
<Label Text="{Binding Title}" FontSize="Subtitle" Margin="10" TextColor="{AppThemeBinding Light={StaticResource Primary}, Dark={StaticResource PrimaryDark}}" />
<Label Text="{Binding Title}" HorizontalOptions="Center" FontSize="Subtitle" Margin="10" TextColor="{AppThemeBinding Light={StaticResource Primary}, Dark={StaticResource PrimaryDark}}" />
</DataTemplate>
</material:DataGrid.TitleTemplate>
</material:DataGrid>
Expand Down
47 changes: 26 additions & 21 deletions demo/UraniumApp/Pages/DataGrids/SimpleDataGridPageViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
using ReactiveUI.Fody.Helpers;
using Bogus;
using System.ComponentModel;

namespace UraniumApp.Pages.DataGrids;
public class SimpleDataGridPageViewModel : BindableObject
{
static Random random = new();
public List<Student> Items { get; } = new();
public SimpleDataGridPageViewModel()
{
for (int i = 0; i < 10; i++)
{
Items.Add(new Student
{
Id = i,
Name = "Person " + i,
Age = random.Next(14, 85),
});
}
public List<Student> Items { get; } = new();
public SimpleDataGridPageViewModel()
{
for (int i = 0; i < 10; i++)
{
Items.Add(GenerateStudent());
}
}

private static Faker<Student> studentFaker = new Faker<Student>()
.RuleFor(x => x.Id, f => f.IndexFaker)
.RuleFor(x => x.Name, f => f.Person.FullName)
.RuleFor(x => x.Age, f => f.Random.Number(14, 85));

public static Student GenerateStudent()
{
return studentFaker.Generate();
}

public class Student
{
[DisplayName("Identity")]
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
public class Student : ReactiveUI.ReactiveObject
{
[DisplayName("Identity Number")]
[Reactive] public int Id { get; set; }
[Reactive] public string Name { get; set; }
[Reactive] public int Age { get; set; }
}
}
Loading

0 comments on commit 92fdfd9

Please sign in to comment.