Shift async responsibility to ProductManager (#360)

SandboxChecker should not care about what actor is running on.

Better than #356
This commit is contained in:
Davide De Rosa 2023-09-10 19:08:31 +02:00 committed by GitHub
parent 27266a6d47
commit c5cf0ca1d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 36 deletions

View File

@ -172,7 +172,7 @@
<EnvironmentVariables>
<EnvironmentVariable
key = "APP_TYPE"
value = "0"
value = "2"
isEnabled = "YES">
</EnvironmentVariable>
<EnvironmentVariable

View File

@ -53,10 +53,6 @@ final class ProductManager: NSObject, ObservableObject {
private let overriddenAppType: AppType?
private let sandboxChecker: SandboxChecker
private var subscriptions: Set<AnyCancellable>
let buildProducts: BuildProducts
let didRefundProducts = PassthroughSubject<Void, Never>()
@ -95,10 +91,7 @@ final class ProductManager: NSObject, ObservableObject {
init(overriddenAppType: AppType?, buildProducts: BuildProducts) {
self.overriddenAppType = overriddenAppType
self.buildProducts = buildProducts
appType = .undefined
sandboxChecker = SandboxChecker(bundle: .main)
subscriptions = []
products = []
inApp = InApp()
@ -113,18 +106,12 @@ final class ProductManager: NSObject, ObservableObject {
SKPaymentQueue.default().add(self)
refreshProducts()
sandboxChecker.$isBeta
.dropFirst() // ignore initial value
.sink { [weak self] in
guard let self else {
return
Task {
let isBeta = await SandboxChecker().isBeta
appType = overriddenAppType ?? (isBeta ? .beta : .freemium)
pp_log.info("App type: \(appType)")
reloadReceipt()
}
self.appType = overriddenAppType ?? ($0 ? .beta : .freemium)
pp_log.info("App type: \(self.appType)")
self.reloadReceipt()
}.store(in: &subscriptions)
sandboxChecker.check()
}
deinit {

View File

@ -28,25 +28,23 @@ import Foundation
// https://stackoverflow.com/a/32238344/784615
// https://gist.github.com/lukaskubanek/cbfcab29c0c93e0e9e0a16ab09586996
public final class SandboxChecker: ObservableObject {
private let bundle: Bundle
@Published public private(set) var isBeta = false
public init(bundle: Bundle) {
self.bundle = bundle
public final actor SandboxChecker: ObservableObject {
public init() {
}
@MainActor
public func check() {
Task {
isBeta = await isBetaBuild()
public var isBeta: Bool {
let isBeta = verifyBetaBuild()
pp_log.info("Beta build: \(isBeta)")
return isBeta
}
}
// MARK: Shared
private extension SandboxChecker {
// IMPORTANT: check Mac first because os(iOS) holds true for Catalyst
private func isBetaBuild() async -> Bool {
func verifyBetaBuild() -> Bool {
#if targetEnvironment(macCatalyst) || os(macOS)
isMacTestFlightBuild
#elseif os(iOS)
@ -55,6 +53,10 @@ public final class SandboxChecker: ObservableObject {
false
#endif
}
var bundle: Bundle {
.main
}
}
// MARK: iOS
@ -70,8 +72,6 @@ private extension SandboxChecker {
// MARK: macOS
#if targetEnvironment(macCatalyst) || os(macOS)
import Security
private extension SandboxChecker {
var isMacTestFlightBuild: Bool {
var status = noErr