DocumentList.xaml.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. public static readonly BindableProperty ItemsSourceProperty = BindableProperty.Create(
  18. nameof(ItemsSource),
  19. typeof(ICoreRepository),
  20. typeof(DocumentList)
  21. );
  22. //
  23. // private static void ItemsSourceChanged(BindableObject bindable, object oldvalue, object newvalue)
  24. // {
  25. // if ((bindable is DocumentList list) && newvalue is ICoreRepository repository)
  26. // {
  27. // list.ItemsSource = null;
  28. // list.ItemsSource = repository;
  29. // list._imagelist.ItemsSource = repository?.Items;
  30. // list._imagelist.LastUpdated = repository?.LastUpdated ?? DateTime.MinValue;
  31. // }
  32. // }
  33. public ICoreRepository? ItemsSource
  34. {
  35. get => GetValue(ItemsSourceProperty) as ICoreRepository;
  36. set
  37. {
  38. SetValue(ItemsSourceProperty, value);
  39. _imagelist.ItemsSource = value?.Items;
  40. _imagelist.LastUpdated = value?.LastUpdated ?? DateTime.MinValue;
  41. }
  42. }
  43. public Guid ParentID { get; set; }
  44. public event MobileListRefreshEvent RefreshRequested;
  45. public bool PullToRefresh
  46. {
  47. get => _imagelist.PullToRefresh;
  48. set => _imagelist.PullToRefresh = value;
  49. }
  50. public bool ShowRecordCount
  51. {
  52. get => _imagelist.ShowRecordCount;
  53. set => _imagelist.ShowRecordCount = value;
  54. }
  55. public string EmptyText
  56. {
  57. get => _imagelist.EmptyText;
  58. set => _imagelist.EmptyText = value;
  59. }
  60. public DocumentList()
  61. {
  62. InitializeComponent();
  63. }
  64. private void Image_Clicked(object sender, EventArgs e)
  65. {
  66. if ((sender as MobileCard)?.BindingContext is IEntityDocumentShell eds && eds.DocumentID != Guid.Empty)
  67. {
  68. DocumentPage docviewer = new DocumentPage();
  69. docviewer.DocumentLoaded += (o, e) =>
  70. {
  71. if ((sender as MobileCard)?.BindingContext is IEntityDocumentShell eds && (eds.Thumbnail?.Any() != true))
  72. {
  73. eds.Thumbnail = docviewer.GetThumbnail();
  74. eds.Save("Thumbnail Updated on Mobile Device");
  75. }
  76. };
  77. docviewer.DocumentID = eds.DocumentID;
  78. Navigation.PushAsync(docviewer);
  79. }
  80. }
  81. public async Task<bool> AddImage<T, TOptions, TShell>(TOptions options, Func<TShell, Task<bool>> customiseshell = null)
  82. where T : MobileDocumentSource
  83. where TOptions : MobileImageOptions<T>
  84. where TShell : class, IEntityDocumentShell
  85. {
  86. MobileDocument file = null;
  87. try
  88. {
  89. file = await MobileDocument.From(options);
  90. }
  91. catch (Exception e)
  92. {
  93. await MaterialDialog.Instance.AlertAsync(e.Message, "ERROR");
  94. }
  95. if (file?.Data?.Any() == true)
  96. {
  97. var ext = System.IO.Path.GetExtension(file.FileName);
  98. file.FileName = System.IO.Path.ChangeExtension(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), ext);
  99. var shell = ItemsSource.AddItem() as TShell;
  100. shell.ParentID = ParentID;
  101. bool confirm = (customiseshell == null)
  102. || await customiseshell.Invoke(shell);
  103. if (confirm)
  104. {
  105. using (await MaterialDialog.Instance.LoadingDialogAsync("Saving Image"))
  106. {
  107. shell.Thumbnail = MobileUtils.ImageTools.CreateThumbnail(file.Data, 256, 256);
  108. shell.FileName = file.FileName;
  109. var docshell = EntityDocumentUtils.SaveDocument<TShell>(
  110. file,
  111. () => shell,
  112. "Created on Mobile Device"
  113. );
  114. Device.BeginInvokeOnMainThread(() =>
  115. {
  116. ItemsSource.Search();
  117. _imagelist.ItemsSource = null;
  118. _imagelist.ItemsSource = ItemsSource.Items;
  119. });
  120. return true;
  121. }
  122. }
  123. }
  124. return false;
  125. }
  126. private void _imagelist_OnRefreshRequested(object sender, MobileListRefreshEventArgs args)
  127. {
  128. RefreshRequested?.Invoke(this,args);
  129. }
  130. }
  131. }