summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPN/Operations
diff options
context:
space:
mode:
authorAndrej Mihajlov <and@mullvad.net>2022-03-15 12:45:35 +0100
committerAndrej Mihajlov <and@mullvad.net>2022-03-24 14:00:24 +0100
commit0cd501d8173087cc46593e6aaf00a15c0f3ca25f (patch)
tree8144b8f60d8165b7261a4ee109b0f5963b554328 /ios/MullvadVPN/Operations
parentc69ebd383d617efbba1b0afac98db783f0143b3d (diff)
downloadmullvadvpn-0cd501d8173087cc46593e6aaf00a15c0f3ca25f.tar.xz
mullvadvpn-0cd501d8173087cc46593e6aaf00a15c0f3ca25f.zip
Drop Promises
Diffstat (limited to 'ios/MullvadVPN/Operations')
-rw-r--r--ios/MullvadVPN/Operations/ExclusivityController.swift2
-rw-r--r--ios/MullvadVPN/Operations/OperationCompletion.swift11
-rw-r--r--ios/MullvadVPN/Operations/ProductsRequestOperation.swift22
-rw-r--r--ios/MullvadVPN/Operations/ReceiptRefreshOperation.swift69
4 files changed, 19 insertions, 85 deletions
diff --git a/ios/MullvadVPN/Operations/ExclusivityController.swift b/ios/MullvadVPN/Operations/ExclusivityController.swift
index d198ffd497..cfbd3d7668 100644
--- a/ios/MullvadVPN/Operations/ExclusivityController.swift
+++ b/ios/MullvadVPN/Operations/ExclusivityController.swift
@@ -13,8 +13,6 @@ class ExclusivityController: NSObject {
private var operations: [String: [Operation]] = [:]
private var categoriesByOperation: [Operation: [String]] = [:]
- static let shared = ExclusivityController()
-
func addOperation(_ operation: Operation, categories: [String]) {
lock.withCriticalBlock {
categories.forEach { category in
diff --git a/ios/MullvadVPN/Operations/OperationCompletion.swift b/ios/MullvadVPN/Operations/OperationCompletion.swift
index 21cb92e169..0157075319 100644
--- a/ios/MullvadVPN/Operations/OperationCompletion.swift
+++ b/ios/MullvadVPN/Operations/OperationCompletion.swift
@@ -21,6 +21,17 @@ enum OperationCompletion<Success, Failure: Error> {
}
}
+ var result: Result<Success, Failure>? {
+ switch self {
+ case .success(let value):
+ return .success(value)
+ case .failure(let error):
+ return .failure(error)
+ case .cancelled:
+ return nil
+ }
+ }
+
init(result: Result<Success, Failure>) {
switch result {
case .success(let value):
diff --git a/ios/MullvadVPN/Operations/ProductsRequestOperation.swift b/ios/MullvadVPN/Operations/ProductsRequestOperation.swift
index 6e34e05977..953fc28dab 100644
--- a/ios/MullvadVPN/Operations/ProductsRequestOperation.swift
+++ b/ios/MullvadVPN/Operations/ProductsRequestOperation.swift
@@ -11,7 +11,7 @@ import StoreKit
class ProductsRequestOperation: AsyncOperation, SKProductsRequestDelegate {
private let productIdentifiers: Set<String>
- private var completionHandler: ((Result<SKProductsResponse, Error>) -> Void)?
+ private var completionHandler: ((OperationCompletion<SKProductsResponse, Error>) -> Void)?
private let maxRetryCount = 10
private let retryDelay: DispatchTimeInterval = .seconds(2)
@@ -20,13 +20,7 @@ class ProductsRequestOperation: AsyncOperation, SKProductsRequestDelegate {
private var retryTimer: DispatchSourceTimer?
private var request: SKProductsRequest?
- struct OperationCancelledError: LocalizedError {
- var errorDescription: String? {
- return "Operation is cancelled"
- }
- }
-
- init(productIdentifiers: Set<String>, completionHandler: @escaping (Result<SKProductsResponse, Error>) -> Void) {
+ init(productIdentifiers: Set<String>, completionHandler: @escaping (OperationCompletion<SKProductsResponse, Error>) -> Void) {
self.productIdentifiers = productIdentifiers
self.completionHandler = completionHandler
@@ -36,7 +30,7 @@ class ProductsRequestOperation: AsyncOperation, SKProductsRequestDelegate {
override func main() {
DispatchQueue.main.async {
guard !self.isCancelled else {
- self.finish(with: .failure(OperationCancelledError()))
+ self.finish(completion: .cancelled)
return
}
@@ -65,14 +59,14 @@ class ProductsRequestOperation: AsyncOperation, SKProductsRequestDelegate {
self.retryCount += 1
self.retry(error: error)
} else {
- self.finish(with: .failure(error))
+ self.finish(completion: .failure(error))
}
}
}
func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
DispatchQueue.main.async {
- self.finish(with: .success(response))
+ self.finish(completion: .success(response))
}
}
@@ -92,17 +86,17 @@ class ProductsRequestOperation: AsyncOperation, SKProductsRequestDelegate {
}
retryTimer?.setCancelHandler { [weak self] in
- self?.finish(with: .failure(error))
+ self?.finish(completion: .failure(error))
}
retryTimer?.schedule(wallDeadline: .now() + self.retryDelay)
retryTimer?.activate()
}
- private func finish(with result: Result<SKProductsResponse, Error>) {
+ private func finish(completion: OperationCompletion<SKProductsResponse, Error>) {
assert(Thread.isMainThread)
- completionHandler?(result)
+ completionHandler?(completion)
completionHandler = nil
finish()
diff --git a/ios/MullvadVPN/Operations/ReceiptRefreshOperation.swift b/ios/MullvadVPN/Operations/ReceiptRefreshOperation.swift
deleted file mode 100644
index 61d9857904..0000000000
--- a/ios/MullvadVPN/Operations/ReceiptRefreshOperation.swift
+++ /dev/null
@@ -1,69 +0,0 @@
-//
-// ReceiptRefreshOperation.swift
-// ReceiptRefreshOperation
-//
-// Created by pronebird on 02/09/2021.
-// Copyright © 2021 Mullvad VPN AB. All rights reserved.
-//
-
-import Foundation
-import StoreKit
-
-class ReceiptRefreshOperation: AsyncOperation, SKRequestDelegate {
- private let request: SKReceiptRefreshRequest
- private var completionHandler: ((Result<(), Error>) -> Void)?
-
- struct OperationCancelledError: LocalizedError {
- var errorDescription: String? {
- return "Operation is cancelled"
- }
- }
-
- init(receiptProperties: [String: Any]?, completionHandler completion: @escaping (Result<(), Error>) -> Void) {
- request = SKReceiptRefreshRequest(receiptProperties: receiptProperties)
- completionHandler = completion
- }
-
- override func main() {
- DispatchQueue.main.async {
- guard !self.isCancelled else {
- self.finish(with: .failure(OperationCancelledError()))
- return
- }
-
- self.request.delegate = self
- self.request.start()
- }
- }
-
- override func cancel() {
- DispatchQueue.main.async {
- super.cancel()
-
- self.request.cancel()
- }
- }
-
- // - MARK: SKRequestDelegate
-
- func requestDidFinish(_ request: SKRequest) {
- DispatchQueue.main.async {
- self.finish(with: .success(()))
- }
- }
-
- func request(_ request: SKRequest, didFailWithError error: Error) {
- DispatchQueue.main.async {
- self.finish(with: .failure(error))
- }
- }
-
- private func finish(with result: Result<(), Error>) {
- assert(Thread.isMainThread)
-
- completionHandler?(result)
- completionHandler = nil
-
- finish()
- }
-}