LiveMapsTwo.xaml.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Globalization;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. using Comal.Classes;
  8. using InABox.Core;
  9. using Xamarin.Forms;
  10. using Xamarin.Forms.Xaml;
  11. using Syncfusion.SfMaps.XForms;
  12. using InABox.Configuration;
  13. using Xamarin.CommunityToolkit.UI.Views;
  14. using XF.Material.Forms.UI.Dialogs;
  15. namespace comal.timesheets
  16. {
  17. public class LiveMapsTwoSettings : ILocalConfigurationSettings
  18. {
  19. public List<Guid> Selected { get; private set; }
  20. public LiveMapsTwoSettings()
  21. {
  22. Selected = new List<Guid>();
  23. }
  24. }
  25. public class EquipmentExpanderItem : ExpanderViewItem
  26. {
  27. public EquipmentShell Equipment { get; private set; }
  28. public EquipmentExpanderItem(EquipmentShell equipment)
  29. {
  30. Equipment = equipment;
  31. Description = equipment.Description;
  32. }
  33. }
  34. public class JobExpanderItem : ExpanderViewItem
  35. {
  36. public JobShell Job { get; private set; }
  37. public JobExpanderItem(JobShell job)
  38. {
  39. Job = job;
  40. Description = job.Name;
  41. }
  42. }
  43. [XamlCompilation(XamlCompilationOptions.Compile)]
  44. public partial class LiveMapsTwo
  45. {
  46. Job job = new Job();
  47. string[] array;
  48. string country = "Australia";
  49. bool bFirstLoad = true;
  50. double deviceHeight = 0.0F;
  51. Color On = Color.LightGreen;
  52. Color Off = Color.White;
  53. string saved = "SavedMapEquipment";
  54. List<Frame> frames = new List<Frame>();
  55. InABox.Core.Location CurrentJobLocation = new InABox.Core.Location();
  56. bool bButtonsTouched = false;
  57. Dictionary<string, List<EquipmentShell>>
  58. EquipmentMappedToTypes = new Dictionary<string, List<EquipmentShell>>();
  59. List<Expander> expanders = new List<Expander>();
  60. private LiveMapsTwoSettings _settings;
  61. private IEnumerable<EquipmentShell> GetSelectedEquipment() =>
  62. App.Data.Equipment.Items.Where(x => _settings.Selected.Contains(x.ID));
  63. private IEnumerable<JobShell> GetJobs() =>
  64. App.Data.Jobs.Items;
  65. double DeviceWidth = 0.0F;
  66. public LiveMapsTwo()
  67. {
  68. LoadSettings();
  69. var tasks = new Task[]
  70. {
  71. Task.Run(() => App.Data.Jobs.Refresh(false)),
  72. Task.Run(() => App.Data.Equipment.Refresh(false)),
  73. Task.Run(() => App.Data.EquipmentGroups.Refresh(false))
  74. };
  75. InitializeComponent();
  76. Task.WhenAll(tasks).ContinueWith(_ =>
  77. {
  78. Dispatcher.BeginInvokeOnMainThread(() => Refresh());
  79. });
  80. }
  81. protected override void UpdateTransportStatus()
  82. {
  83. base.UpdateTransportStatus();
  84. Expander.IsVisible = App.Data.IsConnected();
  85. }
  86. private void LoadSettings() => _settings = new LocalConfiguration<LiveMapsTwoSettings>().Load();
  87. private void SaveSettings() => new LocalConfiguration<LiveMapsTwoSettings>().Save(_settings);
  88. private void Reset_Clicked(object sender, EventArgs e)
  89. {
  90. Refresh();
  91. }
  92. private void Expander_OnSelectionChanged(object sender, EventArgs args)
  93. {
  94. DisplaySelectedMarkers();
  95. }
  96. private void Refresh()
  97. {
  98. ExpanderViewData data = new ExpanderViewData();
  99. var jobitems = App.Data.Jobs.Items.Where(x => (x.Latitude != 0.0F) && (x.Longitude != 0.0F)).ToArray();
  100. if (jobitems.Any())
  101. {
  102. var header = data.AddGroup(
  103. new ExpanderViewGroup()
  104. {
  105. Image = ImageSource.FromResource("construction.png")
  106. }
  107. );
  108. foreach (var job in jobitems)
  109. header.AddItem(new JobExpanderItem(job) { Selected = true });
  110. }
  111. foreach (var group in App.Data.EquipmentGroups.Items)
  112. {
  113. var groupitems = App.Data.Equipment.Items
  114. .Where(x => (x.GroupID == group.ID) && (x.Latitude != 0.0F) && (x.Longitude != 0.0F)).ToArray();
  115. if (groupitems.Any())
  116. {
  117. var header = data.AddGroup(
  118. new ExpanderViewGroup()
  119. {
  120. Image = group.Thumbnail,
  121. Description = group.Description
  122. }
  123. );
  124. foreach (var item in groupitems)
  125. header.AddItem(new EquipmentExpanderItem(item) { Selected = true });
  126. }
  127. }
  128. Expander.Data = data;
  129. DisplaySelectedMarkers();
  130. }
  131. private void DisplaySelectedMarkers()
  132. {
  133. var jobitems = Expander.Data.SelectedItems.OfType<JobExpanderItem>().Select(x => x.Job).ToArray();
  134. var jobpoints = jobitems.Select(x => new Point(x.Longitude, x.Latitude));
  135. var jobmarkers = jobitems.Select(x =>
  136. new MapMarker()
  137. {
  138. Label = x.JobNumber,
  139. Latitude = x.Latitude.ToString(CultureInfo.CurrentCulture),
  140. Longitude = x.Longitude.ToString(CultureInfo.CurrentCulture)
  141. }
  142. );
  143. JobLayer.Markers = new ObservableCollection<MapMarker>(jobmarkers);
  144. var equipmentitems = Expander.Data.SelectedItems.OfType<EquipmentExpanderItem>().Select(x => x.Equipment)
  145. .ToArray();
  146. var equipmentpoints = equipmentitems.Select(x => new Point(x.Longitude, x.Latitude));
  147. var equipmentmarkers = equipmentitems.Select(x =>
  148. new MapMarker()
  149. {
  150. Label = x.Code,
  151. Latitude = x.Latitude.ToString(CultureInfo.CurrentCulture),
  152. Longitude = x.Longitude.ToString(CultureInfo.CurrentCulture)
  153. }
  154. );
  155. EquipmentLayer.Markers = new ObservableCollection<MapMarker>(equipmentmarkers);
  156. CenterAndZoom(jobpoints.Union(equipmentpoints).ToArray());
  157. }
  158. private void CenterAndZoom(Point[] points)
  159. {
  160. Point coords = new Point();
  161. int zoom = 15;
  162. if (points.Length == 0)
  163. coords = new Point(App.GPS.Latitude, App.GPS.Longitude);
  164. else if (points.Length == 1)
  165. coords = new Point(points[0].Y, points[0].X);
  166. else
  167. {
  168. List<double> latitudes = points.Select(x => x.Y).OrderBy(x => x).ToList();
  169. List<double> longitudes = points.Select(x => x.X).OrderBy(x => x).ToList();
  170. double firstLat = latitudes.First();
  171. double lastLat = latitudes.Last();
  172. double firstLong = longitudes.First();
  173. double lastLong = longitudes.Last();
  174. double resultLat = (firstLat + lastLat) / 2;
  175. double resultLong = (firstLong + lastLong) / 2;
  176. InABox.Core.Location firstLocation = new InABox.Core.Location()
  177. { Latitude = firstLat, Longitude = firstLong };
  178. InABox.Core.Location lastLocation = new InABox.Core.Location()
  179. { Latitude = lastLat, Longitude = lastLong };
  180. double distance = firstLocation.DistanceTo(lastLocation, UnitOfLength.Kilometers);
  181. coords = new Point(resultLat, resultLong);
  182. zoom = CalculateZoom(distance);
  183. }
  184. JobLayer.GeoCoordinates = coords;
  185. EquipmentLayer.GeoCoordinates = coords;
  186. Map.ZoomLevel = zoom;
  187. }
  188. private int CalculateZoom(double distance)
  189. {
  190. Dictionary<double, int> thresholds = new Dictionary<double, int>()
  191. {
  192. { 1, 17 },
  193. { 5, 16 },
  194. { 10, 15 },
  195. { 20, 11 },
  196. { 50, 10 },
  197. { 100, 9 },
  198. { 200, 8 },
  199. { 400, 7 },
  200. };
  201. foreach (var key in thresholds.Keys.OrderBy(x => x))
  202. {
  203. if (distance < key)
  204. return thresholds[key];
  205. }
  206. return 6;
  207. }
  208. }
  209. }