DocumentPage.xaml.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. InitializeComponent();
  43. ProgressVisible = true;
  44. }
  45. private void RefreshData(bool force, bool async)
  46. {
  47. _model.FileName = $"{DocumentID}.document";
  48. if (async)
  49. _model.Refresh(force, RefreshScreen);
  50. else
  51. {
  52. _model.Refresh(force);
  53. RefreshScreen();
  54. }
  55. }
  56. private void RefreshScreen()
  57. {
  58. var document = _model.FirstOrDefault();
  59. _viewer.IsVisible = document != null;
  60. _noviewer.IsVisible = document == null;
  61. if (document != null)
  62. _viewer.Load(document.FileName, document.Data);
  63. Title = document?.FileName ?? "Unknown Document";
  64. _launch.IsVisible = (document?.FileName?.ToUpper().EndsWith(".PDF")) == true;
  65. }
  66. private async void _launch_Click(object sender, MobileMenuButtonClickedEventArgs args)
  67. {
  68. var document = _model.FirstOrDefault();
  69. if (document == null)
  70. return;
  71. var filePath = CoreRepository.CacheFileName($"{Path.GetFileNameWithoutExtension(document.FileName)}.pdf");
  72. if (filePath != null)
  73. {
  74. try
  75. {
  76. await File.WriteAllBytesAsync(filePath,document.Data);
  77. }
  78. catch (Exception e)
  79. {
  80. MobileLogging.Log(e,"PDF Launcher");
  81. }
  82. await Launcher.OpenAsync(new OpenFileRequest
  83. {
  84. File = new ReadOnlyFile(filePath)
  85. });
  86. }
  87. }
  88. public event EventHandler DocumentLoaded;
  89. void _viewer_DocumentLoaded(System.Object sender, System.EventArgs e)
  90. {
  91. DocumentLoaded?.Invoke(this, EventArgs.Empty);
  92. }
  93. public byte[] GetThumbnail()
  94. {
  95. return _viewer.GetThumbnail();
  96. }
  97. }
  98. }