SiteDocuments.xaml.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using Comal.Classes;
  7. using InABox.Configuration;
  8. using InABox.Core;
  9. using InABox.Mobile;
  10. using InABox.Mobile.Shared;
  11. using Xamarin.Essentials;
  12. using Xamarin.Forms;
  13. using Xamarin.Forms.Xaml;
  14. namespace PRS.Mobile
  15. {
  16. public class SiteDocumentsSettings : BaseObject, ILocalConfigurationSettings
  17. {
  18. public Guid CategoryID { get; set; }
  19. public Guid TypeID { get; set; }
  20. public Guid DisciplineID { get; set; }
  21. public Guid ItpID { get; set; }
  22. public Guid FolderID { get; set; }
  23. }
  24. public class SiteDocumentCachedColorConverter : AbstractConverter<JobDocumentShell, Color>
  25. {
  26. protected override Color Convert(JobDocumentShell value, object? parameter = null)
  27. {
  28. return App.Data.IsConnected() || CoreRepository.IsCached($"{value.ID}.document")
  29. ? Color.White
  30. : Color.Gainsboro;
  31. }
  32. }
  33. public class SiteDocumentCachedImageConverter : AbstractConverter<JobDocumentShell, ImageSource>
  34. {
  35. private static readonly ImageSource _cloud = ImageSource.FromFile("cloud");
  36. private static readonly ImageSource _cache = ImageSource.FromFile("cache");
  37. protected override ImageSource Convert(JobDocumentShell value, object? parameter = null)
  38. {
  39. return value == null
  40. ? null
  41. : CoreRepository.IsCached($"{value.ID}.document")
  42. ? _cache
  43. : _cloud;
  44. }
  45. }
  46. [XamlCompilation(XamlCompilationOptions.Compile)]
  47. public partial class SiteDocuments
  48. {
  49. private readonly SiteDocumentsSettings _settings;
  50. private readonly JobDocumentModel _documentmodel;
  51. private readonly JobDocumentFolderModel _foldermodel;
  52. public SiteDocuments(JobShell job) : base(job)
  53. {
  54. _documentmodel = new JobDocumentModel(App.Data,() =>
  55. new Filter<JobDocumentSetMileStoneFile>(x=>x.EntityLink.DocumentSet.Job.ID).IsEqualTo(Job.ID)
  56. .And(x=>x.EntityLink.Type.SiteVisible).IsEqualTo(true)
  57. .And(x=>x.EntityLink.Status).IsEqualTo(JobDocumentSetMileStoneStatus.Approved),
  58. () => $"JobDocuments_{Job.ID}"
  59. );
  60. _foldermodel = new JobDocumentFolderModel(App.Data, () =>
  61. new Filter<JobDocumentSetFolder>(x => x.Job.ID).IsEqualTo(Job.ID),
  62. () => $"JobFolders_{Job.ID}"
  63. );
  64. _settings = new LocalConfiguration<SiteDocumentsSettings>().Load();
  65. InitializeComponent();
  66. ProgressVisible = true;
  67. RefreshData(false,true);
  68. }
  69. private void RefreshData(bool force, bool async)
  70. {
  71. if (async)
  72. {
  73. Task[] tasks = new Task[]
  74. {
  75. Task.Run(() => _documentmodel.Refresh(force)),
  76. Task.Run(() => _foldermodel.Refresh(force))
  77. };
  78. Task.WhenAll(tasks).BeginInvokeOnMainThread((_) => RefreshScreen());
  79. }
  80. else
  81. {
  82. _documentmodel.Refresh(force);
  83. RefreshScreen();
  84. }
  85. }
  86. protected override void OnAppearing()
  87. {
  88. base.OnAppearing();
  89. RefreshScreen();
  90. }
  91. private readonly List<Guid> _folders = new List<Guid>();
  92. private void RefreshScreen()
  93. {
  94. _folders.Clear();
  95. var flist = _foldermodel.DocumentFolders.GetChildren(_settings.FolderID).Select(x=>x.ID).ToList();
  96. _folders.AddRange(flist);
  97. _files.ItemsSource = null;
  98. _documentmodel.Search(FilterShell);
  99. _files.ItemsSource = _documentmodel.Items;
  100. _files.LastUpdated = _documentmodel.LastUpdated;
  101. Title = Job.DisplayName;
  102. ProgressVisible = false;
  103. }
  104. private bool FilterShell(JobDocumentShell x)
  105. {
  106. var result =
  107. ((_settings.CategoryID == Guid.Empty) || (x.CategoryID == _settings.CategoryID))
  108. && ((_settings.DisciplineID == Guid.Empty) || (x.DisciplineID == _settings.DisciplineID))
  109. && ((_settings.TypeID == Guid.Empty) || (x.TypeID == _settings.TypeID))
  110. && ((_settings.ItpID == Guid.Empty) || (x.ItpID == _settings.ItpID))
  111. && ((_settings.FolderID == Guid.Empty) || (_settings.FolderID == CoreUtils.FullGuid) || _folders.Contains(x.FolderID))
  112. && (String.IsNullOrWhiteSpace(_currentFilter)
  113. || x.FileName.ToUpper().Contains(_currentFilter)
  114. || x.Name.ToUpper().Contains(_currentFilter)
  115. || x.Code.ToUpper().Contains(_currentFilter)
  116. );
  117. return result;
  118. }
  119. private void SiteDocument_Clicked(object sender, EventArgs e)
  120. {
  121. if ((sender is MobileCard card) && (card.BindingContext is JobDocumentShell shell))
  122. {
  123. ProgressVisible = true;
  124. Navigation.PushAsync(new DocumentPage() { DocumentID = shell.ID });
  125. RefreshScreen();
  126. ProgressVisible = false;
  127. }
  128. }
  129. private void _filter_Clicked(object sender, MobileMenuButtonClickedEventArgs args)
  130. {
  131. var filterpage = new SiteDocumentsFilter(_documentmodel, _settings, Job.ID, _foldermodel);
  132. filterpage.Changed += (_,_) =>
  133. {
  134. new LocalConfiguration<SiteDocumentsSettings>().Save(_settings);
  135. RefreshScreen();
  136. };
  137. Navigation.PushAsync(filterpage);
  138. }
  139. private string _currentFilter;
  140. private void _search_OnTextChanged(object sender, MobileSearchBarTextChangedArgs args)
  141. {
  142. _currentFilter = args.Text.ToUpper();
  143. RefreshScreen();
  144. }
  145. private void _files_OnRefreshRequested(object sender, MobileListRefreshEventArgs args)
  146. {
  147. RefreshData(true,false);
  148. RefreshScreen();
  149. }
  150. }
  151. }