MainPage.xaml.cs 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using Xamarin.Forms;
  8. using Xamarin.Forms.Maps;
  9. using InABox.Core;
  10. using InABox.Configuration;
  11. using InABox.Clients;
  12. using InABox.Mobile;
  13. using Comal.Classes;
  14. using XF.Material.Forms.UI.Dialogs;
  15. using comal.timesheets.CustomControls;
  16. using comal.timesheets.StoreRequis;
  17. using PRSSecurity = InABox.Core.Security;
  18. using Plugin.LocalNotification;
  19. using comal.timesheets.Tasks;
  20. using System.IO;
  21. namespace comal.timesheets
  22. {
  23. public partial class MainPage : ContentPage
  24. {
  25. #region Fields
  26. List<ToolEntry> toolEntries = new List<ToolEntry>();
  27. private TimeSheet _timesheet = null;
  28. private Employee _employee = null;
  29. private CoreTable _jobs = null;
  30. public bool SettingsChanged { get; private set; }
  31. bool bUpdatingTimesheet = false;
  32. bool firstLoad = true;
  33. bool recentlyAskedToUpdate = true;
  34. int updateCounter;
  35. public static ConnectionSettings connectionSettings = null;
  36. bool midnightTimerOn = false;
  37. DateTime oneSecondBeforeMidnight = DateTime.Today.AddSeconds(864399);
  38. bool clockedOffInLast5Seconds = false;
  39. bool bRecentlyUpdatedTiles = false;
  40. bool bSharedDeviceFirstLoad = true;
  41. bool bSharedDevice = false;
  42. int NumberOfNotfications = 0;
  43. private Job _job = new Job();
  44. string deviceName = "";
  45. string matchedDeviceName = "";
  46. int notCount = 1;
  47. #endregion
  48. #region Constructor
  49. public MainPage()
  50. {
  51. InitializeComponent();
  52. try
  53. {
  54. App.GPS.OnLocationFound += LocationFound;
  55. App.GPS.OnLocationError += LocationError;
  56. App.Bluetooth.OnScanFinished += ScanFinished;
  57. App.Data.DataChanged += DataChanged;
  58. App.Data.DataRefreshed += DataRefreshed;
  59. GlobalVariables.EmpID = GlobalVariables.GetEmployeeID();
  60. GlobalVariables.EmpName = GlobalVariables.GetEmployeeName();
  61. MessagingCenter.Subscribe<App>(this, App.MessageOnResume,
  62. (o) =>
  63. {
  64. if (!App.GPS.RecentlyLocated)
  65. App.GPS.GetLocation();
  66. RefreshScreen();
  67. }
  68. );
  69. _timesheet = App.Data.TimeSheets?.Rows.FirstOrDefault()?.ToObject<TimeSheet>();
  70. _employee = App.Data.Employee;
  71. _jobs = App.Data.Jobs;
  72. deviceName = MobileUtils.GetDeviceID();
  73. LoadCacheLists();
  74. InitToolEntryList();
  75. Timer t = new Timer(RecentlyAskedToUpdateTimer, null, 600000, 600000); //user is reminded to update when opening screen after timer of 10 minutes
  76. updateCounter = 1; //user is forced to update after 3rd reminder
  77. Timer t1 = new Timer(RecentlyUpdatedTilesTimer, null, 30000, 30000);
  78. //bluetooth data is allowed to upload once every minute, notifications refreshing is piggybacked to this too
  79. InitNotificationCentre();
  80. firstLoad = false;
  81. //if (GlobalVariables.EmpID == Guid.Parse("40f6ccd9-5272-4b1a-99bf-de7542205aac"))
  82. // RunCustomScript();
  83. NotifyChanges();
  84. SendErrors();
  85. }
  86. catch (Exception e)
  87. {
  88. }
  89. NavigationPage.SetHasBackButton(this, false);
  90. }
  91. private void SendErrors()
  92. {
  93. try
  94. {
  95. const string errorFilename = "Fatal.log";
  96. var libraryPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
  97. var errorFilePath = Path.Combine(libraryPath, errorFilename);
  98. if (!File.Exists(errorFilePath))
  99. {
  100. return;
  101. }
  102. var errorText = File.ReadAllText(errorFilePath);
  103. Notification n = new Notification();
  104. n.Title = "Error log from " + GlobalVariables.EmpName;
  105. n.Description = errorText;
  106. n.Employee.ID = Guid.Parse("40f6ccd9-5272-4b1a-99bf-de7542205aac");
  107. new Client<Notification>().Save(n, "Error from mobile");
  108. File.Delete(errorFilePath);
  109. }
  110. catch { }
  111. }
  112. private void RunCustomScript()
  113. {
  114. }
  115. private void NotifyChanges()
  116. {
  117. string latestChanges = "";
  118. List<string> changes = new List<string>
  119. {
  120. "- Upgrade to Setouts Module"
  121. };
  122. foreach (string s in changes)
  123. {
  124. latestChanges = s + System.Environment.NewLine + latestChanges;
  125. }
  126. Task.Run(() =>
  127. {
  128. if (App.Current.Properties.Count > 0)
  129. {
  130. List<string> toDelete = new List<string>();
  131. foreach (string s in App.Current.Properties.Keys)
  132. {
  133. if (s.Contains("NotifiedOfChanges"))
  134. {
  135. if (!s.Equals("NotifiedOfChanges" + MobileUtils.AppVersion.InstalledVersionNumber))
  136. {
  137. toDelete.Add(s);
  138. }
  139. }
  140. }
  141. foreach (string s in toDelete)
  142. {
  143. App.Current.Properties.Remove(s);
  144. }
  145. }
  146. if (!App.Current.Properties.ContainsKey("NotifiedOfChanges" + MobileUtils.AppVersion.InstalledVersionNumber))
  147. {
  148. App.Current.Properties.Add("NotifiedOfChanges" + MobileUtils.AppVersion.InstalledVersionNumber, "True");
  149. Thread.Sleep(7500);
  150. Device.BeginInvokeOnMainThread(() =>
  151. {
  152. DisplayAlert("Latest Changes", latestChanges
  153. , "OK");
  154. });
  155. }
  156. });
  157. }
  158. #endregion
  159. #region OnAppearing and Display
  160. protected override void OnAppearing()
  161. {
  162. if (!App.IsUserLoggedIn)
  163. {
  164. Navigation.PopAsync();
  165. return;
  166. }
  167. //if (Application.Current.Properties.ContainsKey("IsSharedDevice"))
  168. //{
  169. // if (Application.Current.Properties["IsSharedDevice"].Equals("True"))
  170. // {
  171. // bSharedDevice = true;
  172. // if (!bSharedDeviceFirstLoad)
  173. // {
  174. // App.LogoutUser();
  175. // Navigation.PopToRootAsync();
  176. // }
  177. // else
  178. // {
  179. // bSharedDeviceFirstLoad = false;
  180. // clockOnButton.IsEnabled = false;
  181. // clockOnButton.Text = "Shared Device";
  182. // clockOnButton.BackgroundColor = Color.CornflowerBlue;
  183. // CurrentLocation.IsVisible = false;
  184. // jobBtn.IsVisible = false;
  185. // addNoteBtn.IsVisible = false;
  186. // Grid.SetRowSpan(flexLayoutScrollView, 3);
  187. // Grid.SetRow(flexLayoutScrollView, 1);
  188. // Timer t = new Timer(AutoLogoutUser, null, 600000, Timeout.Infinite);
  189. // }
  190. // }
  191. // else
  192. // bSharedDevice = false;
  193. //}
  194. //getting strange results from .IsAllowed
  195. //if (!PRSSecurity.IsAllowed<CanBypassTimeBench>())
  196. //{
  197. // clockOnButton.IsVisible = false;
  198. // jobBtn.IsVisible = false;
  199. // addNoteBtn.IsVisible = false;
  200. // CurrentLocation.IsVisible = false;
  201. // row0.Height = 0;
  202. // row1.Height = 0;
  203. // row2.Height = 0;
  204. // ForceLayout();
  205. //}
  206. if (!firstLoad)
  207. RefreshScreen();
  208. Task.Run(async () =>
  209. {
  210. bool isLatest = true;
  211. try
  212. {
  213. isLatest = await MobileUtils.AppVersion.IsUsingLatestVersion();
  214. }
  215. catch (Exception eLatest)
  216. {
  217. if (!recentlyAskedToUpdate)
  218. {
  219. Device.BeginInvokeOnMainThread(() =>
  220. {
  221. });
  222. recentlyAskedToUpdate = true;
  223. }
  224. string s = eLatest.Message;
  225. }
  226. if (!isLatest)
  227. {
  228. if (!recentlyAskedToUpdate)
  229. {
  230. string latestVersionNumber = await MobileUtils.AppVersion.GetLatestVersionNumber();
  231. if (updateCounter < 3)
  232. {
  233. Device.BeginInvokeOnMainThread(async () =>
  234. {
  235. string chosenOption = await DisplayActionSheet(String.Format("Version {0} Available. Update now?", latestVersionNumber), "You will be reminded again in 10 minutes.", null, "Yes", "No");
  236. switch (chosenOption)
  237. {
  238. case "No":
  239. break;
  240. case "Cancel":
  241. break;
  242. case "Yes":
  243. Dispatcher.BeginInvokeOnMainThread(() => { MobileUtils.AppVersion.OpenAppInStore(); });
  244. break;
  245. default:
  246. break;
  247. }
  248. });
  249. }
  250. else if (updateCounter >= 3)
  251. {
  252. Device.BeginInvokeOnMainThread(() =>
  253. {
  254. DisplayAlert(String.Format("Version {0} Available", latestVersionNumber), "Please update your software to the latest version.", "OK")
  255. .ContinueWith((Task task) =>
  256. {
  257. Dispatcher.BeginInvokeOnMainThread(() => { MobileUtils.AppVersion.OpenAppInStore(); });
  258. });
  259. });
  260. }
  261. recentlyAskedToUpdate = true;
  262. updateCounter++;
  263. }
  264. }
  265. });
  266. base.OnAppearing();
  267. }
  268. private void RecentlyAskedToUpdateTimer(object o)
  269. {
  270. recentlyAskedToUpdate = false;
  271. }
  272. private void AutoLogoutUser(object o)
  273. {
  274. App.LogoutUser();
  275. Navigation.PopToRootAsync();
  276. }
  277. private void RefreshScreen()
  278. {
  279. //if (bSharedDevice)
  280. // return;
  281. try
  282. {
  283. Device.BeginInvokeOnMainThread(() =>
  284. {
  285. homeScreenGrid.RaiseChild(CurrentLocation);
  286. bBusy = true;
  287. if (GlobalVariables.EmpID == Guid.Empty)
  288. {
  289. GlobalVariables.EmpID = GlobalVariables.GetEmployeeID();
  290. GlobalVariables.EmpName = GlobalVariables.GetEmployeeName();
  291. }
  292. clockOnButton.IsEnabled = false;
  293. bool PRSReady = (App.Data.Employee != null) && (App.Data.TimeSheets != null);
  294. bool GateReady = CheckLocation();
  295. CurrentLocation.Text = DisplayAddress();
  296. if (CurrentLocation.Text.Contains("ERROR"))
  297. CurrentLocation.Text = "Unknown Address";
  298. Title = null;
  299. Title = App.Data.Employee != null ? App.Data.Employee.Name : "";
  300. CoreRow timesheet = App.Data.TimeSheets?.Rows.FirstOrDefault();
  301. clockOnButton.Text = PRSReady && GateReady ? timesheet == null ? "CLOCK ON" : "CLOCK OFF" : "PLEASE WAIT";
  302. clockOnButton.IsEnabled = PRSReady && GateReady;
  303. clockOnButton.BackgroundColor = PRSReady && GateReady ? timesheet == null ? Color.FromHex("#e6e6fa") : Color.FromHex("#15C7C1") : Color.Gainsboro;
  304. clockOnButton.BorderColor = PRSReady && GateReady ? timesheet == null ? Color.Black : Color.FromHex("#15C7C1") : Color.Gainsboro;
  305. if (clockOnButton.Text == "CLOCK OFF")
  306. {
  307. addNoteBtn.IsEnabled = true;
  308. if (GlobalVariables.JobsLoaded)
  309. jobBtn.IsEnabled = true;
  310. }
  311. else
  312. {
  313. addNoteBtn.IsEnabled = false;
  314. jobBtn.IsEnabled = false;
  315. }
  316. if (firstLoad)
  317. RefreshJobFromTimeSheet(timesheet);
  318. homeScreenGrid.RaiseChild(CurrentLocation);
  319. firstLoad = false;
  320. bBusy = false;
  321. });
  322. }
  323. catch (Exception e)
  324. {
  325. }
  326. //CurrentLocation.IsEnabled = PRSReady && GateReady;
  327. }
  328. private void RefreshJobFromTimeSheet(CoreRow timesheet)
  329. {
  330. Guid jobid = timesheet == null ? Guid.Empty : timesheet.Get<TimeSheet, Guid>(x => x.JobLink.ID);
  331. if (!jobid.Equals(Guid.Empty))
  332. {
  333. jobBtn.Text = String.Format("{0}: {1}", timesheet.Get<TimeSheet, String>(x => x.JobLink.JobNumber), timesheet.Get<TimeSheet, String>(x => x.JobLink.Name));
  334. _job.ID = timesheet == null ? Guid.Empty : timesheet.Get<TimeSheet, Guid>(x => x.JobLink.ID);
  335. _job.JobNumber = timesheet == null ? String.Empty : timesheet.Get<TimeSheet, String>(x => x.JobLink.JobNumber);
  336. _job.Name = timesheet == null ? String.Empty : timesheet.Get<TimeSheet, String>(x => x.JobLink.Name);
  337. }
  338. else
  339. {
  340. jobBtn.Text = "No Job Selected";
  341. _job = new Job();
  342. }
  343. }
  344. #endregion
  345. #region Clock on/off
  346. private void DataChanged(object sender, Type type, Exception e)
  347. {
  348. //if (bSharedDevice)
  349. // return;
  350. Device.BeginInvokeOnMainThread(() =>
  351. {
  352. if (e != null)
  353. {
  354. //DisplayAlert("Connection error with server - double check your connection", e.Message, "OK");
  355. }
  356. else
  357. RefreshScreen();
  358. });
  359. }
  360. private void DataRefreshed()
  361. {
  362. //if (bSharedDevice)
  363. // return;
  364. Device.BeginInvokeOnMainThread(() => { RefreshScreen(); });
  365. }
  366. bool bBusy = false;
  367. async void ClockOnOff_Clicked(object sender, System.EventArgs e)
  368. {
  369. if (bBusy)
  370. return;
  371. bBusy = true;
  372. string chosenOption = "Continue";
  373. if (clockOnButton.Text == "CLOCK OFF")
  374. {
  375. chosenOption = await DisplayActionSheet("Clock off?", "Cancel", null, "Continue", "Cancel");
  376. }
  377. switch (chosenOption)
  378. {
  379. case "Continue":
  380. break;
  381. case "Cancel":
  382. bBusy = false;
  383. return;
  384. break;
  385. default:
  386. bBusy = false;
  387. return;
  388. break;
  389. }
  390. if (clockOnButton.Text == "CLOCK ON" && clockedOffInLast5Seconds)
  391. {
  392. bBusy = false;
  393. return;
  394. }
  395. try
  396. {
  397. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading"))
  398. {
  399. InABox.Core.Location here = new InABox.Core.Location()
  400. {
  401. Latitude = App.GPS.Latitude,
  402. Longitude = App.GPS.Longitude,
  403. Timestamp = DateTime.Now
  404. };
  405. TimeSheet timesheet = App.Data.TimeSheets?.Rows.FirstOrDefault()?.ToObject<TimeSheet>();
  406. if (timesheet != null)
  407. {
  408. if (timesheet.ID != Guid.Empty)
  409. {
  410. if (ZeroLengthTimesheet())
  411. {
  412. bUpdatingTimesheet = true;
  413. new Client<TimeSheet>().Delete(timesheet, "Deleted due to zero duration timesheet");
  414. App.Data.TimeSheets.Rows.Clear();
  415. }
  416. else
  417. {
  418. FinishTimeSheet(timesheet, here);
  419. }
  420. }
  421. }
  422. else
  423. {
  424. Guid jobid = Guid.Empty;
  425. String jobnumber = "";
  426. String jobname = "";
  427. if (!App.Data.CanBypassGates)
  428. {
  429. CoreRow row = App.Data.Gates.Rows.FirstOrDefault(r => App.Bluetooth.Devices.Contains(r.Get<JobTracker, String>(c => c.TrackerLink.DeviceID)));
  430. if (row != null)
  431. {
  432. jobid = row.Get<JobTracker, Guid>(x => x.JobLink.ID);
  433. jobnumber = row.Get<JobTracker, String>(x => x.JobLink.JobNumber);
  434. jobname = row.Get<JobTracker, String>(x => x.JobLink.Name);
  435. }
  436. CreateTimeSheet(jobid, jobnumber, jobname, App.Data.Employee.UsualActivity.ID, App.Data.Employee.UsualActivity.Code, App.Data.Employee.UsualActivity.Description, here, App.GPS.Address, "Clocking On");
  437. }
  438. else
  439. {
  440. if ((!App.GPS.Latitude.Equals(0.0F)) && (!App.GPS.Longitude.Equals(0.0F)))
  441. {
  442. ChooseNearbyJob(here);
  443. }
  444. }
  445. }
  446. RefreshScreen();
  447. }
  448. }
  449. catch (Exception e2)
  450. {
  451. }
  452. bBusy = false;
  453. }
  454. #endregion
  455. #region Bluetooth
  456. private async void UploadTiles()
  457. {
  458. try
  459. {
  460. if (App.GPS.Latitude.Equals(0.0F) && App.GPS.Longitude.Equals(0.0F))
  461. return;
  462. if (App.Bluetooth.DetectedBlueToothMACAddresses.Count == 0)
  463. return;
  464. if (bRecentlyUpdatedTiles)
  465. return;
  466. bRecentlyUpdatedTiles = true;
  467. await Task.Run(() =>
  468. {
  469. InABox.Core.Location curlocation = new InABox.Core.Location() { Latitude = App.GPS.Latitude, Longitude = App.GPS.Longitude };
  470. curlocation.Timestamp = DateTime.Now;
  471. List<GPSTrackerLocation> trackersToUpdate = new List<GPSTrackerLocation>();
  472. foreach (String id in App.Bluetooth.DetectedBlueToothMACAddresses)
  473. {
  474. GPSTracker tracker = GlobalVariables.GPSTrackerCache.Find(x => x.DeviceID.Equals(id));
  475. bool stale = tracker.Location.Timestamp < DateTime.Now.Subtract(new TimeSpan(0, 5, 0));
  476. bool moved = tracker.Location.DistanceTo(curlocation, UnitOfLength.Kilometers) > 0.1;
  477. if (stale || moved)
  478. {
  479. GlobalVariables.GPSTrackerCache.Remove(tracker);
  480. tracker.Location = curlocation;
  481. GlobalVariables.GPSTrackerCache.Add(tracker);
  482. //cache is updated
  483. GPSTrackerLocation gpsTrackerLocation = new GPSTrackerLocation();
  484. gpsTrackerLocation.DeviceID = tracker.DeviceID;
  485. gpsTrackerLocation.Location.Timestamp = tracker.Location.Timestamp;
  486. gpsTrackerLocation.Location = curlocation;
  487. trackersToUpdate.Add(gpsTrackerLocation);
  488. }
  489. }
  490. if (trackersToUpdate.Any())
  491. {
  492. if (ClientFactory.UserGuid != Guid.Empty)
  493. new Client<GPSTrackerLocation>().Save(trackersToUpdate, "Updating Bluetooth Device Locations");
  494. }
  495. App.Bluetooth.DetectedBlueToothMACAddresses.Clear();
  496. }
  497. );
  498. }
  499. catch (Exception e)
  500. {
  501. }
  502. //if ((master != null) && (master.Location.Timestamp < DateTime.Now.Subtract(new TimeSpan(0, 15, 0))))
  503. //{
  504. // GPSTrackerLocation device = new GPSTrackerLocation();
  505. // device.DeviceID = MobileUtils.GetDeviceID();
  506. // device.Location.Latitude = App.GPS.Latitude;
  507. // device.Location.Longitude = App.GPS.Longitude;
  508. // device.Location.Timestamp = DateTime.Now;
  509. // locations.Add(device);
  510. // //device.BatteryLevel = ((double)CrossBattery.Current.RemainingChargePercent);
  511. // //new Client<GPSTrackerLocation>().Save(device, "Updating Device Location"); //, SaveTrackerCallback);
  512. //}
  513. #region OLD
  514. //for (int i = 0; i < App.Bluetooth.Devices.Length; i++)
  515. //{
  516. // String id = App.Bluetooth.Devices[i];
  517. // int level = App.Bluetooth.BatteryLevels[i];
  518. // var btmaster = trackers.FirstOrDefault(x => x.DeviceID.Equals(id));
  519. // if ((btmaster != null) && (!locations.Any(x => x.DeviceID.Equals(btmaster.DeviceID))))
  520. // {
  521. // bool stale = btmaster.Location.Timestamp < DateTime.Now.Subtract(new TimeSpan(0, 15, 0));
  522. // bool moved = btmaster.Location.DistanceTo(curlocation, UnitOfLength.Kilometers) > 0.1;
  523. // if (stale || moved)
  524. // {
  525. // GPSTrackerLocation location = new GPSTrackerLocation();
  526. // location.DeviceID = id;
  527. // location.Location.Latitude = App.GPS.Latitude;
  528. // location.Location.Longitude = App.GPS.Longitude;
  529. // location.Location.Timestamp = DateTime.Now;
  530. // location.BatteryLevel = level;
  531. // locations.Add(location);
  532. // }
  533. // }
  534. // //new Client<GPSTrackerLocation>().Save(location, "Found Kontakt Device"); //, SaveTrackerCallback);
  535. //}
  536. //if (locations.Any())
  537. // new Client<GPSTrackerLocation>().Save(locations, "Updating Bluetooth Device Locations", (o, e) => { });
  538. #endregion
  539. }
  540. private void RecentlyUpdatedTilesTimer(object o)
  541. {
  542. bRecentlyUpdatedTiles = false;
  543. App.Data.Refresh(true);
  544. SearchForNewNotifications();
  545. }
  546. private void LocationFound(LocationServices sender)
  547. {
  548. //if (bSharedDevice)
  549. // return;
  550. if (App.Bluetooth.RecentlyScanned)
  551. UploadTiles();
  552. try
  553. {
  554. TimeSheet timesheet = App.Data.TimeSheets?.Rows.FirstOrDefault()?.ToObject<TimeSheet>();
  555. if (timesheet != null)
  556. {
  557. if (timesheet.StartLocation.Latitude.Equals(0.0F) && timesheet.StartLocation.Longitude.Equals(0.0F))
  558. {
  559. timesheet.StartLocation.Latitude = sender.Latitude;
  560. timesheet.StartLocation.Longitude = sender.Longitude;
  561. timesheet.StartLocation.Timestamp = sender.TimeStamp;
  562. timesheet.Address = sender.Address;
  563. new Client<TimeSheet>().Save(timesheet, "Updating Timesheet with GPS Coordinates", (o, e) => { });
  564. }
  565. }
  566. if (!string.IsNullOrWhiteSpace(matchedDeviceName))
  567. {
  568. InABox.Core.Location curlocation = new InABox.Core.Location() { Latitude = App.GPS.Latitude, Longitude = App.GPS.Longitude };
  569. curlocation.Timestamp = DateTime.Now;
  570. GPSTrackerLocation gpsTrackerLocation = new GPSTrackerLocation();
  571. gpsTrackerLocation.DeviceID = matchedDeviceName;
  572. gpsTrackerLocation.Location.Timestamp = curlocation.Timestamp;
  573. gpsTrackerLocation.Location = curlocation;
  574. new Client<GPSTrackerLocation>().Save(gpsTrackerLocation, "Updated company device location from Timebench");
  575. }
  576. Device.BeginInvokeOnMainThread(() =>
  577. {
  578. RefreshScreen();
  579. });
  580. }
  581. catch { }
  582. }
  583. private void LocationError(LocationServices sebder, Exception error)
  584. {
  585. }
  586. private void ScanFinished(Bluetooth sender)
  587. {
  588. try
  589. {
  590. //if (bSharedDevice)
  591. // return;
  592. Device.BeginInvokeOnMainThread(() =>
  593. {
  594. RefreshScreen();
  595. //if (Button2.BackgroundColor == Color.WhiteSmoke)
  596. // Button2.BackgroundColor = Color.Red;
  597. //else
  598. // Button2.BackgroundColor = Color.WhiteSmoke;
  599. });
  600. if (App.GPS.RecentlyLocated)
  601. UploadTiles();
  602. }
  603. catch { }
  604. }
  605. #endregion
  606. #region Utilities
  607. private void InitNotificationCentre()
  608. {
  609. try
  610. {
  611. LocalNotificationCenter.Current.NotificationActionTapped += (Plugin.LocalNotification.EventArgs.NotificationActionEventArgs e) =>
  612. {
  613. string data = e.Request.ReturningData;
  614. int index = data.IndexOf("$");
  615. Guid ID = Guid.Parse(data.Remove(index));
  616. string type = data.Substring(index + 1);
  617. if (type == "Comal.Classes.Kanban")
  618. {
  619. Device.BeginInvokeOnMainThread(() =>
  620. {
  621. AddEditTask taskPage = new AddEditTask(ID);
  622. Navigation.PushAsync(taskPage);
  623. });
  624. }
  625. };
  626. }
  627. catch { }
  628. }
  629. private async void SearchForNewNotifications()
  630. {
  631. //notifications poll reliably in the background for Anroid, unreliable for iOS due to difficulty with cross-platform plugins for notifications
  632. try
  633. {
  634. await Task.Run(() =>
  635. {
  636. if (ClientFactory.UserGuid != Guid.Empty)
  637. {
  638. CoreTable table = new Client<Notification>().Query
  639. (new Filter<Notification>(x => x.Employee.UserLink.ID).IsEqualTo(ClientFactory.UserGuid).And(X => X.Closed).IsEqualTo(DateTime.MinValue),
  640. new Columns<Notification>(
  641. x => x.ID, //0
  642. x => x.Sender.Name, //1
  643. x => x.Title, //2
  644. x => x.Created, //3
  645. x => x.Description, //4
  646. x => x.EntityType, //5
  647. x => x.EntityID //6
  648. )
  649. );
  650. if (NumberOfNotfications == table.Rows.Count()) //no new notifications or none present at all
  651. return;
  652. else //new notifications or previous notifications have now been dismissed
  653. {
  654. NumberOfNotfications = table.Rows.Count();
  655. RefreshOnNotificationsChange();
  656. CheckNotificationsPushed(table);
  657. }
  658. }
  659. });
  660. }
  661. catch { }
  662. }
  663. private void RefreshOnNotificationsChange()
  664. {
  665. try
  666. {
  667. int index = toolEntries.FindIndex(x => x.Text.Equals("Notifications"));
  668. toolEntries.RemoveAt(index);
  669. string notificationsString = "";
  670. if (NumberOfNotfications != 0)
  671. {
  672. notificationsString = NumberOfNotfications.ToString();
  673. }
  674. ToolEntry Notifications = new ToolEntry(notificationsString)
  675. {
  676. Text = "Notifications",
  677. Image = "notifications"
  678. };
  679. Notifications.OnTapped += (async (object sender, EventArgs e) =>
  680. {
  681. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading"))
  682. {
  683. NotificationList notificationList = new NotificationList();
  684. notificationList.NotificationsClosed += (n) =>
  685. {
  686. NumberOfNotfications = n;
  687. RefreshOnNotificationsChange();
  688. };
  689. Navigation.PushAsync(notificationList);
  690. }
  691. });
  692. toolEntries.Insert(index, Notifications);
  693. Device.BeginInvokeOnMainThread(() =>
  694. {
  695. flexLayout.Children.RemoveAt(index);
  696. flexLayout.Children.Insert(index, toolEntries[index]);
  697. });
  698. }
  699. catch { }
  700. }
  701. private void CheckNotificationsPushed(CoreTable table)
  702. {
  703. try
  704. {
  705. if (!Application.Current.Properties.ContainsKey("LastPushedNotifications"))
  706. {
  707. Application.Current.Properties.Add("LastPushedNotifications", DateTime.Now);
  708. }
  709. DateTime lastPushed = DateTime.Parse(Application.Current.Properties["LastPushedNotifications"].ToString());
  710. List<NotificationShell> toNotify = new List<NotificationShell>();
  711. foreach (CoreRow row in table.Rows)
  712. {
  713. List<object> list = row.Values;
  714. DateTime created = DateTime.Parse(list[3].ToString());
  715. if (created > new DateTime(2022, 8, 22)) // prevent spam from buildup of old notifications before this is released
  716. {
  717. if (created > lastPushed)
  718. {
  719. if (list[1] == null) list[1] = "";
  720. if (list[2] == null) list[2] = "";
  721. if (list[3] == null) list[3] = DateTime.MinValue;
  722. if (list[4] == null) list[4] = "";
  723. if (list[5] == null) list[5] = "";
  724. if (list[6] == null) list[6] = Guid.Empty;
  725. NotificationShell shell = new NotificationShell
  726. {
  727. ID = Guid.Parse(list[0].ToString()),
  728. Sender = list[1].ToString(),
  729. Title = list[2].ToString(),
  730. Created = DateTime.Parse(list[3].ToString()),
  731. EntityType = list[5].ToString(),
  732. EntityID = Guid.Parse(list[6].ToString())
  733. };
  734. toNotify.Add(shell); //add notification to be pushed
  735. }
  736. }
  737. }
  738. if (toNotify.Count > 0)
  739. PushNotificationsAsync(toNotify);
  740. }
  741. catch { }
  742. }
  743. private async Task PushNotificationsAsync(List<NotificationShell> shells)
  744. {
  745. try
  746. {
  747. int count = 1;
  748. foreach (NotificationShell shell in shells)
  749. {
  750. var notification = new NotificationRequest
  751. {
  752. BadgeNumber = 1,
  753. Description = shell.Title,
  754. Title = "New PRS Notification: ",
  755. ReturningData = shell.EntityID.ToString() + "$" + shell.EntityType,
  756. NotificationId = count,
  757. };
  758. count++;
  759. NotificationImage img = new NotificationImage();
  760. img.ResourceName = "icon16.png";
  761. notification.Image = img;
  762. await LocalNotificationCenter.Current.Show(notification);
  763. //if (Device.RuntimePlatform.Equals(Device.iOS))
  764. //{
  765. // var content = new UNMutableNotificationContent();
  766. // content.Title = "New PRS Notification: ";
  767. // content.Subtitle = shell.Title;
  768. // content.Body = "";
  769. // content.Badge = 1;
  770. // var trigger = UNTimeIntervalNotificationTrigger.CreateTrigger(1, false);
  771. // var requestID = "request";
  772. // var request = UNNotificationRequest.FromIdentifier(requestID, content, trigger);
  773. // UNUserNotificationCenter.Current.AddNotificationRequest(request, (err) =>
  774. // {
  775. // if (err != null)
  776. // {
  777. // Do something with error...
  778. // }
  779. // });
  780. //}
  781. }
  782. Application.Current.Properties["LastPushedNotifications"] = DateTime.Now;
  783. }
  784. catch { }
  785. }
  786. private void StartMidnightTimeSheetTimer()
  787. {
  788. midnightTimerOn = true;
  789. int msUntilMidnight = (int)(oneSecondBeforeMidnight - DateTime.Now).TotalMilliseconds;
  790. Timer midnightTimer = new Timer(MidnightTimerCallback, null, msUntilMidnight, Timeout.Infinite);
  791. }
  792. private void MidnightTimerCallback(object o)
  793. {
  794. try
  795. {
  796. //if (bSharedDevice)
  797. // return;
  798. if (midnightTimerOn)
  799. {
  800. if (clockOnButton.Text == "CLOCK OFF")
  801. {
  802. InABox.Core.Location here = new InABox.Core.Location()
  803. {
  804. Latitude = App.GPS.Latitude,
  805. Longitude = App.GPS.Longitude,
  806. Timestamp = DateTime.Now
  807. };
  808. TimeSheet timesheet = App.Data.TimeSheets?.Rows.FirstOrDefault()?.ToObject<TimeSheet>();
  809. if (timesheet != null)
  810. {
  811. if (timesheet.ID != Guid.Empty)
  812. {
  813. if (ZeroLengthTimesheet())
  814. {
  815. bUpdatingTimesheet = true;
  816. new Client<TimeSheet>().Delete(timesheet, "Deleted due to zero duration timesheet");
  817. App.Data.TimeSheets.Rows.Clear();
  818. }
  819. else
  820. {
  821. timesheet.Finish = new TimeSpan(23, 59, 59);
  822. timesheet.FinishLocation = here;
  823. bUpdatingTimesheet = true;
  824. new Client<TimeSheet>().Save(timesheet, "Auto Close timesheet at Midnight");
  825. App.Data.TimeSheets.Rows.Clear();
  826. Guid jobid = Guid.Empty;
  827. String jobnumber = "";
  828. String jobname = "";
  829. if (!App.Data.CanBypassGates)
  830. {
  831. CoreRow row = App.Data.Gates.Rows.FirstOrDefault(r => App.Bluetooth.Devices.Contains(r.Get<JobTracker, String>(c => c.TrackerLink.DeviceID)));
  832. if (row != null)
  833. {
  834. jobid = row.Get<JobTracker, Guid>(x => x.JobLink.ID);
  835. jobnumber = row.Get<JobTracker, String>(x => x.JobLink.JobNumber);
  836. jobname = row.Get<JobTracker, String>(x => x.JobLink.Name);
  837. }
  838. CreateTimeSheet(jobid, jobnumber, jobname, App.Data.Employee.UsualActivity.ID, App.Data.Employee.UsualActivity.Code, App.Data.Employee.UsualActivity.Description, here, App.GPS.Address, "Clocking On");
  839. }
  840. else
  841. {
  842. if ((!App.GPS.Latitude.Equals(0.0F)) && (!App.GPS.Longitude.Equals(0.0F)))
  843. {
  844. ChooseNearbyJob(here);
  845. }
  846. }
  847. }
  848. }
  849. }
  850. }
  851. }
  852. }
  853. catch { }
  854. }
  855. private void FinishTimeSheet(TimeSheet timesheet, InABox.Core.Location here)
  856. {
  857. try
  858. {
  859. TimeSpan tod = DateTime.Now - DateTime.Today;
  860. timesheet.Finish = new TimeSpan(tod.Hours, tod.Minutes, 0);
  861. timesheet.FinishLocation = here;
  862. bUpdatingTimesheet = true;
  863. new Client<TimeSheet>().Save(timesheet, "Clocking Off");
  864. App.Data.TimeSheets.Rows.Clear();
  865. midnightTimerOn = false;
  866. Timer last60Seconds = new Timer(Last60SecondsTimerCallBack, null, 5000, Timeout.Infinite);
  867. clockedOffInLast5Seconds = true;
  868. }
  869. catch { }
  870. }
  871. private void Last60SecondsTimerCallBack(object o)
  872. {
  873. clockedOffInLast5Seconds = false;
  874. }
  875. private async void ChooseNearbyJob(InABox.Core.Location here)
  876. {
  877. try
  878. {
  879. JobShell selectedJob = new JobShell();
  880. Dictionary<string, JobShell> nearbyJobs = new Dictionary<string, JobShell>();
  881. foreach (CoreRow row in App.Data.Jobs.Rows)
  882. {
  883. InABox.Core.Location jobLocation = new InABox.Core.Location() { Latitude = row.Get<Job, double>(X => X.SiteAddress.Location.Latitude), Longitude = row.Get<Job, double>(X => X.SiteAddress.Location.Longitude) };
  884. double distance = here.DistanceTo(jobLocation, UnitOfLength.Kilometers);
  885. if (distance < 1.0F)
  886. {
  887. JobShell jobShell = new JobShell();
  888. jobShell.ID = row.Get<Job, Guid>(X => X.ID);
  889. jobShell.JobNumber = row.Get<Job, String>(x => x.JobNumber);
  890. jobShell.Name = row.Get<Job, String>(x => x.Name);
  891. jobShell.DisplayName = jobShell.JobNumber + " " + jobShell.Name;
  892. nearbyJobs.Add(jobShell.DisplayName, jobShell);
  893. }
  894. }
  895. if (nearbyJobs.Count > 1)
  896. {
  897. string[] array = nearbyJobs.Keys.ToArray();
  898. string chosenOption = await DisplayActionSheet("Choose job site", "Cancel", null, array);
  899. if (string.IsNullOrEmpty(chosenOption) || chosenOption.Equals("Cancel"))
  900. {
  901. CreateTimeSheet(selectedJob.ID, selectedJob.JobNumber, selectedJob.Name, App.Data.Employee.UsualActivity.ID, App.Data.Employee.UsualActivity.Code, App.Data.Employee.UsualActivity.Description, here, App.GPS.Address, "Clocking On");
  902. return;
  903. }
  904. else
  905. {
  906. selectedJob = nearbyJobs[chosenOption];
  907. }
  908. }
  909. else if (nearbyJobs.Count == 1)
  910. {
  911. selectedJob = nearbyJobs.Values.First();
  912. }
  913. CreateTimeSheet(selectedJob.ID, selectedJob.JobNumber, selectedJob.Name, App.Data.Employee.UsualActivity.ID, App.Data.Employee.UsualActivity.Code, App.Data.Employee.UsualActivity.Description, here, App.GPS.Address, "Clocking On");
  914. }
  915. catch { }
  916. }
  917. void AddNote_Tapped(System.Object sender, System.EventArgs e)
  918. {
  919. try
  920. {
  921. TimeSheet timesheet = App.Data.TimeSheets?.Rows.FirstOrDefault()?.ToObject<TimeSheet>();
  922. if (timesheet == null)
  923. return;
  924. var notepage = new NotePage(timesheet);
  925. Navigation.PushAsync(notepage);
  926. }
  927. catch { }
  928. }
  929. private void JobBtn_Tapped(object sender, EventArgs e)
  930. {
  931. try
  932. {
  933. JobSelectionPage jobSelectionPage = new JobSelectionPage();
  934. jobSelectionPage.OnItemSelected += (() =>
  935. {
  936. _job.ID = jobSelectionPage.Job.ID;
  937. _job.JobNumber = jobSelectionPage.Job.JobNumber;
  938. _job.Name = jobSelectionPage.Job.Name;
  939. JobPage_OnItemSelected(jobSelectionPage.Job);
  940. });
  941. Navigation.PushAsync(jobSelectionPage);
  942. }
  943. catch { }
  944. }
  945. private void JobPage_OnItemSelected(JobShell job)
  946. {
  947. try
  948. {
  949. TimeSheet timesheet = App.Data.TimeSheets?.Rows.FirstOrDefault()?.ToObject<TimeSheet>();
  950. if (timesheet == null)
  951. return;
  952. String auditmessage = String.Format("Changed Selected Job to: {0}: {1}", timesheet.JobLink.JobNumber, timesheet.JobLink.Name);
  953. if (ZeroLengthTimesheet())
  954. {
  955. timesheet.JobLink.ID = job.ID;
  956. timesheet.JobLink.JobNumber = job.JobNumber;
  957. timesheet.JobLink.Name = job.Name;
  958. bUpdatingTimesheet = true;
  959. new Client<TimeSheet>().Save(timesheet, auditmessage);
  960. Device.BeginInvokeOnMainThread(() =>
  961. {
  962. if (timesheet.JobLink.ID != Guid.Empty)
  963. {
  964. jobBtn.Text = "(" + timesheet.JobLink.JobNumber + ") " + timesheet.JobLink.Name;
  965. }
  966. else
  967. {
  968. jobBtn.Text = "No Job Selected";
  969. }
  970. });
  971. }
  972. else
  973. {
  974. Guid activityid = timesheet.ActivityLink.ID;
  975. String activitycode = timesheet.ActivityLink.Code;
  976. String activitydescription = timesheet.ActivityLink.Description;
  977. InABox.Core.Location here = new InABox.Core.Location()
  978. {
  979. Latitude = App.GPS.Latitude,
  980. Longitude = App.GPS.Longitude,
  981. Timestamp = DateTime.Now
  982. };
  983. TimeSpan tod = DateTime.Now - DateTime.Today;
  984. timesheet.Finish = new TimeSpan(tod.Hours, tod.Minutes, 0);
  985. timesheet.FinishLocation = here;
  986. new Client<TimeSheet>().Save(timesheet, "Changing Job");
  987. CreateTimeSheet(
  988. job.ID,
  989. job.JobNumber,
  990. job.Name,
  991. activityid,
  992. activitycode,
  993. activitydescription,
  994. here,
  995. App.GPS.Address,
  996. auditmessage
  997. );
  998. }
  999. RefreshScreen();
  1000. }
  1001. catch { }
  1002. }
  1003. private bool CheckTimeSheetAgainstGates(TimeSheet timesheet)
  1004. {
  1005. DateTime now = DateTime.Now;
  1006. //var timesheet = CurrentTimeSheet();
  1007. //Can't confirm if there is no timesheet
  1008. if (timesheet == null)
  1009. return false;
  1010. // Can't confirm if there are no devices
  1011. if (App.Bluetooth.Devices.Length == 0)
  1012. return false;
  1013. if (App.Data.Gates == null)
  1014. return false;
  1015. long tsTicks = timesheet.Date.Add(timesheet.Start).Ticks;
  1016. long btTicks = App.Bluetooth.TimeStamp.Ticks;
  1017. if (Math.Abs(tsTicks - btTicks) > new TimeSpan(0, 2, 0).Ticks)
  1018. return false;
  1019. CoreRow firstgate = null;
  1020. List<String> gates = new List<string>();
  1021. // Scan every located d
  1022. foreach (var device in App.Bluetooth.Devices)
  1023. {
  1024. CoreRow gate = App.Data.Gates?.Rows.FirstOrDefault(r => r.Get<JobTracker, String>(c => c.TrackerLink.DeviceID) == device);
  1025. if (gate != null)
  1026. {
  1027. if ((gate.Get<JobTracker, bool>(x => x.IsJobSite) == true) && (firstgate == null))
  1028. firstgate = gate;
  1029. gates.Add(gate.Get<JobTracker, String>(x => x.Gate));
  1030. }
  1031. }
  1032. if (gates.Any())
  1033. {
  1034. timesheet.Gate = String.Join(", ", gates.OrderBy(x => x));
  1035. if (firstgate != null)
  1036. {
  1037. timesheet.JobLink.ID = firstgate.Get<JobTracker, Guid>(x => x.JobLink.ID);
  1038. timesheet.JobLink.JobNumber = firstgate.Get<JobTracker, String>(x => x.JobLink.JobNumber);
  1039. timesheet.JobLink.Name = firstgate.Get<JobTracker, String>(x => x.JobLink.Name);
  1040. }
  1041. return true;
  1042. //new Client<TimeSheet>().Save(timesheet, "Confirmed Gate Entry by Bluetooth Tracker", (o, e) => { });
  1043. }
  1044. return false;
  1045. }
  1046. private bool ZeroLengthTimesheet()
  1047. {
  1048. try
  1049. {
  1050. if (App.Data.TimeSheets == null)
  1051. return true;
  1052. CoreRow row = App.Data.TimeSheets.Rows.FirstOrDefault();
  1053. if (row == null)
  1054. return true;
  1055. String notes = row.Get<TimeSheet, String>(x => x.Notes);
  1056. if (!String.IsNullOrWhiteSpace(notes))
  1057. return false;
  1058. DateTime date = row.Get<TimeSheet, DateTime>(x => x.Date);
  1059. TimeSpan start = row.Get<TimeSheet, TimeSpan>(x => x.Start);
  1060. if (date.Equals(DateTime.Today))
  1061. {
  1062. TimeSpan tod = DateTime.Now - DateTime.Today;
  1063. var diff = (tod - start).TotalSeconds;
  1064. if (Math.Abs(diff) < 120.0F)
  1065. return true;
  1066. }
  1067. }
  1068. catch { }
  1069. return false;
  1070. }
  1071. private async void CreateTimeSheet(Guid jobid, string jobnumber, String jobname, Guid activityid, String activitycode, String activitydescription, InABox.Core.Location location, String address, String auditmessage)
  1072. {
  1073. try
  1074. {
  1075. var timesheet = new TimeSheet();
  1076. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading"))
  1077. {
  1078. timesheet.EmployeeID = App.Data.Employee.ID;
  1079. timesheet.EmployeeLink.ID = App.Data.Employee.ID;
  1080. timesheet.Date = DateTime.Today;
  1081. TimeSpan tod = DateTime.Now - DateTime.Today;
  1082. tod = new TimeSpan(tod.Hours, tod.Minutes, 0);
  1083. timesheet.Start = tod;
  1084. timesheet.StartLocation = location;
  1085. timesheet.JobLink.ID = jobid;
  1086. timesheet.JobLink.JobNumber = jobnumber;
  1087. timesheet.JobLink.Name = jobname;
  1088. timesheet.ActivityLink.ID = activityid;
  1089. timesheet.ActivityLink.Code = activitycode;
  1090. timesheet.ActivityLink.Description = activitydescription;
  1091. timesheet.Address = address;
  1092. timesheet.SoftwareVersion = MobileUtils.AppVersion.InstalledVersionNumber + GlobalVariables.DeviceString;
  1093. //if (ClientFactory.IsAllowed<AllowTimeSheetRollover>()) CheckTimeSheetAgainstGates(timesheet);
  1094. bUpdatingTimesheet = true;
  1095. new Client<TimeSheet>().Save(timesheet, auditmessage);
  1096. if (timesheet.ID == Guid.Empty)
  1097. {
  1098. DisplayAlert("Error creating new timesheet", "Please check your connection and try again", "OK");
  1099. return;
  1100. }
  1101. StartMidnightTimeSheetTimer();
  1102. // Don't Save Completed Timesheets!
  1103. App.Data.TimeSheets.Rows.Clear();
  1104. if (timesheet.Finish.Ticks == 0L)
  1105. {
  1106. CoreRow row = App.Data.TimeSheets.NewRow();
  1107. App.Data.TimeSheets.LoadRow(row, timesheet);
  1108. App.Data.TimeSheets.Rows.Add(row);
  1109. }
  1110. }
  1111. Device.BeginInvokeOnMainThread(() =>
  1112. {
  1113. if (timesheet.JobLink.ID != Guid.Empty)
  1114. {
  1115. jobBtn.Text = "(" + timesheet.JobLink.JobNumber + ") " + timesheet.JobLink.Name;
  1116. }
  1117. else
  1118. {
  1119. jobBtn.Text = "No Job Selected";
  1120. }
  1121. });
  1122. }
  1123. catch { }
  1124. }
  1125. private bool CheckLocation()
  1126. {
  1127. try
  1128. {
  1129. if (App.Data.CanBypassGates)
  1130. {
  1131. if (App.GPS.TimeStamp > DateTime.Now.Subtract(new TimeSpan(0, 5, 0)))
  1132. return true;
  1133. else
  1134. return false;
  1135. }
  1136. if (App.Data.Gates == null)
  1137. return false;
  1138. if (App.Bluetooth.TimeStamp < DateTime.Now.Subtract(new TimeSpan(0, 2, 0)))
  1139. return false;
  1140. if (!App.Bluetooth.Devices.Any())
  1141. return false;
  1142. return App.Data.Gates.Rows.Any(r => App.Bluetooth.Devices.Contains(r.Get<JobTracker, String>(c => c.TrackerLink.DeviceID)));
  1143. }
  1144. catch
  1145. {
  1146. return true;
  1147. }
  1148. }
  1149. private String DisplayAddress()
  1150. {
  1151. try
  1152. {
  1153. bool PRSReady = App.Data.Employee != null; // && (TimeSheet != null); // && (Activities != null);
  1154. if (!PRSReady)
  1155. return "Retrieving Data";
  1156. if (App.Data.CanBypassGates)
  1157. {
  1158. if (App.GPS.TimeStamp < DateTime.Now.Subtract(new TimeSpan(0, 5, 0)))
  1159. {
  1160. App.GPS.GetLocation(true);
  1161. return "Searching for GPS";
  1162. }
  1163. else
  1164. return App.GPS.Address;
  1165. }
  1166. else
  1167. {
  1168. // Hmm.. this can/should be simplified
  1169. // if in range of a gate
  1170. // Show Gate Description
  1171. // else
  1172. // "Looking for Gate"
  1173. if ((App.Data.Gates != null) && (App.Bluetooth.TimeStamp > DateTime.Now.Subtract(new TimeSpan(0, 2, 0))))
  1174. {
  1175. CoreRow row = App.Data.Gates.Rows.FirstOrDefault(r => App.Bluetooth.Devices.Contains(r.Get<JobTracker, String>(c => c.TrackerLink.DeviceID)));
  1176. if (row != null)
  1177. return row.Get<JobTracker, String>(x => x.Gate);
  1178. //else if ((CurrentTimeSheet() != null) && (!GPS.TimeStamp.IsEmpty()))
  1179. // return GPS.Address;
  1180. else
  1181. return "Looking for Gate";
  1182. }
  1183. //else if ((CurrentTimeSheet() != null) && (GPS.TimeStamp > DateTime.Now.Subtract(new TimeSpan(0, 2, 0))))
  1184. // return GPS.Address;
  1185. else
  1186. return "Looking for Gate";
  1187. }
  1188. }
  1189. catch
  1190. {
  1191. return "Address error";
  1192. }
  1193. }
  1194. #region Background Loading
  1195. private void LoadCacheLists()
  1196. {
  1197. GlobalVariables.ProductsLoaded = false;
  1198. GlobalVariables.JobsLoaded = false;
  1199. GlobalVariables.GetXamarinWidth();
  1200. LoadJobShells();
  1201. LoadEmployeeShells();
  1202. LoadProducts();
  1203. LoadCompanyDevices();
  1204. LoadBlueToothAddresses();
  1205. //LoadHRToDos(); to be uncommented when ready for roll out
  1206. }
  1207. private void LoadCompanyDevices()
  1208. {
  1209. Task.Run(() =>
  1210. {
  1211. if (!string.IsNullOrWhiteSpace(deviceName) && deviceName != "unknown")
  1212. {
  1213. List<Equipment> companyDevices = new List<Equipment>();
  1214. CoreTable table = new Client<Equipment>().Query
  1215. (
  1216. new Filter<Equipment>(x => x.GroupLink.Code).IsEqualTo("DEVICE"),
  1217. new Columns<Equipment>(
  1218. x => x.TrackerLink.DeviceID
  1219. )
  1220. );
  1221. if (table.Rows.Any())
  1222. {
  1223. foreach (CoreRow row in table.Rows)
  1224. {
  1225. List<object> list = row.Values;
  1226. if (list[0].ToString().Equals(deviceName))
  1227. {
  1228. matchedDeviceName = deviceName;
  1229. }
  1230. }
  1231. }
  1232. }
  1233. });
  1234. }
  1235. private async void LoadEmployeeShells()
  1236. {
  1237. await Task.Run(() =>
  1238. {
  1239. List<EmployeeShell> employeeShells = new List<EmployeeShell>();
  1240. List<EmployeeShell> teamEmployeeShells = new List<EmployeeShell>();
  1241. MultiQuery query = new MultiQuery();
  1242. query.Add<Employee>(
  1243. LookupFactory.DefineFilter<Employee>(),
  1244. new Columns<Employee>(x => x.ID)
  1245. .Add(x => x.Code)
  1246. .Add(x => x.Name),
  1247. LookupFactory.DefineSort<Employee>()
  1248. );
  1249. query.Add<Team>(
  1250. LookupFactory.DefineFilter<Team>(),
  1251. new Columns<Team>(x => x.Name),
  1252. new SortOrder<Team>(x => x.Name)
  1253. );
  1254. query.Add<EmployeeTeam>(
  1255. LookupFactory.DefineFilter<EmployeeTeam>(),
  1256. new Columns<EmployeeTeam>(x => x.EmployeeLink.ID)
  1257. .Add(x => x.EmployeeLink.Code)
  1258. .Add(x => x.EmployeeLink.Name)
  1259. .Add(x => x.TeamLink.Name),
  1260. new SortOrder<EmployeeTeam>(x => x.EmployeeLink.Name)
  1261. );
  1262. query.Query();
  1263. CoreTable emps = query.Get<Employee>();
  1264. foreach (var row in emps.Rows)
  1265. {
  1266. employeeShells.Add(
  1267. new EmployeeShell()
  1268. {
  1269. ID = row.Get<Employee, Guid>(x => x.ID),
  1270. Code = row.Get<Employee, String>(x => x.Code),
  1271. Name = row.Get<Employee, String>(x => x.Name),
  1272. TeamName = "All Staff"
  1273. }
  1274. );
  1275. }
  1276. GlobalVariables.TeamNames = query.Get<Team>().Rows.Select(r => r.Get<Team, String>(c => c.Name)).ToList();
  1277. CoreTable members = query.Get<EmployeeTeam>();
  1278. foreach (var row in members.Rows)
  1279. {
  1280. teamEmployeeShells.Add(
  1281. new EmployeeShell()
  1282. {
  1283. ID = row.Get<EmployeeTeam, Guid>(x => x.EmployeeLink.ID),
  1284. Code = row.Get<EmployeeTeam, String>(x => x.EmployeeLink.Code),
  1285. Name = row.Get<EmployeeTeam, String>(x => x.EmployeeLink.Name),
  1286. TeamName = row.Get<EmployeeTeam, String>(x => x.TeamLink.Name)
  1287. }
  1288. );
  1289. }
  1290. GlobalVariables.EmployeeShells = employeeShells;
  1291. GlobalVariables.TeamEmployeeShells = teamEmployeeShells;
  1292. });
  1293. }
  1294. async void LoadJobShells()
  1295. {
  1296. try
  1297. {
  1298. await Task.Run(() =>
  1299. {
  1300. List<JobShell> jobShells = new List<JobShell>();
  1301. CoreTable table = new Client<Job>().Query(
  1302. new Filter<Job>(x => x.JobStatus.Active).IsEqualTo(true),
  1303. new Columns<Job>(x => x.ID, x => x.Name, x => x.JobNumber, x => x.JobStatus.Description, x => x.Color),
  1304. new SortOrder<Job>(x => x.JobNumber)
  1305. );
  1306. foreach (CoreRow row in table.Rows)
  1307. {
  1308. List<object> list = row.Values;
  1309. if (list[0] == null) { list[0] = Guid.Empty; } //0
  1310. if (list[1] == null) { list[1] = ""; } //1
  1311. if (list[2] == null) { list[2] = ""; } //2
  1312. if (list[3] == null) { list[3] = ""; } //3
  1313. if (list[4] == null) { list[4] = ""; } //4
  1314. JobShell jobshell = new JobShell
  1315. {
  1316. ID = Guid.Parse(list[0].ToString()),
  1317. Name = list[1].ToString(),
  1318. JobNumber = list[2].ToString(),
  1319. JobStatusDescription = list[3].ToString(),
  1320. Color = Color.FromHex(list[4].ToString())
  1321. };
  1322. if (jobshell.JobStatusDescription.Equals("Active Projects"))
  1323. jobshell.JobStatusDescription = "Active";
  1324. else if (jobshell.JobStatusDescription.Equals("Projects - Hidden from View"))
  1325. jobshell.JobStatusDescription = "Hidden";
  1326. else if (jobshell.JobStatusDescription.Equals("Projects in Defect Liability Period"))
  1327. jobshell.JobStatusDescription = "Defect Liability";
  1328. jobshell.DisplayName = "(" + jobshell.JobNumber + ") " + jobshell.Name;
  1329. jobShells.Add(jobshell);
  1330. }
  1331. GlobalVariables.JobShells = jobShells;
  1332. GlobalVariables.JobShells.Insert(0, new JobShell { ID = Guid.Empty, JobNumber = "No Job", Name = "Empty Job", JobStatusDescription = "Hidden" });
  1333. GlobalVariables.JobsLoaded = true;
  1334. });
  1335. }
  1336. catch { }
  1337. }
  1338. private async void LoadHRToDos()
  1339. {
  1340. try
  1341. {
  1342. await Task.Run(() =>
  1343. {
  1344. Thread.Sleep(10000);
  1345. if (GlobalVariables.UpdateHRItemsNeedingAttention())
  1346. {
  1347. string message = "You have HR Items needing attention. Open My HR now?";
  1348. Device.BeginInvokeOnMainThread(async () =>
  1349. {
  1350. string chosenOption = await DisplayActionSheet(message, "Cancel", null, "Yes", "No");
  1351. switch (chosenOption)
  1352. {
  1353. case "Cancel":
  1354. break;
  1355. case "No":
  1356. break;
  1357. default:
  1358. break;
  1359. case "Yes":
  1360. MyHRHome myHRHome = new MyHRHome();
  1361. Navigation.PushAsync(myHRHome);
  1362. break;
  1363. }
  1364. });
  1365. }
  1366. });
  1367. }
  1368. catch { }
  1369. }
  1370. private async void LoadProducts()
  1371. {
  1372. try
  1373. {
  1374. await Task.Run(() =>
  1375. {
  1376. ProductsLoader productsLoader = new ProductsLoader();
  1377. //if (ClientFactory.IsAllowed<CanViewStoresRequisitions>())
  1378. //{
  1379. //}
  1380. });
  1381. }
  1382. catch { }
  1383. }
  1384. private async void LoadBlueToothAddresses()
  1385. {
  1386. try
  1387. {
  1388. //if (bSharedDevice)
  1389. // return;
  1390. await Task.Run(() =>
  1391. {
  1392. GlobalVariables.GPSTrackerCache = new List<GPSTracker>();
  1393. CoreTable table = new Client<GPSTracker>().Query(new Filter<GPSTracker>(x => x.Type.Description).Contains("Kontakt"));
  1394. foreach (CoreRow row in table.Rows)
  1395. {
  1396. GPSTracker tracker = row.ToObject<GPSTracker>();
  1397. GlobalVariables.GPSTrackerCache.Add(tracker);
  1398. App.Bluetooth.KnownBlueToothMACAddresses.Add(tracker.DeviceID);
  1399. }
  1400. });
  1401. }
  1402. catch { }
  1403. }
  1404. #endregion
  1405. #endregion
  1406. #region Modules
  1407. public async void InitToolEntryList()
  1408. {
  1409. try
  1410. {
  1411. await Task.Run(() =>
  1412. {
  1413. //Assignments
  1414. ToolEntry Assignments = new ToolEntry
  1415. {
  1416. Text = "Assignments",
  1417. Image = "calendar"
  1418. };
  1419. Assignments.IsVisible = PRSSecurity.CanView<Assignment>();
  1420. Assignments.OnTapped += ((object sender, EventArgs e) =>
  1421. {
  1422. var assignment_form = new AssignmentList();
  1423. Navigation.PushAsync(assignment_form);
  1424. });
  1425. toolEntries.Add(Assignments);
  1426. //Deliveries
  1427. ToolEntry Deliveries = new ToolEntry
  1428. {
  1429. Text = "Deliveries",
  1430. Image = "deliveries"
  1431. };
  1432. Deliveries.IsVisible = PRSSecurity.CanView<Delivery>();
  1433. Deliveries.OnTapped += ((object sender, EventArgs e) =>
  1434. {
  1435. var delivery_form = new DeliveryList();
  1436. Navigation.PushAsync(delivery_form);
  1437. });
  1438. toolEntries.Add(Deliveries);
  1439. //Digital Forms
  1440. ToolEntry Forms = new ToolEntry
  1441. {
  1442. Text = "Forms",
  1443. Image = "forms"
  1444. };
  1445. Forms.IsVisible = PRSSecurity.CanView<DigitalForm>();
  1446. Forms.OnTapped += ((object sender, EventArgs e) =>
  1447. {
  1448. var qaFormPicker = new DigitalFormsPicker();
  1449. Navigation.PushAsync(qaFormPicker);
  1450. });
  1451. toolEntries.Add(Forms);
  1452. //Equipment
  1453. ToolEntry Equipment = new ToolEntry
  1454. {
  1455. Text = "Equipment",
  1456. Image = "digger"
  1457. };
  1458. Equipment.IsVisible = PRSSecurity.CanView<Equipment>();
  1459. Equipment.OnTapped += (async (object sender, EventArgs e) =>
  1460. {
  1461. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading"))
  1462. {
  1463. var equipment = new EquipmentModule();
  1464. Navigation.PushAsync(equipment);
  1465. }
  1466. });
  1467. toolEntries.Add(Equipment);
  1468. //InOut
  1469. ToolEntry InOut = new ToolEntry
  1470. {
  1471. Text = "In/Out",
  1472. Image = "inout"
  1473. };
  1474. InOut.IsVisible = PRSSecurity.IsAllowed<CanViewInOutBoard>();
  1475. InOut.OnTapped += ((object sender, EventArgs e) =>
  1476. {
  1477. var staff_form = new StaffStatusPage();
  1478. Navigation.PushAsync(staff_form);
  1479. });
  1480. toolEntries.Add(InOut);
  1481. //Manufacturing
  1482. ToolEntry Manufacturing = new ToolEntry
  1483. {
  1484. Text = "Manufacturing",
  1485. Image = "manufacturingg"
  1486. };
  1487. if (Device.RuntimePlatform.Equals(Device.iOS))
  1488. {
  1489. Manufacturing.Image = "Image";
  1490. }
  1491. Manufacturing.IsVisible = PRSSecurity.IsAllowed<CanViewManufacturingOnMobile>();
  1492. Manufacturing.OnTapped += ((object sender, EventArgs e) =>
  1493. {
  1494. ManufacturingScreen manufacturingScreen = new ManufacturingScreen();
  1495. Navigation.PushAsync(manufacturingScreen);
  1496. });
  1497. toolEntries.Add(Manufacturing);
  1498. //My HR
  1499. ToolEntry MyHR = new ToolEntry
  1500. {
  1501. Text = "My HR",
  1502. Image = "myhr"
  1503. };
  1504. MyHR.OnTapped += ((object sender, EventArgs e) =>
  1505. {
  1506. MyHRHome myHRHome = new MyHRHome();
  1507. Navigation.PushAsync(myHRHome);
  1508. });
  1509. toolEntries.Add(MyHR);
  1510. //Notifications
  1511. ToolEntry Notifications = new ToolEntry()
  1512. {
  1513. Text = "Notifications",
  1514. Image = "notifications"
  1515. };
  1516. Notifications.OnTapped += (object sender, EventArgs e) =>
  1517. {
  1518. NotificationList notificationList = new NotificationList();
  1519. notificationList.NotificationsClosed += (n) =>
  1520. {
  1521. NumberOfNotfications = n;
  1522. RefreshOnNotificationsChange();
  1523. };
  1524. Navigation.PushAsync(notificationList);
  1525. };
  1526. toolEntries.Add(Notifications);
  1527. ToolEntry Products = new ToolEntry()
  1528. {
  1529. Text = "Products",
  1530. Image = "products"
  1531. };
  1532. Products.OnTapped += ((object sender, EventArgs e) =>
  1533. {
  1534. if (GlobalVariables.ProductsLoaded)
  1535. {
  1536. ProductList products = new ProductList(GlobalVariables.ProductShells);
  1537. Navigation.PushAsync(products);
  1538. }
  1539. else
  1540. {
  1541. ProductList products = new ProductList();
  1542. Navigation.PushAsync(products);
  1543. }
  1544. });
  1545. toolEntries.Add(Products);
  1546. //Purchase Orders
  1547. ToolEntry PurchaseOrders = new ToolEntry()
  1548. {
  1549. Text = "Purchase Orders",
  1550. Image = "shoppingcart"
  1551. };
  1552. PurchaseOrders.IsVisible = PRSSecurity.CanView<PurchaseOrder>();
  1553. PurchaseOrders.OnTapped += ((object sender, EventArgs e) =>
  1554. {
  1555. PurchaseOrderModule page = new PurchaseOrderModule();
  1556. Navigation.PushAsync(page);
  1557. });
  1558. toolEntries.Add(PurchaseOrders);
  1559. //Scanner
  1560. ToolEntry Scanner = new ToolEntry
  1561. {
  1562. Text = "Scanner",
  1563. Image = "scanner"
  1564. };
  1565. Scanner.OnTapped += ((object sender, EventArgs e) =>
  1566. {
  1567. ScannerPage scannerPage = new ScannerPage();
  1568. Navigation.PushAsync(scannerPage);
  1569. });
  1570. toolEntries.Add(Scanner);
  1571. //Site
  1572. ToolEntry Site = new ToolEntry
  1573. {
  1574. Text = "Site",
  1575. Image = "construction"
  1576. };
  1577. Site.OnTapped += ((object sender, EventArgs e) =>
  1578. {
  1579. Site site = new Site(_job);
  1580. Navigation.PushAsync(site);
  1581. //if (_job.ID == Guid.Empty)
  1582. //{
  1583. // JobSelectionPage jobSelectionPage = new JobSelectionPage(true);
  1584. // jobSelectionPage.OnItemSelected += (async () =>
  1585. // {
  1586. // if (jobSelectionPage.Job.ID != Guid.Empty)
  1587. // {
  1588. // Job selectedJob = new Job();
  1589. // selectedJob.ID = jobSelectionPage.Job.ID;
  1590. // selectedJob.JobNumber = jobSelectionPage.Job.JobNumber;
  1591. // selectedJob.Name = jobSelectionPage.Job.Name;
  1592. // Site site = new Site(selectedJob);
  1593. // var previousPage = Navigation.NavigationStack.LastOrDefault();
  1594. // await Navigation.PushAsync(site);
  1595. // Navigation.RemovePage(previousPage);
  1596. // }
  1597. // });
  1598. // Navigation.PushAsync(jobSelectionPage);
  1599. //}
  1600. });
  1601. toolEntries.Add(Site);
  1602. //Store Requis
  1603. ToolEntry StoreRequis = new ToolEntry
  1604. {
  1605. Text = "Store Requis",
  1606. Image = "storerequis"
  1607. };
  1608. StoreRequis.IsVisible = PRSSecurity.CanView<Requisition>();
  1609. StoreRequis.OnTapped += ((object sender, EventArgs e) =>
  1610. {
  1611. var storeRequisList = new StoreRequiList();
  1612. Navigation.PushAsync(storeRequisList);
  1613. });
  1614. toolEntries.Add(StoreRequis);
  1615. //Tasks
  1616. ToolEntry Tasks = new ToolEntry
  1617. {
  1618. Text = "Tasks",
  1619. Image = "tasks"
  1620. };
  1621. Tasks.IsVisible = PRSSecurity.IsAllowed<CanViewTasks>();
  1622. Tasks.OnTapped += ((object sender, EventArgs e) =>
  1623. {
  1624. var tasksForm = new TasksList();
  1625. Navigation.PushAsync(tasksForm);
  1626. });
  1627. toolEntries.Add(Tasks);
  1628. //Warehousing
  1629. ToolEntry Warehousing = new ToolEntry
  1630. {
  1631. Text = "Warehousing",
  1632. Image = "newwarehousing"
  1633. };
  1634. Warehousing.IsVisible = PRSSecurity.CanView<StockWarehouse>();
  1635. Warehousing.OnTapped += ((object sender, EventArgs e) =>
  1636. {
  1637. Warehousing2 locations = new Warehousing2();
  1638. Navigation.PushAsync(locations);
  1639. });
  1640. toolEntries.Add(Warehousing);
  1641. AddChildren();
  1642. });
  1643. }
  1644. catch { }
  1645. }
  1646. private void AddChildren()
  1647. {
  1648. Device.BeginInvokeOnMainThread(() =>
  1649. {
  1650. foreach (ToolEntry toolEntry in toolEntries)
  1651. {
  1652. toolEntry.Margin = new Thickness(5, 0, 5, 0);
  1653. flexLayout.Children.Add(toolEntry);
  1654. }
  1655. SearchForNewNotifications();
  1656. AddBlanks();
  1657. });
  1658. }
  1659. private void AddBlanks()
  1660. {
  1661. for (int x = 0; x < 6; x++)
  1662. {
  1663. ToolEntry toolEntry = new ToolEntry(true);
  1664. toolEntry.Margin = new Thickness(5, 0, 5, 0);
  1665. flexLayout.Children.Add(toolEntry);
  1666. }
  1667. }
  1668. private void Settings_Tapped(object sender, EventArgs e)
  1669. {
  1670. try
  1671. {
  1672. Settings settingsform = new Settings();
  1673. settingsform.Disappearing += (object sender2, EventArgs e2) =>
  1674. {
  1675. settingsform = (Settings)sender2;
  1676. if (settingsform.SettingsChanged)
  1677. Navigation.PopModalAsync();
  1678. };
  1679. Navigation.PushAsync(settingsform);
  1680. }
  1681. catch { }
  1682. }
  1683. #endregion
  1684. }
  1685. }