using Comal.Classes; using InABox.Clients; using InABox.Core; using Syncfusion.SfPdfViewer.XForms; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; using Xamarin.Essentials; using Xamarin.Forms; using Xamarin.Forms.Xaml; using XF.Material.Forms.UI.Dialogs; using Document = InABox.Core.Document; using PRSSecurity = InABox.Core.Security; namespace comal.timesheets { [XamlCompilation(XamlCompilationOptions.Compile)] public partial class PDFList : ContentPage { List pDFShells = new List(); bool bAllowPrintShare; bool bAllowDelete; bool bAllowView = true; Entity Entity; DeviceIdiom DeviceIdiom; //allowing of print/share is disabled by default //allowing of upload is disabled by default public PDFList(Dictionary fileNameIDs, bool allowprintshare = false) { InitializeComponent(); NavigationPage.SetHasBackButton(this, false); bAllowPrintShare = allowprintshare; LoadScreen(fileNameIDs); } //2nd constructor requires an entity for uploading of files or deleting (if allowed) public PDFList(Dictionary fileNameIDs, Entity entity, bool allowprintshare = false, bool allowUpload = false, bool allowDelete = false, bool allowView = true) { InitializeComponent(); NavigationPage.SetHasBackButton(this, false); bAllowPrintShare = allowprintshare; Entity = entity; if (allowUpload) uploadBtn.IsVisible = true; bAllowDelete = allowDelete; bAllowView = allowView; LoadScreen(fileNameIDs); } //standard load private void LoadScreen(Dictionary fileNameIDs) { foreach (KeyValuePair pair in fileNameIDs) { DocShell shell = new DocShell(); shell.FileName = pair.Key; shell.DocID = pair.Value; shell = AssignIcon(shell); pDFShells.Add(shell); } pdfListView.ItemsSource = pDFShells; } private DocShell AssignThumbnail(DocShell shell) { shell.ImageRow = 0; shell.ImageRowSpan = 2; shell.ImageSource = ImageSource.FromStream(() => new MemoryStream(shell.ThumbNail)); shell.TypeColumn = 1; shell.TypeColumnSpan = 1; if (DeviceIdiom == DeviceIdiom.Tablet) { shell.ImageHeightRequest = 300; shell.ImageWidthRequest = 400; shell.ColumnWidth = 400; } else { shell.ImageHeightRequest = 150; shell.ImageWidthRequest = 200; shell.ColumnWidth = 200; shell.ExpandVisible = true; } return shell; } 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 void ExpandImage_Tapped(object sender, EventArgs e) { var item = ((TappedEventArgs)e).Parameter as DocShell; if (item == null) return; } void ExitBtn_Clicked(object sender, EventArgs e) { Navigation.PopAsync(); } 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, bAllowPrintShare); 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)) ); } static String UpperCaseFirst(string s) { char[] a = s.ToCharArray(); a[0] = char.ToUpper(a[0]); return new string(a); } } 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; } } }