123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- using Com.Airbnb.Lottie.Model.Layer;
- 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.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
- {
- Filter<Delivery> filter = new Filter<Delivery>(x => x.Created).IsGreaterThanOrEqualTo(DateTime.Now.AddDays(-120));
- public DeliveryGrid(DataGridSaveType saveType = DataGridSaveType.Single)
- {
- 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<DataGridViewModelItem> LoadList()
- {
- try
- {
- CoreTable table = DoQuery();
- if (!table.Rows.Any())
- return null;
- List<DataGridViewModelItem> items = new List<DataGridViewModelItem>();
- 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<Delivery, Guid>(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<string, string> address)
- {
- if (address.ContainsKey("Address") || address.ContainsKey("Latitude"))
- return true;
- return false;
- }
- private List<Tuple<string, string>> GenerateDataTuples(CoreRow row)
- {
- string extraTitle = !string.IsNullOrWhiteSpace(row.Get<Delivery, string>(x => x.Contact.Name)) ? " (" + row.Get<Delivery, string>(x => x.Contact.Name) + ")" :
- "";
- string extraJob = !string.IsNullOrWhiteSpace(row.Get<Delivery, string>(x => x.Job.Name)) ? " (" + row.Get<Delivery, string>(x => x.Job.Name) + ")" :
- "";
- return new List<Tuple<string, string>>
- {
- new Tuple<string, string>("No.", row.Get<Delivery, int>(x => x.Number).ToString() + extraTitle),
- new Tuple<string, string>("Job", row.Get<Delivery, string>(x => x.Job.JobNumber) + extraJob),
- new Tuple<string, string>("Del", row.Get<Delivery, DateTime>(x => x.Delivered) == DateTime.MinValue? " " : row.Get<Delivery, DateTime>(x => x.Delivered).ToString("dd MMM yy")),
- new Tuple<string, string>("Bkd", row.Get<Delivery, DateTime>(x => x.Assignment.Date) == DateTime.MinValue? " " : row.Get<Delivery, DateTime>(x => x.Assignment.Date).ToString("dd MMM yy"))
- };
- }
- private CoreTable DoQuery()
- {
- try
- {
- return new Client<Delivery>().Query(
- filter,
- new Columns<Delivery>(
- x => x.ID,
- x => x.Job.JobNumber,
- x => x.Job.ID,
- x => x.Job.Name,
- x => x.Number,
- x => x.Date,
- x => x.Contact.Name,
- x => x.Location.Latitude,
- x => x.Location.Longitude,
- x => x.Assignment.Date,
- x => x.DeliveredBy.Name,
- x => x.Delivered,
- x => x.Due,
- x => x.Location.Address,
- x => x.Created
- ),
- new SortOrder<Delivery>(x => x.Created, SortDirection.Descending)
- );
- }
- catch (Exception e)
- {
- return null;
- }
- }
- private Filter<Delivery> CheckFilter()
- {
- if (GlobalVariables.IsJobOnlyEmployee)
- {
- filter = new Filter<Delivery>(x => x.Job.ID).IsEqualTo(GlobalVariables.EmployeeJobs.First().ID);
- foreach (var job in GlobalVariables.EmployeeJobs)
- filter = filter.Or(x => x.Job.ID).IsEqualTo(job.ID);
- }
- return filter;
- }
- private Dictionary<string, string> GenerateAddress(CoreRow row)
- {
- var dict = new Dictionary<string, string>();
- if (!string.IsNullOrWhiteSpace(row.Get<Delivery, string>(x => x.Location.Address)))
- {
- dict.Add("Address", row.Get<Delivery, string>(x => x.Location.Address));
- }
- double lat = row.Get<Delivery, double>(x => x.Location.Latitude);
- double longitude = row.Get<Delivery, double>(x => x.Location.Longitude);
- if (lat != 0 && longitude != 0)
- {
- dict.Add("Latitude", lat.ToString());
- dict.Add("Longitude", longitude.ToString());
- }
- if (row.Get<Delivery, DateTime>(x => x.Delivered) != DateTime.MinValue)
- {
- dict.Add("Delivered", row.Get<Delivery, DateTime>(x => x.Delivered).ToString("hh:mm tt dd MMM yy"));
- }
- if (!string.IsNullOrWhiteSpace(row.Get<Delivery, string>(x => x.DeliveredBy.Name)))
- {
- dict.Add("Delivered by", row.Get<Delivery, string>(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 { }
- }
- }
- }
|