SiteModule.xaml.cs 9.3 KB

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