SiteModule.xaml.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. using System;
  2. using System.Linq;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using Comal.Classes;
  6. using Comal.Classes.SecurityDescriptors;
  7. using InABox.Configuration;
  8. using Xamarin.Forms.Xaml;
  9. using InABox.Core;
  10. using InABox.Mobile;
  11. using InABox.Mobile.Shared;
  12. namespace PRS.Mobile
  13. {
  14. [XamlCompilation(XamlCompilationOptions.Compile)]
  15. public partial class SiteModule
  16. {
  17. private JobShell _job;
  18. private SiteModuleSettings _settings;
  19. private CancellationTokenSource _pollingToken;
  20. public SiteModule()
  21. {
  22. _settings = new LocalConfiguration<SiteModuleSettings>().Load();
  23. InitializeComponent();
  24. ProgressVisible = true;
  25. SiteDocuments.IsVisible = Security.IsAllowed<CanViewJobDocumentMileStoneFiles>();
  26. // ** Not Yet Implemented **
  27. //CanApproveJobRequisitions.IsVisible = InABox.Core.Security.CanView<PurchaseOrder>()
  28. Manufacturing.IsVisible = Security.IsAllowed<CanViewManufacturingOnMobile>();
  29. JobTasks.IsVisible = Security.IsAllowed<CanViewOthersTasks>();
  30. JobRequis.IsVisible = Security.IsAllowed<ViewMobileStoreRequisModule>();
  31. Task.Run(() =>
  32. {
  33. App.Data.Jobs.Refresh(false);
  34. _job = App.Data.Jobs.FirstOrDefault(x => x.ID == _settings.JobID);
  35. }).BeginInvokeOnMainThread((task) =>
  36. {
  37. SelectJob.Text = _job?.DisplayName ?? "Select Job";
  38. SiteForms.Alert = "";
  39. SelectJob.IsEnabled = true;
  40. EnableModules();
  41. ProgressVisible = false;
  42. });
  43. }
  44. protected override void OnAppearing()
  45. {
  46. _pollingToken = new CancellationTokenSource();
  47. StartMonitoringITPForms();
  48. StartMonitoringForms();
  49. StartMonitoringTasks();
  50. base.OnAppearing();
  51. }
  52. protected override void OnDisappearing()
  53. {
  54. base.OnDisappearing();
  55. _pollingToken.Cancel();
  56. }
  57. private void StartMonitoringITPForms()
  58. {
  59. var token = _pollingToken.Token;
  60. Task.Run(() =>
  61. {
  62. while (!_pollingToken.Token.IsCancellationRequested)
  63. {
  64. Guid jobid = _job?.ID ?? Guid.Empty;
  65. if (App.Data.IsConnected() && (jobid != Guid.Empty))
  66. {
  67. var _model = new JobITPFormModel(App.Data,
  68. () => new Filter<JobITPForm>(x => x.Parent.Job.ID).IsEqualTo(jobid).And(x=>x.Form.ID).IsNotEqualTo(Guid.Empty)
  69. );
  70. _model.Refresh(true);
  71. Dispatcher.BeginInvokeOnMainThread(() =>
  72. {
  73. int count = _model.Items.Count(x => x.Completed.IsEmpty());
  74. SiteITPForms.Alert = count > 0
  75. ? count.ToString()
  76. : "";
  77. });
  78. }
  79. else
  80. Dispatcher.BeginInvokeOnMainThread(() =>
  81. {
  82. SiteITPForms.Alert = "";
  83. });
  84. Task.Delay(TimeSpan.FromSeconds(30), token)
  85. .Wait(token);
  86. }
  87. },
  88. token
  89. );
  90. }
  91. private void StartMonitoringForms()
  92. {
  93. var token = _pollingToken.Token;
  94. Task.Run(() =>
  95. {
  96. while (!_pollingToken.Token.IsCancellationRequested)
  97. {
  98. Guid jobid = _job?.ID ?? Guid.Empty;
  99. if (App.Data.IsConnected() && (jobid != Guid.Empty))
  100. {
  101. JobFormModel _model = new JobFormModel(App.Data,
  102. () => new Filter<JobForm>(x => x.Parent.ID).IsEqualTo(jobid));
  103. _model.Refresh(true);
  104. Dispatcher.BeginInvokeOnMainThread(() =>
  105. {
  106. int count = _model.Items.Count(x => x.Completed.IsEmpty());
  107. SiteForms.Alert = count > 0
  108. ? count.ToString()
  109. : "";
  110. });
  111. }
  112. else
  113. Dispatcher.BeginInvokeOnMainThread(() =>
  114. {
  115. SiteForms.Alert = "";
  116. });
  117. Task.Delay(TimeSpan.FromSeconds(30), token)
  118. .Wait(token);
  119. }
  120. },
  121. token
  122. );
  123. }
  124. private void StartMonitoringTasks()
  125. {
  126. var token = _pollingToken.Token;
  127. Task.Run(
  128. async () =>
  129. {
  130. while (!_pollingToken.Token.IsCancellationRequested)
  131. {
  132. Guid jobid = _job?.ID ?? Guid.Empty;
  133. if (App.Data.IsConnected() && (jobid != Guid.Empty))
  134. {
  135. var _model = new JobKanbanModel(
  136. App.Data,
  137. () => new Filter<Kanban>(x => x.JobLink.ID).IsEqualTo(jobid)
  138. );
  139. _model.Refresh(true);
  140. Dispatcher.BeginInvokeOnMainThread(() =>
  141. {
  142. int count = _model.Items.Count(x=>x.Status == KanbanStatus.Open);
  143. JobTasks.Alert = count > 0
  144. ? count.ToString()
  145. : "";
  146. });
  147. }
  148. else
  149. Dispatcher.BeginInvokeOnMainThread(() =>
  150. {
  151. JobTasks.Alert = "";
  152. });
  153. Task.Delay(TimeSpan.FromSeconds(30), token)
  154. .Wait(token);
  155. }
  156. },
  157. token
  158. );
  159. }
  160. protected override void UpdateTransportStatus()
  161. {
  162. base.UpdateTransportStatus();
  163. EnableModules();
  164. }
  165. private void EnableModules()
  166. {
  167. foreach (var menu in Menu.Items)
  168. menu.IsEnabled = App.Data.IsConnected() && (_job != null);
  169. }
  170. private void SelectJobBtn_Clicked(object sender, EventArgs e)
  171. {
  172. SelectionPage jobs = new JobSelectionPage(
  173. (job) =>
  174. {
  175. var _sitesettings = new LocalConfiguration<SiteDocumentsSettings>().Load();
  176. _sitesettings.FolderID = Guid.Empty;
  177. new LocalConfiguration<SiteDocumentsSettings>().Save(_sitesettings);
  178. new LocalConfiguration<SiteModuleSettings>().Save(new SiteModuleSettings() { JobID = job.ID });
  179. _job = job;
  180. SelectJob.Text = _job?.DisplayName ?? "Select Job";
  181. SiteForms.Alert = "";
  182. });
  183. Navigation.PushAsync(jobs);
  184. }
  185. private void SiteITPForms_OnTapped(MobileModuleItem sender, ModuleMenuItemTappedArgs args)
  186. {
  187. Navigation.PushAsync(new SiteITPs(_job));
  188. }
  189. private void SiteDocuments_OnTapped(MobileModuleItem sender, ModuleMenuItemTappedArgs args)
  190. {
  191. Navigation.PushAsync(new SiteDocuments(_job));
  192. }
  193. private void SiteForms_OnTapped(MobileModuleItem sender, ModuleMenuItemTappedArgs args)
  194. {
  195. Navigation.PushAsync(new SiteForms(_job));
  196. }
  197. private void GeneralDocuments_OnTapped(MobileModuleItem sender, ModuleMenuItemTappedArgs args)
  198. {
  199. Navigation.PushAsync(new JobWebDocuments(_job));
  200. }
  201. // private void JobRequisitions_OnTapped(ModuleMenuItem sender, ModuleMenuItemTappedArgs args)
  202. // {
  203. // throw new NotImplementedException();
  204. // }
  205. private void Manufacturing_OnTapped(MobileModuleItem sender, ModuleMenuItemTappedArgs args)
  206. {
  207. Navigation.PushAsync(new SiteManufacturing(_job));
  208. }
  209. private void JobTasks_OnTapped(MobileModuleItem sender, ModuleMenuItemTappedArgs args)
  210. {
  211. var model = new JobKanbanModel(
  212. App.Data,
  213. () => new Filter<Kanban>(x => x.JobLink.ID).IsEqualTo(_job?.ID ?? CoreUtils.FullGuid)
  214. );
  215. model.FileName = $"{_job.ID}.tasks";
  216. model.ItemAdded += (o, createdArgs) =>
  217. {
  218. createdArgs.Item.JobID = _job.ID;
  219. createdArgs.Item.JobNumber = _job.JobNumber;
  220. createdArgs.Item.JobName = _job.Name;
  221. };
  222. var jobtasks = new KanbanList()
  223. {
  224. Model = model,
  225. Title = "Job Tasks"
  226. };
  227. Navigation.PushAsync(jobtasks);
  228. }
  229. private void JobRequis_OnTapped(MobileModuleItem sender, ModuleMenuItemTappedArgs args)
  230. {
  231. Navigation.PushAsync(new RequisitionList(_job, RequisitionEditMode.EditRequest));
  232. }
  233. }
  234. }