PDFList.xaml.cs 10.0 KB

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