StockTakeCompletionPage.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. 
  2. using Comal.Classes;
  3. using InABox.Clients;
  4. using InABox.Core;
  5. using Plugin.Media;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. using InABox.Mobile;
  14. using Xamarin.Forms;
  15. using Xamarin.Forms.Xaml;
  16. using XF.Material.Forms.UI.Dialogs;
  17. using LogType = InABox.Core.LogType;
  18. namespace comal.timesheets
  19. {
  20. [XamlCompilation(XamlCompilationOptions.Compile)]
  21. public partial class StockTakeCompletionPage
  22. {
  23. public delegate void StockTakeCompleted();
  24. public event StockTakeCompleted OnStockTakeCompleted;
  25. List<string> favourites = new List<string>
  26. { "Stocktake complete" };
  27. List<StockMovement> _movements = new List<StockMovement>();
  28. Dictionary<Image, Document> imagesDocuments = new Dictionary<Image, Document>();
  29. public StockTakeCompletionPage(List<StockMovement> movements)
  30. {
  31. InitializeComponent();
  32. _movements = movements;
  33. AddFavButtons();
  34. }
  35. private void AddFavButtons()
  36. {
  37. foreach (string s in favourites)
  38. {
  39. Button button = new Button
  40. {
  41. Text = s,
  42. TextColor = Color.White,
  43. BackgroundColor = Color.FromHex("#15C7C1"),
  44. CornerRadius = 10,
  45. Margin = 2,
  46. FontAttributes = FontAttributes.Bold,
  47. VerticalOptions = LayoutOptions.Center,
  48. HorizontalOptions = LayoutOptions.Center,
  49. Padding = new Thickness(6, 3, 6, 3)
  50. };
  51. button.Clicked += FavButton_Clicked;
  52. optionsFlexLayout.Children.Add(button);
  53. }
  54. }
  55. private void FavButton_Clicked(object sender, EventArgs e)
  56. {
  57. Button button = sender as Button;
  58. if (string.IsNullOrWhiteSpace(notesEdt.Text))
  59. {
  60. notesEdt.Text = button.Text;
  61. }
  62. else
  63. {
  64. notesEdt.Text = notesEdt.Text + " " + button.Text;
  65. }
  66. }
  67. private async void SaveBatch_Clicked(object sender, EventArgs e)
  68. {
  69. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Saving"))
  70. {
  71. StockMovementBatch batch = new StockMovementBatch() { Notes = notesEdt.Text };
  72. batch.Type = StockMovementBatchType.Stocktake;
  73. batch = DoSaveBatch(batch);
  74. SavePhotos(batch.ID);
  75. foreach (StockMovement movement in _movements)
  76. {
  77. movement.Batch.ID = batch.ID;
  78. movement.Date = batch.Created;
  79. }
  80. Task.Run(() =>
  81. {
  82. new Client<StockMovement>().Save(_movements, "Created on mobile device");
  83. });
  84. Device.BeginInvokeOnMainThread(async () =>
  85. {
  86. saveBatchBtn.IsVisible = false;
  87. await DisplayAlert("Success", "Batch Saved", "OK");
  88. OnStockTakeCompleted?.Invoke();
  89. Navigation.PopAsync();
  90. });
  91. }
  92. }
  93. private StockMovementBatch DoSaveBatch(StockMovementBatch batch)
  94. {
  95. try
  96. {
  97. new Client<StockMovementBatch>().Save(batch, "Created on mobile");
  98. return batch;
  99. }
  100. catch (Exception ex)
  101. {
  102. InABox.Mobile.MobileLogging.Log(ex);
  103. return DoSaveBatch(batch);
  104. }
  105. }
  106. #region Photos
  107. private async void SavePhotos(Guid batchID)
  108. {
  109. Task.Run(() =>
  110. {
  111. DoSaveDocuments();
  112. // Link the photos to the batch
  113. List<StockMovementBatchDocument> stockMovementBatchDocuments = new List<StockMovementBatchDocument>();
  114. foreach (var doc in imagesDocuments.Values)
  115. {
  116. var smd = new StockMovementBatchDocument();
  117. smd.EntityLink.ID = batchID;
  118. smd.DocumentLink.ID = doc.ID;
  119. smd.DocumentLink.FileName = doc.FileName;
  120. stockMovementBatchDocuments.Add(smd);
  121. }
  122. DoSaveStockMovementBatchDocuments(stockMovementBatchDocuments);
  123. });
  124. }
  125. private void DoSaveStockMovementBatchDocuments(List<StockMovementBatchDocument> stockMovementBatchDocuments)
  126. {
  127. try
  128. {
  129. new Client<StockMovementBatchDocument>().Save(stockMovementBatchDocuments, "Photo taken on mobile device for stocktake");
  130. }
  131. catch (Exception ex)
  132. {
  133. DoSaveStockMovementBatchDocuments(stockMovementBatchDocuments);
  134. }
  135. }
  136. private void DoSaveDocuments()
  137. {
  138. try
  139. {
  140. new Client<Document>().Save(imagesDocuments.Values, "Photo taken on mobile device for stocktake");
  141. }
  142. catch (Exception ex)
  143. {
  144. InABox.Mobile.MobileLogging.Log(ex);
  145. DoSaveDocuments();
  146. }
  147. }
  148. async void TakePhoto_Clicked(object sender, EventArgs e)
  149. {
  150. TakeAPhoto();
  151. }
  152. async void ChooseImage_Clicked(object sender, EventArgs e)
  153. {
  154. ChooseAPhoto();
  155. }
  156. private async void TakeAPhoto()
  157. {
  158. await CrossMedia.Current.Initialize();
  159. if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
  160. {
  161. await DisplayAlert("No Camera", ":( No camera available.", "OK");
  162. return;
  163. }
  164. String filename = String.Format("{0:yyyy-MM-dd HH:mm:ss.fff}.png", DateTime.Now);
  165. var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
  166. {
  167. Name = filename,
  168. CompressionQuality = 10,
  169. PhotoSize = Plugin.Media.Abstractions.PhotoSize.Full
  170. });
  171. if (file == null)
  172. return;
  173. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Adding Photo"))
  174. {
  175. Image img = null;
  176. var memoryStream = new MemoryStream();
  177. file.GetStream().CopyTo(memoryStream);
  178. var data = memoryStream.ToArray();
  179. Document doc = new Document()
  180. {
  181. FileName = filename,
  182. Data = data,
  183. CRC = CoreUtils.CalculateCRC(data),
  184. TimeStamp = DateTime.Now
  185. };
  186. ImageSource src = ImageSource.FromStream(() => new MemoryStream(data));
  187. img = new Image();
  188. img.HeightRequest = 150;
  189. img.WidthRequest = 150;
  190. img.Aspect = Aspect.AspectFit;
  191. img.Source = src;
  192. img.GestureRecognizers.Add(new TapGestureRecognizer
  193. {
  194. Command = new Command(OnTap),
  195. CommandParameter = src,
  196. NumberOfTapsRequired = 1
  197. });
  198. imagesDocuments.Add(img, doc);
  199. file.Dispose();
  200. if (img != null)
  201. {
  202. Device.BeginInvokeOnMainThread(() =>
  203. {
  204. ImageScroller.IsVisible = true;
  205. images.Children.Add(img);
  206. UpdateColours();
  207. });
  208. }
  209. }
  210. }
  211. private async void ChooseAPhoto()
  212. {
  213. await CrossMedia.Current.Initialize();
  214. if (!CrossMedia.Current.IsPickPhotoSupported)
  215. {
  216. await DisplayAlert("No Library", ":( No Photo Library available.", "OK");
  217. return;
  218. }
  219. var file = await CrossMedia.Current.PickPhotoAsync(new Plugin.Media.Abstractions.PickMediaOptions()
  220. {
  221. CompressionQuality = 10,
  222. PhotoSize = Plugin.Media.Abstractions.PhotoSize.Full
  223. });
  224. if (file == null)
  225. return;
  226. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Adding Photo"))
  227. {
  228. Image img = null;
  229. var memoryStream = new MemoryStream();
  230. file.GetStream().CopyTo(memoryStream);
  231. var data = memoryStream.ToArray();
  232. Document doc = new Document()
  233. {
  234. FileName = Path.GetFileName(file.Path),
  235. Data = data,
  236. CRC = CoreUtils.CalculateCRC(data),
  237. TimeStamp = DateTime.Now
  238. };
  239. ImageSource src = ImageSource.FromStream(() => new MemoryStream(data));
  240. img = new Image();
  241. img.HeightRequest = 150;
  242. img.WidthRequest = 150;
  243. img.Aspect = Aspect.AspectFit;
  244. img.Source = src;
  245. img.GestureRecognizers.Add(new TapGestureRecognizer
  246. {
  247. Command = new Command(OnTap),
  248. CommandParameter = src,
  249. NumberOfTapsRequired = 1
  250. });
  251. imagesDocuments.Add(img, doc);
  252. file.Dispose();
  253. if (img != null)
  254. {
  255. Device.BeginInvokeOnMainThread(() =>
  256. {
  257. ImageScroller.IsVisible = true;
  258. images.Children.Add(img);
  259. UpdateColours();
  260. });
  261. }
  262. }
  263. }
  264. private void OnTap(object obj)
  265. {
  266. ImageViewer viewer = new ImageViewer(obj as ImageSource);
  267. Navigation.PushAsync(viewer);
  268. viewer.ChooseDelete();
  269. viewer.OnDeleteSelected += () =>
  270. {
  271. Image img = imagesDocuments.Keys.First(x => x.Source.Equals(obj as ImageSource));
  272. imagesDocuments.Remove(img);
  273. Device.BeginInvokeOnMainThread(() =>
  274. {
  275. images.Children.Clear();
  276. if (imagesDocuments.Count > 0)
  277. {
  278. foreach (Image image in imagesDocuments.Keys)
  279. {
  280. images.Children.Add(image);
  281. }
  282. }
  283. UpdateColours();
  284. });
  285. };
  286. }
  287. #endregion
  288. #region Updating Screen
  289. private void NotesEdt_Changed(object sender, EventArgs e)
  290. {
  291. UpdateColours();
  292. }
  293. private void UpdateColours()
  294. {
  295. if (imagesDocuments.Values.Count > 0)
  296. {
  297. photosFrame.BorderColor = Color.Gray;
  298. }
  299. else
  300. {
  301. photosFrame.BorderColor = Color.Red;
  302. }
  303. if (!string.IsNullOrWhiteSpace(notesEdt.Text))
  304. {
  305. if (notesFrame.BorderColor == Color.Red)
  306. {
  307. notesFrame.BorderColor = Color.Gray;
  308. }
  309. }
  310. else
  311. {
  312. notesFrame.BorderColor = Color.Red;
  313. }
  314. ShowSave();
  315. }
  316. private void ShowSave()
  317. {
  318. if (notesFrame.BorderColor == Color.Gray && photosFrame.BorderColor == Color.Gray)
  319. {
  320. saveBatchBtn.IsVisible = true;
  321. }
  322. else
  323. {
  324. saveBatchBtn.IsVisible = false;
  325. }
  326. }
  327. #endregion
  328. }
  329. }