DocumentList.xaml.cs 4.4 KB

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