ProductsGrid.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using comal.timesheets.Products;
  2. using Comal.Classes;
  3. using InABox.Clients;
  4. using InABox.Core;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using Xamarin.Forms;
  11. using XF.Material.Forms.UI.Dialogs;
  12. namespace comal.timesheets
  13. {
  14. public delegate void ProductsGridItemSelected(DataGridViewModelItem item);
  15. public delegate Task<string> ProductsGridShowOptions();
  16. public class ProductsGrid : MobileDataGrid
  17. {
  18. public event ProductsGridItemSelected ItemSelected;
  19. public event ProductsGridShowOptions ShowOptions;
  20. public ProductsGrid(DataGridSaveType savetype = DataGridSaveType.None)
  21. {
  22. OnItemSelected += ProductsGrid_OnItemSelected;
  23. LoadItems(savetype);
  24. }
  25. private void ProductsGrid_OnItemSelected(DataGridViewModelItem item)
  26. {
  27. ItemSelected?.Invoke(item);
  28. DisplayOptions(item);
  29. }
  30. private void LoadItems(DataGridSaveType savetype = DataGridSaveType.None)
  31. {
  32. Task.Run(async () =>
  33. {
  34. if (GlobalVariables.ProductsLoaded)
  35. {
  36. List<DataGridViewModelItem> shells = new List<DataGridViewModelItem>();
  37. foreach (var product in GlobalVariables.ProductShells)
  38. {
  39. List<Tuple<string, string>> tuples = new List<Tuple<string, string>>();
  40. tuples.Add(new Tuple<string, string>("Code", product.Code));
  41. tuples.Add(new Tuple<string, string>("Name", product.Name));
  42. tuples.Add(new Tuple<string, string>("Family", product.Group));
  43. shells.Add(new DataGridViewModelItem
  44. (
  45. id: product.ID,
  46. data: tuples,
  47. image: product.ImageID == Guid.Empty ? new Image() : new Image { Source = "productimage.png", HeightRequest = 30, WidthRequest = 30 },
  48. imageid: product.ImageID
  49. ));
  50. }
  51. Setup(shells, typeof(Product), savetype);
  52. }
  53. else
  54. {
  55. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading"))
  56. {
  57. CoreTable table = new Client<Product>().Query(null,
  58. new Columns<Product>(x => x.ID,
  59. x => x.Code,
  60. x => x.Name,
  61. x => x.Group.Description,
  62. x => x.Image.ID
  63. ));
  64. if (!table.Rows.Any())
  65. return;
  66. List<DataGridViewModelItem> shells = new List<DataGridViewModelItem>();
  67. foreach (var row in table.Rows)
  68. {
  69. List<Tuple<string, string>> tuples = new List<Tuple<string, string>>();
  70. tuples.Add(new Tuple<string, string>("Code", row.Get<Product, string>(x => x.Code)));
  71. tuples.Add(new Tuple<string, string>("Name", row.Get<Product, string>(x => x.Name)));
  72. tuples.Add(new Tuple<string, string>("Family", row.Get<Product, string>(x => x.Group.Description)));
  73. shells.Add(new DataGridViewModelItem
  74. (
  75. id: row.Get<Product, Guid>(x => x.ID),
  76. data: tuples,
  77. image: row.Get<Product, Guid>(x => x.Image.ID) == Guid.Empty ? new Image() : new Image { Source = "productimage.png", HeightRequest = 30, WidthRequest = 30 },
  78. imageid: row.Get<Product, Guid>(x => x.Image.ID)
  79. ));
  80. }
  81. Setup(shells, typeof(Product), savetype);
  82. }
  83. }
  84. });
  85. }
  86. public async void DisplayOptions(DataGridViewModelItem item)
  87. {
  88. ProductShell product = GlobalVariables.ProductShells.FirstOrDefault(x => x.ID == item.ID);
  89. ListSelectionPage page = new ListSelectionPage(new List<String> { "View Holdings", "View Movements" });
  90. page.OnSimpleListTapped += ((chosenOption) =>
  91. {
  92. Device.BeginInvokeOnMainThread(() =>
  93. {
  94. switch (chosenOption)
  95. {
  96. case "Cancel":
  97. return;
  98. case "View Holdings":
  99. HoldingViewer holdingViewer = new HoldingViewer(product.ID, product.Name, product.ImageID);
  100. holdingViewer.OnExitSelected += () =>
  101. {
  102. Navigation.PopAsync();
  103. };
  104. Navigation.PushAsync(holdingViewer);
  105. break;
  106. case "View Movements":
  107. MovementViewer movementViewer = new MovementViewer(product.ID, product.Name);
  108. Navigation.PushAsync(movementViewer);
  109. break;
  110. default: break;
  111. }
  112. });
  113. });
  114. Navigation.PushAsync(page);
  115. }
  116. }
  117. }