| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Globalization;
- using System.Linq;
- using System.Threading.Tasks;
- using Comal.Classes;
- using InABox.Core;
- using Xamarin.Forms;
- using Xamarin.Forms.Xaml;
- using Syncfusion.SfMaps.XForms;
- using InABox.Configuration;
- using Xamarin.CommunityToolkit.UI.Views;
- using XF.Material.Forms.UI.Dialogs;
- namespace comal.timesheets
- {
- public class LiveMapsTwoSettings : ILocalConfigurationSettings
- {
- public List<Guid> Selected { get; private set; }
- public LiveMapsTwoSettings()
- {
- Selected = new List<Guid>();
- }
- }
-
- public class EquipmentExpanderItem : ExpanderViewItem
- {
- public EquipmentShell Equipment { get; private set; }
- public EquipmentExpanderItem(EquipmentShell equipment)
- {
- Equipment = equipment;
- Description = equipment.Description;
- }
- }
-
- public class JobExpanderItem : ExpanderViewItem
- {
- public JobShell Job { get; private set; }
- public JobExpanderItem(JobShell job)
- {
- Job = job;
- Description = job.Name;
- }
- }
- [XamlCompilation(XamlCompilationOptions.Compile)]
- public partial class LiveMapsTwo
- {
- Job job = new Job();
- string[] array;
- string country = "Australia";
- bool bFirstLoad = true;
- double deviceHeight = 0.0F;
- Color On = Color.LightGreen;
- Color Off = Color.White;
- string saved = "SavedMapEquipment";
- List<Frame> frames = new List<Frame>();
- InABox.Core.Location CurrentJobLocation = new InABox.Core.Location();
- bool bButtonsTouched = false;
- Dictionary<string, List<EquipmentShell>>
- EquipmentMappedToTypes = new Dictionary<string, List<EquipmentShell>>();
- List<Expander> expanders = new List<Expander>();
- private LiveMapsTwoSettings _settings;
- private IEnumerable<EquipmentShell> GetSelectedEquipment() =>
- App.Data.Equipment.Items.Where(x => _settings.Selected.Contains(x.ID));
- private IEnumerable<JobShell> GetJobs() =>
- App.Data.Jobs.Items;
- double DeviceWidth = 0.0F;
- public LiveMapsTwo()
- {
- LoadSettings();
-
- var tasks = new Task[]
- {
- Task.Run(() => App.Data.Jobs.Refresh(false)),
- Task.Run(() => App.Data.Equipment.Refresh(false)),
- Task.Run(() => App.Data.EquipmentGroups.Refresh(false))
- };
-
- InitializeComponent();
-
- Task.WhenAll(tasks).ContinueWith(_ =>
- {
- Dispatcher.BeginInvokeOnMainThread(() => Refresh());
- });
- }
-
-
- protected override void UpdateTransportStatus()
- {
- base.UpdateTransportStatus();
- Expander.IsVisible = App.Data.IsConnected();
- }
- private void LoadSettings() => _settings = new LocalConfiguration<LiveMapsTwoSettings>().Load();
- private void SaveSettings() => new LocalConfiguration<LiveMapsTwoSettings>().Save(_settings);
-
- private void Reset_Clicked(object sender, EventArgs e)
- {
- Refresh();
- }
-
- private void Expander_OnSelectionChanged(object sender, EventArgs args)
- {
- DisplaySelectedMarkers();
- }
-
- private void Refresh()
- {
- ExpanderViewData data = new ExpanderViewData();
- var jobitems = App.Data.Jobs.Items.Where(x => (x.Latitude != 0.0F) && (x.Longitude != 0.0F)).ToArray();
- if (jobitems.Any())
- {
- var header = data.AddGroup(
- new ExpanderViewGroup()
- {
- Image = ImageSource.FromResource("construction.png")
- }
- );
- foreach (var job in jobitems)
- header.AddItem(new JobExpanderItem(job) { Selected = true });
- }
- foreach (var group in App.Data.EquipmentGroups.Items)
- {
- var groupitems = App.Data.Equipment.Items
- .Where(x => (x.GroupID == group.ID) && (x.Latitude != 0.0F) && (x.Longitude != 0.0F)).ToArray();
- if (groupitems.Any())
- {
- var header = data.AddGroup(
- new ExpanderViewGroup()
- {
- Image = group.Thumbnail,
- Description = group.Description
- }
- );
- foreach (var item in groupitems)
- header.AddItem(new EquipmentExpanderItem(item) { Selected = true });
- }
- }
- Expander.Data = data;
- DisplaySelectedMarkers();
- }
- private void DisplaySelectedMarkers()
- {
- var jobitems = Expander.Data.SelectedItems.OfType<JobExpanderItem>().Select(x => x.Job).ToArray();
- var jobpoints = jobitems.Select(x => new Point(x.Longitude, x.Latitude));
- var jobmarkers = jobitems.Select(x =>
- new MapMarker()
- {
- Label = x.JobNumber,
- Latitude = x.Latitude.ToString(CultureInfo.CurrentCulture),
- Longitude = x.Longitude.ToString(CultureInfo.CurrentCulture)
- }
- );
- JobLayer.Markers = new ObservableCollection<MapMarker>(jobmarkers);
- var equipmentitems = Expander.Data.SelectedItems.OfType<EquipmentExpanderItem>().Select(x => x.Equipment)
- .ToArray();
- var equipmentpoints = equipmentitems.Select(x => new Point(x.Longitude, x.Latitude));
- var equipmentmarkers = equipmentitems.Select(x =>
- new MapMarker()
- {
- Label = x.Code,
- Latitude = x.Latitude.ToString(CultureInfo.CurrentCulture),
- Longitude = x.Longitude.ToString(CultureInfo.CurrentCulture)
- }
- );
- EquipmentLayer.Markers = new ObservableCollection<MapMarker>(equipmentmarkers);
- CenterAndZoom(jobpoints.Union(equipmentpoints).ToArray());
- }
- private void CenterAndZoom(Point[] points)
- {
- Point coords = new Point();
- int zoom = 15;
- if (points.Length == 0)
- coords = new Point(App.GPS.Latitude, App.GPS.Longitude);
- else if (points.Length == 1)
- coords = new Point(points[0].Y, points[0].X);
- else
- {
- List<double> latitudes = points.Select(x => x.Y).OrderBy(x => x).ToList();
- List<double> longitudes = points.Select(x => x.X).OrderBy(x => x).ToList();
- double firstLat = latitudes.First();
- double lastLat = latitudes.Last();
- double firstLong = longitudes.First();
- double lastLong = longitudes.Last();
- double resultLat = (firstLat + lastLat) / 2;
- double resultLong = (firstLong + lastLong) / 2;
- InABox.Core.Location firstLocation = new InABox.Core.Location()
- { Latitude = firstLat, Longitude = firstLong };
- InABox.Core.Location lastLocation = new InABox.Core.Location()
- { Latitude = lastLat, Longitude = lastLong };
- double distance = firstLocation.DistanceTo(lastLocation, UnitOfLength.Kilometers);
- coords = new Point(resultLat, resultLong);
- zoom = CalculateZoom(distance);
- }
- JobLayer.GeoCoordinates = coords;
- EquipmentLayer.GeoCoordinates = coords;
- Map.ZoomLevel = zoom;
- }
- private int CalculateZoom(double distance)
- {
- Dictionary<double, int> thresholds = new Dictionary<double, int>()
- {
- { 1, 17 },
- { 5, 16 },
- { 10, 15 },
- { 20, 11 },
- { 50, 10 },
- { 100, 9 },
- { 200, 8 },
- { 400, 7 },
- };
- foreach (var key in thresholds.Keys.OrderBy(x => x))
- {
- if (distance < key)
- return thresholds[key];
- }
- return 6;
- }
- }
- }
|