| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 | 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<IDocumentShell> 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<Document>().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<EmployeeQualificationDocument>().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<NotificationDocument>().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<Document>().Delete(document, "Deleted from mobile device");        //             }        //             break;        //     }        // }        //        // void DeleteEntityDocument(Guid id)        // {        //     if (Entity is EmployeeQualification)        //     {        //         CoreTable table = new Client<EmployeeQualificationDocument>().Query(new Filter<EmployeeQualificationDocument>(x => x.DocumentLink.ID).IsEqualTo(id),        //             new Columns<EmployeeQualificationDocument>(x => x.ID));        //         EmployeeQualificationDocument empDoc = new EmployeeQualificationDocument();        //         empDoc.ID = Guid.Parse(table.Rows.First().Values[0].ToString());        //         new Client<EmployeeQualificationDocument>().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<CanOpenMobileNativePDFViewer>())                    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<Document>().Query(new Filter<Document>(x => x.ID).IsEqualTo(shell.DocID),                    new Columns<Document>(x => x.Data));                Document doc = table.Rows.First().ToObject<Document>();                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<Document>().Query(new Filter<Document>(x => x.ID).IsEqualTo(shell.DocID),                        new Columns<Document>(x => x.Data));                Document doc = table.Rows.First().ToObject<Document>();                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<Document>().Query(new Filter<Document>(x => x.ID).IsEqualTo(shell.DocID));            Document doc = table.Rows.First().ToObject<Document>();            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;        }    }}
 |