using comal.timesheets.Products; using Comal.Classes; using InABox.Clients; using InABox.Core; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Xamarin.Forms; using XF.Material.Forms.UI.Dialogs; namespace comal.timesheets { public delegate void ProductsGridItemSelected(DataGridViewModelItem item); public delegate Task ProductsGridShowOptions(); public class ProductsGrid : MobileDataGrid { public event ProductsGridItemSelected ItemSelected; public event ProductsGridShowOptions ShowOptions; public ProductsGrid(DataGridSaveType savetype = DataGridSaveType.None) { OnItemSelected += ProductsGrid_OnItemSelected; LoadItems(savetype); } private void ProductsGrid_OnItemSelected(DataGridViewModelItem item) { ItemSelected?.Invoke(item); DisplayOptions(item); } private void LoadItems(DataGridSaveType savetype = DataGridSaveType.None) { Task.Run(async () => { if (GlobalVariables.ProductsLoaded) { List shells = new List(); foreach (var product in GlobalVariables.ProductShells) { List> tuples = new List>(); tuples.Add(new Tuple("Code", product.Code)); tuples.Add(new Tuple("Name", product.Name)); tuples.Add(new Tuple("Family", product.Group)); shells.Add(new DataGridViewModelItem ( id: product.ID, data: tuples, image: product.ImageID == Guid.Empty ? new Image() : new Image { Source = "productimage.png", HeightRequest = 30, WidthRequest = 30 }, imageid: product.ImageID )); } Setup(shells, typeof(Product), savetype); } else { using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading")) { CoreTable table = new Client().Query(null, new Columns(x => x.ID, x => x.Code, x => x.Name, x => x.Group.Description, x => x.Image.ID )); if (!table.Rows.Any()) return; List shells = new List(); foreach (var row in table.Rows) { List> tuples = new List>(); tuples.Add(new Tuple("Code", row.Get(x => x.Code))); tuples.Add(new Tuple("Name", row.Get(x => x.Name))); tuples.Add(new Tuple("Family", row.Get(x => x.Group.Description))); shells.Add(new DataGridViewModelItem ( id: row.Get(x => x.ID), data: tuples, image: row.Get(x => x.Image.ID) == Guid.Empty ? new Image() : new Image { Source = "productimage.png", HeightRequest = 30, WidthRequest = 30 }, imageid: row.Get(x => x.Image.ID) )); } Setup(shells, typeof(Product), savetype); } } }); } public async void DisplayOptions(DataGridViewModelItem item) { ProductShell product = GlobalVariables.ProductShells.FirstOrDefault(x => x.ID == item.ID); ListSelectionPage page = new ListSelectionPage(new List { "View Holdings", "View Movements" }); page.OnSimpleListTapped += ((chosenOption) => { Device.BeginInvokeOnMainThread(() => { switch (chosenOption) { case "Cancel": return; case "View Holdings": HoldingViewer holdingViewer = new HoldingViewer(product.ID, product.Name, product.ImageID); holdingViewer.OnExitSelected += () => { Navigation.PopAsync(); }; Navigation.PushAsync(holdingViewer); break; case "View Movements": MovementViewer movementViewer = new MovementViewer(product.ID, product.Name); Navigation.PushAsync(movementViewer); break; default: break; } }); }); Navigation.PushAsync(page); } } }