MainPage.xaml.cs 70 KB

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