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> <EnvironmentVariables>
<EnvironmentVariable <EnvironmentVariable
key = "APP_TYPE" key = "APP_TYPE"
value = "0" value = "2"
isEnabled = "YES"> isEnabled = "YES">
</EnvironmentVariable> </EnvironmentVariable>
<EnvironmentVariable <EnvironmentVariable

View File

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

View File

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