MainPageUtils.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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. if (CurrentAssignment != null)
  129. SaveCurrentAssignment("PRS Mobile main screen - Saving assignment on 5 minute timer");
  130. }
  131. public static void SaveCurrentAssignment(string auditnote, bool complete = false)
  132. {
  133. if (!complete)
  134. CurrentAssignment.Booked.Finish = RoundToNearestInterval(DateTime.Now, new TimeSpan(0, 5, 0)).TimeOfDay;
  135. else
  136. {
  137. CurrentAssignment.Actual.Finish = RoundToNearestInterval(DateTime.Now, new TimeSpan(0, 5, 0)).TimeOfDay;
  138. CurrentAssignment.Completed = DateTime.Now;
  139. }
  140. new Client<Assignment>().Save(CurrentAssignment, auditnote);
  141. }
  142. static DateTime RoundToNearestInterval(DateTime dt, TimeSpan d)
  143. {
  144. int f = 0;
  145. double m = (double)(dt.Ticks % d.Ticks) / d.Ticks;
  146. if (m >= 0.5)
  147. f = 1;
  148. return new DateTime(((dt.Ticks / d.Ticks) + f) * d.Ticks);
  149. }
  150. public static void UseCurrentAssignment(Assignment assgn)
  151. {
  152. CurrentAssignment = assgn;
  153. SaveCurrentAssignment("PRS Mobile main screen - saving assignment on re-login to App");
  154. if (CurrentAssignment.JobLink.ID != Guid.Empty)
  155. {
  156. Job.ID = CurrentAssignment.JobLink.ID;
  157. var job = new Client<Job>().Query(new Filter<Job>(x => x.ID).IsEqualTo(Job.ID)).Rows.FirstOrDefault().ToObject<Job>();
  158. Job.JobNumber = job.JobNumber;
  159. Job.Name = job.Name;
  160. Job.OnJobIDChanged += OnJobIDChanged;
  161. }
  162. if (CurrentAssignment.Task.ID != Guid.Empty)
  163. {
  164. var task = new Client<Kanban>().Query(new Filter<Kanban>(x => x.ID).IsEqualTo(CurrentAssignment.Task.ID)).Rows.FirstOrDefault().ToObject<Kanban>();
  165. OnTaskTitleChanged?.Invoke(task.Title);
  166. }
  167. }
  168. public static void OnJobIDChanged(Guid jobid)
  169. {
  170. if (CurrentAssignment == null)
  171. CreateNewAssignment(jobid, Guid.Empty);
  172. else
  173. {
  174. SaveCurrentAssignment("PRS Mobile main screen - saving assignment on job change", true);
  175. CreateNewAssignment(jobid, Guid.Empty);
  176. }
  177. }
  178. public static void CreateNewAssignment(Guid jobid, Guid taskID)
  179. {
  180. CurrentAssignment = new Assignment { Date = DateTime.Now.Date };
  181. CurrentAssignment.EmployeeLink.ID = GlobalVariables.EmpID;
  182. CurrentAssignment.Actual.Start = RoundToNearestInterval(DateTime.Now, new TimeSpan(0, 5, 0)).TimeOfDay;
  183. CurrentAssignment.Booked.Start = CurrentAssignment.Actual.Start;
  184. CurrentAssignment.Booked.Finish = RoundToNearestInterval(DateTime.Now, new TimeSpan(0, 5, 0)).TimeOfDay.Add(new TimeSpan(0, 5, 0));
  185. if (jobid != Guid.Empty)
  186. AddJobDetails(jobid);
  187. if (taskID != Guid.Empty)
  188. AddTaskDetails(taskID);
  189. }
  190. private static void AddJobDetails(Guid jobid)
  191. {
  192. CurrentAssignment.JobLink.ID = jobid;
  193. var job = new Client<Job>().Query(new Filter<Job>(x => x.ID).IsEqualTo(jobid)).Rows.FirstOrDefault().ToObject<Job>();
  194. CurrentAssignment.Title = "Clocking on to job " + job.Name + " (" + job.JobNumber + ") on PRS Mobile";
  195. OnTaskTitleChanged?.Invoke("Task");
  196. new Client<Assignment>().Save(CurrentAssignment, "Changed job on mobile - creating new assignment");
  197. }
  198. private static void AddTaskDetails(Guid taskID)
  199. {
  200. CurrentAssignment.Task.ID = taskID;
  201. var task = new Client<Kanban>().Query(new Filter<Kanban>(x => x.ID).IsEqualTo(taskID)).Rows.FirstOrDefault().ToObject<Kanban>();
  202. CurrentAssignment.Title = "Clocking on to task " + task.Title + " on PRS Mobile";
  203. OnTaskTitleChanged?.Invoke(task.Title);
  204. if (!string.IsNullOrWhiteSpace(task.JobLink.JobNumber))
  205. Job.JobNumber = task.JobLink.JobNumber;
  206. new Client<Assignment>().Save(CurrentAssignment, "Changed task on mobile - creating new assignment");
  207. }
  208. #endregion
  209. #region Notifications
  210. public static Page DetermineCorrectPage(Plugin.LocalNotification.EventArgs.NotificationActionEventArgs e)
  211. {
  212. string data = e.Request.ReturningData;
  213. int index = data.IndexOf("$");
  214. Guid ID = Guid.Parse(data.Remove(index));
  215. string type = data.Substring(index + 1);
  216. if (type == "Comal.Classes.Kanban")
  217. return new AddEditTask(ID);
  218. else if (type == "Comal.Classes.Delivery")
  219. return new DeliveryDetails(ID);
  220. else
  221. return null;
  222. }
  223. public static async void SearchForNewNotifications()
  224. {
  225. //notifications poll reliably in the background for Anroid, unreliable for iOS due to difficulty with cross-platform plugins for notifications
  226. try
  227. {
  228. await Task.Run(() =>
  229. {
  230. if (ClientFactory.UserGuid != Guid.Empty)
  231. {
  232. CoreTable table = new Client<Notification>().Query
  233. (new Filter<Notification>(x => x.Employee.UserLink.ID).IsEqualTo(ClientFactory.UserGuid).And(X => X.Closed).IsEqualTo(DateTime.MinValue),
  234. new Columns<Notification>(
  235. x => x.ID, //0
  236. x => x.Sender.Name, //1
  237. x => x.Title, //2
  238. x => x.Created, //3
  239. x => x.Description, //4
  240. x => x.EntityType, //5
  241. x => x.EntityID //6
  242. )
  243. );
  244. if (NumberOfNotifications == table.Rows.Count()) //no new notifications or none present at all
  245. return;
  246. else //new notifications or previous notifications have now been dismissed
  247. {
  248. NumberOfNotifications = table.Rows.Count();
  249. OnMainPageNotificationsChanged?.Invoke();
  250. CheckNotificationsPushed(table);
  251. }
  252. }
  253. });
  254. }
  255. catch { }
  256. }
  257. private static void CheckNotificationsPushed(CoreTable table)
  258. {
  259. try
  260. {
  261. if (!Application.Current.Properties.ContainsKey("LastPushedNotifications"))
  262. {
  263. Application.Current.Properties.Add("LastPushedNotifications", DateTime.Now);
  264. }
  265. DateTime lastPushed = DateTime.Parse(Application.Current.Properties["LastPushedNotifications"].ToString());
  266. List<NotificationShell> toNotify = new List<NotificationShell>();
  267. foreach (CoreRow row in table.Rows)
  268. {
  269. List<object> list = row.Values;
  270. DateTime created = DateTime.Parse(list[3].ToString());
  271. if (created > new DateTime(2022, 8, 22)) // prevent spam from buildup of old notifications before this is released
  272. {
  273. if (created > lastPushed)
  274. {
  275. if (list[1] == null) list[1] = "";
  276. if (list[2] == null) list[2] = "";
  277. if (list[3] == null) list[3] = DateTime.MinValue;
  278. if (list[4] == null) list[4] = "";
  279. if (list[5] == null) list[5] = "";
  280. if (list[6] == null) list[6] = Guid.Empty;
  281. NotificationShell shell = new NotificationShell
  282. {
  283. ID = Guid.Parse(list[0].ToString()),
  284. Sender = list[1].ToString(),
  285. Title = list[2].ToString(),
  286. Created = DateTime.Parse(list[3].ToString()),
  287. EntityType = list[5].ToString(),
  288. EntityID = Guid.Parse(list[6].ToString())
  289. };
  290. toNotify.Add(shell); //add notification to be pushed
  291. }
  292. }
  293. }
  294. if (toNotify.Count > 0)
  295. PushNotificationsAsync(toNotify);
  296. }
  297. catch { }
  298. }
  299. private static async Task PushNotificationsAsync(List<NotificationShell> shells)
  300. {
  301. try
  302. {
  303. int count = 1;
  304. foreach (NotificationShell shell in shells)
  305. {
  306. var notification = new NotificationRequest
  307. {
  308. BadgeNumber = 1,
  309. Description = shell.Title,
  310. Title = "New PRS Notification: ",
  311. ReturningData = shell.EntityID.ToString() + "$" + shell.EntityType,
  312. NotificationId = count,
  313. };
  314. count++;
  315. NotificationImage img = new NotificationImage();
  316. img.ResourceName = "icon16.png";
  317. notification.Image = img;
  318. await LocalNotificationCenter.Current.Show(notification);
  319. }
  320. Application.Current.Properties["LastPushedNotifications"] = DateTime.Now;
  321. }
  322. catch { }
  323. }
  324. #endregion
  325. #region Location / Bluetooth
  326. private static void LocationFound(LocationServices sender)
  327. {
  328. //if (bSharedDevice)
  329. // return;
  330. if (App.Bluetooth.RecentlyScanned)
  331. UploadTiles();
  332. try
  333. {
  334. TimeSheet timesheet = App.Data.TimeSheets?.Rows.FirstOrDefault()?.ToObject<TimeSheet>();
  335. if (timesheet != null)
  336. {
  337. if (timesheet.StartLocation.Latitude.Equals(0.0F) && timesheet.StartLocation.Longitude.Equals(0.0F))
  338. {
  339. timesheet.StartLocation.Latitude = sender.Latitude;
  340. timesheet.StartLocation.Longitude = sender.Longitude;
  341. timesheet.StartLocation.Timestamp = sender.TimeStamp;
  342. timesheet.Address = sender.Address;
  343. new Client<TimeSheet>().Save(timesheet, "Updating Timesheet with GPS Coordinates", (o, e) => { });
  344. }
  345. }
  346. if (!string.IsNullOrWhiteSpace(matchedDeviceName))
  347. {
  348. InABox.Core.Location curlocation = new InABox.Core.Location() { Latitude = App.GPS.Latitude, Longitude = App.GPS.Longitude };
  349. curlocation.Timestamp = DateTime.Now;
  350. GPSTrackerLocation gpsTrackerLocation = new GPSTrackerLocation();
  351. gpsTrackerLocation.DeviceID = matchedDeviceName;
  352. gpsTrackerLocation.Location.Timestamp = curlocation.Timestamp;
  353. gpsTrackerLocation.Location = curlocation;
  354. new Client<GPSTrackerLocation>().Save(gpsTrackerLocation, "Updated company device location from Timebench");
  355. }
  356. OnRefreshScreen?.Invoke();
  357. }
  358. catch { }
  359. }
  360. private static async void UploadTiles()
  361. {
  362. try
  363. {
  364. if (App.GPS.Latitude.Equals(0.0F) && App.GPS.Longitude.Equals(0.0F))
  365. return;
  366. if (App.Bluetooth.DetectedBlueToothMACAddresses.Count == 0)
  367. return;
  368. if (bRecentlyUpdatedTiles)
  369. return;
  370. bRecentlyUpdatedTiles = true;
  371. await Task.Run(() =>
  372. {
  373. InABox.Core.Location curlocation = new InABox.Core.Location() { Latitude = App.GPS.Latitude, Longitude = App.GPS.Longitude };
  374. curlocation.Timestamp = DateTime.Now;
  375. List<GPSTrackerLocation> trackersToUpdate = new List<GPSTrackerLocation>();
  376. foreach (String id in App.Bluetooth.DetectedBlueToothMACAddresses)
  377. {
  378. GPSTracker tracker = GlobalVariables.GPSTrackerCache.Find(x => x.DeviceID.Equals(id));
  379. bool stale = tracker.Location.Timestamp < DateTime.Now.Subtract(new TimeSpan(0, 5, 0));
  380. bool moved = tracker.Location.DistanceTo(curlocation, UnitOfLength.Kilometers) > 0.1;
  381. if (stale || moved)
  382. {
  383. GlobalVariables.GPSTrackerCache.Remove(tracker);
  384. tracker.Location = curlocation;
  385. GlobalVariables.GPSTrackerCache.Add(tracker);
  386. //cache is updated
  387. GPSTrackerLocation gpsTrackerLocation = new GPSTrackerLocation();
  388. gpsTrackerLocation.DeviceID = tracker.DeviceID;
  389. gpsTrackerLocation.Location.Timestamp = tracker.Location.Timestamp;
  390. gpsTrackerLocation.Location = curlocation;
  391. trackersToUpdate.Add(gpsTrackerLocation);
  392. }
  393. }
  394. if (trackersToUpdate.Any())
  395. {
  396. if (ClientFactory.UserGuid != Guid.Empty)
  397. new Client<GPSTrackerLocation>().Save(trackersToUpdate, "Updating Bluetooth Device Locations");
  398. }
  399. App.Bluetooth.DetectedBlueToothMACAddresses.Clear();
  400. }
  401. );
  402. }
  403. catch (Exception e)
  404. {
  405. }
  406. //if ((master != null) && (master.Location.Timestamp < DateTime.Now.Subtract(new TimeSpan(0, 15, 0))))
  407. //{
  408. // GPSTrackerLocation device = new GPSTrackerLocation();
  409. // device.DeviceID = MobileUtils.GetDeviceID();
  410. // device.Location.Latitude = App.GPS.Latitude;
  411. // device.Location.Longitude = App.GPS.Longitude;
  412. // device.Location.Timestamp = DateTime.Now;
  413. // locations.Add(device);
  414. // //device.BatteryLevel = ((double)CrossBattery.Current.RemainingChargePercent);
  415. // //new Client<GPSTrackerLocation>().Save(device, "Updating Device Location"); //, SaveTrackerCallback);
  416. //}
  417. #region OLD
  418. //for (int i = 0; i < App.Bluetooth.Devices.Length; i++)
  419. //{
  420. // String id = App.Bluetooth.Devices[i];
  421. // int level = App.Bluetooth.BatteryLevels[i];
  422. // var btmaster = trackers.FirstOrDefault(x => x.DeviceID.Equals(id));
  423. // if ((btmaster != null) && (!locations.Any(x => x.DeviceID.Equals(btmaster.DeviceID))))
  424. // {
  425. // bool stale = btmaster.Location.Timestamp < DateTime.Now.Subtract(new TimeSpan(0, 15, 0));
  426. // bool moved = btmaster.Location.DistanceTo(curlocation, UnitOfLength.Kilometers) > 0.1;
  427. // if (stale || moved)
  428. // {
  429. // GPSTrackerLocation location = new GPSTrackerLocation();
  430. // location.DeviceID = id;
  431. // location.Location.Latitude = App.GPS.Latitude;
  432. // location.Location.Longitude = App.GPS.Longitude;
  433. // location.Location.Timestamp = DateTime.Now;
  434. // location.BatteryLevel = level;
  435. // locations.Add(location);
  436. // }
  437. // }
  438. // //new Client<GPSTrackerLocation>().Save(location, "Found Kontakt Device"); //, SaveTrackerCallback);
  439. //}
  440. //if (locations.Any())
  441. // new Client<GPSTrackerLocation>().Save(locations, "Updating Bluetooth Device Locations", (o, e) => { });
  442. #endregion
  443. }
  444. private static void LocationError(LocationServices sebder, Exception error)
  445. {
  446. }
  447. private static void ScanFinished(Bluetooth sender)
  448. {
  449. try
  450. {
  451. OnRefreshScreen?.Invoke();
  452. if (App.GPS.RecentlyLocated)
  453. UploadTiles();
  454. }
  455. catch { }
  456. }
  457. #endregion
  458. public static Dictionary<Guid, string> GetTasks()
  459. {
  460. Dictionary<Guid, string> dict = new Dictionary<Guid, string>();
  461. CoreTable table = new Client<Kanban>().Query(
  462. new Filter<Kanban>(x => x.EmployeeLink.ID).IsEqualTo(GlobalVariables.EmpID)
  463. .And(x => x.Category).IsNotEqualTo("Complete"),
  464. new Columns<Kanban>(x => x.ID, x => x.Title)
  465. );
  466. foreach (CoreRow row in table.Rows)
  467. {
  468. dict.Add
  469. (
  470. row.Get<Kanban, Guid>(x => x.ID),
  471. row.Get<Kanban, string>(x => x.Title)
  472. );
  473. }
  474. return dict;
  475. }
  476. public static void OnTaskSelected(Guid ID, string title)
  477. {
  478. if (CurrentAssignment != null)
  479. OnRequestUserInput?.Invoke(ID);
  480. else
  481. CreateNewAssignment(Guid.Empty, ID);
  482. }
  483. public static void ChangeAssignmentTask(Guid taskID)
  484. {
  485. AddTaskDetails(taskID);
  486. }
  487. }
  488. }