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 InABox.Configuration; using Xamarin.Essentials; namespace comal.timesheets { public class DeliveryModuleSettings : ILocalConfigurationSettings { public int CurrentSearch { get; set; } } public partial class DeliveryList { private Tuple>[] _searches = new Tuple>[] { new Tuple>("Today's Deliveries", (shell) => shell.Date == DateTime.Today), new Tuple>("Last 7 Days", (shell) => shell.Delivered > DateTime.Today.AddDays(-7)), new Tuple>("Last 30 Days", (shell) => shell.Delivered > DateTime.Today.AddDays(-30)), new Tuple>("Future Deliveries", (shell) => shell.Delivered.IsEmpty()) }; private String _currentfilter = ""; private DeliveryModuleSettings _settings; public DeliveryList() { _settings = new LocalConfiguration().Load(); InitializeComponent(); RefreshData(false, true); } private void RefreshData(bool force, bool async) { if (async) { App.Data.Deliveries.Refresh(force, Refresh); } else { App.Data.Deliveries.Refresh(true); Refresh(); } } private void Refresh() { App.Data.Deliveries.Search((shell) => _searches[_settings.CurrentSearch].Item2(shell) && FilterShell(shell)); _deliveries.ItemsSource = App.Data.Deliveries.Items; } private bool FilterShell(DeliveryShell shell) { if (String.IsNullOrWhiteSpace(_currentfilter)) return true; return shell.Address.ToUpper().Contains(_currentfilter.ToUpper()) || shell.Job.ToUpper().Contains(_currentfilter.ToUpper()) || shell.Number.ToString().Contains(_currentfilter.ToUpper()); } private void MobileList_OnRefresh(object sender, MobileListRefreshEventArgs args) { RefreshData(true, false); } private async void _filter_OnClicked(object sender, EventArgs e) { var view = new MaterialRadioButtonGroup() { Choices = _searches.Select(x => x.Item1).ToList(), SelectedIndex = _settings.CurrentSearch }; bool? wasConfirmed = await MaterialDialog.Instance.ShowCustomContentAsync(view, "Select deliveries to display", "Filter List"); if (wasConfirmed == true) { _settings.CurrentSearch = view.SelectedIndex; new LocalConfiguration().Save(_settings); Refresh(); } } private void _search_OnTextChanged(object sender, MobileSearchBarTextChangedArgs args) { _currentfilter = args.Text; Refresh(); } private async void Deliveries_OnItemTapped(object sender, MobileListItemTappedEventArgs args) { await MaterialDialog.Instance.AlertAsync("Not Implemented Yet!"); return; // if (sender is DeliveryShell shell) // { // DeliveryDetails page = new DeliveryDetails() { DeliveryID = shell.ID }; // Navigation.PushAsync(page); // } } private async void AddDelivery_Clicked(object sender, EventArgs e) { await MaterialDialog.Instance.AlertAsync("Not Implemented Yet!"); // Delivery delivery = new Delivery(); // var details = new DeliveryDetails(delivery.ID); // Navigation.PushAsync(details); } /* 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().Query( new Filter(x => x.Completed).IsEqualTo(DateTime.MinValue).Or(x => x.Completed).IsGreaterThanOrEqualTo(DateTime.Today.AddDays(-7)), new Columns( 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(x => x.Completed, SortDirection.Descending) ); string emptyString = ""; foreach (var row in table.Rows) { List 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 excludeUnbooked = new List(); 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(); } */ } }