Skip to content

Commit

Permalink
BaseViewModel implementation.
Browse files Browse the repository at this point in the history
A simple base VM has implemented for the sample project to reduce complexity.
  • Loading branch information
akgulebubekir committed Jan 12, 2024
1 parent c54d042 commit aaa82e3
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 91 deletions.
6 changes: 3 additions & 3 deletions Maui.DataGrid.Sample/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<dg:DataGrid Grid.Row="1" ItemsSource="{Binding Teams}" SelectionEnabled="True" SelectedItem="{Binding SelectedTeam}"
RowHeight="70" HeaderHeight="50" BorderColor="{StaticResource GridBorderColor}" RowToEdit="{Binding TeamToEdit}"
HeaderBackground="{StaticResource GridHeaderBgColor}" HeaderBordersVisible="{Binding HeaderBordersVisible}"
PullToRefreshCommand="{Binding RefreshCommand}" IsRefreshing="{Binding IsRefreshing}" PaginationEnabled="{Binding PaginationEnabled}" PageSize="6"
PullToRefreshCommand="{Binding Commands[Refresh]}" IsRefreshing="{Binding IsRefreshing}" PaginationEnabled="{Binding PaginationEnabled}" PageSize="6"
ActiveRowColor="{StaticResource ActiveRowColor}" FooterBackground="{StaticResource GridFooterBgColor}" x:Name="_dataGrid1">
<dg:DataGrid.Columns>
<dg:DataGridColumn Title="Logo" PropertyName="Logo" SortingEnabled="False">
Expand Down Expand Up @@ -82,12 +82,12 @@
<dg:DataGridColumn PropertyName="." Width="0.75*">
<dg:DataGridColumn.CellTemplate>
<DataTemplate>
<Button Text="Edit" BackgroundColor="LightSkyBlue" Command="{Binding BindingContext.EditCommand, Source={Reference self}}" CommandParameter="{Binding .}" />
<Button Text="Edit" BackgroundColor="LightSkyBlue" Command="{Binding BindingContext.Commands[Edit], Source={Reference self}}" CommandParameter="{Binding .}" />
</DataTemplate>
</dg:DataGridColumn.CellTemplate>
<dg:DataGridColumn.EditCellTemplate>
<DataTemplate>
<Button Text="Cancel" BackgroundColor="Orange" Command="{Binding BindingContext.CancelEditCommand, Source={Reference self}}" CommandParameter="{Binding .}" />
<Button Text="Done" BackgroundColor="MediumSeaGreen" Command="{Binding BindingContext.Commands[CompleteEdit], Source={Reference self}}" CommandParameter="{Binding .}" />
</DataTemplate>
</dg:DataGridColumn.EditCellTemplate>
</dg:DataGridColumn>
Expand Down
119 changes: 31 additions & 88 deletions Maui.DataGrid.Sample/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,128 +1,79 @@
namespace Maui.DataGrid.Sample.ViewModels;

using System.ComponentModel;
using System.Diagnostics;
using System.Windows.Input;
using Maui.DataGrid.Sample.Models;
using Maui.DataGrid.Sample.Utils;
using Models;
using Utils;

