using Comal.Classes; using InABox.Clients; using InABox.Core; using System; using System.Collections.Generic; using System.IO; using System.Linq; using Xamarin.Essentials; using Xamarin.Forms; using Xamarin.Forms.Xaml; using Document = InABox.Core.Document; using PRSSecurity = InABox.Core.Security; namespace comal.timesheets { [XamlCompilation(XamlCompilationOptions.Compile)] public partial class PDFList { public IList Documents { get; set; } public bool AllowUpload { get; set; } public bool AllowPrintShare { get; set; } public bool AllowView { get; set; } public bool AllowDelete { get; set; } public PDFList() { InitializeComponent(); } protected override void OnAppearing() { _documents.ItemsSource = Documents; _upload.IsVisible = AllowUpload; base.OnAppearing(); } // private DocShell AssignIcon(DocShell shell) // { // if (shell.FileName.ToLower().EndsWith("pdf")) // shell.ImageSource = "pdficon.png"; // else if (shell.FileName.ToLower().EndsWith("docx") || shell.FileName.ToLower().EndsWith("doc")) // shell.ImageSource = "worddoc.png"; // else if (shell.FileName.ToLower().EndsWith("png") || shell.FileName.ToLower().EndsWith("jpg")) // shell.ImageSource = "productimage.png"; // return shell; // } private async void Upload_Clicked(object sender, EventArgs e) { // var result = await FilePicker.PickAsync(new PickOptions { FileTypes = FilePickerFileType.Pdf }); // if (result != null) // { // using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Saving")) // { // string fileName = $"File Name: {result.FileName}"; // var stream = await result.OpenReadAsync(); // var memoryStream = new MemoryStream(); // stream.CopyTo(memoryStream); // var data = memoryStream.ToArray(); // var doc = new Document { FileName = fileName, Data = data }; // new Client().Save(doc, "Uploaded from mobile device"); // pDFShells.Add(new DocShell { FileName = fileName, DocID = doc.ID }); // pdfListView.ItemsSource = null; // pdfListView.ItemsSource = pDFShells; // SaveEntityDoc(doc.ID); // } // } } // private void SaveEntityDoc(Guid docID) // { // if (Entity is EmployeeQualification) // { // EmployeeQualificationDocument empDoc = new EmployeeQualificationDocument(); // empDoc.DocumentLink.ID = docID; // empDoc.EntityLink.ID = Entity.ID; // new Client().Save(empDoc, "Upload from mobile device"); // } // if (Entity is Notification) // { // NotificationDocument notificationDoc = new NotificationDocument(); // notificationDoc.DocumentLink.ID = docID; // notificationDoc.EntityLink.ID = Entity.ID; // new Client().Save(notificationDoc, "Upload from mobile device"); // } // } // private async void List_Tapped(object sender, EventArgs e) // { // DocShell shell = pdfListView.SelectedItem as DocShell; // if (!bAllowView) // { // DisplayAlert("Alert", "Opening is not allowed from this module", "OK"); // return; // } // // if (bAllowDelete) // { // string chosenOption = await DisplayActionSheet("Choose an option", "Cancel", null, "View", "Delete File"); // switch (chosenOption) // { // case "Cancel": // return; // default: // return; // case "View": // OpenDocViewer(shell); // break; // case "Delete File": // ConfirmDelete(shell); // break; // } // } // else // { // OpenDocViewer(shell); // } // } // // private async void ConfirmDelete(DocShell shell) // { // string chosenOption = await DisplayActionSheet("Confirm Delete?", "Cancel", null, "Yes", "No"); // switch (chosenOption) // { // case "Cancel": // return; // default: // return; // case "No": // return; // case "Yes": // using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Working")) // { // pDFShells.Remove(shell); // pdfListView.ItemsSource = null; // pdfListView.ItemsSource = pDFShells; // Document document = new Document { ID = shell.DocID }; // DeleteEntityDocument(shell.DocID); // new Client().Delete(document, "Deleted from mobile device"); // } // break; // } // } // // void DeleteEntityDocument(Guid id) // { // if (Entity is EmployeeQualification) // { // CoreTable table = new Client().Query(new Filter(x => x.DocumentLink.ID).IsEqualTo(id), // new Columns(x => x.ID)); // EmployeeQualificationDocument empDoc = new EmployeeQualificationDocument(); // empDoc.ID = Guid.Parse(table.Rows.First().Values[0].ToString()); // new Client().Delete(empDoc, "Deleted from mobile device"); // } // } void OpenDocViewer(DocShell shell) { shell.FileName = shell.FileName.ToLower(); if (shell.FileName.EndsWith("pdf")) { if (Device.RuntimePlatform.Equals(Device.Android) && PRSSecurity.IsAllowed()) OpenNativeViewer(shell); else { PDFViewer viewer = new PDFViewer(shell.DocID) { AllowPrintShare = this.AllowPrintShare }; Navigation.PushAsync(viewer); } } else if (shell.FileName.EndsWith("docx") || shell.FileName.EndsWith("doc")) { CoreTable table = new Client().Query(new Filter(x => x.ID).IsEqualTo(shell.DocID), new Columns(x => x.Data)); Document doc = table.Rows.First().ToObject(); MemoryStream memoryStream = new MemoryStream(doc.Data); DisplayAlert("Error", "Word documents not available at this time", "OK"); } else if (shell.FileName.EndsWith("png") || shell.FileName.EndsWith("jpg") || shell.FileName.EndsWith("jpeg")) { CoreTable table = new Client().Query(new Filter(x => x.ID).IsEqualTo(shell.DocID), new Columns(x => x.Data)); Document doc = table.Rows.First().ToObject(); ImageSource src = ImageSource.FromStream(() => new MemoryStream(doc.Data)); ImageViewer viewer = new ImageViewer(src); Navigation.PushAsync(viewer); } } private async void OpenNativeViewer(DocShell shell) { CoreTable table = new Client().Query(new Filter(x => x.ID).IsEqualTo(shell.DocID)); Document doc = table.Rows.First().ToObject(); var filePath = Path.Combine(FileSystem.AppDataDirectory, doc.FileName); File.WriteAllBytes(filePath, doc.Data); await Launcher.OpenAsync(new OpenFileRequest { File = new ReadOnlyFile(filePath) }); } // void SearchEnt_Changed(object sender, EventArgs e) // { // pdfListView.ItemsSource = pDFShells.Where(x => // x.FileName.Contains(searchEnt.Text) || x.FileName.Contains(searchEnt.Text.ToLower()) // || x.FileName.Contains(searchEnt.Text.ToUpper()) || x.FileName.Contains(UpperCaseFirst(searchEnt.Text)) // ); // } private void MaterialCard_OnClicked(object sender, EventArgs e) { //throw new NotImplementedException(); } private void _documents_OnItemTapped(object sender, MobileListItemTappedEventArgs args) { //throw new NotImplementedException(); } } public class DocShell { public string FileName { get; set; } public Guid DocID { get; set; } public ImageSource ImageSource { get; set; } public double FirstRowHeight { get; set; } public string Type { get; set; } public double ImageHeightRequest { get; set; } public double ImageWidthRequest { get; set; } public double ColumnWidth { get; set; } public double ImageRow { get; set; } public double ImageRowSpan { get; set; } public byte[] ThumbNail { get; set; } public double TypeColumn { get; set; } public double TypeColumnSpan { get; set; } public string FileDetails { get; set; } public bool ExpandVisible { get; set; } public DocShell() { FileName = ""; DocID = Guid.Empty; ImageSource = ""; FirstRowHeight = 0; Type = ""; ImageHeightRequest = 30; ImageWidthRequest = 30; ColumnWidth = 40; ImageRow = 1; ImageRowSpan = 1; TypeColumn = 0; TypeColumnSpan = 2; FileDetails = ""; ExpandVisible = false; } } }