| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- using System;
- using System.Collections.Generic;
- using System.Threading.Tasks;
- using Xamarin.Forms;
- using InABox.Core;
- using InABox.Clients;
- using Comal.Classes;
- using XF.Material.Forms.UI;
- using XF.Material.Forms.UI.Dialogs;
- using System.Linq;
- using comal.timesheets.Deliveries;
- using System.Threading;
- using System.IO;
- using System.Linq.Expressions;
- using Xamarin.Essentials;
- namespace comal.timesheets
- {
- public partial class DeliveryList : ContentPage
- {
- List<DeliveryShell> deliveries = new List<DeliveryShell>();
- DateTime Today = DateTime.Today.Date;
- bool includeUnbooked = false;
- bool firstLoad = true;
- public DeliveryList()
- {
- includeUnbooked = false;
- InitializeComponent();
- Load();
- }
- private async void Load()
- {
- Title = "Refreshing";
- await Task.Run(() =>
- {
- LoadData();
- Device.BeginInvokeOnMainThread(async () =>
- {
- AdjustOpenList();
- multiButton.SetFirstButton();
- multiButton.ButtonChangedEvent += ButtonChanged;
- Title = "Deliveries";
- if (firstLoad)
- {
- await Task.Run(() => { Thread.Sleep(1500); firstLoad = false; });
- }
- });
- });
- }
- protected override async void OnAppearing()
- {
- if (firstLoad)
- return;
- Title = "Refreshing";
- await Task.Run(() =>
- {
- LoadData();
- Device.BeginInvokeOnMainThread(() =>
- {
- ListSelectFromButton();
- Title = "Deliveries";
- });
- });
- base.OnAppearing();
- }
- private void LoadData()
- {
- deliveries.Clear();
- CoreTable table = new Client<Delivery>().Query(
- new Filter<Delivery>(x => x.Completed).IsEqualTo(DateTime.MinValue).Or(x => x.Completed).IsGreaterThanOrEqualTo(DateTime.Today.AddDays(-7)),
- new Columns<Delivery>(
- x => x.ID, //0
- x => x.Number, //1
- x => x.Job.Name, //2
- x => x.Assignment.Date, //3
- x => x.DeliveredBy.Name, //4
- x => x.Completed, //5
- x => x.Documents, //6
- x => x.Location.Latitude, //7
- x => x.Location.Longitude, //8
- x => x.Due, //9
- x => x.Delivered, //10
- x => x.Contact.Name, //11
- x => x.Location.Address //12
- ),
- new SortOrder<Delivery>(x => x.Completed, SortDirection.Descending)
- );
- string emptyString = "";
- foreach (var row in table.Rows)
- {
- List<object> list = row.Values;
- if (list[0] == null) { list[0] = Guid.Empty; }
- if (list[1] == null) { list[1] = 0; }
- if (list[2] == null) { list[2] = emptyString; }
- if (list[3] == null) { list[3] = DateTime.MinValue; }
- if (list[4] == null) { list[4] = emptyString; }
- if (list[5] == null) { list[5] = DateTime.MinValue; }
- if (list[6] == null) { list[6] = 0; }
- if (list[7] == null) { list[7] = 0; }
- if (list[8] == null) { list[8] = 0; }
- if (list[9] == null) { list[9] = DateTime.MinValue; }
- if (list[10] == null) { list[10] = DateTime.MinValue; }
- if (list[11] == null) { list[11] = emptyString; }
- if (list[12] == null) { list[12] = emptyString; }
- DeliveryShell deliveryShell = new DeliveryShell();
- deliveryShell.ID = Guid.Parse(list[0].ToString());
- deliveryShell.Number = int.Parse(list[1].ToString());
- deliveryShell.JobName = list[2].ToString();
- if (list[3].Equals(DateTime.MinValue))
- {
- deliveryShell.AssignmentDate = "";
- }
- else
- {
- deliveryShell.AssignmentDate = DateTime.Parse(list[3].ToString()).ToString("dd MMM yy");
- }
- deliveryShell.EmployeeName = list[4].ToString();
- deliveryShell.Completed = DateTime.Parse(list[5].ToString());
- deliveryShell.Documents = int.Parse(list[6].ToString());
- deliveryShell.Due = DateTime.Parse(list[9].ToString());
- deliveryShell.Delivered = DateTime.Parse(list[10].ToString());
- if (deliveryShell.Documents != 0)
- {
- if (Device.RuntimePlatform.Equals(Device.iOS))
- {
- deliveryShell.ClipImagePath = "attachments";
- }
- else deliveryShell.ClipImagePath = "paperclip.png";
- }
- if (double.Parse(list[7].ToString()) != 0 && double.Parse(list[8].ToString()) != 0)
- {
- if (Device.RuntimePlatform.Equals(Device.iOS))
- {
- deliveryShell.LocationImagePath = "locationmarker";
- }
- else deliveryShell.LocationImagePath = "locationpointer.png";
- }
- deliveryShell.Latitude = double.Parse(list[7].ToString());
- deliveryShell.Longitude = double.Parse(list[8].ToString());
- deliveryShell.Contact = list[11].ToString();
- if (deliveryShell.Delivered != DateTime.MinValue)
- deliveryShell.DisplayDelivered = deliveryShell.Delivered.ToString("dd MMM yy");
- deliveryShell.DeliveredAddress = list[12].ToString();
- deliveries.Add(deliveryShell);
- }
- }
- private async void Delivery_Clicked(object sender, EventArgs e)
- {
- var delivery = deliveriesListView.SelectedItem as DeliveryShell;
- string loadingMessage = "Loading Delivery " + delivery.Number;
- if (delivery.Documents != 0)
- {
- loadingMessage = loadingMessage + " with " + delivery.Documents + " attached document(s). Please wait for photos to appear.";
- }
- using (await MaterialDialog.Instance.LoadingDialogAsync(message: loadingMessage))
- {
- var details = new DeliveryDetails(delivery.ID);
- Navigation.PushAsync(details);
- }
- }
- private void AddDelivery_Clicked(object sender, EventArgs e)
- {
- Delivery delivery = new Delivery();
- var details = new DeliveryDetails(delivery.ID);
- Navigation.PushAsync(details);
- }
- private void ButtonChanged(object sender, EventArgs e)
- {
- ListSelectFromButton();
- }
- private void ListSelectFromButton()
- {
- if (multiButton.ButtonSelected != null)
- {
- if (multiButton.ButtonSelected.Equals(multiButton.Button1Text))
- {
- AdjustOpenList();
- includUnbookedBtn.IsEnabled = true;
- }
- if (multiButton.ButtonSelected.Equals(multiButton.Button2Text))
- {
- deliveriesListView.ItemsSource = deliveries.Where(x => x.Delivered.Date.Equals(Today));
- includUnbookedBtn.IsEnabled = false;
- }
- if (multiButton.ButtonSelected.Equals(multiButton.Button3Text))
- {
- deliveriesListView.ItemsSource = deliveries.Where(x => x.Delivered >= (DateTime.Today.AddDays(-7)) || x.Completed >= (DateTime.Today.AddDays(-7)));
- includUnbookedBtn.IsEnabled = false;
- }
- }
- }
- private void Image_Tapped(object sender, EventArgs e)
- {
- var shell = ((TappedEventArgs)e).Parameter as DeliveryShell;
- if (shell == null)
- return;
- if (!string.IsNullOrWhiteSpace(shell.DeliveredAddress))
- {
- popupLayout.PopupView.ShowHeader = false;
- popupLayout.PopupView.ShowFooter = false;
- popupLayout.PopupView.ContentTemplate = new DataTemplate(() =>
- {
- Label lbl = new Label();
- lbl.Margin = new Thickness(10);
- lbl.FontSize = Device.GetNamedSize(NamedSize.Medium, lbl);
- lbl.FontAttributes = FontAttributes.Bold;
- lbl.HorizontalOptions = LayoutOptions.Center;
- lbl.VerticalOptions = LayoutOptions.Center;
- lbl.HorizontalTextAlignment = TextAlignment.Center;
- lbl.LineBreakMode = LineBreakMode.WordWrap;
- lbl.Text = shell.DeliveredAddress + " at " + shell.Delivered.ToString("hh:mm:ss tt dd MMM yy") + System.Environment.NewLine
- + "by: " + shell.EmployeeName;
- Button btn = new Button
- {
- BackgroundColor = Color.FromHex("#15C7C1"),
- Text = "View in Maps",
- FontAttributes = FontAttributes.Bold,
- TextColor = Color.White,
- CornerRadius = 10,
- HorizontalOptions = LayoutOptions.Center,
- };
- btn.FontSize = Device.GetNamedSize(NamedSize.Medium, btn);
- btn.Clicked += async (object sender, EventArgs e) =>
- {
- var location = new Xamarin.Essentials.Location(shell.Latitude, shell.Longitude);
- var options = new MapLaunchOptions { };
- await Map.OpenAsync(location, options);
- };
- Image img = new Image
- {
- HorizontalOptions = LayoutOptions.Center,
- HeightRequest = 40,
- WidthRequest = 40
- };
- if (Device.RuntimePlatform.Equals(Device.iOS))
- {
- img.Source = "locationmarker.png";
- }
- else img.Source = "locationpointer.png";
- img.Margin = new Thickness(10, 30, 10, 10);
- StackLayout stack = new StackLayout();
- stack.Margin = new Thickness(20, 50, 20, 50);
- stack.Children.Add(img);
- stack.Children.Add(lbl);
- stack.Children.Add(btn);
- return stack;
- });
- popupLayout.Show();
- }
- }
- private void AdjustOpenList()
- {
- if (includeUnbooked)
- {
- deliveriesListView.ItemsSource = deliveries.Where(x => x.Delivered.Equals(DateTime.MinValue));
- }
- else if (!includeUnbooked)
- {
- var list = deliveries.Where(x => x.Delivered.Equals(DateTime.MinValue));
- List<DeliveryShell> excludeUnbooked = new List<DeliveryShell>();
- foreach (DeliveryShell shell in list)
- {
- if (!string.IsNullOrWhiteSpace(shell.AssignmentDate))
- {
- excludeUnbooked.Add(shell);
- }
- }
- deliveriesListView.ItemsSource = excludeUnbooked;
- }
- }
- private void IncludeUnbookedBtn_Clicked(object sender, EventArgs e)
- {
- if (includeUnbooked)
- {
- includeUnbooked = false;
- includUnbookedBtn.BackgroundColor = Color.FromHex("#15C7C1");
- includUnbookedBtn.Text = "Include Unbooked (Off)";
- }
- else if (!includeUnbooked)
- {
- includeUnbooked = true;
- includUnbookedBtn.BackgroundColor = Color.Gray;
- includUnbookedBtn.Text = "Include Unbooked (On)";
- }
- AdjustOpenList();
- }
- }
- }
|