Skip to content

Commit

Permalink
Datagrid using DatagridProvider.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
bachtiarpanjaitan committed Aug 12, 2024
1 parent 84f8ad6 commit 4903c26
Show file tree
Hide file tree
Showing 12 changed files with 175 additions and 181 deletions.
45 changes: 13 additions & 32 deletions Bepe/Components/FilterOne.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System.ComponentModel;
using IhandCashier.Bepe.Providers;

namespace IhandCashier.Bepe.Components;

public class FilterOne<T> where T : class
public static class FilterOne<T> where T : class
{
private readonly string _moduleName;
private readonly Entry _search = new()
private readonly static Entry Search = new()
{
HorizontalOptions = LayoutOptions.End,
VerticalOptions = LayoutOptions.Center,
Expand All @@ -15,15 +15,15 @@ public class FilterOne<T> where T : class
FontAttributes = FontAttributes.Bold,
MinimumWidthRequest = 50
};
private readonly Button _searchBtn = new()
private readonly static Button SearchBtn = new()
{
HorizontalOptions = LayoutOptions.End,
VerticalOptions = LayoutOptions.Center,
Text = "Cari",
WidthRequest = 100,
Margin = new Thickness(10, 0)
};
private readonly Label _moduleLabel = new()
private readonly static Label ModuleLabel = new()
{
Text = "",
HorizontalOptions = LayoutOptions.Start,
Expand All @@ -33,7 +33,7 @@ public class FilterOne<T> where T : class
FontSize = 20

};
private readonly Grid _grid = new()
private readonly static Grid Grid = new()
{
ColumnDefinitions =
{
Expand All @@ -43,34 +43,15 @@ public class FilterOne<T> where T : class
}
};

private readonly Frame _frame = new()
{
CornerRadius = 5,
BackgroundColor = Colors.Transparent,
Margin = new Thickness(5,0),
};

public event PropertyChangedEventHandler PropertyChanged;

public static event PropertyChangedEventHandler PropertyChanged;

public FilterOne(string moduleName)
{
_moduleName = moduleName;
_grid.Add(_moduleLabel,0);
_grid.Add(_search,1);
_grid.Add(_searchBtn,2);
}

private Frame CreateLayout()
public static void Initialize(string moduleName)
{
_moduleLabel.Text = _moduleName;
_frame.Content = _grid;
return _frame;
ModuleLabel.Text = moduleName;
Grid.Add(ModuleLabel,0);
Grid.Add(Search,1);
Grid.Add(SearchBtn,2);
DatagridProvider.HeaderFrame.Content = Grid;
}

public Frame Build()
{
return CreateLayout();
}

}
53 changes: 0 additions & 53 deletions Bepe/Components/IcDataGrid.cs

This file was deleted.

80 changes: 18 additions & 62 deletions Bepe/Components/Pagination.cs
Original file line number Diff line number Diff line change
@@ -1,81 +1,39 @@

using System.ComponentModel;
using System.Runtime.CompilerServices;
using IhandCashier.Bepe.Helpers;
using Syncfusion.Maui.Core;
using Syncfusion.Maui.DataGrid;
using IhandCashier.Bepe.Configs;
using IhandCashier.Bepe.Providers;

