using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Linq; using System.Linq.Expressions; using System.Threading.Tasks; using comal.timesheets.Tasks; using Comal.Classes; using InABox.Clients; using InABox.Configuration; using InABox.Core; using Xamarin.Forms; using Xamarin.Forms.Xaml; using XF.Material.Forms.UI; using XF.Material.Forms.UI.Dialogs; using Syncfusion.SfMaps.XForms; using System.Threading; using System.IO; using Xamarin.Essentials; namespace comal.timesheets.LiveMaps { [XamlCompilation(XamlCompilationOptions.Compile)] public partial class LiveMapPage : ContentPage { Equipment equipment = new Equipment(); Job job = new Job(); List types = new List(); string[] array; string country = "Australia"; public LiveMapPage() { InitializeComponent(); job = new Job(); equipment = new Equipment(); sfMapLayer.GeoCoordinates = new Point(-32.0410801, 115.9957434); sfMap.ZoomLevel = 10; LoadData(); } protected override void OnAppearing() { base.OnAppearing(); } private async Task LoadData() { Title = "Live Maps - Loading"; await Task.Run(() => { var client = new Client(); var table = client.Query( new Filter(x => x.Description).IsNotEqualTo(string.Empty), new Columns(x => x.Description), new SortOrder(x => x.Description) ); foreach (CoreRow row in table.Rows) { EquipmentGroup group = row.ToObject(); types.Add(group.Description); } array = types.ToArray(); Device.BeginInvokeOnMainThread(() => { Title = "Live Maps"; selectBtn.IsEnabled = true; }); }); } private async void SelectBtn_Clicked(object sender, EventArgs e) { try { string chosenOption = await DisplayActionSheet("Select", "Cancel", null, "Equipment", "Job Locations"); if (!chosenOption.Equals("Cancel")) { if (chosenOption == "Equipment") { SelectEquipmentList(); } if (chosenOption == "Job Locations") { SelectJob(); } } } catch { return; } } private async void SelectEquipmentList() { try { string chosenOption = await DisplayActionSheet("Choose Equipment Type", "Cancel", null, array); if (!chosenOption.Equals("Cancel")) { Filter filter = new Filter(x => x.GroupLink.Description).IsEqualTo(chosenOption).And(x => x.TrackerLink.ID).IsNotEqualTo(Guid.Empty); SelectEquipment(filter); } } catch { } } void SelectEquipment(Filter filter) { lastUpdatedLbl.Text = "Last Updated: "; sfMapLayer.Markers.Clear(); GenericSelectionPage page = new GenericSelectionPage ( "Select Equipment", new SelectionViewModel ( filter, new Expression>[] { x => x.Description, x => x.SerialNumber }, new Expression>[] { x => x.TrackerLink.Location.Latitude, x => x.TrackerLink.Location.Longitude, x => x.TrackerLink.Location.Timestamp, x => x.Code, x => x.ID }, new SortOrder(x => x.Description) )); page.OnItemSelected += (row) => { equipment = row.ToObject(); sfMap.ZoomLevel = 15; sfMapLayer.GeoCoordinates = new Point(equipment.TrackerLink.Location.Latitude, equipment.TrackerLink.Location.Longitude); MapMarker marker1 = new MapMarker(); marker1.Label = equipment.Description; marker1.Latitude = equipment.TrackerLink.Location.Latitude.ToString(); marker1.Longitude = equipment.TrackerLink.Location.Longitude.ToString(); sfMapLayer.Markers.Add(marker1); sfMapLayer.MarkerSettings.IconSize = 15; sfMapLayer.MarkerSettings.FontAttributes = FontAttributes.Bold; sfMapLayer.MarkerSettings.LabelSize = 20; sfMapLayer.MarkerSettings.LabelColor = Color.DarkRed; sfMapLayer.MarkerSettings.IconColor = Color.Red; if (equipment.TrackerLink.Location.Timestamp != DateTime.MinValue) { Device.BeginInvokeOnMainThread(() => { lastUpdatedLbl.Text = "Last updated: " + equipment.TrackerLink.Location.Timestamp.ToString("hh-mm-tt dd-MMM-yy"); lastUpdatedLbl.TextColor = Color.HotPink; }); } else { Device.BeginInvokeOnMainThread(() => { Title = "Live Maps"; lastUpdatedLbl.Text = "Last updated: NA"; lastUpdatedLbl.TextColor = Color.Default; }); } job = null; ChangeTitle(); }; Navigation.PushAsync(page); } private async void SelectJob() { Filter filter = new Filter(x => x.JobStatus.Active).IsEqualTo(true).And(x => x.JobStatus.Code).IsEqualTo("01 ACTIVE"); GenericSelectionPage page = new GenericSelectionPage ( "Select Job", new SelectionViewModel ( filter, new Expression>[] { x => x.JobNumber, x => x.Name }, new Expression>[] { x => x.SiteAddress.Street, x => x.SiteAddress.City, x => x.Name, x => x.ID }, new SortOrder(x => x.JobNumber) )); page.OnItemSelected += (row) => { job = row.ToObject(); ShowJobLocation(job.SiteAddress.Street, job.SiteAddress.City, job.Name); }; Navigation.PushAsync(page); } private async void ShowJobLocation(string street, string city, string name) { try { var location = (await Geocoding.GetLocationsAsync($"{street}, {city}, {country}")).FirstOrDefault(); if (location == null) return; sfMap.ZoomLevel = 15; sfMapLayer.GeoCoordinates = new Point(location.Latitude, location.Longitude); MapMarker marker1 = new MapMarker(); marker1.Label = name; marker1.Latitude = location.Latitude.ToString(); marker1.Longitude = location.Longitude.ToString(); sfMapLayer.Markers.Add(marker1); sfMapLayer.MarkerSettings.IconSize = 15; sfMapLayer.MarkerSettings.FontAttributes = FontAttributes.Bold; sfMapLayer.MarkerSettings.LabelSize = 20; sfMapLayer.MarkerSettings.LabelColor = Color.DarkRed; sfMapLayer.MarkerSettings.IconColor = Color.Red; if (!string.IsNullOrWhiteSpace(name)) { Title = name; } equipment = null; } catch (Exception e) { await DisplayAlert("Error Loading Address.", street + " " + city + " Not found.", "OK"); return; } } private async void OpenInMapsBtn_Clicked(object sender, EventArgs e) { try { if (job.ID != Guid.Empty) { var location = (await Geocoding.GetLocationsAsync($"{job.SiteAddress.Street}, {job.SiteAddress.City}, {country}")).FirstOrDefault(); if (location == null) return; var options = new MapLaunchOptions { Name = job.Name }; await Map.OpenAsync(location, options); } } catch { } try { if (equipment.ID != Guid.Empty) { var location = new Xamarin.Essentials.Location(equipment.TrackerLink.Location.Latitude, equipment.TrackerLink.Location.Longitude); var options = new MapLaunchOptions { }; await Map.OpenAsync(location, options); } } catch { } } private async void ChangeTitle() { await Task.Run(() => { if (!string.IsNullOrEmpty(equipment.Code)) { Thread.Sleep(2000); Device.BeginInvokeOnMainThread(() => { Title = equipment.Code; ForceLayout(); }); } }); } } }