DeliveryGrid.cs 9.7 KB

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