123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575 |
- using comal.timesheets.CustomControls;
- using comal.timesheets.Data_Classes;
- using Comal.Classes;
- using InABox.Clients;
- using InABox.Core;
- using Syncfusion.TreeView.Engine;
- using Syncfusion.XForms.PopupLayout;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.ComponentModel;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using Xamarin.Essentials;
- using Xamarin.Forms;
- using Xamarin.Forms.Xaml;
- using XF.Material.Forms.UI.Dialogs;
- using PRSSecurity = InABox.Core.Security;
- namespace comal.timesheets
- {
- public delegate void OnFolderListChanged();
- [XamlCompilation(XamlCompilationOptions.Compile)]
- public partial class JobDocViewer : ContentPage
- {
- #region Fields and Constructor / Navigation
- ObservableCollection<FolderViewItem> folderList = new ObservableCollection<FolderViewItem>();
- ObservableCollection<FolderViewItem> displayList = new ObservableCollection<FolderViewItem>();
- DeviceIdiom idiom;
- bool foldersLoaded = false;
- bool filesLoaded = false;
- bool specificQueryLoading = false;
- Guid jobID = Guid.Empty;
- List<Guid> loadedFolders = new List<Guid>();
- List<MileStoneShell> mileStoneShells = new List<MileStoneShell>();
- List<JobDocSetFileShell> shells = new List<JobDocSetFileShell>();
- List<JobDocSetFileShell> searchList = new List<JobDocSetFileShell>();
- List<MileStoneShell> specificQueryMileStones = new List<MileStoneShell>();
- List<JobDocSetFileShell> specificQueryFileShells = new List<JobDocSetFileShell>();
- List<JobDocSetFileShell> currentQueryFileShells = new List<JobDocSetFileShell>();
- public JobDocViewer(Job job)
- {
- InitializeComponent();
- NavigationPage.SetHasBackButton(this, false);
- titleLbl.Text = "Job " + job.JobNumber;
- treeView.QueryNodeSize += TreeView_QueryNodeSize;
- idiom = DeviceInfo.Idiom;
- if (Device.RuntimePlatform.Equals(Device.iOS))
- {
- expandImg.HeightRequest = 50;
- expandImg.WidthRequest = 50;
- collapseImg.HeightRequest = 50;
- collapseImg.WidthRequest = 50;
- }
- jobID = job.ID;
- LoadFiles(new Filter<JobDocumentSetMileStone>(x => x.DocumentSet.Job.ID).IsEqualTo(jobID));
- LoadFolders(jobID);
- }
- void ExitBtn_Clicked(object sender, EventArgs e)
- {
- Navigation.PopAsync();
- }
- #endregion
- #region TreeView / Folder Interaction
- void Folder_Tapped(object sender, EventArgs e)
- {
- var folder = treeView.SelectedItem as FolderViewItem;
- if (folder.Documents == 0)
- return;
- if (specificQueryLoading)
- return;
- if (filesLoaded)
- {
- if (folder.ItemName == "All")
- {
- AddSearchItems(shells);
- }
- else
- {
- var list = shells.Where(x => x.FolderID == folder.ID);
- AddSearchItems(list);
- }
- }
- else if (loadedFolders.Contains(folder.ID))
- {
- var list = specificQueryFileShells.Where(x => x.FolderID == folder.ID);
- AddSearchItems(list);
- }
- else
- LoadFilesForFolder(folder.ID);
- }
- private async void LoadFilesForFolder(Guid folderID)
- {
- currentQueryFileShells.Clear();
- specificQueryLoading = true;
- ShowLoading();
- LoadFiles(new Filter<JobDocumentSetMileStone>(x => x.DocumentSet.Folder.ID).IsEqualTo(folderID), true, folderID);
- }
- void Expand_Tapped(object sender, EventArgs e)
- {
- if (!foldersLoaded)
- return;
- treeViewFrame.HeightRequest = 28 * folderList.Count;
- expandImg.IsVisible = false;
- collapseImg.IsVisible = true;
- }
- void Collapse_Tapped(object sender, EventArgs e)
- {
- treeViewFrame.HeightRequest = 120;
- expandImg.IsVisible = true;
- collapseImg.IsVisible = false;
- }
- #endregion
- #region Searching
- private void SearchEnt_Changed(object sender, EventArgs e)
- {
- if (specificQueryLoading)
- return;
- RunSearch();
- }
- private void AddSearchItems(IEnumerable<JobDocSetFileShell> list)
- {
- searchList.Clear();
- foreach (var v in list)
- {
- searchList.Add(v);
- }
- RunSearch();
- }
- private void RunSearch()
- {
- Device.BeginInvokeOnMainThread(() =>
- {
- listView.ItemsSource = null;
- if (string.IsNullOrWhiteSpace(searchEnt.Text))
- {
- listView.ItemsSource = searchList;
- fileCountLbl.Text = "Files (" + searchList.Count() + ")";
- }
- else
- {
- var list = searchList.Where
- (
- x =>
- x.FileName.Contains(searchEnt.Text) || x.FileName.Contains(searchEnt.Text.ToLower()) ||
- x.FileName.Contains(searchEnt.Text.ToUpper()) || x.FileName.Contains(SearchUtils.UpperCaseFirst(searchEnt.Text)) ||
- x.DocSetDescription.Contains(searchEnt.Text) || x.DocSetDescription.Contains(searchEnt.Text.ToLower()) ||
- x.DocSetDescription.Contains(searchEnt.Text.ToUpper()) || x.DocSetDescription.Contains(SearchUtils.UpperCaseFirst(searchEnt.Text)) ||
- x.Issued.Contains(searchEnt.Text) || x.Issued.Contains(searchEnt.Text.ToLower()) ||
- x.Issued.Contains(searchEnt.Text.ToUpper()) || x.Issued.Contains(SearchUtils.UpperCaseFirst(searchEnt.Text)) ||
- x.TrimmedIssued.Contains(searchEnt.Text) || x.TrimmedIssued.Contains(searchEnt.Text.ToLower()) ||
- x.TrimmedIssued.Contains(searchEnt.Text.ToUpper()) || x.TrimmedIssued.Contains(SearchUtils.UpperCaseFirst(searchEnt.Text)) ||
- x.TrimmedIssued.Contains(SearchUtils.UpperCaseSecond(searchEnt.Text)) || x.TrimmedIssued.Contains(SearchUtils.UpperCaseThird(searchEnt.Text)) || x.TrimmedIssued.Contains(SearchUtils.UpperCaseFourth(searchEnt.Text))
- );
- listView.ItemsSource = list;
- fileCountLbl.Text = "Files (" + list.Count() + ")";
- }
- });
- }
- #endregion
- #region Loading
- private void TreeView_QueryNodeSize(object sender, Syncfusion.XForms.TreeView.QueryNodeSizeEventArgs e)
- {
- e.Height = e.GetActualNodeHeight();
- e.Handled = true;
- }
- private void LoadFolders(Guid jobid)
- {
- CoreTable table = new Client<JobDocumentSetFolder>().Query(new Filter<JobDocumentSetFolder>(x => x.Job.ID).IsEqualTo(jobid),
- new Columns<JobDocumentSetFolder>(x => x.ID, x => x.Parent.ID, x => x.Name, x => x.Documents));
- foreach (CoreRow row in table.Rows)
- {
- FolderViewItem folderItem = new FolderViewItem
- {
- ID = row.Get<Guid>("ID"),
- ParentID = row.Get<Guid>("Parent.ID"),
- ItemName = row.Get<string>("Name"),
- Documents = row.Get<int>("Documents")
- };
- folderList.Add(folderItem);
- }
- foreach (var folder in folderList)
- {
- folder.List = folderList;
- if (folder.ParentID == Guid.Empty || folder.ParentID == CoreUtils.FullGuid)
- displayList.Add(folder);
- }
- displayList = new ObservableCollection<FolderViewItem>(displayList.OrderBy(x => x.ItemName));
- treeView.ItemsSource = displayList;
- foldersLoaded = true;
- }
- private async void LoadFiles(Filter<JobDocumentSetMileStone> filter, bool specificQuery = false, Guid loadFolder = new Guid())
- {
- await Task.Run(() =>
- {
- try
- {
- filter = filter.And(x => x.Status).IsEqualTo(JobDocumentSetMileStoneStatus.Approved)
- .And(x => x.Type.SiteVisible).IsEqualTo(true);
- CoreTable milestones = new Client<JobDocumentSetMileStone>().Query(filter,
- new Columns<JobDocumentSetMileStone>(
- x => x.ID,
- x => x.DocumentSet.Folder.Name,
- x => x.DocumentSet.Folder.ID,
- x => x.Submitted,
- x => x.Employee.Name,
- x => x.Type.Description,
- x => x.DocumentSet.Description
- )
- );
- if (milestones.Rows.Any())
- {
- var table = QueryFiles(milestones, specificQuery);
- if (table.Rows.Any())
- {
- foreach (CoreRow filerow in table.Rows)
- {
- AddFileShell(filerow, specificQuery);
- }
- Device.BeginInvokeOnMainThread(() =>
- {
- if (specificQuery)
- {
- ShowFiles();
- specificQueryLoading = false;
- loadedFolders.Add(loadFolder);
- AddSearchItems(currentQueryFileShells);
- }
- else
- {
- filesLoaded = true;
- displayList.Insert(0, new FolderViewItem { ItemName = "All", Documents = shells.Count });
- folderList.Insert(0, new FolderViewItem { ItemName = "All", Documents = shells.Count });
- treeView.ItemsSource = null;
- treeView.ItemsSource = displayList;
- }
- });
- }
- }
- else
- {
- specificQueryLoading = false;
- ShowFiles();
- }
- }
- catch
- {
- specificQueryLoading = false;
- ShowFiles();
- }
- });
- }
- private CoreTable QueryFiles(CoreTable milestones, bool specificQuery = false)
- {
- Filter<JobDocumentSetMileStoneFile> filter = new Filter<JobDocumentSetMileStoneFile>(x => x.EntityLink.ID).IsEqualTo(milestones.Rows.FirstOrDefault().Get<Guid>("ID"));
- foreach (CoreRow milestonerow in milestones.Rows)
- {
- Guid id = AddMileStoneShell(milestonerow, specificQuery);
- filter = filter.Or(x => x.EntityLink.ID).IsEqualTo(milestonerow.Get<Guid>("ID"));
- }
- CoreTable table = new Client<JobDocumentSetMileStoneFile>().Query(
- filter,
- new Columns<JobDocumentSetMileStoneFile>(
- x => x.DocumentLink.ID,
- x => x.Thumbnail,
- x => x.DocumentLink.FileName,
- x => x.EntityLink.ID
- ));
- return table;
- }
- private Guid AddMileStoneShell(CoreRow milestonerow, bool specificQuery = false)
- {
- Guid id = milestonerow.Get<Guid>("ID");
- MileStoneShell shell = new MileStoneShell
- {
- ID = milestonerow.Get<Guid>("ID"),
- FolderName = milestonerow.Get<string>("DocumentSet.Folder.Name"),
- FolderID = milestonerow.Get<Guid>("DocumentSet.Folder.ID"),
- EmployeeName = milestonerow.Get<string>("Employee.Name"),
- Type = milestonerow.Get<string>("Type.Description"),
- DocSetDescription = milestonerow.Get<string>("DocumentSet.Description"),
- Issued = "Issued: " + milestonerow.Get<DateTime>("Issued").ToString("dd MMM yy")
- };
- if (specificQuery)
- specificQueryMileStones.Add(shell);
- else
- mileStoneShells.Add(shell);
- return id;
- }
- private void AddFileShell(CoreRow filerow, bool specificQuery = false)
- {
- JobDocSetFileShell file = new JobDocSetFileShell
- {
- DocLinkID = filerow.Get<Guid>("DocumentLink.ID"),
- FileName = filerow.Get<string>("DocumentLink.FileName"),
- MileStoneID = filerow.Get<Guid>("EntityLink.ID")
- };
- MileStoneShell mileStone = mileStoneShells.Find(x => x.ID == file.MileStoneID);
- file.FolderName = mileStone.FolderName;
- file.FolderID = mileStone.FolderID;
- file.EmployeeName = mileStone.EmployeeName;
- file.Type = mileStone.Type;
- file.DocSetDescription = mileStone.DocSetDescription;
- file.Issued = mileStone.Issued;
- file.TrimmedIssued = TrimWhiteSpace(file.Issued);
- byte[] data = filerow.Get<byte[]>("Thumbnail");
- if (data != null)
- {
- file.ImageSource = ImageSource.FromStream(() => new MemoryStream(data));
- if (idiom == DeviceIdiom.Tablet)
- {
- file.HeightRequest = 400;
- file.WidthRequest = 500;
- }
- file.Thumbnail = data;
- }
- if (specificQuery)
- {
- specificQueryFileShells.Add(file);
- currentQueryFileShells.Add(file);
- }
- else
- shells.Add(file);
- }
- private void ShowFiles()
- {
- Device.BeginInvokeOnMainThread(() =>
- {
- treeView.IsEnabled = true;
- loadingLayout.IsVisible = false;
- loadingColumn.Width = 0;
- filesLayout.IsVisible = true;
- filesColumn.Width = new GridLength(1, GridUnitType.Star);
- });
- }
- private void ShowLoading()
- {
- Device.BeginInvokeOnMainThread(async () =>
- {
- treeView.IsEnabled = false;
- filesLayout.IsVisible = false;
- filesColumn.Width = 0;
- loadingLayout.IsVisible = true;
- loadingColumn.Width = new GridLength(1, GridUnitType.Star);
- Random random = new Random();
- uint number = (uint)random.Next(500, 3000);
- await loadingLbl.TranslateTo(0, 15, 500);
- await loadingLbl.TranslateTo(0, 0, 500);
- loadingLbl.RotateTo(360, number);
- await loadingLbl.TranslateTo(0, 15, 500);
- await loadingLbl.TranslateTo(0, 0, 500);
- await loadingLbl.TranslateTo(0, 15, 500);
- await loadingLbl.TranslateTo(0, 0, 500);
- await loadingLbl.TranslateTo(0, 15, 500);
- number = (uint)random.Next(500, 3000);
- await loadingLbl.TranslateTo(0, 0, 500);
- loadingLbl.RotateTo(360, number);
- });
- }
- static string TrimWhiteSpace(string s)
- {
- s = String.Concat(s.Where(c => !Char.IsWhiteSpace(c)));
- return s;
- }
- #endregion
- #region ListView Interaction
- void List_Tapped(object sender, EventArgs e)
- {
- var shell = listView.SelectedItem as JobDocSetFileShell;
- if (Device.RuntimePlatform.Equals(Device.Android) && PRSSecurity.IsAllowed<CanOpenMobileNativePDFViewer>())
- OpenNativeViewer(shell.DocLinkID);
- else
- {
- PDFViewer viewer = new PDFViewer(shell.DocLinkID);
- Navigation.PushAsync(viewer);
- }
- }
- private async void OpenNativeViewer(Guid docID)
- {
- CoreTable table = new Client<Document>().Query(new Filter<Document>(x => x.ID).IsEqualTo(docID),
- new Columns<Document>(x => x.Data, x => x.FileName));
- 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 Image_Tapped(object sender, EventArgs e)
- {
- var shell = ((TappedEventArgs)e).Parameter as JobDocSetFileShell;
- if (shell == null)
- return;
- if (shell.Thumbnail != null)
- {
- Image popupContent = new Image();
- popupContent.HeightRequest = 500;
- popupContent.WidthRequest = 600;
- popupContent.HorizontalOptions = LayoutOptions.FillAndExpand;
- popupContent.VerticalOptions = LayoutOptions.FillAndExpand;
- popupContent.Aspect = Aspect.AspectFit;
- popupContent.Source = ImageSource.FromStream(() => new MemoryStream(shell.Thumbnail));
- popupLayout.PopupView.ShowHeader = false;
- popupLayout.PopupView.ShowFooter = false;
- popupLayout.PopupView.ContentTemplate = new DataTemplate(() =>
- {
- return popupContent;
- });
- popupLayout.Show();
- }
- }
- #endregion
- }
- #region Classes
- public class FolderViewItem : INotifyPropertyChanged
- {
- public event OnFolderListChanged OnFolderListChanged;
- public event PropertyChangedEventHandler PropertyChanged;
- public Guid ID { get; set; }
- public Guid ParentID { get; set; }
- public ImageSource ImageIcon { get; set; }
- private ObservableCollection<FolderViewItem> list;
- public ObservableCollection<FolderViewItem> List
- {
- get
- {
- return list;
- }
- set
- {
- list = value;
- OnFolderListChanged?.Invoke();
- }
- }
- private string itemName;
- public string ItemName
- {
- get { return itemName; }
- set
- {
- itemName = value;
- RaisedOnPropertyChanged("ItemName");
- }
- }
- public int Documents { get; set; }
- public ObservableCollection<FolderViewItem> SubFiles { get; set; }
- public Guid DocID { get; set; }
- public FolderViewItem()
- {
- ID = Guid.Empty;
- ItemName = "";
- ImageIcon = "folder.png";
- DocID = Guid.Empty;
- ParentID = Guid.Empty;
- OnFolderListChanged += FolderViewItem_OnFolderListChanged;
- Documents = 0;
- }
- private void FolderViewItem_OnFolderListChanged()
- {
- GetChildren();
- }
- public void RaisedOnPropertyChanged(string _PropertyName)
- {
- if (PropertyChanged != null)
- {
- PropertyChanged(this, new PropertyChangedEventArgs(_PropertyName));
- }
- }
- private void GetChildren()
- {
- SubFiles = new ObservableCollection<FolderViewItem>(list.Where(x => x.ParentID.Equals(ID)));
- }
- }
- public class MileStoneShell
- {
- public Guid ID { get; set; }
- public string FolderName { get; set; }
- public Guid FolderID { get; set; }
- public string EmployeeName { get; set; }
- public string Type { get; set; }
- public string DocSetDescription { get; set; }
- public string Issued { get; set; }
- public MileStoneShell()
- {
- ID = Guid.Empty;
- FolderID = Guid.Empty;
- FolderName = "";
- EmployeeName = "";
- Type = "";
- DocSetDescription = "";
- Issued = "";
- }
- }
- public class JobDocSetFileShell
- {
- public Guid DocLinkID { get; set; }
- public string FileName { get; set; }
- public string FolderName { get; set; }
- public Guid FolderID { get; set; }
- public string EmployeeName { get; set; }
- public string Type { get; set; }
- public string DocSetDescription { get; set; }
- public string Issued { get; set; }
- public string TrimmedIssued { get; set; }
- public ImageSource ImageSource { get; set; }
- public double HeightRequest { get; set; }
- public double WidthRequest { get; set; }
- public byte[] Thumbnail { get; set; }
- public Guid MileStoneID { get; set; }
- public JobDocSetFileShell()
- {
- DocLinkID = Guid.Empty;
- FileName = "";
- FolderName = "";
- EmployeeName = "";
- Type = "";
- DocSetDescription = "";
- ImageSource = null;
- Issued = "";
- TrimmedIssued = "";
- HeightRequest = 150;
- WidthRequest = 200;
- Thumbnail = null;
- MileStoneID = Guid.Empty;
- FolderID = Guid.Empty;
- }
- }
- #endregion
- }
|