|
|
@@ -3,52 +3,167 @@ using InABox.Core;
|
|
|
using Syncfusion.SfDataGrid.XForms;
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
+using System.ComponentModel;
|
|
|
using System.Linq;
|
|
|
using System.Text;
|
|
|
+using System.Threading;
|
|
|
+using System.Threading.Tasks;
|
|
|
using Xamarin.Forms;
|
|
|
|
|
|
namespace comal.timesheets
|
|
|
{
|
|
|
- public abstract class MobileGrid<TEntity> : SfDataGrid, BaseMobileGrid<TEntity> where TEntity : Entity, IRemotable, IPersistent, new()
|
|
|
+ public abstract class MobileGrid : SfDataGrid
|
|
|
{
|
|
|
- List<string> ColumnNames = new List<string>();
|
|
|
+ List<MobileGridDataModelShell> Data;
|
|
|
|
|
|
- CoreTable Data;
|
|
|
+ public MobileGrid()
|
|
|
+ {
|
|
|
|
|
|
- public Columns<TEntity> VisibleColumns { get; set; }
|
|
|
+ }
|
|
|
|
|
|
- public MobileGrid(Filter<TEntity> filter, Columns<TEntity> columns, SortOrder<TEntity> sortOrder)
|
|
|
+ public void Setup(List<MobileGridDataModelShell> data)
|
|
|
{
|
|
|
- Data = new Client<TEntity>().Query(filter, CheckColumns(columns), sortOrder);
|
|
|
- Grid grid = new Grid();
|
|
|
- grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
|
|
|
+ AutoGenerateColumns = false;
|
|
|
+ AssignPropertiesToColumns(data);
|
|
|
+ GenerateColumns(data.First());
|
|
|
+ ColumnSizer = ColumnSizer.Star;
|
|
|
+ AllowResizingColumn = true;
|
|
|
+ AllowSorting = true;
|
|
|
+ NavigationMode = NavigationMode.Row;
|
|
|
+ SelectionMode = Syncfusion.SfDataGrid.XForms.SelectionMode.Single;
|
|
|
+ SelectionUnit = SelectionUnit.Row;
|
|
|
+ Data = data;
|
|
|
+
|
|
|
+ Device.BeginInvokeOnMainThread(() =>
|
|
|
+ {
|
|
|
+ ItemsSource = data;
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
- public void Init(Filter<TEntity> filter, Columns<TEntity> columns, SortOrder<TEntity> sortOrder)
|
|
|
+ protected void AssignPropertiesToColumns(List<MobileGridDataModelShell> data)
|
|
|
{
|
|
|
- Data = new Client<TEntity>().Query(filter, CheckColumns(columns), sortOrder);
|
|
|
+ foreach (var shell in data)
|
|
|
+ {
|
|
|
+ if (shell.Data.Count > 0)
|
|
|
+ shell.Column1 = shell.Data[0].Item2;
|
|
|
+ if (shell.Data.Count > 1)
|
|
|
+ shell.Column2 = shell.Data[1].Item2;
|
|
|
+ if (shell.Data.Count > 2)
|
|
|
+ shell.Column3 = shell.Data[2].Item2;
|
|
|
+ if (shell.Data.Count > 3)
|
|
|
+ shell.Column4 = shell.Data[3].Item2;
|
|
|
+ if (shell.Data.Count > 4)
|
|
|
+ shell.Column5 = shell.Data[4].Item2;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- private Columns<TEntity> CheckColumns(Columns<TEntity> columns)
|
|
|
+ protected void GenerateColumns(MobileGridDataModelShell shell)
|
|
|
{
|
|
|
- foreach (var colname in ColumnNames)
|
|
|
+ foreach (var col in shell.Data)
|
|
|
{
|
|
|
- if (!columns.ColumnNames().Contains(colname))
|
|
|
- columns.Add(new Column<TEntity>(colname));
|
|
|
+ GenerateColumn(new Tuple<string, object>(col.Item1, col.Item2), shell.Data.IndexOf(col));
|
|
|
+ }
|
|
|
+ if (shell.HasImage)
|
|
|
+ {
|
|
|
+ GenerateColumn(new Tuple<string, object>("Image", shell.Image), -1);
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- if (!columns.ColumnNames().Contains("ID"))
|
|
|
- columns.Add(new Column<TEntity>(x => x.ID));
|
|
|
+ protected void GenerateColumn(Tuple<string, object> col, int index)
|
|
|
+ {
|
|
|
+ if (index > 4)
|
|
|
+ return;
|
|
|
|
|
|
- return columns;
|
|
|
+ var column = FindColumnType(col.Item2);
|
|
|
+ column = AddMapping(column, index);
|
|
|
+ column = AddCaption(column, col.Item1);
|
|
|
+
|
|
|
+ Device.BeginInvokeOnMainThread(() =>
|
|
|
+ {
|
|
|
+ Columns.Add(column);
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
- public void DefineVisibleColumns(Columns<TEntity> columns)
|
|
|
+ protected GridColumn FindColumnType(object value)
|
|
|
{
|
|
|
- foreach(var colname in columns.ColumnNames())
|
|
|
- ColumnNames.Add(colname);
|
|
|
+ if(value == null || value.GetType() == typeof(ImageSource))
|
|
|
+ return new GridImageColumn();
|
|
|
+ if (value.GetType() == typeof(string))
|
|
|
+ return new GridTextColumn();
|
|
|
+
|
|
|
+ return new GridTextColumn();
|
|
|
+ }
|
|
|
|
|
|
- VisibleColumns = columns;
|
|
|
+ protected GridColumn AddMapping(GridColumn col, int index)
|
|
|
+ {
|
|
|
+ switch (index)
|
|
|
+ {
|
|
|
+ case -1:
|
|
|
+ col.MappingName = "Image";
|
|
|
+ break;
|
|
|
+ case 0:
|
|
|
+ col.MappingName = "Column1";
|
|
|
+ break;
|
|
|
+ case 1:
|
|
|
+ col.MappingName = "Column2";
|
|
|
+ break;
|
|
|
+ case 2:
|
|
|
+ col.MappingName = "Column3";
|
|
|
+ break;
|
|
|
+ case 3:
|
|
|
+ col.MappingName = "Column4";
|
|
|
+ break;
|
|
|
+ case 4:
|
|
|
+ col.MappingName = "Column5";
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ return col;
|
|
|
+ }
|
|
|
+
|
|
|
+ protected GridColumn AddCaption(GridColumn col, string caption)
|
|
|
+ {
|
|
|
+ col.HeaderText = caption;
|
|
|
+ col.HeaderCellTextSize = 18;
|
|
|
+ col.HeaderFontAttribute = FontAttributes.Bold;
|
|
|
+
|
|
|
+ return col;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public class MobileGridDataModelShell
|
|
|
+ {
|
|
|
+ public Guid ID { get; set; }
|
|
|
+ public List<Tuple<string, string>> Data { get; set; }
|
|
|
+
|
|
|
+ public string Column1 { get; set; }
|
|
|
+ public string Column2 { get; set; }
|
|
|
+ public string Column3 { get; set; }
|
|
|
+ public string Column4 { get; set; }
|
|
|
+ public string Column5 { get; set; }
|
|
|
+
|
|
|
+ public ImageSource Image { get; set; }
|
|
|
+
|
|
|
+ public bool HasImage { get; set; }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// max of 5 columns to display on mobile
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="id"></param>
|
|
|
+ /// <param name="data"></param>
|
|
|
+ public MobileGridDataModelShell(Guid id, List<Tuple<string, string>> data, Image image = null)
|
|
|
+ {
|
|
|
+ ID = id;
|
|
|
+ Data = data;
|
|
|
+ if (image != null)
|
|
|
+ {
|
|
|
+ Image = image.Source;
|
|
|
+ HasImage = true;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ Image = null;
|
|
|
+ HasImage = false;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|