| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974 | 
							- using System;
 
- using System.Collections.Generic;
 
- using System.IO;
 
- using System.Linq;
 
- using System.Threading;
 
- using System.Threading.Tasks;
 
- using Xamarin.Forms;
 
- using InABox.Core;
 
- using InABox.Clients;
 
- using InABox.Mobile;
 
- using Comal.Classes;
 
- using InABox.Configuration;
 
- using JetBrains.Annotations;
 
- using XF.Material.Forms.UI.Dialogs;
 
- using Comal.Classes.SecurityDescriptors;
 
- namespace PRS.Mobile
 
- {
 
-     public class MainPageSettings : ILocalConfigurationSettings
 
-     {
 
-         public bool ClockOnDisabled
 
-         {
 
-             get;
 
-             set;
 
-         }
 
-         public MainPageSettings()
 
-         {
 
-             ClockOnDisabled = true;
 
-         }
 
-         
 
-     }
 
-     
 
-     public partial class MainPage
 
-     {
 
-         #region Fields
 
-         
 
-         private readonly MainPageSettings _settings;
 
-         private readonly Dictionary<String, MobileDatabaseSettings> _profiles;
 
-         private TimeSheet _currenttimesheet;
 
-         
 
-         #endregion
 
-         #region Constructor
 
-         private readonly Task[] _setuptasks;
 
-         
 
-         public MainPage()
 
-         {
 
-             
 
-             _settings = new LocalConfiguration<MainPageSettings>().Load();
 
-             
 
-             _setuptasks = new Task[]
 
-             {
 
-                 Task.Run(() => App.Data.GPSTrackers.Refresh(false)),
 
-                 Task.Run(() => App.Data.BluetoothGates.Refresh(false)),
 
-                 Task.Run(() => CheckTimeSheet())
 
-             };
 
-             
 
-             App.Data.GPSLocationUpdated += OnGPSLocationUpdated;
 
-             App.Data.BluetoothScanFinished += OnBluetothScanFinished;
 
-             
 
-             InitializeComponent();
 
-             
 
-             ProgressVisible = true;
 
-             
 
-             var logo = Path.Combine(CoreRepository.CacheFolder(), "logo.png");
 
-             if (File.Exists(logo))
 
-                 _splash.Source = FileImageSource.FromFile(logo);
 
-             else
 
-                 _splash.Source = ImageSource.FromFile("splash");
 
-             
 
-             _profiles = new LocalConfiguration<MobileDatabaseSettings>().LoadAll();
 
-             if (_profiles.Count > 1)
 
-             {
 
-                 _menu.Items.Clear();
 
-                 _menu.Clicked -= SettingsTapped;
 
-                 foreach (var profile in _profiles)
 
-                 {
 
-                     var item = new MobileMenuItem()
 
-                     {
 
-                         Image = profile.Value.IsDefault ? ImageSource.FromFile("tick") : null,
 
-                         Text = profile.Key,
 
-                         BindingContext = profile.Value
 
-                     };
 
-                     item.Clicked += ProfileTapped;
 
-                     _menu.Items.Add(item);
 
-                 }
 
-                 _menu.Items.Add(new MobileMenuSeparator());
 
-                 var settings = new MobileMenuItem()
 
-                 {
 
-                     Text = "Settings"
 
-                 };
 
-                 settings.Clicked += SettingsTapped;
 
-                 _menu.Items.Add(settings);
 
-             }
 
-             
 
-             BackButtonEnabled = App.IsSharedDevice;
 
-             SetupModules();
 
-             
 
-             // LocalNotificationCenter.Current.NotificationActionTapped += (Plugin.LocalNotification.EventArgs.NotificationActionEventArgs e) =>
 
-             // {
 
-             //     if (MainPageUtils.DetermineCorrectPage(e) != null)
 
-             //     {
 
-             //         Device.BeginInvokeOnMainThread(() =>
 
-             //         {
 
-             //             Navigation.PushAsync(MainPageUtils.DetermineCorrectPage(e));
 
-             //         });
 
-             //     }
 
-             // };
 
-             //
 
-             // MainPageUtils.OnMainPageNotificationsChanged += RefreshOnNotificationsChange;
 
-             
 
-             MessagingCenter.Subscribe<App>(this, App.MessageOnResume,
 
-                 (o) =>
 
-                 {
 
-                     if (!App.GPS.RecentlyLocated)
 
-                         App.GPS.GetLocation();
 
-                     RefreshScreen();
 
-                 }
 
-             );
 
-    
 
-         }
 
-         private async void ProfileTapped(object sender, EventArgs e)
 
-         {
 
-             if (sender is MobileMenuItem item && item.BindingContext is MobileDatabaseSettings settings)
 
-             {
 
-                 foreach (var profile in _profiles)
 
-                     profile.Value.IsDefault = profile.Value == settings;
 
-                 new LocalConfiguration<MobileDatabaseSettings>().SaveAll(_profiles);
 
-                 
 
-                 ClientFactory.InvalidateUser();
 
-                 if (App.Current.Properties.ContainsKey("SessionID"))
 
-                     App.Current.Properties.Remove("SessionID");
 
-             
 
-                 App.Data.Reset();
 
-                 
 
-                 ClientFactory.InvalidateUser();
 
-                 if (App.Current.Properties.ContainsKey("SessionID"))
 
-                     App.Current.Properties.Remove("SessionID");
 
-             
 
-                 App.Data.Reset();
 
-             
 
-                 TransportStatus connection = TransportStatus.None;
 
-                 using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Connecting"))
 
-                 {
 
-                 
 
-                     await Task.Run(() =>
 
-                     {
 
-                         CoreRepository.CacheID = settings.CacheID;
 
-                         connection = App.ConnectTransport(settings.URLs);
 
-                         var info = App.Transport.Info();
 
-                         if (info?.Logo?.Any() == true)
 
-                             File.WriteAllBytes(Path.Combine(CoreRepository.CacheFolder(),"logo.png"), info.Logo);
 
-                     });
 
-                 }
 
-                 if (connection != TransportStatus.OK)
 
-                 {
 
-                     await DisplayAlert("Connection Error", $"Unable to establish a connection!\n\nERR: {connection}", "OK");
 
-                     return;
 
-                 }
 
-                 
 
-                 Navigation.PopAsync();
 
-             }
 
-         }
 
-         #endregion
 
-         #region OnAppearing and Display
 
-         private SelectionPage _selectionpage;
 
-         protected override void OnAppearing()
 
-         {
 
-             _pollingToken = new CancellationTokenSource();
 
-             
 
-             StartMonitoringNotifications();
 
-             StartMonitoringTasks();
 
-             StartMonitoringVersion();
 
-             StartMonitoringEmpoyeeDetails();
 
-             
 
-             //ProgressVisible = false;
 
-             if (_selectionpage == null)
 
-             {
 
-                 CheckTimeSheet();
 
-             }
 
-             
 
-             _selectionpage = null;
 
-             Task.WaitAll(_setuptasks);
 
-             
 
-             RefreshScreen();
 
-             base.OnAppearing();
 
-         }
 
-         
 
-         protected override void OnDisappearing()
 
-         {
 
-             EnableModules(false);
 
-             _pollingToken.Cancel();
 
-             base.OnDisappearing();
 
-         }
 
-         
 
-         private void OnBluetothScanFinished(BluetoothEventArgs args)
 
-         {
 
-             Dispatcher.BeginInvokeOnMainThread(RefreshScreen);
 
-         }
 
-         private void OnGPSLocationUpdated(GPSEventArgs args)
 
-         {
 
-             Dispatcher.BeginInvokeOnMainThread(RefreshScreen);
 
-         }
 
-         
 
-         void SetupClockOnButton(bool disable)
 
-         {
 
-             if (disable != _settings.ClockOnDisabled)
 
-             {
 
-                 _settings.ClockOnDisabled = disable;
 
-                 new LocalConfiguration<MainPageSettings>().Save(_settings);
 
-             }
 
-             
 
-             SplashCard.IsVisible = disable;
 
-             
 
-             ClockOnButton.IsVisible = !disable;
 
-             jobBtn.IsVisible = !disable;
 
-             addNoteBtn.IsVisible = !disable;
 
-             taskBtn.IsVisible = !disable;
 
-         }
 
-         private void SetupModules()
 
-         {          
 
-             SetupClockOnButton(Security.IsAllowed<IsJobOnlyEmployee>());
 
-             
 
-             Tools.BeginUpdate();
 
-             Assignments.IsVisible = Security.IsAllowed<ViewMobileAssignmentsModule>();
 
-             Deliveries.IsVisible = Security.IsAllowed<ViewMobileDeliveriesModule>();
 
-             Forms.IsVisible = Security.IsAllowed<ViewMobileFormsModule>();
 
-             Equipment.IsVisible = Security.IsAllowed<ViewMobileEquipmentModule>();
 
-             MyHR.IsVisible =
 
-                 Security.IsAllowed<ViewMobileEmployeeDetailsModule>()
 
-                 || Security.IsAllowed<ViewMobileEmployeeFormsModule>()
 
-                 || Security.IsAllowed<ViewMobileLeaveRequestsModule>()
 
-                 || Security.IsAllowed<ViewMobileQualificationsModule>()
 
-                 || Security.IsAllowed<ViewMobileTimesheetsModule>();
 
-                
 
-             InOut.IsVisible = Security.IsAllowed<ViewMobileInOutModule>();
 
-             Manufacturing.IsVisible = Security.IsAllowed<ViewMobileManufacturingModule>();
 
-             Meetings.IsVisible = Security.IsAllowed<ViewMobileMeetingsModule>();
 
-             Products.IsVisible = Security.IsAllowed<ViewMobileProductsModule>();
 
-             PurchaseOrders.IsVisible = Security.IsAllowed<ViewMobilePurchaseOrdersModule>();
 
-             Scans.IsVisible = Security.IsAllowed<ViewMobileDocumentScannerModule>();
 
-             MyTasks.IsVisible = Security.IsAllowed<ViewMobileMyTasksModule>();
 
-             Site.IsVisible = Security.IsAllowed<ViewMobileSiteModule>();
 
-             Warehousing.IsVisible = Security.IsAllowed<ViewMobileWarehousingModule>();
 
-             Tools.EndUpdate();
 
-         }
 
-         
 
-         protected override void UpdateTransportStatus()
 
-         {
 
-             base.UpdateTransportStatus();
 
-             
 
-             CheckTimeSheet();
 
-             
 
-             EnableModules(App.Data.IsConnected());
 
-         }
 
-         private void EnableModules(bool enabled)
 
-         {
 
-             Tools.BeginUpdate();
 
-             foreach (var item in Tools.Items)
 
-                 item.IsEnabled = enabled;
 
-             Tools.EndUpdate();
 
-         }
 
-         private CancellationTokenSource _pollingToken = new CancellationTokenSource();
 
-         
 
-         private void StartMonitoringNotifications()
 
-         {
 
-             //notifications are allowed to upload once every 30 seconds
 
-             // This will eventually be replaced with websocket pushes
 
-             // but we're not ready for that yet :-(
 
-             var token = _pollingToken.Token;
 
-             Task.Run(
 
-                 () =>
 
-                 {
 
-                     while (!_pollingToken.Token.IsCancellationRequested)
 
-                     {
 
-                         App.Data.Notifications.Refresh(true);
 
-                         int _notificationcount = App.Data.Notifications.Items.Where(x=>x.Closed.IsEmpty()).ToList().Count;
 
-                         Dispatcher.BeginInvokeOnMainThread(() =>
 
-                         {
 
-                             Notifications.Alert = _notificationcount > 0
 
-                                 ? _notificationcount.ToString()
 
-                                 : "";
 
-                         });
 
-                         
 
-                         Task.Delay(TimeSpan.FromSeconds(30), token)
 
-                             .Wait(token);
 
-                     }
 
-                 },
 
-                 token
 
-             );
 
-         }
 
-         
 
-         private void StartMonitoringTasks()
 
-         {
 
-             var token = _pollingToken.Token;
 
-             Task.Run(
 
-                 async () =>
 
-                 {
 
-                     while (!_pollingToken.Token.IsCancellationRequested)
 
-                     {
 
-                         App.Data.Kanbans.Refresh(true);
 
-                         int _taskcount = App.Data.Kanbans.Items.Where(x=>x.Status == KanbanStatus.Open).ToList().Count;
 
-                         Dispatcher.BeginInvokeOnMainThread(() =>
 
-                         {
 
-                             MyTasks.Alert = _taskcount > 0
 
-                                 ? _taskcount.ToString()
 
-                                 : "";
 
-                         });
 
-                         await Task.Delay(TimeSpan.FromSeconds(30), token);
 
-                     }
 
-                 },
 
-                 token
 
-             );
 
-         }
 
-         
 
-         private void StartMonitoringVersion()
 
-         {
 
-             var token = _pollingToken.Token;
 
-             Task.Run(
 
-                 async () =>
 
-                 {
 
-                     while (!_pollingToken.Token.IsCancellationRequested)
 
-                     {
 
-                         bool isLatest = true;
 
-                         try
 
-                         {
 
-                             isLatest = await MobileUtils.AppVersion.IsUsingLatestVersion();
 
-                         }
 
-                         catch (Exception e)
 
-                         {
 
-                            InABox.Mobile.MobileLogging.Log(e);
 
-                         }
 
-                         //bool isLatest = MobileUtils.AppVersion.IsUsingLatestVersion().Result;
 
-                         
 
-                         Dispatcher.BeginInvokeOnMainThread(() =>
 
-                         {
 
-                             if (Update.IsVisible == isLatest)
 
-                             {
 
-                                 Update.IsVisible = !isLatest;
 
-                                 Tools.Refresh();
 
-                                 Update.Alert = isLatest
 
-                                     ? ""
 
-                                     : "!";                     
 
-                             }
 
-                         });
 
-                         
 
-                         Task.Delay(TimeSpan.FromSeconds(30), token)
 
-                             .Wait(token);
 
-                     }
 
-                 },
 
-                 token
 
-             );
 
-         }
 
-         
 
-         private void StartMonitoringEmpoyeeDetails()
 
-         {
 
-             var token = _pollingToken.Token;
 
-             Task.Run(() =>
 
-                  {
 
-                     while (!_pollingToken.Token.IsCancellationRequested)
 
-                     {
 
-                         App.Data.EmployeeQualifications.Refresh(true);
 
-                         bool bQualificationIssues = App.Data.EmployeeQualifications.NeedsAttention > 0;
 
-                         App.Data.EmployeeForms.Refresh(true);
 
-                         bool bFormIssues = App.Data.EmployeeForms.NeedsAttention > 0;
 
-                         
 
-                         Dispatcher.BeginInvokeOnMainThread(() =>
 
-                         {
 
-                             MyHR.Alert = (bQualificationIssues || bFormIssues)
 
-                                 ? "!"
 
-                                 : "";
 
-                         });
 
-                         
 
-                         Task.Delay(TimeSpan.FromSeconds(30), token)
 
-                             .Wait(token);
 
-                     }
 
-                  },
 
-                 token
 
-             );
 
-         }
 
-         
 
-         private void RefreshScreen()
 
-         {
 
-             
 
-             SetupClockOnButton(_settings.ClockOnDisabled);
 
-             
 
-             bool GateReady = CheckLocation();
 
-             var location = GetAddress();
 
-             
 
-             Title = App.Data.Me != null 
 
-                 ? App.Data.Me.Name
 
-                 : "(Not Logged In)";
 
-             ClockOnButton.IsEnabled = App.Data.IsConnected() && GateReady;
 
-             ClockOnButton.IsClickable = ClockOnButton.IsEnabled;
 
-             ClockOnOffLabel.Text = GateReady ? _currenttimesheet == null ? "CLOCK ON" : "CLOCK OFF" : "PLEASE WAIT";
 
-             CurrentLocation.Text = location.ToUpper().Contains("ERROR")
 
-                 ? "Unknown Address"
 
-                 : location;
 
-             
 
-             ClockOnButton.BackgroundColor = App.Data.IsConnected() && GateReady 
 
-                 ? _currenttimesheet == null 
 
-                     ? Color.FromHex("#e6e6fa") 
 
-                     : Color.FromHex("#15C7C1") 
 
-                 : Color.Silver;
 
-             
 
-             ClockOnButton.BorderColor = App.Data.IsConnected() && GateReady 
 
-                 ? _currenttimesheet == null 
 
-                     ? Color.FromHex("#96969a") 
 
-                     : Color.FromHex("#059791") 
 
-                 : Color.Gray;
 
-             
 
-             addNoteBtn.IsEnabled = App.Data.IsConnected() && _currenttimesheet != null;
 
-             jobBtn.IsEnabled = App.Data.IsConnected() && _currenttimesheet != null;
 
-             taskBtn.IsEnabled = App.Data.IsConnected() && _currenttimesheet != null;
 
-             
 
-             jobBtn.Text = ((_currenttimesheet?.JobID ?? Guid.Empty) != Guid.Empty)
 
-                 ? $"{_currenttimesheet?.JobLink.JobNumber}: {_currenttimesheet?.JobLink.Name}"
 
-                 : "Select Job";
 
-             
 
-            
 
-         }
 
-         
 
-         #endregion
 
-         #region Clock on/off
 
-         
 
-         private void CheckTimeSheet()
 
-         {
 
-             if (App.Data.IsConnected())
 
-             {
 
-                 var client = new Client<TimeSheet>();
 
-                 var filter = new Filter<TimeSheet>(x => x.EmployeeLink.ID).IsEqualTo(App.Data.Me.ID)
 
-                     .And(x => x.Date).IsEqualTo(DateTime.Today)
 
-                     .And(x => x.Finish).IsEqualTo(TimeSpan.Zero);
 
-                 var table = client.Query(
 
-                     filter, 
 
-                     Columns.None<TimeSheet>()
 
-                         .Add(x=>x.ID)
 
-                         .Add(x=>x.Date)
 
-                         .Add(x=>x.Start)
 
-                         .Add(x=>x.StartLocation.Longitude)
 
-                         .Add(x=>x.StartLocation.Latitude)
 
-                         .Add(x=>x.StartLocation.Address)
 
-                         .Add(x=>x.StartLocation.Timestamp)
 
-                         .Add(x=>x.Finish)
 
-                         .Add(x=>x.FinishLocation.Longitude)
 
-                         .Add(x=>x.FinishLocation.Latitude)
 
-                         .Add(x=>x.FinishLocation.Address)
 
-                         .Add(x=>x.FinishLocation.Timestamp)
 
-                         .Add(x=>x.JobLink.ID)
 
-                         .Add(x=>x.JobLink.JobNumber)
 
-                         .Add(x=>x.JobLink.Name)
 
-                         .Add(x=>x.Notes)
 
-                     );
 
-                 _currenttimesheet = table.Rows.FirstOrDefault()?.ToObject<TimeSheet>();
 
-                     
 
-                 var token = _pollingToken.Token;
 
-                 Task.Run(
 
-                     () =>
 
-                     {
 
-                         while (!token.IsCancellationRequested)
 
-                         {
 
-                             if (_currenttimesheet != null)
 
-                             {
 
-                                 if (_currenttimesheet.Date != DateTime.Today)
 
-                                 {
 
-                                     var here = GPSLocation();
 
-                                     _currenttimesheet.FinishLocation.CopyFrom(here);
 
-                                     _currenttimesheet.Finish = TimeSpan.FromDays(1).Subtract(TimeSpan.FromSeconds(1));
 
-                                     new Client<TimeSheet>().Save(_currenttimesheet,
 
-                                         "Closing Timesheet at Midnight on Mobile Device");
 
-                                     CreateTimeSheet(_currenttimesheet.JobLink.ID, _currenttimesheet.JobLink.JobNumber, _currenttimesheet.JobLink.Name, here, here.Address, "Creating Timeheet at Midnight on Mobile Device");
 
-                                 }
 
-                             }
 
-                             Task.Delay(TimeSpan.FromMinutes(1), token).Wait(token);
 
-                         }
 
-                     },
 
-                     token
 
-                 );
 
-             }
 
-         }
 
-         
 
-         private DateTime _debounce = DateTime.MinValue;
 
-         
 
-         async void ClockOnOff_Clicked(object sender, EventArgs e)
 
-         {
 
-             
 
-             if (_debounce > DateTime.Now.Subtract(TimeSpan.FromMilliseconds(500)))
 
-                 return;
 
-             _debounce = DateTime.MaxValue;
 
-             
 
-             string chosenOption = _currenttimesheet != null
 
-                 ? await DisplayActionSheet("Clock off?", "Cancel", null, "Continue", "Cancel")
 
-                 : "Continue";
 
-             
 
-             if (!String.Equals(chosenOption, "Continue"))
 
-             {
 
-                 _debounce = DateTime.Now;
 
-                 return;
 
-             }
 
-             
 
-             try
 
-             {
 
-                 using (await MaterialDialog.Instance.LoadingDialogAsync(message: $"Clocking {(_currenttimesheet == null ? "On" : "Off")}"))
 
-                 {
 
-                     var here = GPSLocation();
 
-                     
 
-                     if (_currenttimesheet != null)
 
-                     {
 
-                         FinishTimeSheet(here);
 
-                     }
 
-                     else
 
-                     {
 
-                         if (!Security.IsAllowed<CanBypassBluetoothGates>())
 
-                         {
 
-                             var tracker = App.Data.BluetoothGates.FirstOrDefault(x => App.Bluetooth.Devices.Contains(x.DeviceID));
 
-                             if (tracker != null)
 
-                                 CreateTimeSheet(tracker.JobID, tracker.JobNumber, tracker.JobName, here, here.Address, "Clocking On");
 
-                         }
 
-                         else
 
-                         { 
 
-                             if ((!here.Latitude.Equals(0.0F) && !here.Longitude.Equals(0.0F)) 
 
-                                 || Security.IsAllowed<CanBypassGPSClockIn>())
 
-                             {
 
-                                 var job = await ChooseNearbyJob(here);
 
-                                 CreateTimeSheet(job?.ID ?? Guid.Empty, job?.JobNumber ?? string.Empty, job?.Name ?? string.Empty , here, here.Address, "Clocking On");
 
-                             }
 
-                         }
 
-                     }
 
-                     Dispatcher.BeginInvokeOnMainThread(RefreshScreen);
 
-                 }
 
-             }
 
-             catch (Exception ex)
 
-             {
 
-                 InABox.Mobile.MobileLogging.Log(ex);
 
-             }
 
-             
 
-             _debounce = DateTime.Now; 
 
-         }
 
-         
 
-                 
 
-         private void FinishTimeSheet(Location here)
 
-         {
 
-             if ((_currenttimesheet == null) || (_currenttimesheet.ID == Guid.Empty))
 
-             {
 
-                 _currenttimesheet = null;
 
-                 return;
 
-             }
 
-             try
 
-             {
 
-                 if (ZeroLengthTimesheet())
 
-                     new Client<TimeSheet>().Delete(_currenttimesheet, "Deleted due to zero duration timesheet");
 
-                 else
 
-                 {
 
-                     _currenttimesheet.Finish =
 
-                         new TimeSpan(DateTime.Now.TimeOfDay.Hours, DateTime.Now.TimeOfDay.Minutes, 0);
 
-                     _currenttimesheet.FinishLocation .CopyFrom(here);
 
-                     //bUpdatingTimesheet = true;
 
-                     new Client<TimeSheet>().Save(_currenttimesheet, "Clocking Off");
 
-                 }
 
-                 _currenttimesheet = null;
 
-             }
 
-             catch (Exception ex)
 
-             {
 
-                 InABox.Mobile.MobileLogging.Log(ex);
 
-             }
 
-         }
 
-         
 
-         #endregion
 
-         #region Utilities
 
-         
 
-         //private void CheckNotificationsPushed(CoreTable table)
 
-         //{
 
-             // try
 
-             // {
 
-             //     if (!Application.Current.Properties.ContainsKey("LastPushedNotifications"))
 
-             //     {
 
-             //         Application.Current.Properties.Add("LastPushedNotifications", DateTime.Now);
 
-             //     }
 
-             //     DateTime lastPushed = DateTime.Parse(Application.Current.Properties["LastPushedNotifications"].ToString());
 
-             //     List<NotificationShell> toNotify = new List<NotificationShell>();
 
-             //     foreach (CoreRow row in table.Rows)
 
-             //     {
 
-             //         List<object> list = row.Values;
 
-             //         DateTime created = DateTime.Parse(list[3].ToString());
 
-             //         if (created > new DateTime(2022, 8, 22)) // prevent spam from buildup of old notifications before this is released
 
-             //         {
 
-             //             if (created > lastPushed)
 
-             //             {
 
-             //                 if (list[1] == null) list[1] = "";
 
-             //                 if (list[2] == null) list[2] = "";
 
-             //                 if (list[3] == null) list[3] = DateTime.MinValue;
 
-             //                 if (list[4] == null) list[4] = "";
 
-             //                 if (list[5] == null) list[5] = "";
 
-             //                 if (list[6] == null) list[6] = Guid.Empty;
 
-             //
 
-             //                 NotificationShell shell = new NotificationShell
 
-             //                 {
 
-             //                     ID = Guid.Parse(list[0].ToString()),
 
-             //                     Sender = list[1].ToString(),
 
-             //                     Title = list[2].ToString(),
 
-             //                     Created = DateTime.Parse(list[3].ToString()),
 
-             //                     EntityType = list[5].ToString(),
 
-             //                     EntityID = Guid.Parse(list[6].ToString())
 
-             //                 };
 
-             //                 toNotify.Add(shell); //add notification to be pushed
 
-             //             }
 
-             //         }
 
-             //     }
 
-             //     if (toNotify.Count > 0)
 
-             //         PushNotificationsAsync(toNotify);
 
-             // }
 
-             // catch { }
 
-         //}
 
-         //private async Task PushNotificationsAsync(List<NotificationShell> shells)
 
-         //{
 
-             // try
 
-             // {
 
-             //     int count = 1;
 
-             //
 
-             //     foreach (NotificationShell shell in shells)
 
-             //     {
 
-             //         var notification = new NotificationRequest
 
-             //         {
 
-             //             BadgeNumber = 1,
 
-             //             Description = shell.Title,
 
-             //             Title = "New PRS Notification: ",
 
-             //             ReturningData = shell.EntityID.ToString() + "$" + shell.EntityType,
 
-             //             NotificationId = count,
 
-             //         };
 
-             //         count++;
 
-             //         NotificationImage img = new NotificationImage();
 
-             //         img.ResourceName = "icon16.png";
 
-             //         notification.Image = img;
 
-             //
 
-             //         await LocalNotificationCenter.Current.Show(notification);
 
-             //     }
 
-             //     Application.Current.Properties["LastPushedNotifications"] = DateTime.Now;
 
-             // }
 
-             // catch { }
 
-         //}
 
-         
 
-         private async Task<JobShell> ChooseNearbyJob(Location here)
 
-         {
 
-             var nearbyjobs = App.Data.Jobs.Where(x => here.DistanceTo(x.Location, UnitOfLength.Kilometers) < 1.0F)
 
-                 .ToArray();
 
-             if (nearbyjobs.Length > 1)
 
-             {
 
-                 Dictionary<String, JobShell> dict = new Dictionary<string, JobShell>();
 
-                 foreach (var job in nearbyjobs)
 
-                     dict[job.DisplayName] = job;
 
-                 string chosenOption = await DisplayActionSheet("Choose job site", "Cancel", null, dict.Keys.ToArray());
 
-                 if (string.IsNullOrEmpty(chosenOption) || chosenOption.Equals("Cancel"))
 
-                     return null;
 
-                 return dict[chosenOption];
 
-             }
 
-             return nearbyjobs.FirstOrDefault();
 
-         }
 
-         void AddNote_Tapped(object sender, EventArgs e)
 
-         {
 
-             if (_currenttimesheet == null)
 
-                 return;
 
-             var notepage = new TimeSheetNotePage(_currenttimesheet);
 
-             Navigation.PushAsync(notepage);
 
-         }
 
-         private void TaskBtn_Tapped(object sender, EventArgs e)
 
-         {
 
-             _selectionpage = new TaskSelectionPage( (task) =>
 
-                 {
 
-                     if (_currenttimesheet != null)
 
-                     {
 
-                         // Not sure hwat to do here...
 
-                     }
 
-                 }
 
-             );
 
-             Navigation.PushAsync(_selectionpage);
 
-         }
 
-         // private async void RequestUserInput(Guid taskID)
 
-         // {
 
-         //     const string addtask = "Change current assignment task";
 
-         //     const string newassignment = "Start a new assignment with this task";
 
-         //     string chosenOption = await DisplayActionSheet("Choose an option", "Cancel", null, addtask, newassignment);
 
-         //     switch (chosenOption)
 
-         //     {
 
-         //         case addtask:
 
-         //             MainPageUtils.ChangeAssignmentTask(taskID);
 
-         //             break;
 
-         //         case newassignment:
 
-         //             MainPageUtils.SaveCurrentAssignment("PRS Mobile main screen - saving assignment on task change", true);
 
-         //             MainPageUtils.CreateNewAssignment(Guid.Empty, taskID);
 
-         //             break;
 
-         //         default:
 
-         //             break;
 
-         //     }
 
-         // }
 
-         private void SaveSiteModuleJobID([CanBeNull] JobShell job)
 
-         {
 
-             using (var config = new LocalConfiguration<SiteModuleSettings>())
 
-             {
 
-                 var _sitesettings = config.Load();
 
-                 _sitesettings.JobID = job?.ID ?? Guid.Empty;
 
-                 config.Save(_sitesettings);
 
-             }
 
-         }
 
-         private void JobBtn_Tapped(object sender, EventArgs e)
 
-         {
 
-             _selectionpage = new JobSelectionPage(
 
-                 (job) =>
 
-                 {
 
-                     if (_currenttimesheet != null)
 
-                     {
 
-                         if (ZeroLengthTimesheet())
 
-                         {
 
-                             _currenttimesheet.JobLink.ID = job?.ID ?? Guid.Empty;
 
-                             _currenttimesheet.JobLink.JobNumber = job?.JobNumber ?? string.Empty;
 
-                             _currenttimesheet.JobLink.Name = job?.Name ?? string.Empty;
 
-                             new Client<TimeSheet>().Save(_currenttimesheet,"Updated Job Number via mobile device");
 
-                         }
 
-                         else
 
-                         {
 
-                             var here = GPSLocation();
 
-                             FinishTimeSheet(here);
 
-                             CreateTimeSheet(job?.ID ?? Guid.Empty, job?.JobNumber ?? string.Empty, job?.Name ?? string.Empty, here, here.Address,
 
-                                 "Switched Jobs via Mobile Device");
 
-                         }
 
-                     }
 
-                     SaveSiteModuleJobID(job);
 
-                 }
 
-                 
 
-             );
 
-             Navigation.PushAsync(_selectionpage);
 
-         }
 
-         
 
-         // private bool CheckTimeSheetAgainstGates(TimeSheet timesheet)
 
-         // {
 
-         //     DateTime now = DateTime.Now;
 
-         //
 
-         //     //var timesheet = CurrentTimeSheet();
 
-         //
 
-         //     //Can't confirm if there is no timesheet
 
-         //     if (timesheet == null)
 
-         //         return false;
 
-         //
 
-         //     // Can't confirm if there are no devices
 
-         //     if (App.Bluetooth.Devices.Length == 0)
 
-         //         return false;
 
-         //
 
-         //     if (App.Data.Gates == null)
 
-         //         return false;
 
-         //
 
-         //     long tsTicks = timesheet.Date.Add(timesheet.Start).Ticks;
 
-         //     long btTicks = App.Bluetooth.TimeStamp.Ticks;
 
-         //
 
-         //     if (Math.Abs(tsTicks - btTicks) > new TimeSpan(0, 2, 0).Ticks)
 
-         //         return false;
 
-         //
 
-         //     CoreRow firstgate = null;
 
-         //     List<String> gates = new List<string>();
 
-         //     // Scan every located d
 
-         //     foreach (var device in App.Bluetooth.Devices)
 
-         //     {
 
-         //         CoreRow gate = App.Data.Gates?.Rows.FirstOrDefault(r => r.Get<JobTracker, String>(c => c.TrackerLink.DeviceID) == device);
 
-         //         if (gate != null)
 
-         //         {
 
-         //
 
-         //             if ((gate.Get<JobTracker, bool>(x => x.IsJobSite) == true) && (firstgate == null))
 
-         //                 firstgate = gate;
 
-         //
 
-         //             gates.Add(gate.Get<JobTracker, String>(x => x.Gate));
 
-         //         }
 
-         //     }
 
-         //     if (gates.Any())
 
-         //     {
 
-         //         timesheet.Gate = String.Join(", ", gates.OrderBy(x => x));
 
-         //         if (firstgate != null)
 
-         //         {
 
-         //             timesheet.JobLink.ID = firstgate.Get<JobTracker, Guid>(x => x.JobLink.ID);
 
-         //             timesheet.JobLink.JobNumber = firstgate.Get<JobTracker, String>(x => x.JobLink.JobNumber);
 
-         //             timesheet.JobLink.Name = firstgate.Get<JobTracker, String>(x => x.JobLink.Name);
 
-         //         }
 
-         //         return true;
 
-         //         //new Client<TimeSheet>().Save(timesheet, "Confirmed Gate Entry by Bluetooth Tracker", (o, e) => { });
 
-         //     }
 
-         //
 
-         //     return false;
 
-         //
 
-         // }
 
-         private bool ZeroLengthTimesheet()
 
-         {
 
-             if (_currenttimesheet == null)
 
-                 return true;
 
-             
 
-             if (!String.IsNullOrWhiteSpace(_currenttimesheet.Notes))
 
-                 return false;
 
-             
 
-             if (_currenttimesheet.Date.Equals(DateTime.Today))
 
-             {
 
-                 var diff = (DateTime.Now.TimeOfDay - _currenttimesheet.Start).TotalSeconds;
 
-                 if (Math.Abs(diff) < 120.0F)
 
-                     return true;
 
-             }
 
-             return false;
 
-         }
 
-         private void CreateTimeSheet(Guid jobid, string jobnumber, String jobname, Location location, String address, String auditmessage)
 
-         {
 
-             try
 
-             {
 
-                 _currenttimesheet = new TimeSheet
 
-                 {
 
-                     Date = DateTime.Today,
 
-                     Address = address,
 
-                     SoftwareVersion = MobileUtils.AppVersion.InstalledVersionNumber + App.DeviceString,
 
-                     Start = (DateTime.Now - DateTime.Today).Floor(TimeSpan.FromMinutes(1))
 
-                 };
 
-                 _currenttimesheet.EmployeeLink.ID = App.Data.Me.ID;
 
-                 _currenttimesheet.JobLink.ID = jobid;
 
-                 _currenttimesheet.JobLink.JobNumber = jobnumber;
 
-                 _currenttimesheet.JobLink.Name = jobname;
 
-                 _currenttimesheet.StartLocation.CopyFrom(location);
 
-                 new Client<TimeSheet>().Save(_currenttimesheet, auditmessage);
 
-             }
 
-             catch (Exception ex)
 
-             {
 
-                 InABox.Mobile.MobileLogging.Log(ex);
 
-             }
 
-         }
 
-         private bool CheckLocation()
 
-         {
 
-             // a 15 minute timeout is awfully long for this process.
 
-             // The App times tick over every 30 seconds, so surely we can
 
-             // drop this to max 1 or 2 minutes..
 
-             // Also, we would expect the GPS / Bluetooth subsystems to take care
 
-             // of purging stale data
 
-             if (Security.IsAllowed<CanBypassGPSClockIn>())
 
-                 return true;
 
-             if (Security.IsAllowed<CanBypassBluetoothGates>())
 
-                 return GPSTimeStamp() > DateTime.Now.Subtract(new TimeSpan(0, 15, 0));
 
-             if (App.Bluetooth.TimeStamp < DateTime.Now.Subtract(new TimeSpan(0, 15, 0)))
 
-                 return false;
 
-             return App.Data.BluetoothGates.Any(x =>
 
-                 App.Bluetooth.DetectedBlueToothMACAddresses.Contains(x.DeviceID));
 
-         }
 
-         private DateTime GPSTimeStamp()
 
-         {
 
-             return App.GPS.TimeStamp;
 
-         }
 
-         
 
-         
 
-         private static Location GPSLocation()
 
-         {
 
-             Location here = new InABox.Core.Location()
 
-             {
 
-                 Latitude = App.GPS.Latitude,
 
-                 Longitude = App.GPS.Longitude,
 
-                 Address = App.GPS.Address,
 
-                 Timestamp = App.GPS.TimeStamp
 
-             };
 
-             return here;
 
-         }
 
-         private String GPSAddress()
 
-         {
 
-             return App.GPS.Address;
 
-         }
 
-         
 
-         private String GetAddress()
 
-         {
 
-             if (Security.IsAllowed<CanBypassBluetoothGates>())
 
-             {
 
-                 if (GPSTimeStamp() < DateTime.Now.Subtract(new TimeSpan(0, 5, 0)))
 
-                 {
 
-                     App.GPS.GetLocation(true);
 
-                     return InABox.Core.Security.IsAllowed<CanBypassGPSClockIn>()? "" : "Searching for GPS";
 
-                 }
 
-                 return GPSAddress();
 
-             }
 
-             else
 
-             {
 
-                 var gate = App.Data.BluetoothGates.FirstOrDefault(x =>
 
-                     App.Bluetooth.DetectedBlueToothMACAddresses.Contains(x.DeviceID));
 
-                 return gate?.Gate ?? "Looking for Gate";
 
-             }
 
-         }
 
-         
 
-         #endregion
 
-         
 
-         public void SettingsTapped(object sender, EventArgs args) => Navigation.PushAsync(new SettingsPage());
 
-         public void AssignmentListTapped(object sender, EventArgs args) => Navigation.PushAsync(new AssignmentList());
 
-         
 
-         public void DeliveryModuleTapped(object sender, EventArgs args) => Navigation.PushAsync(new DeliveryModule());
 
-         
 
-         public void EquipmentModuleTapped(object sender, EventArgs args) => Navigation.PushAsync(new EquipmentModule());
 
-         
 
-         public void KanbanFormsTapped(object sender, EventArgs args) => Navigation.PushAsync(new KanbanForms());
 
-         
 
-         public void StaffStatusPageTapped(object sender, EventArgs args) => Navigation.PushAsync(new StaffStatusPage());
 
-         
 
-         public void ManufacturingListTapped(object sender, EventArgs args) => Navigation.PushAsync(new ManufacturingList());
 
-         private void MeetingsModuleTapped(object sender, EventArgs args) => Navigation.PushAsync(new MeetingList());
 
-         
 
-         public void HumanResourcesModuleTapped(object sender, EventArgs args) => Navigation.PushAsync(new HumanResourcesModule());
 
-         
 
-         public void NotificationListTapped(object sender, EventArgs args) => Navigation.PushAsync(new NotificationList());
 
-         
 
-         public void ProductListTapped(object sender, EventArgs args) => Navigation.PushAsync(new ProductList());
 
-         
 
-         public void PurchaseOrderListTapped(object sender, EventArgs args) => Navigation.PushAsync(new PurchaseOrderList());
 
-         
 
-         public void SiteModuleTapped(object sender, EventArgs args) => Navigation.PushAsync(new SiteModule());
 
-         public void KanbanListTapped(object sender, EventArgs args) => Navigation.PushAsync(new KanbanList()
 
-         {
 
-             Model = App.Data.Kanbans,
 
-             Title = "My Tasks"
 
-         });
 
-         public void WarehouseModuleTapped(object sender, EventArgs args) => Navigation.PushAsync(new WarehouseModule());
 
-         public void UpdatePageTapped(object sender, EventArgs args) => Navigation.PushAsync(new UpdatePage());
 
-         
 
-         private void ScanModuleTapped(object sender, EventArgs e) => Navigation.PushAsync(new DocScannerModule());
 
-     }
 
- }
 
 
  |