| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372 |
- 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;
- 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);
- }
- //Loading from site module
- public PDFList(Guid jobID)
- {
- InitializeComponent();
- NavigationPage.SetHasBackButton(this, false);
- DeviceIdiom = DeviceInfo.Idiom;
- LoadMileStones(jobID);
- }
- //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;
- }
- //load from site module
- private async void LoadMileStones(Guid jobID)
- {
- using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading"))
- {
- CoreTable table = new Client<JobDocumentSetMileStone>().Query(
- new Filter<JobDocumentSetMileStone>(x => x.DocumentSet.Job.ID).IsEqualTo(jobID)
- .And(x => x.Status).IsEqualTo(JobDocumentSetMileStoneStatus.Submitted)
- .And(x => x.Type.SiteVisible).IsEqualTo(true),
- new Columns<JobDocumentSetMileStone>(
- x => x.ID, //0
- x => x.Type.Description, //1
- x => x.Submitted, //2
- x => x.Employee.Name //3
- ));
- foreach (CoreRow row in table.Rows)
- {
- LoadFilesForMileStone(row);
- }
- pdfListView.ItemsSource = pDFShells;
- }
- }
- private void LoadFilesForMileStone(CoreRow row)
- {
- List<object> list = row.Values;
- if (list[0] == null) list[0] = Guid.Empty; //0
- if (list[1] == null) list[1] = ""; //1
- if (list[2] == null) list[2] = DateTime.MinValue; //2
- if (list[3] == null) list[3] = ""; //3
- Guid id = Guid.Parse(list[0].ToString());
- string type = list[1].ToString();
- string date = DateTime.Parse(list[2].ToString()).ToString("dd MMM yy");
- string empName = list[3].ToString();
- string fileDetails = "Issued: " + date + Environment.NewLine + "By " + empName;
- CoreTable fileTable = new Client<JobDocumentSetMileStoneFile>().Query
- (
- new Filter<JobDocumentSetMileStoneFile>(x => x.EntityLink.ID).IsEqualTo(id),
- new Columns<JobDocumentSetMileStoneFile>(x => x.DocumentLink.ID, x => x.Thumbnail, x => x.DocumentLink.FileName)
- );
- foreach (CoreRow fileTableRow in fileTable.Rows)
- {
- List<object> fileTableList = fileTableRow.Values;
- DocShell shell = new DocShell();
- shell.FirstRowHeight = 30;
- shell.Type = type;
- shell.FileName = fileTableRow.Values[2].ToString();
- shell.DocID = Guid.Parse(fileTableRow.Values[0].ToString());
- shell.FileDetails = shell.FileName + Environment.NewLine + fileDetails;
- if (fileTableRow.Values[1] != null)
- {
- shell.ThumbNail = fileTableRow.Get<byte[]>("Thumbnail");
- shell = AssignThumbnail(shell);
- }
- else
- shell = AssignIcon(shell);
- pDFShells.Add(shell);
- }
- }
- 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"))
- {
- 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);
- }
- }
- 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;
- }
- }
- }
|