| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using InABox.Core;
- using InABox.Mobile;
- using Xamarin.Forms;
- using Xamarin.Forms.Xaml;
- using XF.Material.Forms.UI.Dialogs;
- namespace PRS.Mobile
- {
- public class ReqisitionItemEditorProductConverter : AbstractConverter<ProductShell?, String>
- {
- protected override string Convert(ProductShell? value, object? parameter = null)
- {
- return (value?.ID ?? Guid.Empty) != Guid.Empty
- ? $"{value?.Code}: {value?.Name}"
- : "Select Product";
- }
- }
-
- public class RequisitionItemEditorViewModel : BindableObject
- {
- public ProductShell? Product { get; set; }
- public bool HasProduct => Product != null;
-
- public bool HasProductImage => (Product?.ImageID ?? Guid.Empty) != Guid.Empty;
- public String? Description { get; set; }
-
- public Guid ImageID { get; set; }
- public byte[]? Image { get; set; }
-
- public double Quantity { get; set; }
- public RequisitionItemEditorViewModel()
- {
- Quantity = 1.0F;
- }
- }
-
- public class RequisitionItemEditorSaveArgs : EventArgs
- {
- public ProductShell? Product { get; private set; }
-
- public String? Description { get; private set; }
-
- public Guid ImageID { get; private set; }
- public byte[]? Image { get; private set; }
-
- public double Quantity { get; private set; }
- public RequisitionItemEditorSaveArgs(ProductShell? product, string? description, Guid imageid, byte[]? image, double quantity)
- {
- Product = product;
- Description = description;
- ImageID = imageid;
- Image = image;
- Quantity = quantity;
- }
- }
-
- public delegate void RequisitionItemEditorSaveEvent(object sender, RequisitionItemEditorSaveArgs args);
-
- [XamlCompilation(XamlCompilationOptions.Compile)]
- public partial class RequisitionItemEditor : MobilePage
- {
- public event RequisitionItemEditorSaveEvent Saving;
-
- public RequisitionItemEditor(RequisitionItemShell? item)
- {
- InitializeComponent();
- if (item != null)
- {
- App.Data.Products.Refresh(false);
- _viewModel.Product = App.Data.Products.FirstOrDefault(x => x.ID == item.ProductID);
- _viewModel.Description = item.Description;
- _viewModel.ImageID = item.ImageID;
- _viewModel.Image = item.Image;
- _viewModel.Quantity = item.Quantity;
- }
- }
- private void SaveItem_Clicked(object sender, MobileMenuButtonClickedEventArgs args)
- {
- Saving?.Invoke(this,new RequisitionItemEditorSaveArgs(_viewModel.Product, _viewModel.Description, _viewModel.ImageID, _viewModel.Image, _viewModel.Quantity));
- Navigation.PopAsync();
- }
- private async void TakePicture_Clicked(object sender, EventArgs e)
- {
- var file = await AddImage<MobileDocumentCameraSource, MobileDocumentCameraOptions>(PhotoUtils.CreateCameraOptions());
- if (file != null)
- {
- _viewModel.ImageID = Guid.Empty;
- _viewModel.Image = file.Data;
- }
- }
- private async void SelectImage_Clicked(object sender, EventArgs e)
- {
- var file = await AddImage<MobileDocumentPhotoLibrarySource, MobileDocumentPhotoLibraryOptions>(PhotoUtils.CreatePhotoLibraryOptions());
- if (file != null)
- {
- _viewModel.ImageID = Guid.Empty;
- _viewModel.Image = file.Data;
- }
- }
-
- private async Task<MobileDocument?> AddImage<T, TOptions>(TOptions options)
- where T : MobileImageSource<T,TOptions>
- where TOptions : MobileImageOptions<T>, new()
- {
-
- MobileDocument file = null;
- try
- {
- file = await MobileDocument.From<T>(options);
- }
- catch (Exception e)
- {
- await MaterialDialog.Instance.AlertAsync(e.Message, "ERROR");
- }
- return file;
- }
- private void _selectProduct_OnClicked(object sender, MobileButtonClickEventArgs args)
- {
- ShowPopup(() => SelectionView.Execute<ProductShell>(
- (columns) =>
- {
- columns.Add(new MobileGridTextColumn<ProductShell>()
- {
- Column = x => x.Code,
- Width = 100,
- Caption = "Code",
- Alignment = TextAlignment.Start
- });
- columns.Add(new MobileGridTextColumn<ProductShell>()
- {
- Column = x => x.Name,
- Width = GridLength.Star,
- Caption = "Product Name",
- Alignment = TextAlignment.Start
- });
- },
- (refresh) => App.Data.Products.Refresh(false),
- (products) =>
- {
- _viewModel.Product = products.FirstOrDefault() ?? new ProductShell();
- _viewModel.Description = products.FirstOrDefault()?.Name ?? string.Empty;
- Guid imageid = products.FirstOrDefault()?.ImageID ?? Guid.Empty;
- if (imageid != Guid.Empty)
- {
- var docmodel = new DocumentModel(App.Data,
- () => new Filter<Document>(x => x.ID).IsEqualTo(imageid));
- docmodel.Refresh(
- true,
- () => Dispatcher.BeginInvokeOnMainThread(
- () =>
- {
- _viewModel.ImageID = imageid;
- _viewModel.Image = docmodel.Items.FirstOrDefault()?.Data;
- })
- );
- }
- DismissPopup();
- }));
- }
-
- private void _clearProduct_OnClicked(object sender, MobileButtonClickEventArgs args)
- {
- if (_viewModel.HasProductImage)
- {
- _viewModel.ImageID = Guid.Empty;
- _viewModel.Image = null;
- }
- _viewModel.Product = null;
- _viewModel.Description = "";
- }
- private void Minus_Clicked(object sender, MobileMenuButtonClickedEventArgs args)
- {
- _viewModel.Quantity = Math.Max(0.0F, _viewModel.Quantity - 1.0F);
- }
- private void Plus_Clicked(object sender, MobileMenuButtonClickedEventArgs args)
- {
- _viewModel.Quantity += 1.0F;
- }
- }
- }
|