using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Comal.Classes; using InABox.Clients; using InABox.Configuration; using InABox.Core; using Plugin.Media; using Xamarin.Forms; using Xamarin.Forms.Xaml; using Xamarin.Forms; using Xamarin.Forms.Xaml; namespace comal.timesheets { [XamlCompilation(XamlCompilationOptions.Compile)] public partial class PurchaseOrderModule : ContentPage { List purchaseOrderShells = new List(); bool bLoading = true; List filterOptions = new List(); bool firstLoad = true; public PurchaseOrderModule() { InitializeComponent(); LoadList(); } private async void LoadList() { await Task.Run(() => { bLoading = true; purchaseOrderShells.Clear(); filterOptions.Clear(); filterOptions.Add("All"); CoreTable table = DoQuery(); if (table.Rows.Any()) LoadAndDisplayFromTable(table); }); } private void LoadAndDisplayFromTable(CoreTable table) { foreach (CoreRow row in table.Rows) { purchaseOrderShells.Add(GenerateShellFromRow(row)); } firstLoad = false; DisplayList(); } private void DisplayList() { Device.BeginInvokeOnMainThread(() => { searchEnt.Text = ""; filterOptionsControl.Options = filterOptions; filterOptionsControl.CreateRadioButtonsAndSetDefault(filterOptions.First()); purchaseOrderListView.ItemsSource = purchaseOrderShells; filterOptionsControl.OnFilterOptionChanged += FilterOptionsControl_OnFilterOptionChanged; bLoading = false; }); } private PurchaseOrderShell GenerateShellFromRow(CoreRow row) { var list = CheckListForNulls(row); var purchaseOrderShell = CreateShell(list); AddFilterOptions(purchaseOrderShell); return purchaseOrderShell; } private void AddFilterOptions(PurchaseOrderShell purchaseOrderShell) { if (!string.IsNullOrWhiteSpace(purchaseOrderShell.SupplierName)) { if (!filterOptions.Contains(purchaseOrderShell.SupplierName)) { filterOptions.Add(purchaseOrderShell.SupplierName); } } } private PurchaseOrderShell CreateShell(List list) { PurchaseOrderShell purchaseOrderShell = new PurchaseOrderShell() { ID = Guid.Parse(list[0].ToString()), PONumber = list[1].ToString(), DueDate = "Due: " + DateTime.Parse(list[2].ToString()).ToString("dd-MMMM-yy"), Status = list[3].ToString(), SupplierID = Guid.Parse(list[4].ToString()), SupplierName = list[5].ToString(), Notes = list[6].ToString() }; return purchaseOrderShell; } private List CheckListForNulls(CoreRow row) { List list = row.Values; if (list[0] == null) { list[0] = Guid.Empty; } //0 if (list[1] == null) { list[1] = ""; } //1 if (list[2] == null) { list[2] = DateTime.MinValue; } //2 if (list[3] == null) { list[3] = ""; } //3 if (list[4] == null) { list[4] = Guid.Empty; } //4 if (list[5] == null) { list[5] = ""; } //5 if (list[6] == null) { list[6] = ""; } //6 return list; } private CoreTable DoQuery() { return new Client().Query ( new Filter(x => x.ClosedDate).IsEqualTo(DateTime.MinValue), new Columns( x => x.ID, //0 x => x.PONumber, //1 x => x.DueDate, //2 x => x.Status, //3 x => x.SupplierLink.ID, //4 x => x.SupplierName, //5 x => x.Notes //6 ), new SortOrder(x => x.DueDate, SortDirection.Descending) ); } private void FilterOptionsControl_OnFilterOptionChanged(string filterOption) { if (filterOption == filterOptionsControl.CurrentOption) return; bLoading = true; searchEnt.Text = ""; bLoading = false; filterOptionsControl.CurrentOption = filterOption; if (filterOption == "All") { purchaseOrderListView.ItemsSource = purchaseOrderShells; listCountLbl.Text = "Number of items in list: " + purchaseOrderShells.Count; } else { var list = purchaseOrderShells.Where(x => x.SupplierName.Equals(filterOption)); purchaseOrderListView.ItemsSource = list; listCountLbl.Text = "Number of items in list: " + list.Count(); } } private async void PurchaseOrder_Clicked(object sender, EventArgs e) { PurchaseOrderShell purchaseOrderShell = purchaseOrderListView.SelectedItem as PurchaseOrderShell; PurchaseOrderDetails details = new PurchaseOrderDetails(purchaseOrderShell); Navigation.PushAsync(details); } private void SearchEnt_Changed(object sender, EventArgs e) { if (bLoading) return; if (!string.IsNullOrWhiteSpace(searchEnt.Text)) { RunSearch(); } else { listCountLbl.Text = "Number of items in list: " + purchaseOrderShells.Count; purchaseOrderListView.ItemsSource = purchaseOrderShells; } } private async void RunSearch() { await Task.Run(() => { List searchList = new List(); if (filterOptionsControl.CurrentOption == "All") { searchList = purchaseOrderShells; } else { var newList = purchaseOrderShells.Where(x => x.SupplierName.Equals(filterOptionsControl.CurrentOption)); foreach (var shell in newList) { searchList.Add(shell); } } var list = searchList.Where(x => x.PONumber.Contains(searchEnt.Text) || x.PONumber.Contains(UpperCaseFirst(searchEnt.Text)) || x.PONumber.Contains(searchEnt.Text.ToLower()) || x.PONumber.Contains(searchEnt.Text.ToUpper()) || x.Notes.Contains(searchEnt.Text) || x.Notes.Contains(UpperCaseFirst(searchEnt.Text)) || x.Notes.Contains(searchEnt.Text.ToLower()) || x.Notes.Contains(searchEnt.Text.ToUpper()) || x.SupplierName.Contains(searchEnt.Text) || x.SupplierName.Contains(UpperCaseFirst(searchEnt.Text)) || x.SupplierName.Contains(searchEnt.Text.ToLower()) || x.SupplierName.Contains(searchEnt.Text.ToUpper()) || x.Status.Contains(searchEnt.Text) || x.Status.Contains(UpperCaseFirst(searchEnt.Text)) || x.Status.Contains(searchEnt.Text.ToLower()) || x.Status.Contains(searchEnt.Text.ToUpper()) || x.DueDate.Contains(searchEnt.Text) || x.DueDate.Contains(UpperCaseFirst(searchEnt.Text)) || x.DueDate.Contains(searchEnt.Text.ToLower()) || x.DueDate.Contains(searchEnt.Text.ToUpper()) ); Device.BeginInvokeOnMainThread(() => { listCountLbl.Text = "Number of items in list: " + list.Count(); purchaseOrderListView.ItemsSource = list; }); }); } static String UpperCaseFirst(string s) { char[] a = s.ToCharArray(); a[0] = char.ToUpper(a[0]); return new string(a); } } }