123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320 |
- 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<DocShell> pDFShells = new List<DocShell>();
- 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<string, Guid> 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<string, Guid> 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<string, Guid> fileNameIDs)
- {
- foreach (KeyValuePair<string, Guid> 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<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, bAllowPrintShare);
- 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))
- );
- }
- 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;
- }
- }
- }
|