namespace IhandCashier.Bepe.Components
{
public sealed class Pagination<T> : INotifyPropertyChanged where T : class
public class Pagination<T> : INotifyPropertyChanged where T : class
{
private int _pageIndex = 0;
private readonly int _pageSize = 0;
private readonly Button _prevButton = new() {Margin = new Thickness(10, 0),WidthRequest = 150,VerticalOptions = LayoutOptions.Center, Text = "Sebelumnya" };
private readonly Button _nextButton = new() {Margin = new Thickness(10, 0),WidthRequest = 150,VerticalOptions = LayoutOptions.Center, Text = "Selanjutnya" };
private readonly Label _pageLabel = new() {VerticalOptions = LayoutOptions.Center,VerticalTextAlignment = TextAlignment.Center, Text = "", Margin = new Thickness(10,0)};
private readonly Label _totalLabel = new() {VerticalOptions = LayoutOptions.Center,VerticalTextAlignment = TextAlignment.Center, Text = "", Margin = new Thickness(10, 0)};

private readonly Grid _containerLayout = new()
{
ColumnDefinitions =
{
new ColumnDefinition { Width = GridLength.Auto },
new ColumnDefinition { Width = GridLength.Auto },
new ColumnDefinition { Width = GridLength.Auto },
new ColumnDefinition { Width = GridLength.Auto }
},
HorizontalOptions = LayoutOptions.Center
};

private readonly Frame _frameContainerLayout = new()
{
CornerRadius = 5,
BackgroundColor = Colors.Transparent,
Margin = new Thickness(5, 0),
};

private int _total = 0;
private int _pageCount = 0;
private Task<List<T>> _pagedData;
private readonly SfDataGrid _dataGrid;
public Task<List<T>> PagedData
{
get => _pagedData;
private set
{
_pagedData = value;
OnPropertyChanged(nameof(PagedData));
UpdatePageLabel();
}
}


public Pagination(int index, int size, SfDataGrid grid)
{
_pageIndex = index;
_pageSize = size;
_dataGrid = grid;
_containerLayout.Add(_prevButton,0);
_containerLayout.Add(_pageLabel,1);
_containerLayout.Add(_nextButton,2);
_containerLayout.Add(_totalLabel,3);
}

private Frame CreateLayout()
{
UpdatePageLabel();
_frameContainerLayout.Content = _containerLayout;
_prevButton.Clicked += OnPrevButtonClicked;
_nextButton.Clicked += OnNextButtonClicked;
return _frameContainerLayout;
}

public Frame Build()
public Pagination()
{
_pageSize = AppConfig.DATA_ROW_PER_PAGE;
_ = UpdatePagedData();
return CreateLayout();
UpdatePageLabel();
DatagridProvider.PrevButton.Clicked += OnPrevButtonClicked;
DatagridProvider.NextButton.Clicked += OnNextButtonClicked;
DatagridProvider.DataGrid.Columns.Clear();
}

private void OnPrevButtonClicked(object sender, EventArgs e)
{
if (_pageIndex <= 0) return;
Expand All @@ -90,22 +48,20 @@ private void OnNextButtonClicked(object sender, EventArgs e)
_ = UpdatePagedData();
}

private async Task UpdatePagedData()
public async Task UpdatePagedData()
{
var ph = new PaginationHandler(_pageIndex, _pageSize);
PagedData = Task.FromResult(await ph.GetDataAsync<T>());
_total = ph.GetTotalDataAsync<T>();

PagedData = Task.FromResult(await DatagridProvider.PaginationHandler.Limit(_pageIndex).Take(_pageSize).GetAsync<T>());
_total = DatagridProvider.PaginationHandler.GetTotalAsync<T>();

var result = (double)_total / _pageSize;
_pageCount = (int)Math.Ceiling(result);
_dataGrid.ItemsSource = PagedData.Result;
UpdatePageLabel();
DatagridProvider.DataGrid.ItemsSource = PagedData.Result;
}

private void UpdatePageLabel()
{
_pageLabel.Text = $"{_pageIndex + 1}/{_pageCount}";
_totalLabel.Text = $"Total {_total} data";
DatagridProvider.PageLabel.Text = $"{_pageIndex + 1}/{_pageCount}";
DatagridProvider.TotalLabel.Text = $"Total {_total} data";
}

public event PropertyChangedEventHandler PropertyChanged;
Expand Down
5 changes: 2 additions & 3 deletions Bepe/Database/BaseRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ public BaseRepository()
DB = new AppDbContext(optionsBuilder.Options);
}

public IQueryable<T> GetEntities<T>() where T : class
public IQueryable<T> GetData<T>() where T : class
{
var dbSet = DB.Set<T>();
return dbSet;
return DB.Set<T>();
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions Bepe/Entities/Unit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class Unit : IEntity
[Key]
[PrimaryKey, AutoIncrement]
public int id { get; set; }

public int? basic_unit_id { get; set; }

[Required]
Expand All @@ -24,7 +24,7 @@ public class Unit : IEntity
public decimal konversi { get; set; }

[Ignore]
public BasicUnit BasicUnit { get; set; }
public BasicUnit basic_unit_ { get; set; }
}
}

35 changes: 23 additions & 12 deletions Bepe/Helpers/PaginationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,47 @@ namespace IhandCashier.Bepe.Helpers
{
public class PaginationHandler
{
private int Index = 0;
private int Size = 0;
private int _index = 0;
private int _size = 0;
private readonly BaseRepository _baseRepository = new();
public PaginationHandler(int index, int size)
public PaginationHandler()
{
Index = index;
Size = size;

}

public PaginationHandler Limit(int index)
{
_index = index;
return this;
}

public PaginationHandler Take(int size)
{
_size = size;
return this;
}

public async Task<List<T>> GetDataAsync<T>() where T : class
public async Task<List<T>> GetAsync<T>() where T : class
{
try
{
var items = await _baseRepository.GetEntities<T>()
.Skip(Index * Size)
.Take(Size)
var items = await _baseRepository.GetData<T>()
.Skip(_index * _size)
.Take(_size)
.ToListAsync();
return items;
}
catch (Exception e)
{
Console.WriteLine($"Error AppDbContext:29 : {e.Message}");
Console.WriteLine($"Error PaginationHandler:40 : {e.Message}");
}

throw new InvalidOperationException();
}

public int GetTotalDataAsync<T>() where T : class
public int GetTotalAsync<T>() where T : class
{
return _baseRepository.GetEntities<T>().Count();
return _baseRepository.GetData<T>().Count();
}
}
}
Expand Down
Loading

0 comments on commit 4903c26

Please sign in to comment.