RequisitionItemEditor.xaml.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using InABox.Core;
  7. using InABox.Mobile;
  8. using Xamarin.Forms;
  9. using Xamarin.Forms.Xaml;
  10. using XF.Material.Forms.UI.Dialogs;
  11. namespace PRS.Mobile
  12. {
  13. public class ReqisitionItemEditorProductConverter : AbstractConverter<ProductShell?, String>
  14. {
  15. protected override string Convert(ProductShell? value, object? parameter = null)
  16. {
  17. return (value?.ID ?? Guid.Empty) != Guid.Empty
  18. ? $"{value?.Code}: {value?.Name}"
  19. : "Select Product";
  20. }
  21. }
  22. public class RequisitionItemEditorViewModel : BindableObject
  23. {
  24. public ProductShell? Product { get; set; }
  25. public bool HasProduct => Product != null;
  26. public bool HasProductImage => (Product?.ImageID ?? Guid.Empty) != Guid.Empty;
  27. public String? Description { get; set; }
  28. public Guid ImageID { get; set; }
  29. public byte[]? Image { get; set; }
  30. public double Quantity { get; set; }
  31. public RequisitionItemEditorViewModel()
  32. {
  33. Quantity = 1.0F;
  34. }
  35. }
  36. public class RequisitionItemEditorSaveArgs : EventArgs
  37. {
  38. public ProductShell? Product { get; private set; }
  39. public String? Description { get; private set; }
  40. public Guid ImageID { get; private set; }
  41. public byte[]? Image { get; private set; }
  42. public double Quantity { get; private set; }
  43. public RequisitionItemEditorSaveArgs(ProductShell? product, string? description, Guid imageid, byte[]? image, double quantity)
  44. {
  45. Product = product;
  46. Description = description;
  47. ImageID = imageid;
  48. Image = image;
  49. Quantity = quantity;
  50. }
  51. }
  52. public delegate void RequisitionItemEditorSaveEvent(object sender, RequisitionItemEditorSaveArgs args);
  53. [XamlCompilation(XamlCompilationOptions.Compile)]
  54. public partial class RequisitionItemEditor : MobilePage
  55. {
  56. public event RequisitionItemEditorSaveEvent Saving;
  57. public RequisitionItemEditor(RequisitionItemShell? item)
  58. {
  59. InitializeComponent();
  60. if (item != null)
  61. {
  62. App.Data.Products.Refresh(false);
  63. _viewModel.Product = App.Data.Products.FirstOrDefault(x => x.ID == item.ProductID);
  64. _viewModel.Description = item.Description;
  65. _viewModel.ImageID = item.ImageID;
  66. _viewModel.Image = item.Image;
  67. _viewModel.Quantity = item.Quantity;
  68. }
  69. }
  70. private void SaveItem_Clicked(object sender, MobileMenuButtonClickedEventArgs args)
  71. {
  72. Saving?.Invoke(this,new RequisitionItemEditorSaveArgs(_viewModel.Product, _viewModel.Description, _viewModel.ImageID, _viewModel.Image, _viewModel.Quantity));
  73. Navigation.PopAsync();
  74. }
  75. private async void TakePicture_Clicked(object sender, EventArgs e)
  76. {
  77. var file = await AddImage<MobileDocumentCameraSource, MobileDocumentCameraOptions>(PhotoUtils.CreateCameraOptions());
  78. if (file != null)
  79. {
  80. _viewModel.ImageID = Guid.Empty;
  81. _viewModel.Image = file.Data;
  82. }
  83. }
  84. private async void SelectImage_Clicked(object sender, EventArgs e)
  85. {
  86. var file = await AddImage<MobileDocumentPhotoLibrarySource, MobileDocumentPhotoLibraryOptions>(PhotoUtils.CreatePhotoLibraryOptions());
  87. if (file != null)
  88. {
  89. _viewModel.ImageID = Guid.Empty;
  90. _viewModel.Image = file.Data;
  91. }
  92. }
  93. private async Task<MobileDocument?> AddImage<T, TOptions>(TOptions options)
  94. where T : MobileImageSource<T,TOptions>
  95. where TOptions : MobileImageOptions<T>, new()
  96. {
  97. MobileDocument file = null;
  98. try
  99. {
  100. file = await MobileDocument.From<T>(options);
  101. }
  102. catch (Exception e)
  103. {
  104. await MaterialDialog.Instance.AlertAsync(e.Message, "ERROR");
  105. }
  106. return file;
  107. }
  108. private void _selectProduct_OnClicked(object sender, MobileButtonClickEventArgs args)
  109. {
  110. ShowPopup(() => SelectionView.Execute<ProductShell>(
  111. (columns) =>
  112. {
  113. columns.Add(new MobileGridTextColumn<ProductShell>()
  114. {
  115. Column = x => x.Code,
  116. Width = 100,
  117. Caption = "Code",
  118. Alignment = TextAlignment.Start
  119. });
  120. columns.Add(new MobileGridTextColumn<ProductShell>()
  121. {
  122. Column = x => x.Name,
  123. Width = GridLength.Star,
  124. Caption = "Product Name",
  125. Alignment = TextAlignment.Start
  126. });
  127. },
  128. (refresh) => App.Data.Products.Refresh(false),
  129. (products) =>
  130. {
  131. _viewModel.Product = products.FirstOrDefault() ?? new ProductShell();
  132. _viewModel.Description = products.FirstOrDefault()?.Name ?? string.Empty;
  133. Guid imageid = products.FirstOrDefault()?.ImageID ?? Guid.Empty;
  134. if (imageid != Guid.Empty)
  135. {
  136. var docmodel = new DocumentModel(App.Data,
  137. () => new Filter<Document>(x => x.ID).IsEqualTo(imageid));
  138. docmodel.Refresh(
  139. true,
  140. () => Dispatcher.BeginInvokeOnMainThread(
  141. () =>
  142. {
  143. _viewModel.ImageID = imageid;
  144. _viewModel.Image = docmodel.Items.FirstOrDefault()?.Data;
  145. })
  146. );
  147. }
  148. DismissPopup();
  149. }));
  150. }
  151. private void _clearProduct_OnClicked(object sender, MobileButtonClickEventArgs args)
  152. {
  153. if (_viewModel.HasProductImage)
  154. {
  155. _viewModel.ImageID = Guid.Empty;
  156. _viewModel.Image = null;
  157. }
  158. _viewModel.Product = null;
  159. _viewModel.Description = "";
  160. }
  161. private void Minus_Clicked(object sender, MobileMenuButtonClickedEventArgs args)
  162. {
  163. _viewModel.Quantity = Math.Max(0.0F, _viewModel.Quantity - 1.0F);
  164. }
  165. private void Plus_Clicked(object sender, MobileMenuButtonClickedEventArgs args)
  166. {
  167. _viewModel.Quantity += 1.0F;
  168. }
  169. }
  170. }