DocumentPage.xaml.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using InABox.Core;
  5. using InABox.Mobile;
  6. using Xamarin.Essentials;
  7. using Xamarin.Forms;
  8. using Xamarin.Forms.Xaml;
  9. namespace PRS.Mobile
  10. {
  11. [XamlCompilation(XamlCompilationOptions.Compile)]
  12. public partial class DocumentPage
  13. {
  14. //public static BindableProperty DocumentProperty = BindableProperty.Create(
  15. // nameof(Document),
  16. // typeof(IDocumentShell),
  17. // typeof(DocumentPage));
  18. //public IDocumentShell Document
  19. //{
  20. // get => (IDocumentShell)GetValue(DocumentProperty);
  21. // set
  22. // {
  23. // SetValue(DocumentProperty, value);
  24. // RefreshData(false,true);
  25. // }
  26. //}
  27. private Guid documentID;
  28. public Guid DocumentID
  29. {
  30. get => documentID;
  31. set
  32. {
  33. documentID = value;
  34. RefreshData(false, true);
  35. }
  36. }
  37. private DocumentModel _model;
  38. public DocumentPage()
  39. {
  40. _model = new DocumentModel(App.Data,
  41. () => new Filter<Document>(x => x.ID).IsEqualTo(DocumentID),
  42. () => CoreRepository.CacheFileName<Document>($"{DocumentID}")
  43. );
  44. InitializeComponent();
  45. ProgressVisible = true;
  46. }
  47. private void RefreshData(bool force, bool async)
  48. {
  49. if (async)
  50. _model.Refresh(force, RefreshScreen);
  51. else
  52. {
  53. _model.Refresh(force);
  54. RefreshScreen();
  55. }
  56. }
  57. private void RefreshScreen()
  58. {
  59. var document = _model.FirstOrDefault();
  60. _viewer.IsVisible = document != null;
  61. _noviewer.IsVisible = document == null;
  62. if (document != null)
  63. _viewer.Load(document.FileName, document.Data);
  64. Title = document?.FileName ?? "Unknown Document";
  65. _launch.IsVisible = (document?.FileName?.ToUpper().EndsWith(".PDF")) == true;
  66. }
  67. private async void _launch_Click(object sender, MobileMenuButtonClickedEventArgs args)
  68. {
  69. var document = _model.FirstOrDefault();
  70. if (document == null)
  71. return;
  72. var filePath = CoreRepository.CacheFileName($"{Path.GetFileNameWithoutExtension(document.FileName)}.pdf");
  73. if (filePath != null)
  74. {
  75. try
  76. {
  77. await File.WriteAllBytesAsync(filePath,document.Data);
  78. }
  79. catch (Exception e)
  80. {
  81. MobileLogging.Log(e,"PDF Launcher");
  82. }
  83. await Launcher.OpenAsync(new OpenFileRequest
  84. {
  85. File = new ReadOnlyFile(filePath)
  86. });
  87. }
  88. }
  89. public event EventHandler DocumentLoaded;
  90. void _viewer_DocumentLoaded(System.Object sender, System.EventArgs e)
  91. {
  92. DocumentLoaded?.Invoke(this, EventArgs.Empty);
  93. }
  94. public byte[] GetThumbnail()
  95. {
  96. return _viewer.GetThumbnail();
  97. }
  98. }
  99. }