From 7da13e22ada791d0dd890899e2cfb17dc734292d Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Fri, 12 Jul 2019 20:56:54 -0400 Subject: [PATCH] process event uploads on ios --- src/iOS/AppDelegate.cs | 48 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/src/iOS/AppDelegate.cs b/src/iOS/AppDelegate.cs index cea7d4cd9..fc08d9de6 100644 --- a/src/iOS/AppDelegate.cs +++ b/src/iOS/AppDelegate.cs @@ -31,12 +31,15 @@ namespace Bit.iOS private nint _clipboardBackgroundTaskId; private NSTimer _lockTimer = null; private nint _lockBackgroundTaskId; + private NSTimer _eventTimer = null; + private nint _eventBackgroundTaskId; private IDeviceActionService _deviceActionService; private IMessagingService _messagingService; private IBroadcasterService _broadcasterService; private IStorageService _storageService; private ILockService _lockService; + private IEventService _eventService; public override bool FinishedLaunching(UIApplication app, NSDictionary options) { @@ -53,6 +56,7 @@ namespace Bit.iOS _broadcasterService = ServiceContainer.Resolve("broadcasterService"); _storageService = ServiceContainer.Resolve("storageService"); _lockService = ServiceContainer.Resolve("lockService"); + _eventService = ServiceContainer.Resolve("eventService"); LoadApplication(new App.App(null)); iOSCoreHelpers.AppearanceAdjustments(); @@ -68,6 +72,14 @@ namespace Bit.iOS { CancelLockTimer(); } + else if(message.Command == "startEventTimer") + { + StartEventTimer(); + } + else if(message.Command == "stopEventTimer") + { + var task = StopEventTimerAsync(); + } else if(message.Command == "updatedTheme") { // ThemeManager.SetThemeStyle(message.Data as string); @@ -181,7 +193,7 @@ namespace Bit.iOS UIApplication.SharedApplication.KeyWindow.BringSubviewToFront(view); UIApplication.SharedApplication.KeyWindow.EndEditing(true); UIApplication.SharedApplication.SetStatusBarHidden(true, false); - _storageService.SaveAsync(Bit.Core.Constants.LastActiveKey, DateTime.UtcNow); + _storageService.SaveAsync(Constants.LastActiveKey, DateTime.UtcNow); base.DidEnterBackground(uiApplication); } @@ -431,5 +443,39 @@ namespace Bit.iOS } modal.PresentViewController(activityViewController, true, null); } + + private void StartEventTimer() + { + _eventTimer?.Invalidate(); + _eventTimer?.Dispose(); + _eventTimer = null; + Device.BeginInvokeOnMainThread(() => + { + _eventTimer = NSTimer.CreateScheduledTimer(60, true, timer => + { + var task = Task.Run(() => _eventService.UploadEventsAsync()); + }); + }); + } + + private async Task StopEventTimerAsync() + { + _eventTimer?.Invalidate(); + _eventTimer?.Dispose(); + _eventTimer = null; + if(_eventBackgroundTaskId > 0) + { + UIApplication.SharedApplication.EndBackgroundTask(_eventBackgroundTaskId); + _eventBackgroundTaskId = 0; + } + _eventBackgroundTaskId = UIApplication.SharedApplication.BeginBackgroundTask(() => + { + UIApplication.SharedApplication.EndBackgroundTask(_eventBackgroundTaskId); + _eventBackgroundTaskId = 0; + }); + await _eventService.UploadEventsAsync(); + UIApplication.SharedApplication.EndBackgroundTask(_eventBackgroundTaskId); + _eventBackgroundTaskId = 0; + } } }