DocumentList.xaml.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System;
  2. using System.Threading.Tasks;
  3. using InABox.Mobile;
  4. using Xamarin.Forms;
  5. using Xamarin.Forms.Xaml;
  6. using Xamarin.Forms.Xaml.Internals;
  7. using XF.Material.Forms.UI.Dialogs;
  8. namespace PRS.Mobile
  9. {
  10. [XamlCompilation(XamlCompilationOptions.Compile)]
  11. public partial class DocumentList
  12. {
  13. private ICoreRepository _itemsSource;
  14. public ICoreRepository ItemsSource
  15. {
  16. get => _itemsSource;
  17. set
  18. {
  19. _itemsSource = value;
  20. _imagelist.ItemsSource = value?.Items;
  21. _imagelist.LastUpdated = value?.LastUpdated ?? DateTime.MinValue;
  22. }
  23. }
  24. public Guid ParentID { get; set; }
  25. public event MobileListRefreshEvent RefreshRequested;
  26. public bool PullToRefresh
  27. {
  28. get => _imagelist.PullToRefresh;
  29. set => _imagelist.PullToRefresh = value;
  30. }
  31. public bool ShowRecordCount
  32. {
  33. get => _imagelist.ShowRecordCount;
  34. set => _imagelist.ShowRecordCount = value;
  35. }
  36. public DocumentList()
  37. {
  38. InitializeComponent();
  39. }
  40. private void Image_Clicked(object sender, EventArgs e)
  41. {
  42. if ((sender as MobileCard)?.BindingContext is IEntityDocumentShell shell)
  43. {
  44. if (shell.FileName.ToLower().EndsWith(".pdf"))
  45. {
  46. PDFViewer pdf = new PDFViewer(shell.DocumentID);
  47. Navigation.PushAsync(pdf);
  48. }
  49. else
  50. {
  51. ImageViewerPage img = new ImageViewerPage(shell.DocumentID);
  52. Navigation.PushAsync(img);
  53. }
  54. }
  55. }
  56. public async Task<bool> AddImage<T, TShell>(bool pdf = false, Func<TShell, Task<bool>> customiseshell = null)
  57. where T : MobileDocumentSource, new()
  58. where TShell : class, IEntityDocumentShell
  59. {
  60. MobileDocument file = null;
  61. try
  62. {
  63. file = await MobileDocument.From<T>();
  64. }
  65. catch (Exception e)
  66. {
  67. await MaterialDialog.Instance.AlertAsync(e.Message, "ERROR");
  68. }
  69. if (file != null)
  70. {
  71. var shell = ItemsSource.AddItem() as TShell;
  72. shell.ParentID = ParentID;
  73. bool confirm = (customiseshell == null)
  74. || await customiseshell.Invoke(shell);
  75. if (confirm)
  76. {
  77. using (await MaterialDialog.Instance.LoadingDialogAsync("Saving Image"))
  78. {
  79. var thumbnail = MobileUtils.ImageTools.CreateThumbnail(file.Data, 256, 256);
  80. shell.Thumbnail = thumbnail;
  81. if (pdf)
  82. file = file.ToPDF();
  83. shell.FileName = file.FileName;
  84. var docshell = EntityDocumentUtils.SaveDocument<TShell>(
  85. file,
  86. () => shell,
  87. "Created on Mobile Device"
  88. );
  89. Device.BeginInvokeOnMainThread(() =>
  90. {
  91. ItemsSource.Search();
  92. _imagelist.ItemsSource = null;
  93. _imagelist.ItemsSource = ItemsSource.Items;
  94. });
  95. return true;
  96. }
  97. }
  98. }
  99. return false;
  100. }
  101. private void _imagelist_OnRefreshRequested(object sender, MobileListRefreshEventArgs args)
  102. {
  103. RefreshRequested?.Invoke(this,args);
  104. }
  105. }
  106. }