diff options
| author | Andrej Mihajlov <and@mullvad.net> | 2021-09-14 10:57:50 +0200 |
|---|---|---|
| committer | Andrej Mihajlov <and@mullvad.net> | 2021-09-15 10:58:43 +0200 |
| commit | fedea0de95f2a64c3274049ecb9fc32cf902f59f (patch) | |
| tree | c125714b1dfd2f66e4a49d1255dd60f7c33afbbc | |
| parent | 0637057254b2a9b24d29ce87df3c6f49d16b4cf6 (diff) | |
| download | mullvadvpn-fedea0de95f2a64c3274049ecb9fc32cf902f59f.tar.xz mullvadvpn-fedea0de95f2a64c3274049ecb9fc32cf902f59f.zip | |
Promise: add background task extension
| -rw-r--r-- | ios/MullvadVPN.xcodeproj/project.pbxproj | 4 | ||||
| -rw-r--r-- | ios/MullvadVPN/Promise/Promise+BackgroundTask.swift | 57 |
2 files changed, 61 insertions, 0 deletions
diff --git a/ios/MullvadVPN.xcodeproj/project.pbxproj b/ios/MullvadVPN.xcodeproj/project.pbxproj index 6158d44473..52571baf85 100644 --- a/ios/MullvadVPN.xcodeproj/project.pbxproj +++ b/ios/MullvadVPN.xcodeproj/project.pbxproj @@ -27,6 +27,7 @@ 581CBCEE229826FD00727D7F /* StaticTableViewDataSource.swift in Sources */ = {isa = PBXBuildFile; fileRef = 581CBCED229826FD00727D7F /* StaticTableViewDataSource.swift */; }; 581FC4FA2695ACE100AA97BA /* Account.strings in Resources */ = {isa = PBXBuildFile; fileRef = 581FC4F82695ACE100AA97BA /* Account.strings */; }; 5823FA5026CA690600283BF8 /* OSLogHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5823FA4F26CA690600283BF8 /* OSLogHandler.swift */; }; + 5820674926E63EC900655B05 /* Promise+BackgroundTask.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5820674826E63EC800655B05 /* Promise+BackgroundTask.swift */; }; 58293FAE2510CA58005D0BB5 /* ProblemReportViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58293FAC2510CA58005D0BB5 /* ProblemReportViewController.swift */; }; 58293FB125124117005D0BB5 /* CustomTextField.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58293FB025124117005D0BB5 /* CustomTextField.swift */; }; 58293FB3251241B4005D0BB5 /* CustomTextView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58293FB2251241B3005D0BB5 /* CustomTextView.swift */; }; @@ -312,6 +313,7 @@ 581503A524D6F4AE00C9C50E /* Logging.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Logging.swift; sourceTree = "<group>"; }; 581CBCED229826FD00727D7F /* StaticTableViewDataSource.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StaticTableViewDataSource.swift; sourceTree = "<group>"; }; 581FC4F92695ACE100AA97BA /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/Account.strings; sourceTree = "<group>"; }; + 5820674826E63EC800655B05 /* Promise+BackgroundTask.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Promise+BackgroundTask.swift"; sourceTree = "<group>"; }; 5823FA4F26CA690600283BF8 /* OSLogHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OSLogHandler.swift; sourceTree = "<group>"; }; 58293FAC2510CA58005D0BB5 /* ProblemReportViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProblemReportViewController.swift; sourceTree = "<group>"; }; 58293FB025124117005D0BB5 /* CustomTextField.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomTextField.swift; sourceTree = "<group>"; }; @@ -771,6 +773,7 @@ 58E1337426D2BEC400CC316B /* Promise+Optional.swift */, 58E1337826D2BEDD00CC316B /* Promise+ReceiveOn.swift */, 58E1338026D2BF5C00CC316B /* Promise+Result.swift */, + 5820674826E63EC800655B05 /* Promise+BackgroundTask.swift */, ); path = Promise; sourceTree = "<group>"; @@ -1160,6 +1163,7 @@ 5873884D239E6D7E00E96C4E /* EmbeddedViewContainerView.swift in Sources */, 583BC70724FE4DC500C9DE04 /* Optional+DispatchQueue.swift in Sources */, 58F3C0A4249CB069003E76BE /* HeaderBarView.swift in Sources */, + 5820674926E63EC900655B05 /* Promise+BackgroundTask.swift in Sources */, 58B9EB132488ED2100095626 /* AlertPresenter.swift in Sources */, 587A01FC23F1F0BE00B68763 /* SimulatorTunnelProviderHost.swift in Sources */, 5862805422428EF100F5A6E1 /* TranslucentButtonBlurView.swift in Sources */, diff --git a/ios/MullvadVPN/Promise/Promise+BackgroundTask.swift b/ios/MullvadVPN/Promise/Promise+BackgroundTask.swift new file mode 100644 index 0000000000..1d7bfc3280 --- /dev/null +++ b/ios/MullvadVPN/Promise/Promise+BackgroundTask.swift @@ -0,0 +1,57 @@ +// +// Promise+BackgroundTask.swift +// Promise+BackgroundTask +// +// Created by pronebird on 06/09/2021. +// Copyright © 2021 Mullvad VPN AB. All rights reserved. +// + +import UIKit + +extension Promise { + + /// Start the background task for the duration of the upstream execution. + func requestBackgroundTime(taskName: String? = nil) -> Promise<Value> { + return Promise<Value> { resolver in + var backgroundTaskIdentifier: UIBackgroundTaskIdentifier? + + let beginBackgroundTask = { + backgroundTaskIdentifier = UIApplication.shared.beginBackgroundTask(withName: taskName) { + resolver.resolve(completion: .cancelled) + } + } + + let endBackgroundTask = { + guard let taskIdentifier = backgroundTaskIdentifier, + taskIdentifier != .invalid else { return } + + UIApplication.shared.endBackgroundTask(taskIdentifier) + backgroundTaskIdentifier = nil + } + + let endBackgroundTaskOnMainQueue = { + if Thread.isMainThread { + endBackgroundTask() + } else { + DispatchQueue.main.async(execute: endBackgroundTask) + } + } + + if Thread.isMainThread { + beginBackgroundTask() + } else { + DispatchQueue.main.async(execute: beginBackgroundTask) + } + + resolver.setCancelHandler { + endBackgroundTaskOnMainQueue() + } + + self.observe { completion in + resolver.resolve(completion: completion) + + endBackgroundTaskOnMainQueue() + } + } + } +} |