public class MainViewModel : INotifyPropertyChanged
public class MainViewModel : ViewModelBase
{
private List<Team> _teams;
private Team _teamToEdit;
private Team _selectedItem;
private bool _isRefreshing;
private bool _teamColumnVisible = true;
private bool _wonColumnVisible = true;
private bool _headerBordersVisible = true;
private bool _paginationEnabled = true;
private ushort _teamColumnWidth = 70;

public MainViewModel()
{
Teams = DummyDataProvider.GetTeams();
CancelEditCommand = new Command(CmdCancelEdit);
EditCommand = new Command<Team>(CmdEdit);
RefreshCommand = new Command(CmdRefresh);
TeamColumnVisible = true;
WonColumnVisible = true;
HeaderBordersVisible = true;
PaginationEnabled = true;
TeamColumnWidth = 70;

Commands.Add("CompleteEdit", new Command(CmdCompleteEdit));
Commands.Add("Edit", new Command<Team>(CmdEdit));
Commands.Add("Refresh", new Command(CmdRefresh));
}

public Team TeamToEdit
{
get => _teamToEdit;
set
{
_teamToEdit = value;
OnPropertyChanged(nameof(TeamToEdit));
}
get => GetValue<Team>();
set => SetValue(value);
}

public List<Team> Teams
{
get => _teams;
set
{
_teams = value;
OnPropertyChanged(nameof(Teams));
}
get => GetValue<List<Team>>();
set => SetValue(value);
}

public bool HeaderBordersVisible
{
get => _headerBordersVisible;
set
{
_headerBordersVisible = value;
OnPropertyChanged(nameof(HeaderBordersVisible));
}
get => GetValue<bool>();
set => SetValue(value);
}

public bool TeamColumnVisible
{
get => _teamColumnVisible;
set
{
_teamColumnVisible = value;
OnPropertyChanged(nameof(TeamColumnVisible));
}
get => GetValue<bool>();
set => SetValue(value);
}

public bool WonColumnVisible
{
get => _wonColumnVisible;
set
{
_wonColumnVisible = value;
OnPropertyChanged(nameof(WonColumnVisible));
}
get => GetValue<bool>();
set => SetValue(value);
}

public ushort TeamColumnWidth
{
get => _teamColumnWidth;
set
{
_teamColumnWidth = value;
OnPropertyChanged(nameof(TeamColumnWidth));
}
get => GetValue<ushort>();
set => SetValue(value);
}

public bool PaginationEnabled
{
get => _paginationEnabled;
set
{
_paginationEnabled = value;
OnPropertyChanged(nameof(PaginationEnabled));
}
get => GetValue<bool>();
set => SetValue(value);
}

public Team SelectedTeam
{
get => _selectedItem;
set
{
_selectedItem = value;
Debug.WriteLine("Team Selected : " + value?.Name);
}
get => GetValue<Team>();
set => SetValue(value);
}

public bool IsRefreshing
{
get => _isRefreshing;
set
{
_isRefreshing = value;
OnPropertyChanged(nameof(IsRefreshing));
}
get => GetValue<bool>();
set => SetValue(value);
}

public ICommand CancelEditCommand { get; }

public ICommand EditCommand { get; }

public ICommand RefreshCommand { get; }

private void CmdCancelEdit()
private void CmdCompleteEdit()
{
TeamToEdit = null;
}
Expand All @@ -141,12 +92,4 @@ private async void CmdRefresh()
await Task.Delay(3000);
IsRefreshing = false;
}

#region INotifyPropertyChanged implementation

public event PropertyChangedEventHandler PropertyChanged;

private void OnPropertyChanged(string property) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));

#endregion INotifyPropertyChanged implementation
}
47 changes: 47 additions & 0 deletions Maui.DataGrid.Sample/ViewModels/ViewModelBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
namespace Maui.DataGrid.Sample.ViewModels;

using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Input;

public abstract class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private readonly Dictionary<string, object> _properties = new();


public Dictionary<string, ICommand> Commands
{
get => GetValue<Dictionary<string, ICommand>>();
private init => SetValue(value);
}

protected ViewModelBase()
{
Commands = new();
}
protected void SetValue(object value, [CallerMemberName] string propertyName = null)
{
if (_properties.TryGetValue(propertyName!, out var item) && item == value)
{
return;
}

_properties[propertyName!] = value;
OnPropertyChanged(propertyName);
}

protected T GetValue<T>([CallerMemberName] string propertyName = null)
{
if (_properties.TryGetValue(propertyName!, out var value))
{
return (T)value;
}

return default;
}

protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

0 comments on commit aaa82e3

Please sign in to comment.