PDFList.xaml.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. using Comal.Classes;
  2. using InABox.Clients;
  3. using InABox.Core;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using Xamarin.Essentials;
  9. using Xamarin.Forms;
  10. using Xamarin.Forms.Xaml;
  11. using Document = InABox.Core.Document;
  12. using PRSSecurity = InABox.Core.Security;
  13. namespace comal.timesheets
  14. {
  15. [XamlCompilation(XamlCompilationOptions.Compile)]
  16. public partial class PDFList
  17. {
  18. public IList<IDocumentShell> Documents { get; set; }
  19. public bool AllowUpload { get; set; }
  20. public bool AllowPrintShare { get; set; }
  21. public bool AllowView { get; set; }
  22. public bool AllowDelete { get; set; }
  23. public PDFList()
  24. {
  25. InitializeComponent();
  26. }
  27. protected override void OnAppearing()
  28. {
  29. _documents.ItemsSource = Documents;
  30. _upload.IsVisible = AllowUpload;
  31. base.OnAppearing();
  32. }
  33. // private DocShell AssignIcon(DocShell shell)
  34. // {
  35. // if (shell.FileName.ToLower().EndsWith("pdf"))
  36. // shell.ImageSource = "pdficon.png";
  37. // else if (shell.FileName.ToLower().EndsWith("docx") || shell.FileName.ToLower().EndsWith("doc"))
  38. // shell.ImageSource = "worddoc.png";
  39. // else if (shell.FileName.ToLower().EndsWith("png") || shell.FileName.ToLower().EndsWith("jpg"))
  40. // shell.ImageSource = "productimage.png";
  41. // return shell;
  42. // }
  43. private async void Upload_Clicked(object sender, EventArgs e)
  44. {
  45. // var result = await FilePicker.PickAsync(new PickOptions { FileTypes = FilePickerFileType.Pdf });
  46. // if (result != null)
  47. // {
  48. // using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Saving"))
  49. // {
  50. // string fileName = $"File Name: {result.FileName}";
  51. // var stream = await result.OpenReadAsync();
  52. // var memoryStream = new MemoryStream();
  53. // stream.CopyTo(memoryStream);
  54. // var data = memoryStream.ToArray();
  55. // var doc = new Document { FileName = fileName, Data = data };
  56. // new Client<Document>().Save(doc, "Uploaded from mobile device");
  57. // pDFShells.Add(new DocShell { FileName = fileName, DocID = doc.ID });
  58. // pdfListView.ItemsSource = null;
  59. // pdfListView.ItemsSource = pDFShells;
  60. // SaveEntityDoc(doc.ID);
  61. // }
  62. // }
  63. }
  64. // private void SaveEntityDoc(Guid docID)
  65. // {
  66. // if (Entity is EmployeeQualification)
  67. // {
  68. // EmployeeQualificationDocument empDoc = new EmployeeQualificationDocument();
  69. // empDoc.DocumentLink.ID = docID;
  70. // empDoc.EntityLink.ID = Entity.ID;
  71. // new Client<EmployeeQualificationDocument>().Save(empDoc, "Upload from mobile device");
  72. // }
  73. // if (Entity is Notification)
  74. // {
  75. // NotificationDocument notificationDoc = new NotificationDocument();
  76. // notificationDoc.DocumentLink.ID = docID;
  77. // notificationDoc.EntityLink.ID = Entity.ID;
  78. // new Client<NotificationDocument>().Save(notificationDoc, "Upload from mobile device");
  79. // }
  80. // }
  81. // private async void List_Tapped(object sender, EventArgs e)
  82. // {
  83. // DocShell shell = pdfListView.SelectedItem as DocShell;
  84. // if (!bAllowView)
  85. // {
  86. // DisplayAlert("Alert", "Opening is not allowed from this module", "OK");
  87. // return;
  88. // }
  89. //
  90. // if (bAllowDelete)
  91. // {
  92. // string chosenOption = await DisplayActionSheet("Choose an option", "Cancel", null, "View", "Delete File");
  93. // switch (chosenOption)
  94. // {
  95. // case "Cancel":
  96. // return;
  97. // default:
  98. // return;
  99. // case "View":
  100. // OpenDocViewer(shell);
  101. // break;
  102. // case "Delete File":
  103. // ConfirmDelete(shell);
  104. // break;
  105. // }
  106. // }
  107. // else
  108. // {
  109. // OpenDocViewer(shell);
  110. // }
  111. // }
  112. //
  113. // private async void ConfirmDelete(DocShell shell)
  114. // {
  115. // string chosenOption = await DisplayActionSheet("Confirm Delete?", "Cancel", null, "Yes", "No");
  116. // switch (chosenOption)
  117. // {
  118. // case "Cancel":
  119. // return;
  120. // default:
  121. // return;
  122. // case "No":
  123. // return;
  124. // case "Yes":
  125. // using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Working"))
  126. // {
  127. // pDFShells.Remove(shell);
  128. // pdfListView.ItemsSource = null;
  129. // pdfListView.ItemsSource = pDFShells;
  130. // Document document = new Document { ID = shell.DocID };
  131. // DeleteEntityDocument(shell.DocID);
  132. // new Client<Document>().Delete(document, "Deleted from mobile device");
  133. // }
  134. // break;
  135. // }
  136. // }
  137. //
  138. // void DeleteEntityDocument(Guid id)
  139. // {
  140. // if (Entity is EmployeeQualification)
  141. // {
  142. // CoreTable table = new Client<EmployeeQualificationDocument>().Query(new Filter<EmployeeQualificationDocument>(x => x.DocumentLink.ID).IsEqualTo(id),
  143. // new Columns<EmployeeQualificationDocument>(x => x.ID));
  144. // EmployeeQualificationDocument empDoc = new EmployeeQualificationDocument();
  145. // empDoc.ID = Guid.Parse(table.Rows.First().Values[0].ToString());
  146. // new Client<EmployeeQualificationDocument>().Delete(empDoc, "Deleted from mobile device");
  147. // }
  148. // }
  149. void OpenDocViewer(DocShell shell)
  150. {
  151. shell.FileName = shell.FileName.ToLower();
  152. if (shell.FileName.EndsWith("pdf"))
  153. {
  154. if (Device.RuntimePlatform.Equals(Device.Android) && PRSSecurity.IsAllowed<CanOpenMobileNativePDFViewer>())
  155. OpenNativeViewer(shell);
  156. else
  157. {
  158. PDFViewer viewer = new PDFViewer(shell.DocID) { AllowPrintShare = this.AllowPrintShare };
  159. Navigation.PushAsync(viewer);
  160. }
  161. }
  162. else if (shell.FileName.EndsWith("docx") || shell.FileName.EndsWith("doc"))
  163. {
  164. CoreTable table = new Client<Document>().Query(new Filter<Document>(x => x.ID).IsEqualTo(shell.DocID),
  165. new Columns<Document>(x => x.Data));
  166. Document doc = table.Rows.First().ToObject<Document>();
  167. MemoryStream memoryStream = new MemoryStream(doc.Data);
  168. DisplayAlert("Error", "Word documents not available at this time", "OK");
  169. }
  170. else if (shell.FileName.EndsWith("png") || shell.FileName.EndsWith("jpg") || shell.FileName.EndsWith("jpeg"))
  171. {
  172. CoreTable table = new Client<Document>().Query(new Filter<Document>(x => x.ID).IsEqualTo(shell.DocID),
  173. new Columns<Document>(x => x.Data));
  174. Document doc = table.Rows.First().ToObject<Document>();
  175. ImageSource src = ImageSource.FromStream(() => new MemoryStream(doc.Data));
  176. ImageViewer viewer = new ImageViewer(src);
  177. Navigation.PushAsync(viewer);
  178. }
  179. }
  180. private async void OpenNativeViewer(DocShell shell)
  181. {
  182. CoreTable table = new Client<Document>().Query(new Filter<Document>(x => x.ID).IsEqualTo(shell.DocID));
  183. Document doc = table.Rows.First().ToObject<Document>();
  184. var filePath = Path.Combine(FileSystem.AppDataDirectory, doc.FileName);
  185. File.WriteAllBytes(filePath, doc.Data);
  186. await Launcher.OpenAsync(new OpenFileRequest
  187. {
  188. File = new ReadOnlyFile(filePath)
  189. });
  190. }
  191. // void SearchEnt_Changed(object sender, EventArgs e)
  192. // {
  193. // pdfListView.ItemsSource = pDFShells.Where(x =>
  194. // x.FileName.Contains(searchEnt.Text) || x.FileName.Contains(searchEnt.Text.ToLower())
  195. // || x.FileName.Contains(searchEnt.Text.ToUpper()) || x.FileName.Contains(UpperCaseFirst(searchEnt.Text))
  196. // );
  197. // }
  198. private void MaterialCard_OnClicked(object sender, EventArgs e)
  199. {
  200. //throw new NotImplementedException();
  201. }
  202. private void _documents_OnItemTapped(object sender, MobileListItemTappedEventArgs args)
  203. {
  204. //throw new NotImplementedException();
  205. }
  206. }
  207. public class DocShell
  208. {
  209. public string FileName { get; set; }
  210. public Guid DocID { get; set; }
  211. public ImageSource ImageSource { get; set; }
  212. public double FirstRowHeight { get; set; }
  213. public string Type { get; set; }
  214. public double ImageHeightRequest { get; set; }
  215. public double ImageWidthRequest { get; set; }
  216. public double ColumnWidth { get; set; }
  217. public double ImageRow { get; set; }
  218. public double ImageRowSpan { get; set; }
  219. public byte[] ThumbNail { get; set; }
  220. public double TypeColumn { get; set; }
  221. public double TypeColumnSpan { get; set; }
  222. public string FileDetails { get; set; }
  223. public bool ExpandVisible { get; set; }
  224. public DocShell()
  225. {
  226. FileName = "";
  227. DocID = Guid.Empty;
  228. ImageSource = "";
  229. FirstRowHeight = 0;
  230. Type = "";
  231. ImageHeightRequest = 30;
  232. ImageWidthRequest = 30;
  233. ColumnWidth = 40;
  234. ImageRow = 1;
  235. ImageRowSpan = 1;
  236. TypeColumn = 0;
  237. TypeColumnSpan = 2;
  238. FileDetails = "";
  239. ExpandVisible = false;
  240. }
  241. }
  242. }