using System; using System.IO; using System.Linq; using InABox.Core; using InABox.Mobile; using Xamarin.Essentials; using Xamarin.Forms; using Xamarin.Forms.Xaml; namespace PRS.Mobile { [XamlCompilation(XamlCompilationOptions.Compile)] public partial class DocumentPage { //public static BindableProperty DocumentProperty = BindableProperty.Create( // nameof(Document), // typeof(IDocumentShell), // typeof(DocumentPage)); //public IDocumentShell Document //{ // get => (IDocumentShell)GetValue(DocumentProperty); // set // { // SetValue(DocumentProperty, value); // RefreshData(false,true); // } //} private Guid documentID; public Guid DocumentID { get => documentID; set { documentID = value; RefreshData(false, true); } } private DocumentModel _model; public DocumentPage() { _model = new DocumentModel(App.Data, () => new Filter(x => x.ID).IsEqualTo(DocumentID)); InitializeComponent(); ProgressVisible = true; } private void RefreshData(bool force, bool async) { _model.FileName = $"{DocumentID}.document"; if (async) _model.Refresh(force, RefreshScreen); else { _model.Refresh(force); RefreshScreen(); } } private void RefreshScreen() { var document = _model.FirstOrDefault(); _viewer.IsVisible = document != null; _noviewer.IsVisible = document == null; if (document != null) _viewer.Load(document.FileName, document.Data); Title = document?.FileName ?? "Unknown Document"; _launch.IsVisible = (document?.FileName?.ToUpper().EndsWith(".PDF")) == true; } private async void _launch_Click(object sender, MobileMenuButtonClickedEventArgs args) { var document = _model.FirstOrDefault(); if (document == null) return; var filePath = CoreRepository.CacheFileName($"{Path.GetFileNameWithoutExtension(document.FileName)}.pdf"); if (filePath != null) { try { await File.WriteAllBytesAsync(filePath,document.Data); } catch (Exception e) { MobileLogging.Log(e,"PDF Launcher"); } await Launcher.OpenAsync(new OpenFileRequest { File = new ReadOnlyFile(filePath) }); } } public event EventHandler DocumentLoaded; void _viewer_DocumentLoaded(System.Object sender, System.EventArgs e) { DocumentLoaded?.Invoke(this, EventArgs.Empty); } public byte[] GetThumbnail() { return _viewer.GetThumbnail(); } } }