DeliveryGrid.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. 
  2. using Comal.Classes;
  3. using InABox.Clients;
  4. using InABox.Core;
  5. using Syncfusion.SfMaps.XForms;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. using Xamarin.CommunityToolkit.Converters;
  13. using Xamarin.Forms;
  14. using XF.Material.Forms.UI.Dialogs;
  15. using PRSSecurity = InABox.Core.Security;
  16. namespace comal.timesheets
  17. {
  18. public class DeliveryGrid : MobileDataGrid
  19. {
  20. private DeliveryShells _deliveries = new DeliveryShells();
  21. public DeliveryGrid(DataGridSaveType saveType = DataGridSaveType.Single)
  22. {
  23. if (Security.IsAllowed<CanViewAllJobs>())
  24. _deliveries.Load(DeliveryShells.AllJobs());
  25. else
  26. _deliveries.Load(DeliveryShells.AllJobs());
  27. Load(saveType);
  28. OnItemSelected += DeliveryGrid_OnItemSelected;
  29. OnImageSelected += DeliveryGrid_OnImageSelected;
  30. }
  31. private async void Load(DataGridSaveType saveType)
  32. {
  33. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading"))
  34. {
  35. CheckFilter();
  36. Setup(LoadList(), typeof(Delivery), saveType);
  37. }
  38. }
  39. private List<DataGridViewModelItem> LoadList()
  40. {
  41. try
  42. {
  43. CoreTable table = DoQuery();
  44. while(table == null)
  45. table = DoQuery();
  46. List<DataGridViewModelItem> items = new List<DataGridViewModelItem>();
  47. foreach (var row in table.Rows)
  48. items.Add(CreateItem(row));
  49. return items;
  50. }
  51. catch
  52. {
  53. return null;
  54. }
  55. }
  56. private DataGridViewModelItem CreateItem(CoreRow row)
  57. {
  58. var tuples = GenerateDataTuples(row);
  59. var address = GenerateAddress(row);
  60. return new DataGridViewModelItem(
  61. id: row.Get<Delivery, Guid>(x => x.ID),
  62. data: tuples,
  63. image: CheckAddress(address) ? new Image
  64. {
  65. Source = Device.RuntimePlatform.Equals(Device.iOS) ? "locationmarker" : "locationpointer.png"
  66. }
  67. : new Image(),
  68. imagePopupDetail: address
  69. );
  70. }
  71. private bool CheckAddress(Dictionary<string, string> address)
  72. {
  73. if (address.ContainsKey("Address") || address.ContainsKey("Latitude"))
  74. return true;
  75. return false;
  76. }
  77. private List<Tuple<string, string>> GenerateDataTuples(CoreRow row)
  78. {
  79. string extraTitle = !string.IsNullOrWhiteSpace(row.Get<Delivery, string>(x => x.Contact.Name)) ? " (" + row.Get<Delivery, string>(x => x.Contact.Name) + ")" :
  80. "";
  81. string extraJob = !string.IsNullOrWhiteSpace(row.Get<Delivery, string>(x => x.Job.Name)) ? " (" + row.Get<Delivery, string>(x => x.Job.Name) + ")" :
  82. "";
  83. return new List<Tuple<string, string>>
  84. {
  85. new Tuple<string, string>("No.", row.Get<Delivery, int>(x => x.Number).ToString() + extraTitle),
  86. new Tuple<string, string>("Job", row.Get<Delivery, string>(x => x.Job.JobNumber) + extraJob),
  87. new Tuple<string, string>("Del", row.Get<Delivery, DateTime>(x => x.Delivered) == DateTime.MinValue? " " : row.Get<Delivery, DateTime>(x => x.Delivered).ToString("dd MMM yy")),
  88. new Tuple<string, string>("Bkd", row.Get<Delivery, DateTime>(x => x.Assignment.Date) == DateTime.MinValue? " " : row.Get<Delivery, DateTime>(x => x.Assignment.Date).ToString("dd MMM yy"))
  89. };
  90. }
  91. private Dictionary<string, string> GenerateAddress(CoreRow row)
  92. {
  93. var dict = new Dictionary<string, string>();
  94. if (!string.IsNullOrWhiteSpace(row.Get<Delivery, string>(x => x.Location.Address)))
  95. {
  96. dict.Add("Address", row.Get<Delivery, string>(x => x.Location.Address));
  97. }
  98. double lat = row.Get<Delivery, double>(x => x.Location.Latitude);
  99. double longitude = row.Get<Delivery, double>(x => x.Location.Longitude);
  100. if (lat != 0 && longitude != 0)
  101. {
  102. dict.Add("Latitude", lat.ToString());
  103. dict.Add("Longitude", longitude.ToString());
  104. }
  105. if (row.Get<Delivery, DateTime>(x => x.Delivered) != DateTime.MinValue)
  106. {
  107. dict.Add("Delivered", row.Get<Delivery, DateTime>(x => x.Delivered).ToString("hh:mm tt dd MMM yy"));
  108. }
  109. if (!string.IsNullOrWhiteSpace(row.Get<Delivery, string>(x => x.DeliveredBy.Name)))
  110. {
  111. dict.Add("Delivered by", row.Get<Delivery, string>(x => x.DeliveredBy.Name));
  112. }
  113. return dict;
  114. }
  115. private object DeliveryGrid_OnItemSelected(DataGridViewModelItem item)
  116. {
  117. DeliveryDetails page = new DeliveryDetails(item.ID);
  118. Navigation.PushAsync(page);
  119. return null;
  120. }
  121. private object DeliveryGrid_OnImageSelected(DataGridViewModelItem item)
  122. {
  123. StackLayout stack = new StackLayout();
  124. if (item.ImagePopupDetail.ContainsKey("Address"))
  125. stack.Children.Add(new Label { HorizontalOptions = LayoutOptions.Center, Text = item.ImagePopupDetail["Address"], FontAttributes = FontAttributes.Bold });
  126. if (item.ImagePopupDetail.ContainsKey("Delivered"))
  127. stack.Children.Add(new Label { HorizontalOptions = LayoutOptions.Center, Text = "Delivered: " + item.ImagePopupDetail["Delivered"], FontAttributes = FontAttributes.Bold });
  128. if (item.ImagePopupDetail.ContainsKey("Delivered by"))
  129. stack.Children.Add(new Label { HorizontalOptions = LayoutOptions.Center, Text = "Delivered by: " + item.ImagePopupDetail["Delivered by"], FontAttributes = FontAttributes.Bold });
  130. if (!item.ImagePopupDetail.ContainsKey("Latitude"))
  131. return stack;
  132. SfMaps map = new SfMaps();
  133. ImageryLayer layer = new ImageryLayer();
  134. if (Device.RuntimePlatform.Equals(Device.iOS))
  135. {
  136. //ImageSource imageSource = "mapmarker.png";
  137. //sfMapLayer.MarkerSettings.MarkerIcon = MapMarkerIcon.Image;
  138. //sfMapLayer.MarkerSettings.ImageSource = "location-pin.pdf";
  139. //sfMapLayer.MarkerSettings.IconSize = 35;
  140. layer.MarkerSettings.MarkerIcon = MapMarkerIcon.Circle;
  141. layer.MarkerSettings.IconColor = Color.DarkBlue;
  142. layer.MarkerSettings.IconSize = 15;
  143. }
  144. else
  145. {
  146. layer.MarkerSettings.MarkerIcon = MapMarkerIcon.Image;
  147. layer.MarkerSettings.ImageSource = "mapmarker.png";
  148. layer.MarkerSettings.IconSize = 35;
  149. }
  150. layer.MarkerSettings.FontAttributes = FontAttributes.Bold;
  151. layer.MarkerSettings.LabelSize = 20;
  152. layer.MarkerSettings.LabelColor = Color.DarkBlue;
  153. layer.GeoCoordinates = new Point(double.Parse(item.ImagePopupDetail["Latitude"]), double.Parse(item.ImagePopupDetail["Longitude"]));
  154. map.ZoomLevel = 15;
  155. map.MinZoom = 10;
  156. map.MaxZoom = 18;
  157. MapMarker marker = new MapMarker();
  158. marker.Latitude = item.ImagePopupDetail["Latitude"];
  159. marker.Longitude = item.ImagePopupDetail["Longitude"];
  160. layer.Markers.Add(marker);
  161. map.Layers.Add(layer);
  162. map.HorizontalOptions = LayoutOptions.CenterAndExpand;
  163. map.VerticalOptions = LayoutOptions.CenterAndExpand;
  164. stack.Children.Add(map);
  165. return new ScrollView { Content = stack };
  166. }
  167. public void RefreshGrid()
  168. {
  169. try
  170. {
  171. var list = LoadList();
  172. if (list == null)
  173. return;
  174. Items.Clear();
  175. Items.AddRange(list);
  176. Refresh(Items);
  177. }
  178. catch { }
  179. }
  180. }
  181. }