| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 | 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<String> types = new List<String>();        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<EquipmentGroup>();                var table = client.Query(                    new Filter<EquipmentGroup>(x => x.Description).IsNotEqualTo(string.Empty),                    new Columns<EquipmentGroup>(x => x.Description),                    new SortOrder<EquipmentGroup>(x => x.Description)                    );                foreach (CoreRow row in table.Rows)                {                    EquipmentGroup group = row.ToObject<EquipmentGroup>();                    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<Equipment> filter = new Filter<Equipment>(x => x.GroupLink.Description).IsEqualTo(chosenOption).And(x => x.TrackerLink.ID).IsNotEqualTo(Guid.Empty);                    SelectEquipment(filter);                }            }            catch { }        }        void SelectEquipment(Filter<Equipment> filter)        {            lastUpdatedLbl.Text = "Last Updated: ";            sfMapLayer.Markers.Clear();            GenericSelectionPage page = new GenericSelectionPage            (            "Select Equipment",            new SelectionViewModel<Equipment>            (            filter,            new Expression<Func<Equipment, object>>[] { x => x.Description, x => x.SerialNumber },            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 },            new SortOrder<Equipment>(x => x.Description)            ));            page.OnItemSelected += (row) =>            {                equipment = row.ToObject<Equipment>();                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<Job> filter = new Filter<Job>(x => x.JobStatus.Active).IsEqualTo(true).And(x => x.JobStatus.Code).IsEqualTo("01 ACTIVE");            GenericSelectionPage page = new GenericSelectionPage                        (                        "Select Job",                        new SelectionViewModel<Job>                        (                        filter,                        new Expression<Func<Job, object>>[] { x => x.JobNumber, x => x.Name },                        new Expression<Func<Job, object>>[] { x => x.SiteAddress.Street, x => x.SiteAddress.City, x => x.Name, x => x.ID },                        new SortOrder<Job>(x => x.JobNumber)                        ));            page.OnItemSelected += (row) =>            {                job = row.ToObject<Job>();                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();                    });                }            });        }    }}
 |