MainPage.xaml.cs 74 KB

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