using comal.timesheets.CustomControls; using comal.timesheets.Data_Classes; using Comal.Classes; using InABox.Clients; using InABox.Core; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using Xamarin.Forms; using Xamarin.Forms.Xaml; using XF.Material.Forms.UI.Dialogs; namespace comal.timesheets { [XamlCompilation(XamlCompilationOptions.Compile)] public partial class SetoutsScreen : ContentPage { #region Constructor / navigation Guid JobID = new Guid(); List Setouts = new List(); List packetShells = new List(); List packets = new List(); public SetoutsScreen() { InitializeComponent(); NavigationPage.SetHasBackButton(this, false); } private void ExitBtn_Clicked(object sender, EventArgs e) { Navigation.PopAsync(); } #endregion #region Loading Setouts private async void LoadSetouts() { try { using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading")) { AddSetoutsToList(DoSetoutsQuery()); } } catch { } } private CoreTable DoSetoutsQuery() { CoreTable table = new Client().Query ( new Filter(x => x.JobLink.ID).IsEqualTo(JobID), new Columns( x => x.ID, x => x.Number, x => x.Description ) ); DoPacketsQuery(table); return table; } private void DoPacketsQuery(CoreTable table) { List ids = new List(); foreach (CoreRow row in table.Rows) ids.Add(row.Get(x => x.ID)); CoreTable packetstable = new Client().Query(new Filter(x => x.SetoutLink.ID).InList(ids.ToArray()) , Columns); foreach (CoreRow row in packetstable.Rows) { packets.Add(new MiniManufacturingPacket { ID = row.Get(x => x.ID), OrderID = row.Get(x => x.OrderItem.ID), Serial = row.Get(x => x.Serial), Location = row.Get(x => x.Location), SetoutID = row.Get(x => x.SetoutLink.ID), }); } } private void AddSetoutsToList(CoreTable table) { foreach (CoreRow row in table.Rows) Setouts.Add(CreateSetoutShell(row)); foreach (var shell in Setouts) { var item = CreateSetOutViewItem(shell); setoutsList.Children.Add(item); } titleLbl.Text = "Setouts (" + setoutsList.Children.Count().ToString() + ")"; } private SetoutPacketGrid CreateSetOutViewItem(SetoutShell shell) { return new SetoutPacketGrid(shell); } private SetoutShell CreateSetoutShell(CoreRow row) { SetoutShell shell = AddSetoutDetails(row); shell = AddPacketDetails(shell); return shell; } private SetoutShell AddSetoutDetails(CoreRow row) { SetoutShell shell = new SetoutShell(); shell.ID = row.Get(x => x.ID); shell.Number = row.Get(x => x.Number); shell.Description = row.Get(x => x.Description); return shell; } private SetoutShell AddPacketDetails(SetoutShell shell) { var list = packets.Where(x => x.SetoutID == shell.ID); foreach (var packet in list) { shell.Packets.Add(packet); } return shell; } #endregion #region Button Presses private void JobsBtn_Clicked(object sender, EventArgs e) { JobSelectionPage page = new JobSelectionPage(); page.OnItemSelected += () => { JobID = page.Job.ID; jobBtn.Text = page.Job.JobNumber + " " + page.Job.Name; LoadSetouts(); }; Navigation.PushAsync(page); } #endregion Columns Columns = new Columns( x => x.ID, x => x.OrderItem.ID, x => x.Serial, x => x.Location, x => x.SetoutLink.ID ); #region Searching private void SearchEnt_Changed(object sender, EventArgs e) { if (!string.IsNullOrWhiteSpace(searchEnt.Text)) { int count = 0; foreach (var child in setoutsList.Children) { if (ConditionsMet(child, searchEnt.Text)) { child.IsVisible = true; count++; } else child.IsVisible = false; } titleLbl.Text = "Setouts (" + count.ToString() + ")"; } else { foreach (var c in setoutsList.Children) { c.IsVisible = true; } titleLbl.Text = "Setouts (" + setoutsList.Children.Count().ToString() + ")"; } } private bool ConditionsMet(View child, string text) { var item = child as SetoutPacketGrid; if (SetOutConditionsMatch(item, text) || PacketConditionsMet(item, text)) return true; else return false; } private bool SetOutConditionsMatch(SetoutPacketGrid child, string text) { if (child.Shell.Number.Contains(text) || child.Shell.Number.Contains(text.ToLower()) || child.Shell.Number.Contains(text.ToUpper()) || child.Shell.Number.Contains(SearchUtils.UpperCaseFirst(text)) || child.Shell.Description.Contains(text) || child.Shell.Description.Contains(text.ToLower()) || child.Shell.Description.Contains(text.ToUpper()) || child.Shell.Description.Contains(SearchUtils.UpperCaseFirst(text))) return true; else return false; } private bool PacketConditionsMet(SetoutPacketGrid child, string text) { foreach (var packet in child.Shell.Packets) { if (packet.Serial.Contains(text) || packet.Serial.Contains(text.ToUpper()) || packet.Serial.Contains(text.ToLower()) || packet.Serial.Contains(SearchUtils.UpperCaseFirst(text)) || packet.Location.Contains(text) || packet.Location.Contains(text.ToUpper()) || packet.Location.Contains(text.ToLower()) || packet.Location.Contains(SearchUtils.UpperCaseFirst(text))) return true; } return false; } #endregion } public class SetoutShell { public Guid ID { get; set; } public string Number { get; set; } public string Description { get; set; } public List Packets { get; set; } public SetoutShell() { ID = Guid.Empty; Number = ""; Description = ""; Packets = new List(); } } }