using Comal.Classes; using InABox.Clients; using InABox.Core; using Syncfusion.SfMaps.XForms; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using Xamarin.CommunityToolkit.Converters; using Xamarin.Forms; using XF.Material.Forms.UI.Dialogs; using PRSSecurity = InABox.Core.Security; namespace comal.timesheets { public class DeliveryGrid : MobileDataGrid { private DeliveryShells _deliveries = new DeliveryShells(); public DeliveryGrid(DataGridSaveType saveType = DataGridSaveType.Single) { if (Security.IsAllowed()) _deliveries.Load(DeliveryShells.AllJobs()); else _deliveries.Load(DeliveryShells.AllJobs()); Load(saveType); OnItemSelected += DeliveryGrid_OnItemSelected; OnImageSelected += DeliveryGrid_OnImageSelected; } private async void Load(DataGridSaveType saveType) { using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading")) { CheckFilter(); Setup(LoadList(), typeof(Delivery), saveType); } } private List LoadList() { try { CoreTable table = DoQuery(); while(table == null) table = DoQuery(); List items = new List(); foreach (var row in table.Rows) items.Add(CreateItem(row)); return items; } catch { return null; } } private DataGridViewModelItem CreateItem(CoreRow row) { var tuples = GenerateDataTuples(row); var address = GenerateAddress(row); return new DataGridViewModelItem( id: row.Get(x => x.ID), data: tuples, image: CheckAddress(address) ? new Image { Source = Device.RuntimePlatform.Equals(Device.iOS) ? "locationmarker" : "locationpointer.png" } : new Image(), imagePopupDetail: address ); } private bool CheckAddress(Dictionary address) { if (address.ContainsKey("Address") || address.ContainsKey("Latitude")) return true; return false; } private List> GenerateDataTuples(CoreRow row) { string extraTitle = !string.IsNullOrWhiteSpace(row.Get(x => x.Contact.Name)) ? " (" + row.Get(x => x.Contact.Name) + ")" : ""; string extraJob = !string.IsNullOrWhiteSpace(row.Get(x => x.Job.Name)) ? " (" + row.Get(x => x.Job.Name) + ")" : ""; return new List> { new Tuple("No.", row.Get(x => x.Number).ToString() + extraTitle), new Tuple("Job", row.Get(x => x.Job.JobNumber) + extraJob), new Tuple("Del", row.Get(x => x.Delivered) == DateTime.MinValue? " " : row.Get(x => x.Delivered).ToString("dd MMM yy")), new Tuple("Bkd", row.Get(x => x.Assignment.Date) == DateTime.MinValue? " " : row.Get(x => x.Assignment.Date).ToString("dd MMM yy")) }; } private Dictionary GenerateAddress(CoreRow row) { var dict = new Dictionary(); if (!string.IsNullOrWhiteSpace(row.Get(x => x.Location.Address))) { dict.Add("Address", row.Get(x => x.Location.Address)); } double lat = row.Get(x => x.Location.Latitude); double longitude = row.Get(x => x.Location.Longitude); if (lat != 0 && longitude != 0) { dict.Add("Latitude", lat.ToString()); dict.Add("Longitude", longitude.ToString()); } if (row.Get(x => x.Delivered) != DateTime.MinValue) { dict.Add("Delivered", row.Get(x => x.Delivered).ToString("hh:mm tt dd MMM yy")); } if (!string.IsNullOrWhiteSpace(row.Get(x => x.DeliveredBy.Name))) { dict.Add("Delivered by", row.Get(x => x.DeliveredBy.Name)); } return dict; } private object DeliveryGrid_OnItemSelected(DataGridViewModelItem item) { DeliveryDetails page = new DeliveryDetails(item.ID); Navigation.PushAsync(page); return null; } private object DeliveryGrid_OnImageSelected(DataGridViewModelItem item) { StackLayout stack = new StackLayout(); if (item.ImagePopupDetail.ContainsKey("Address")) stack.Children.Add(new Label { HorizontalOptions = LayoutOptions.Center, Text = item.ImagePopupDetail["Address"], FontAttributes = FontAttributes.Bold }); if (item.ImagePopupDetail.ContainsKey("Delivered")) stack.Children.Add(new Label { HorizontalOptions = LayoutOptions.Center, Text = "Delivered: " + item.ImagePopupDetail["Delivered"], FontAttributes = FontAttributes.Bold }); if (item.ImagePopupDetail.ContainsKey("Delivered by")) stack.Children.Add(new Label { HorizontalOptions = LayoutOptions.Center, Text = "Delivered by: " + item.ImagePopupDetail["Delivered by"], FontAttributes = FontAttributes.Bold }); if (!item.ImagePopupDetail.ContainsKey("Latitude")) return stack; SfMaps map = new SfMaps(); ImageryLayer layer = new ImageryLayer(); if (Device.RuntimePlatform.Equals(Device.iOS)) { //ImageSource imageSource = "mapmarker.png"; //sfMapLayer.MarkerSettings.MarkerIcon = MapMarkerIcon.Image; //sfMapLayer.MarkerSettings.ImageSource = "location-pin.pdf"; //sfMapLayer.MarkerSettings.IconSize = 35; layer.MarkerSettings.MarkerIcon = MapMarkerIcon.Circle; layer.MarkerSettings.IconColor = Color.DarkBlue; layer.MarkerSettings.IconSize = 15; } else { layer.MarkerSettings.MarkerIcon = MapMarkerIcon.Image; layer.MarkerSettings.ImageSource = "mapmarker.png"; layer.MarkerSettings.IconSize = 35; } layer.MarkerSettings.FontAttributes = FontAttributes.Bold; layer.MarkerSettings.LabelSize = 20; layer.MarkerSettings.LabelColor = Color.DarkBlue; layer.GeoCoordinates = new Point(double.Parse(item.ImagePopupDetail["Latitude"]), double.Parse(item.ImagePopupDetail["Longitude"])); map.ZoomLevel = 15; map.MinZoom = 10; map.MaxZoom = 18; MapMarker marker = new MapMarker(); marker.Latitude = item.ImagePopupDetail["Latitude"]; marker.Longitude = item.ImagePopupDetail["Longitude"]; layer.Markers.Add(marker); map.Layers.Add(layer); map.HorizontalOptions = LayoutOptions.CenterAndExpand; map.VerticalOptions = LayoutOptions.CenterAndExpand; stack.Children.Add(map); return new ScrollView { Content = stack }; } public void RefreshGrid() { try { var list = LoadList(); if (list == null) return; Items.Clear(); Items.AddRange(list); Refresh(Items); } catch { } } } }