MobileGrid.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using InABox.Clients;
  2. using InABox.Core;
  3. using Syncfusion.SfDataGrid.XForms;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using Xamarin.Forms;
  12. namespace comal.timesheets
  13. {
  14. public abstract class MobileGrid : SfDataGrid
  15. {
  16. List<MobileGridDataModelShell> Data;
  17. public MobileGrid()
  18. {
  19. }
  20. public void Setup(List<MobileGridDataModelShell> data)
  21. {
  22. AutoGenerateColumns = false;
  23. AssignPropertiesToColumns(data);
  24. GenerateColumns(data.First());
  25. ColumnSizer = ColumnSizer.Star;
  26. AllowResizingColumn = true;
  27. AllowSorting = true;
  28. NavigationMode = NavigationMode.Row;
  29. SelectionMode = Syncfusion.SfDataGrid.XForms.SelectionMode.Single;
  30. SelectionUnit = SelectionUnit.Row;
  31. Data = data;
  32. Device.BeginInvokeOnMainThread(() =>
  33. {
  34. ItemsSource = data;
  35. });
  36. }
  37. protected void AssignPropertiesToColumns(List<MobileGridDataModelShell> data)
  38. {
  39. foreach (var shell in data)
  40. {
  41. if (shell.Data.Count > 0)
  42. shell.Column1 = shell.Data[0].Item2;
  43. if (shell.Data.Count > 1)
  44. shell.Column2 = shell.Data[1].Item2;
  45. if (shell.Data.Count > 2)
  46. shell.Column3 = shell.Data[2].Item2;
  47. if (shell.Data.Count > 3)
  48. shell.Column4 = shell.Data[3].Item2;
  49. if (shell.Data.Count > 4)
  50. shell.Column5 = shell.Data[4].Item2;
  51. }
  52. }
  53. protected void GenerateColumns(MobileGridDataModelShell shell)
  54. {
  55. foreach (var col in shell.Data)
  56. {
  57. GenerateColumn(new Tuple<string, object>(col.Item1, col.Item2), shell.Data.IndexOf(col));
  58. }
  59. if (shell.HasImage)
  60. {
  61. GenerateColumn(new Tuple<string, object>("Image", shell.Image), -1);
  62. }
  63. }
  64. protected void GenerateColumn(Tuple<string, object> col, int index)
  65. {
  66. if (index > 4)
  67. return;
  68. var column = FindColumnType(col.Item2);
  69. column = AddMapping(column, index);
  70. column = AddCaption(column, col.Item1);
  71. Device.BeginInvokeOnMainThread(() =>
  72. {
  73. Columns.Add(column);
  74. });
  75. }
  76. protected GridColumn FindColumnType(object value)
  77. {
  78. if(value == null || value.GetType() == typeof(ImageSource))
  79. return new GridImageColumn();
  80. if (value.GetType() == typeof(string))
  81. return new GridTextColumn();
  82. return new GridTextColumn();
  83. }
  84. protected GridColumn AddMapping(GridColumn col, int index)
  85. {
  86. switch (index)
  87. {
  88. case -1:
  89. col.MappingName = "Image";
  90. break;
  91. case 0:
  92. col.MappingName = "Column1";
  93. break;
  94. case 1:
  95. col.MappingName = "Column2";
  96. break;
  97. case 2:
  98. col.MappingName = "Column3";
  99. break;
  100. case 3:
  101. col.MappingName = "Column4";
  102. break;
  103. case 4:
  104. col.MappingName = "Column5";
  105. break;
  106. }
  107. return col;
  108. }
  109. protected GridColumn AddCaption(GridColumn col, string caption)
  110. {
  111. col.HeaderText = caption;
  112. col.HeaderCellTextSize = 18;
  113. col.HeaderFontAttribute = FontAttributes.Bold;
  114. return col;
  115. }
  116. }
  117. public class MobileGridDataModelShell
  118. {
  119. public Guid ID { get; set; }
  120. public List<Tuple<string, string>> Data { get; set; }
  121. public string Column1 { get; set; }
  122. public string Column2 { get; set; }
  123. public string Column3 { get; set; }
  124. public string Column4 { get; set; }
  125. public string Column5 { get; set; }
  126. public ImageSource Image { get; set; }
  127. public bool HasImage { get; set; }
  128. /// <summary>
  129. /// max of 5 columns to display on mobile
  130. /// </summary>
  131. /// <param name="id"></param>
  132. /// <param name="data"></param>
  133. public MobileGridDataModelShell(Guid id, List<Tuple<string, string>> data, Image image = null)
  134. {
  135. ID = id;
  136. Data = data;
  137. if (image != null)
  138. {
  139. Image = image.Source;
  140. HasImage = true;
  141. }
  142. else
  143. {
  144. Image = null;
  145. HasImage = false;
  146. }
  147. }
  148. }
  149. }