EmployeeQualificationEditImagesView.xaml.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System;
  2. using System.Linq;
  3. using System.Threading.Tasks;
  4. using InABox.Clients;
  5. using InABox.Core;
  6. using InABox.Mobile;
  7. using Xamarin.Forms.Xaml;
  8. using XF.Material.Forms.UI.Dialogs;
  9. namespace PRS.Mobile
  10. {
  11. public enum EmployeeQualificationImageType
  12. {
  13. Front,
  14. Back,
  15. Other
  16. }
  17. [XamlCompilation(XamlCompilationOptions.Compile)]
  18. public partial class EmployeeQualificationEditImagesView
  19. {
  20. public EmployeeQualificationEditImagesView()
  21. {
  22. InitializeComponent();
  23. }
  24. public override void Refresh()
  25. {
  26. if (ViewModel == null)
  27. return;
  28. _otherImages.ParentID = ViewModel.Item.ID;
  29. _otherImages.ItemsSource = ViewModel.Documents;
  30. var front = ViewModel.Photos.FirstOrDefault(x => x.ID == ViewModel.Item.FrontPhotoID);
  31. var back = ViewModel.Photos.FirstOrDefault(x => x.ID == ViewModel.Item.BackPhotoID);
  32. _frontImage.Load(front?.FileName, front?.Data);
  33. _backImage.Load(back?.FileName, back?.Data);
  34. }
  35. public EmployeeQualificationImageType ImageType =>
  36. _accordion.SelectedItem == _frontitem
  37. ? EmployeeQualificationImageType.Front
  38. : _accordion.SelectedItem == _backitem
  39. ? EmployeeQualificationImageType.Back
  40. : EmployeeQualificationImageType.Other;
  41. public async Task AddImage<T, TOptions>(TOptions options)
  42. where T : MobileImageSource<T,TOptions>
  43. where TOptions : MobileImageOptions<T>, new()
  44. {
  45. if (_accordion.SelectedItem == _otheritem)
  46. {
  47. await _otherImages.AddImage<T, TOptions, EmployeeQualificationDocumentShell>(options);
  48. return;
  49. }
  50. MobileDocument file = null;
  51. try
  52. {
  53. file = await MobileDocument.From(options);
  54. }
  55. catch (Exception e)
  56. {
  57. await MaterialDialog.Instance.AlertAsync(e.Message, "ERROR");
  58. }
  59. if (file != null)
  60. {
  61. using (await MaterialDialog.Instance.LoadingDialogAsync("Saving Image"))
  62. {
  63. Document doc = new Document()
  64. {
  65. FileName = file.FileName,
  66. Data = file.Data,
  67. CRC = CoreUtils.CalculateCRC(file.Data),
  68. TimeStamp = DateTime.Now
  69. };
  70. new Client<Document>().Save(doc, "Created on Mobile Device");
  71. if (_accordion.SelectedItem == _frontitem)
  72. {
  73. ViewModel.Item.FrontPhotoID = doc.ID;
  74. ViewModel.Item.Save("Front Image Loaded from Mobile Device");
  75. _frontImage.Load(file.FileName,file.Data);
  76. }
  77. else if (_accordion.SelectedItem == _backitem)
  78. {
  79. ViewModel.Item.BackPhotoID = doc.ID;
  80. ViewModel.Item.Save("Back Image Loaded from Mobile Device");
  81. _backImage.Load(file.FileName,file.Data);
  82. }
  83. }
  84. }
  85. }
  86. }
  87. }