| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- 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<Document>(x => x.ID).IsEqualTo(DocumentID),
- () => CoreRepository.CacheFileName<Document>($"{DocumentID}")
- );
- InitializeComponent();
- ProgressVisible = true;
- }
-
- private void RefreshData(bool force, bool async)
- {
-
- 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();
- }
- }
- }
|