Skip to content

Commit 8f449fe

Browse files
committed
fix: Ensure main thread execution on loading state updates
1 parent 75037ce commit 8f449fe

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

Sources/PrimerSDK/Classes/PCI/User Interface/InternalCardComponentsManager.swift

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import UIKit
1313

1414
@objc
15-
internal protocol InternalCardComponentsManagerDelegate {
15+
protocol InternalCardComponentsManagerDelegate {
1616
/// The cardComponentsManager(_:clientTokenCallback:) can be used to provide the CardComponentsManager
1717
/// with an access token from the merchants backend.
1818
/// This delegate function is optional since you can initialize the CardComponentsManager with an access token.
@@ -70,18 +70,20 @@ final class InternalCardComponentsManager: NSObject, InternalCardComponentsManag
7070
var merchantIdentifier: String?
7171
var amount: Int?
7272
var currency: Currency?
73-
internal var decodedJWTToken: DecodedJWTToken? {
73+
var decodedJWTToken: DecodedJWTToken? {
7474
return PrimerAPIConfigurationModule.decodedJWTToken
7575
}
76-
internal var paymentMethodsConfig: PrimerAPIConfiguration?
77-
internal var primerPaymentMethodType: PrimerPaymentMethodType
78-
private(set) public var isLoading: Bool = false
79-
internal private(set) var paymentMethod: PrimerPaymentMethodTokenData?
76+
var paymentMethodsConfig: PrimerAPIConfiguration?
77+
var primerPaymentMethodType: PrimerPaymentMethodType
78+
public private(set) var isLoading: Bool = false
79+
private(set) var paymentMethod: PrimerPaymentMethodTokenData?
8080

8181
let tokenizationService: TokenizationServiceProtocol
8282

8383
deinit {
84-
setIsLoading(false)
84+
if isLoading {
85+
delegate.cardComponentsManager?(self, isLoading: false)
86+
}
8587
}
8688

8789
/// The CardComponentsManager can be initialized with/out an access token.
@@ -119,7 +121,8 @@ final class InternalCardComponentsManager: NSObject, InternalCardComponentsManag
119121
super.init()
120122
}
121123

122-
internal func setIsLoading(_ isLoading: Bool) {
124+
@MainActor
125+
func setIsLoading(_ isLoading: Bool) {
123126
if self.isLoading == isLoading { return }
124127
self.isLoading = isLoading
125128
delegate.cardComponentsManager?(self, isLoading: isLoading)
@@ -201,7 +204,7 @@ and 4 characters for expiry year separated by '/'.
201204
billingAddressFieldViews?.filter { $0.isTextValid == false }.forEach {
202205
if let simpleCardFormTextFieldView = $0 as? PrimerSimpleCardFormTextFieldView {
203206
switch simpleCardFormTextFieldView.validation {
204-
case .invalid(let error): errors.append(handled(error: error!))
207+
case let .invalid(error): errors.append(handled(error: error!))
205208
default: break
206209
}
207210
}
@@ -254,9 +257,8 @@ and 4 characters for expiry year separated by '/'.
254257
}
255258

256259
public func tokenize() {
257-
setIsLoading(true)
258-
259260
Task {
261+
await setIsLoading(true)
260262
do {
261263
try validateCardComponents()
262264
_ = try await self.fetchClientTokenIfNeeded()
@@ -317,12 +319,12 @@ and 4 characters for expiry year separated by '/'.
317319
throw handled(primerError: error.asPrimerError)
318320
}
319321

320-
} catch PrimerError.underlyingErrors(let errors, _) {
322+
} catch let PrimerError.underlyingErrors(errors, _) {
321323
delegate.cardComponentsManager?(self, tokenizationFailedWith: errors)
322-
setIsLoading(false)
324+
await setIsLoading(false)
323325
} catch {
324326
delegate.cardComponentsManager?(self, tokenizationFailedWith: [error])
325-
setIsLoading(false)
327+
await setIsLoading(false)
326328
}
327329
}
328330
}

0 commit comments

Comments
 (0)