LiveMapPage.xaml.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Linq.Expressions;
  7. using System.Threading.Tasks;
  8. using comal.timesheets.Tasks;
  9. using Comal.Classes;
  10. using InABox.Clients;
  11. using InABox.Configuration;
  12. using InABox.Core;
  13. using Xamarin.Forms;
  14. using Xamarin.Forms.Xaml;
  15. using XF.Material.Forms.UI;
  16. using XF.Material.Forms.UI.Dialogs;
  17. using Syncfusion.SfMaps.XForms;
  18. using System.Threading;
  19. using System.IO;
  20. using Xamarin.Essentials;
  21. namespace comal.timesheets.LiveMaps
  22. {
  23. [XamlCompilation(XamlCompilationOptions.Compile)]
  24. public partial class LiveMapPage : ContentPage
  25. {
  26. Equipment equipment = new Equipment();
  27. Job job = new Job();
  28. List<String> types = new List<String>();
  29. string[] array;
  30. string country = "Australia";
  31. public LiveMapPage()
  32. {
  33. InitializeComponent();
  34. job = new Job();
  35. equipment = new Equipment();
  36. sfMapLayer.GeoCoordinates = new Point(-32.0410801, 115.9957434);
  37. sfMap.ZoomLevel = 10;
  38. LoadData();
  39. }
  40. protected override void OnAppearing()
  41. {
  42. base.OnAppearing();
  43. }
  44. private async Task LoadData()
  45. {
  46. Title = "Live Maps - Loading";
  47. await Task.Run(() =>
  48. {
  49. var client = new Client<EquipmentGroup>();
  50. var table = client.Query(
  51. new Filter<EquipmentGroup>(x => x.Description).IsNotEqualTo(string.Empty),
  52. new Columns<EquipmentGroup>(x => x.Description),
  53. new SortOrder<EquipmentGroup>(x => x.Description)
  54. );
  55. foreach (CoreRow row in table.Rows)
  56. {
  57. EquipmentGroup group = row.ToObject<EquipmentGroup>();
  58. types.Add(group.Description);
  59. }
  60. array = types.ToArray();
  61. Device.BeginInvokeOnMainThread(() =>
  62. {
  63. Title = "Live Maps";
  64. selectBtn.IsEnabled = true;
  65. });
  66. });
  67. }
  68. private async void SelectBtn_Clicked(object sender, EventArgs e)
  69. {
  70. try
  71. {
  72. string chosenOption = await DisplayActionSheet("Select", "Cancel", null, "Equipment", "Job Locations");
  73. if (!chosenOption.Equals("Cancel"))
  74. {
  75. if (chosenOption == "Equipment")
  76. {
  77. SelectEquipmentList();
  78. }
  79. if (chosenOption == "Job Locations")
  80. {
  81. SelectJob();
  82. }
  83. }
  84. }
  85. catch
  86. {
  87. return;
  88. }
  89. }
  90. private async void SelectEquipmentList()
  91. {
  92. try
  93. {
  94. string chosenOption = await DisplayActionSheet("Choose Equipment Type", "Cancel", null, array);
  95. if (!chosenOption.Equals("Cancel"))
  96. {
  97. Filter<Equipment> filter = new Filter<Equipment>(x => x.GroupLink.Description).IsEqualTo(chosenOption).And(x => x.TrackerLink.ID).IsNotEqualTo(Guid.Empty);
  98. SelectEquipment(filter);
  99. }
  100. }
  101. catch { }
  102. }
  103. void SelectEquipment(Filter<Equipment> filter)
  104. {
  105. lastUpdatedLbl.Text = "Last Updated: ";
  106. sfMapLayer.Markers.Clear();
  107. GenericSelectionPage page = new GenericSelectionPage
  108. (
  109. "Select Equipment",
  110. new SelectionViewModel<Equipment>
  111. (
  112. filter,
  113. new Expression<Func<Equipment, object>>[] { x => x.Description, x => x.SerialNumber },
  114. new Expression<Func<Equipment, object>>[] { x => x.TrackerLink.Location.Latitude, x => x.TrackerLink.Location.Longitude, x => x.TrackerLink.Location.Timestamp, x => x.Code, x => x.ID },
  115. new SortOrder<Equipment>(x => x.Description)
  116. ));
  117. page.OnItemSelected += (row) =>
  118. {
  119. equipment = row.ToObject<Equipment>();
  120. sfMap.ZoomLevel = 15;
  121. sfMapLayer.GeoCoordinates = new Point(equipment.TrackerLink.Location.Latitude, equipment.TrackerLink.Location.Longitude);
  122. MapMarker marker1 = new MapMarker();
  123. marker1.Label = equipment.Description;
  124. marker1.Latitude = equipment.TrackerLink.Location.Latitude.ToString();
  125. marker1.Longitude = equipment.TrackerLink.Location.Longitude.ToString();
  126. sfMapLayer.Markers.Add(marker1);
  127. sfMapLayer.MarkerSettings.IconSize = 15;
  128. sfMapLayer.MarkerSettings.FontAttributes = FontAttributes.Bold;
  129. sfMapLayer.MarkerSettings.LabelSize = 20;
  130. sfMapLayer.MarkerSettings.LabelColor = Color.DarkRed;
  131. sfMapLayer.MarkerSettings.IconColor = Color.Red;
  132. if (equipment.TrackerLink.Location.Timestamp != DateTime.MinValue)
  133. {
  134. Device.BeginInvokeOnMainThread(() =>
  135. {
  136. lastUpdatedLbl.Text = "Last updated: " + equipment.TrackerLink.Location.Timestamp.ToString("hh-mm-tt dd-MMM-yy");
  137. lastUpdatedLbl.TextColor = Color.HotPink;
  138. });
  139. }
  140. else
  141. {
  142. Device.BeginInvokeOnMainThread(() =>
  143. {
  144. Title = "Live Maps";
  145. lastUpdatedLbl.Text = "Last updated: NA";
  146. lastUpdatedLbl.TextColor = Color.Default;
  147. });
  148. }
  149. job = null;
  150. ChangeTitle();
  151. };
  152. Navigation.PushAsync(page);
  153. }
  154. private async void SelectJob()
  155. {
  156. Filter<Job> filter = new Filter<Job>(x => x.JobStatus.Active).IsEqualTo(true).And(x => x.JobStatus.Code).IsEqualTo("01 ACTIVE");
  157. GenericSelectionPage page = new GenericSelectionPage
  158. (
  159. "Select Job",
  160. new SelectionViewModel<Job>
  161. (
  162. filter,
  163. new Expression<Func<Job, object>>[] { x => x.JobNumber, x => x.Name },
  164. new Expression<Func<Job, object>>[] { x => x.SiteAddress.Street, x => x.SiteAddress.City, x => x.Name, x => x.ID },
  165. new SortOrder<Job>(x => x.JobNumber)
  166. ));
  167. page.OnItemSelected += (row) =>
  168. {
  169. job = row.ToObject<Job>();
  170. ShowJobLocation(job.SiteAddress.Street, job.SiteAddress.City, job.Name);
  171. };
  172. Navigation.PushAsync(page);
  173. }
  174. private async void ShowJobLocation(string street, string city, string name)
  175. {
  176. try
  177. {
  178. var location = (await Geocoding.GetLocationsAsync($"{street}, {city}, {country}")).FirstOrDefault();
  179. if (location == null) return;
  180. sfMap.ZoomLevel = 15;
  181. sfMapLayer.GeoCoordinates = new Point(location.Latitude, location.Longitude);
  182. MapMarker marker1 = new MapMarker();
  183. marker1.Label = name;
  184. marker1.Latitude = location.Latitude.ToString();
  185. marker1.Longitude = location.Longitude.ToString();
  186. sfMapLayer.Markers.Add(marker1);
  187. sfMapLayer.MarkerSettings.IconSize = 15;
  188. sfMapLayer.MarkerSettings.FontAttributes = FontAttributes.Bold;
  189. sfMapLayer.MarkerSettings.LabelSize = 20;
  190. sfMapLayer.MarkerSettings.LabelColor = Color.DarkRed;
  191. sfMapLayer.MarkerSettings.IconColor = Color.Red;
  192. if (!string.IsNullOrWhiteSpace(name))
  193. {
  194. Title = name;
  195. }
  196. equipment = null;
  197. }
  198. catch (Exception e)
  199. {
  200. await DisplayAlert("Error Loading Address.",
  201. street + " " + city + " Not found.", "OK");
  202. return;
  203. }
  204. }
  205. private async void OpenInMapsBtn_Clicked(object sender, EventArgs e)
  206. {
  207. try
  208. {
  209. if (job.ID != Guid.Empty)
  210. {
  211. var location = (await Geocoding.GetLocationsAsync($"{job.SiteAddress.Street}, {job.SiteAddress.City}, {country}")).FirstOrDefault();
  212. if (location == null) return;
  213. var options = new MapLaunchOptions { Name = job.Name };
  214. await Map.OpenAsync(location, options);
  215. }
  216. }
  217. catch { }
  218. try
  219. {
  220. if (equipment.ID != Guid.Empty)
  221. {
  222. var location = new Xamarin.Essentials.Location(equipment.TrackerLink.Location.Latitude, equipment.TrackerLink.Location.Longitude);
  223. var options = new MapLaunchOptions { };
  224. await Map.OpenAsync(location, options);
  225. }
  226. }
  227. catch { }
  228. }
  229. private async void ChangeTitle()
  230. {
  231. await Task.Run(() =>
  232. {
  233. if (!string.IsNullOrEmpty(equipment.Code))
  234. {
  235. Thread.Sleep(2000);
  236. Device.BeginInvokeOnMainThread(() =>
  237. {
  238. Title = equipment.Code;
  239. ForceLayout();
  240. });
  241. }
  242. });
  243. }
  244. }
  245. }