MainPageUtils.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using Xamarin.Forms;
  7. using InABox.Core;
  8. using InABox.Configuration;
  9. using InABox.Clients;
  10. using InABox.Mobile;
  11. using Comal.Classes;
  12. using XF.Material.Forms.UI.Dialogs;
  13. using comal.timesheets.CustomControls;
  14. using comal.timesheets.StoreRequis;
  15. using PRSSecurity = InABox.Core.Security;
  16. using Plugin.LocalNotification;
  17. using comal.timesheets.Tasks;
  18. using System.IO;
  19. using static comal.timesheets.CustomControls.JobShell;
  20. using static InABox.Mobile.LocationServices;
  21. using iText.Kernel.XMP.Impl;
  22. namespace comal.timesheets
  23. {
  24. public delegate void MainPageNotificationsChanged();
  25. public delegate void RefreshScreen();
  26. public delegate void RequestUserInputForTask(Guid taskID);
  27. public delegate void TaskTitleChanged(string title);
  28. public static class MainPageUtils
  29. {
  30. public static event MainPageNotificationsChanged OnMainPageNotificationsChanged;
  31. public static event RefreshScreen OnRefreshScreen;
  32. public static event RequestUserInputForTask OnRequestUserInput;
  33. public static event TaskTitleChanged OnTaskTitleChanged;
  34. public static Assignment CurrentAssignment = null;
  35. public static JobShell Job = new JobShell();
  36. public static int NumberOfNotifications = 0;
  37. public static string matchedDeviceName = "";
  38. public static string deviceName = "";
  39. public static bool bRecentlyUpdatedTiles = false;
  40. public static TimeSheet _timesheet = null;
  41. public static Employee _employee = null;
  42. public static CoreTable _jobs = null;
  43. public static bool firstLoad = true;
  44. public static bool recentlyAskedToUpdate = true;
  45. public static int updateCounter;
  46. public static Kanban CurrentTask = null;
  47. public static void Init()
  48. {
  49. InitEvents();
  50. InitData();
  51. InitTimers();
  52. }
  53. private static void InitEvents()
  54. {
  55. try
  56. {
  57. App.GPS.OnLocationFound += LocationFound;
  58. App.GPS.OnLocationError += LocationError;
  59. App.Bluetooth.OnScanFinished += ScanFinished;
  60. App.Data.DataChanged += (s, t, e) => { OnRefreshScreen?.Invoke(); };
  61. App.Data.DataRefreshed += () => { OnRefreshScreen?.Invoke(); };
  62. }
  63. catch { }
  64. }
  65. private static void InitData()
  66. {
  67. try
  68. {
  69. GlobalVariables.EmpID = GlobalVariables.GetEmployeeID();
  70. GlobalVariables.EmpName = GlobalVariables.GetEmployeeName();
  71. App.Data.Employee.ID = GlobalVariables.EmpID;
  72. App.Data.Employee.Name = GlobalVariables.EmpName;
  73. }
  74. catch { }
  75. try
  76. {
  77. _timesheet = App.Data.TimeSheets?.Rows.FirstOrDefault()?.ToObject<TimeSheet>();
  78. _employee = App.Data.Employee;
  79. _jobs = App.Data.Jobs;
  80. deviceName = MobileUtils.GetDeviceID();
  81. firstLoad = false;
  82. }
  83. catch { }
  84. }
  85. private static void InitTimers()
  86. {
  87. Timer t = new Timer(RecentlyAskedToUpdateTimer, null, 600000, 600000); //user is reminded to update when opening screen after timer of 10 minutes
  88. updateCounter = 1; //user is forced to update after 3rd reminder
  89. Timer t1 = new Timer(RecentlyUpdatedTilesTimer, null, 30000, 30000);
  90. //bluetooth data is allowed to upload once every minute, notifications refreshing is piggybacked to this too
  91. }
  92. private static void RecentlyAskedToUpdateTimer(object o)
  93. {
  94. recentlyAskedToUpdate = false;
  95. }
  96. private static void RecentlyUpdatedTilesTimer(object o)
  97. {
  98. bRecentlyUpdatedTiles = false;
  99. App.Data.Refresh(true);
  100. SearchForNewNotifications();
  101. }
  102. #region Assignments
  103. public static Assignment CheckCurrentAssignment()
  104. {
  105. Thread.Sleep(5000);
  106. StartAssignmentTimer();
  107. CoreTable table = new Client<Assignment>().Query(
  108. new Filter<Assignment>(x => x.EmployeeLink.ID).IsEqualTo(GlobalVariables.EmpID)
  109. .And(x => x.Date).IsEqualTo(DateTime.Today.Date)
  110. .And(x => x.Completed).IsEqualTo(null)
  111. .And(x => x.Booked.Finish).IsEqualTo(null),
  112. null,
  113. new SortOrder<Assignment>(x => x.Actual.Start, SortDirection.Ascending));
  114. if (!table.Rows.Any())
  115. {
  116. Job.OnJobIDChanged += OnJobIDChanged;
  117. return null;
  118. }
  119. else
  120. return table.Rows.LastOrDefault().ToObject<Assignment>();
  121. }
  122. private static void StartAssignmentTimer()
  123. {
  124. Timer t = new Timer(AssignmentTimerCallback, null, 300000, 300000);
  125. }
  126. private static void AssignmentTimerCallback(object state)
  127. {
  128. try
  129. {
  130. if (CurrentAssignment != null)
  131. SaveCurrentAssignment("PRS Mobile main screen - Saving assignment on 5 minute timer");
  132. }
  133. catch
  134. { }
  135. }
  136. public static void SaveCurrentAssignment(string auditnote, bool complete = false)
  137. {
  138. if (!complete)
  139. CurrentAssignment.Booked.Finish = RoundToNearestInterval(DateTime.Now, new TimeSpan(0, 5, 0)).TimeOfDay;
  140. else
  141. {
  142. CurrentAssignment.Actual.Finish = RoundToNearestInterval(DateTime.Now, new TimeSpan(0, 5, 0)).TimeOfDay;
  143. CurrentAssignment.Completed = DateTime.Now;
  144. }
  145. new Client<Assignment>().Save(CurrentAssignment, auditnote);
  146. }
  147. static DateTime RoundToNearestInterval(DateTime dt, TimeSpan d)
  148. {
  149. int f = 0;
  150. double m = (double)(dt.Ticks % d.Ticks) / d.Ticks;
  151. if (m >= 0.5)
  152. f = 1;
  153. return new DateTime(((dt.Ticks / d.Ticks) + f) * d.Ticks);
  154. }
  155. public static void UseCurrentAssignment(Assignment assgn)
  156. {
  157. try
  158. {
  159. CurrentAssignment = assgn;
  160. SaveCurrentAssignment("PRS Mobile main screen - saving assignment on re-login to App");
  161. if (CurrentAssignment.JobLink.ID != Guid.Empty)
  162. {
  163. Job.ID = CurrentAssignment.JobLink.ID;
  164. var job = new Client<Job>().Query(new Filter<Job>(x => x.ID).IsEqualTo(Job.ID)).Rows.FirstOrDefault().ToObject<Job>();
  165. Job.JobNumber = job.JobNumber;
  166. Job.Name = job.Name;
  167. Job.OnJobIDChanged += OnJobIDChanged;
  168. }
  169. if (CurrentAssignment.Task.ID != Guid.Empty)
  170. {
  171. var task = new Client<Kanban>().Query(new Filter<Kanban>(x => x.ID).IsEqualTo(CurrentAssignment.Task.ID)).Rows.FirstOrDefault().ToObject<Kanban>();
  172. OnTaskTitleChanged?.Invoke(task.Title);
  173. }
  174. }
  175. catch { }
  176. }
  177. public static void OnJobIDChanged(Guid jobid)
  178. {
  179. if (CurrentAssignment == null)
  180. CreateNewAssignment(jobid, Guid.Empty);
  181. else
  182. {
  183. SaveCurrentAssignment("PRS Mobile main screen - saving assignment on job change", true);
  184. CreateNewAssignment(jobid, Guid.Empty);
  185. }
  186. }
  187. public static void CreateNewAssignment(Guid jobid, Guid taskID)
  188. {
  189. CurrentAssignment = new Assignment { Date = DateTime.Now.Date };
  190. CurrentAssignment.EmployeeLink.ID = GlobalVariables.EmpID;
  191. CurrentAssignment.Actual.Start = RoundToNearestInterval(DateTime.Now, new TimeSpan(0, 5, 0)).TimeOfDay;
  192. CurrentAssignment.Booked.Start = CurrentAssignment.Actual.Start;
  193. CurrentAssignment.Booked.Finish = RoundToNearestInterval(DateTime.Now, new TimeSpan(0, 5, 0)).TimeOfDay.Add(new TimeSpan(0, 5, 0));
  194. if (jobid != Guid.Empty)
  195. AddJobDetails(jobid);
  196. if (taskID != Guid.Empty)
  197. AddTaskDetails(taskID);
  198. }
  199. private static void AddJobDetails(Guid jobid)
  200. {
  201. CurrentAssignment.JobLink.ID = jobid;
  202. var job = new Client<Job>().Query(new Filter<Job>(x => x.ID).IsEqualTo(jobid)).Rows.FirstOrDefault().ToObject<Job>();
  203. CurrentAssignment.Title = "Clocking on to job " + job.Name + " (" + job.JobNumber + ") on PRS Mobile";
  204. OnTaskTitleChanged?.Invoke("Task");
  205. new Client<Assignment>().Save(CurrentAssignment, "Changed job on mobile - creating new assignment");
  206. }
  207. private static void AddTaskDetails(Guid taskID)
  208. {
  209. CurrentAssignment.Task.ID = taskID;
  210. var task = new Client<Kanban>().Query(new Filter<Kanban>(x => x.ID).IsEqualTo(taskID)).Rows.FirstOrDefault().ToObject<Kanban>();
  211. CurrentAssignment.Title = "Clocking on to task " + task.Title + " on PRS Mobile";
  212. OnTaskTitleChanged?.Invoke(task.Title);
  213. if (!string.IsNullOrWhiteSpace(task.JobLink.JobNumber))
  214. Job.JobNumber = task.JobLink.JobNumber;
  215. new Client<Assignment>().Save(CurrentAssignment, "Changed task on mobile - creating new assignment");
  216. }
  217. #endregion
  218. #region Notifications
  219. public static Page DetermineCorrectPage(Plugin.LocalNotification.EventArgs.NotificationActionEventArgs e)
  220. {
  221. string data = e.Request.ReturningData;
  222. int index = data.IndexOf("$");
  223. Guid ID = Guid.Parse(data.Remove(index));
  224. string type = data.Substring(index + 1);
  225. if (type == "Comal.Classes.Kanban")
  226. return new AddEditTask(ID);
  227. else if (type == "Comal.Classes.Delivery")
  228. return new DeliveryDetails(ID);
  229. else
  230. return null;
  231. }
  232. public static async void SearchForNewNotifications()
  233. {
  234. //notifications poll reliably in the background for Anroid, unreliable for iOS due to difficulty with cross-platform plugins for notifications
  235. try
  236. {
  237. await Task.Run(() =>
  238. {
  239. if (ClientFactory.UserGuid != Guid.Empty)
  240. {
  241. CoreTable table = new Client<Notification>().Query
  242. (new Filter<Notification>(x => x.Employee.UserLink.ID).IsEqualTo(ClientFactory.UserGuid).And(X => X.Closed).IsEqualTo(DateTime.MinValue),
  243. new Columns<Notification>(
  244. x => x.ID, //0
  245. x => x.Sender.Name, //1
  246. x => x.Title, //2
  247. x => x.Created, //3
  248. x => x.Description, //4
  249. x => x.EntityType, //5
  250. x => x.EntityID //6
  251. )
  252. );
  253. if (NumberOfNotifications == table.Rows.Count()) //no new notifications or none present at all
  254. return;
  255. else //new notifications or previous notifications have now been dismissed
  256. {
  257. NumberOfNotifications = table.Rows.Count();
  258. OnMainPageNotificationsChanged?.Invoke();
  259. CheckNotificationsPushed(table);
  260. }
  261. }
  262. });
  263. }
  264. catch { }
  265. }
  266. private static void CheckNotificationsPushed(CoreTable table)
  267. {
  268. try
  269. {
  270. if (!Application.Current.Properties.ContainsKey("LastPushedNotifications"))
  271. {
  272. Application.Current.Properties.Add("LastPushedNotifications", DateTime.Now);
  273. }
  274. DateTime lastPushed = DateTime.Parse(Application.Current.Properties["LastPushedNotifications"].ToString());
  275. List<NotificationShell> toNotify = new List<NotificationShell>();
  276. foreach (CoreRow row in table.Rows)
  277. {
  278. List<object> list = row.Values;
  279. DateTime created = DateTime.Parse(list[3].ToString());
  280. if (created > new DateTime(2022, 8, 22)) // prevent spam from buildup of old notifications before this is released
  281. {
  282. if (created > lastPushed)
  283. {
  284. if (list[1] == null) list[1] = "";
  285. if (list[2] == null) list[2] = "";
  286. if (list[3] == null) list[3] = DateTime.MinValue;
  287. if (list[4] == null) list[4] = "";
  288. if (list[5] == null) list[5] = "";
  289. if (list[6] == null) list[6] = Guid.Empty;
  290. NotificationShell shell = new NotificationShell
  291. {
  292. ID = Guid.Parse(list[0].ToString()),
  293. Sender = list[1].ToString(),
  294. Title = list[2].ToString(),
  295. Created = DateTime.Parse(list[3].ToString()),
  296. EntityType = list[5].ToString(),
  297. EntityID = Guid.Parse(list[6].ToString())
  298. };
  299. toNotify.Add(shell); //add notification to be pushed
  300. }
  301. }
  302. }
  303. if (toNotify.Count > 0)
  304. PushNotificationsAsync(toNotify);
  305. }
  306. catch { }
  307. }
  308. private static async Task PushNotificationsAsync(List<NotificationShell> shells)
  309. {
  310. try
  311. {
  312. int count = 1;
  313. foreach (NotificationShell shell in shells)
  314. {
  315. var notification = new NotificationRequest
  316. {
  317. BadgeNumber = 1,
  318. Description = shell.Title,
  319. Title = "New PRS Notification: ",
  320. ReturningData = shell.EntityID.ToString() + "$" + shell.EntityType,
  321. NotificationId = count,
  322. };
  323. count++;
  324. NotificationImage img = new NotificationImage();
  325. img.ResourceName = "icon16.png";
  326. notification.Image = img;
  327. await LocalNotificationCenter.Current.Show(notification);
  328. }
  329. Application.Current.Properties["LastPushedNotifications"] = DateTime.Now;
  330. }
  331. catch { }
  332. }
  333. #endregion
  334. #region Location / Bluetooth
  335. private static void LocationFound(LocationServices sender)
  336. {
  337. //if (bSharedDevice)
  338. // return;
  339. if (App.Bluetooth.RecentlyScanned)
  340. UploadTiles();
  341. try
  342. {
  343. TimeSheet timesheet = App.Data.TimeSheets?.Rows.FirstOrDefault()?.ToObject<TimeSheet>();
  344. if (timesheet != null)
  345. {
  346. if (timesheet.StartLocation.Latitude.Equals(0.0F) && timesheet.StartLocation.Longitude.Equals(0.0F))
  347. {
  348. timesheet.StartLocation.Latitude = sender.Latitude;
  349. timesheet.StartLocation.Longitude = sender.Longitude;
  350. timesheet.StartLocation.Timestamp = sender.TimeStamp;
  351. timesheet.Address = sender.Address;
  352. new Client<TimeSheet>().Save(timesheet, "Updating Timesheet with GPS Coordinates", (o, e) => { });
  353. }
  354. }
  355. if (!string.IsNullOrWhiteSpace(matchedDeviceName))
  356. {
  357. InABox.Core.Location curlocation = new InABox.Core.Location() { Latitude = App.GPS.Latitude, Longitude = App.GPS.Longitude };
  358. curlocation.Timestamp = DateTime.Now;
  359. GPSTrackerLocation gpsTrackerLocation = new GPSTrackerLocation();
  360. gpsTrackerLocation.DeviceID = matchedDeviceName;
  361. gpsTrackerLocation.Location.Timestamp = curlocation.Timestamp;
  362. gpsTrackerLocation.Location = curlocation;
  363. new Client<GPSTrackerLocation>().Save(gpsTrackerLocation, "Updated company device location from Timebench");
  364. }
  365. OnRefreshScreen?.Invoke();
  366. }
  367. catch { }
  368. }
  369. private static async void UploadTiles()
  370. {
  371. try
  372. {
  373. if (App.GPS.Latitude.Equals(0.0F) && App.GPS.Longitude.Equals(0.0F))
  374. return;
  375. if (App.Bluetooth.DetectedBlueToothMACAddresses.Count == 0)
  376. return;
  377. if (bRecentlyUpdatedTiles)
  378. return;
  379. bRecentlyUpdatedTiles = true;
  380. await Task.Run(() =>
  381. {
  382. InABox.Core.Location curlocation = new InABox.Core.Location() { Latitude = App.GPS.Latitude, Longitude = App.GPS.Longitude };
  383. curlocation.Timestamp = DateTime.Now;
  384. List<GPSTrackerLocation> trackersToUpdate = new List<GPSTrackerLocation>();
  385. foreach (String id in App.Bluetooth.DetectedBlueToothMACAddresses)
  386. {
  387. GPSTracker tracker = GlobalVariables.GPSTrackerCache.Find(x => x.DeviceID.Equals(id));
  388. bool stale = tracker.Location.Timestamp < DateTime.Now.Subtract(new TimeSpan(0, 5, 0));
  389. bool moved = tracker.Location.DistanceTo(curlocation, UnitOfLength.Kilometers) > 0.1;
  390. if (stale || moved)
  391. {
  392. GlobalVariables.GPSTrackerCache.Remove(tracker);
  393. tracker.Location = curlocation;
  394. GlobalVariables.GPSTrackerCache.Add(tracker);
  395. //cache is updated
  396. GPSTrackerLocation gpsTrackerLocation = new GPSTrackerLocation();
  397. gpsTrackerLocation.DeviceID = tracker.DeviceID;
  398. gpsTrackerLocation.Location.Timestamp = tracker.Location.Timestamp;
  399. gpsTrackerLocation.Location = curlocation;
  400. trackersToUpdate.Add(gpsTrackerLocation);
  401. }
  402. }
  403. if (trackersToUpdate.Any())
  404. {
  405. if (ClientFactory.UserGuid != Guid.Empty)
  406. new Client<GPSTrackerLocation>().Save(trackersToUpdate, "Updating Bluetooth Device Locations");
  407. }
  408. App.Bluetooth.DetectedBlueToothMACAddresses.Clear();
  409. }
  410. );
  411. }
  412. catch (Exception e)
  413. {
  414. }
  415. //if ((master != null) && (master.Location.Timestamp < DateTime.Now.Subtract(new TimeSpan(0, 15, 0))))
  416. //{
  417. // GPSTrackerLocation device = new GPSTrackerLocation();
  418. // device.DeviceID = MobileUtils.GetDeviceID();
  419. // device.Location.Latitude = App.GPS.Latitude;
  420. // device.Location.Longitude = App.GPS.Longitude;
  421. // device.Location.Timestamp = DateTime.Now;
  422. // locations.Add(device);
  423. // //device.BatteryLevel = ((double)CrossBattery.Current.RemainingChargePercent);
  424. // //new Client<GPSTrackerLocation>().Save(device, "Updating Device Location"); //, SaveTrackerCallback);
  425. //}
  426. #region OLD
  427. //for (int i = 0; i < App.Bluetooth.Devices.Length; i++)
  428. //{
  429. // String id = App.Bluetooth.Devices[i];
  430. // int level = App.Bluetooth.BatteryLevels[i];
  431. // var btmaster = trackers.FirstOrDefault(x => x.DeviceID.Equals(id));
  432. // if ((btmaster != null) && (!locations.Any(x => x.DeviceID.Equals(btmaster.DeviceID))))
  433. // {
  434. // bool stale = btmaster.Location.Timestamp < DateTime.Now.Subtract(new TimeSpan(0, 15, 0));
  435. // bool moved = btmaster.Location.DistanceTo(curlocation, UnitOfLength.Kilometers) > 0.1;
  436. // if (stale || moved)
  437. // {
  438. // GPSTrackerLocation location = new GPSTrackerLocation();
  439. // location.DeviceID = id;
  440. // location.Location.Latitude = App.GPS.Latitude;
  441. // location.Location.Longitude = App.GPS.Longitude;
  442. // location.Location.Timestamp = DateTime.Now;
  443. // location.BatteryLevel = level;
  444. // locations.Add(location);
  445. // }
  446. // }
  447. // //new Client<GPSTrackerLocation>().Save(location, "Found Kontakt Device"); //, SaveTrackerCallback);
  448. //}
  449. //if (locations.Any())
  450. // new Client<GPSTrackerLocation>().Save(locations, "Updating Bluetooth Device Locations", (o, e) => { });
  451. #endregion
  452. }
  453. private static void LocationError(LocationServices sebder, Exception error)
  454. {
  455. }
  456. private static void ScanFinished(Bluetooth sender)
  457. {
  458. try
  459. {
  460. OnRefreshScreen?.Invoke();
  461. if (App.GPS.RecentlyLocated)
  462. UploadTiles();
  463. }
  464. catch { }
  465. }
  466. #endregion
  467. public static Dictionary<Guid, string> GetTasks()
  468. {
  469. Dictionary<Guid, string> dict = new Dictionary<Guid, string>();
  470. CoreTable table = new Client<Kanban>().Query(
  471. new Filter<Kanban>(x => x.EmployeeLink.ID).IsEqualTo(GlobalVariables.EmpID)
  472. .And(x => x.Category).IsNotEqualTo("Complete"),
  473. new Columns<Kanban>(x => x.ID, x => x.Title)
  474. );
  475. foreach (CoreRow row in table.Rows)
  476. {
  477. dict.Add
  478. (
  479. row.Get<Kanban, Guid>(x => x.ID),
  480. row.Get<Kanban, string>(x => x.Title)
  481. );
  482. }
  483. return dict;
  484. }
  485. public static void OnTaskSelected(Guid ID, string title)
  486. {
  487. if (CurrentAssignment != null)
  488. OnRequestUserInput?.Invoke(ID);
  489. else
  490. CreateNewAssignment(Guid.Empty, ID);
  491. }
  492. public static void ChangeAssignmentTask(Guid taskID)
  493. {
  494. AddTaskDetails(taskID);
  495. }
  496. }
  497. }