MainPage.xaml.cs 74 KB

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