Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove presentCodeRedemptionSheet from PaymentQueueWrapper & Use SK2 APIs #4378

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions RevenueCat.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1000,6 +1000,7 @@
F5FCD3EA27DA0D0B003BDC04 /* PriceFormatterProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = F5FCD3E927DA0D0B003BDC04 /* PriceFormatterProvider.swift */; };
F5FCD3FC27DA2034003BDC04 /* PriceFormatterProviderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F5FCD3FB27DA2034003BDC04 /* PriceFormatterProviderTests.swift */; };
FD18ED4E2837F89200C5AA4F /* StoreKitWorkaroundsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = FD18ED4D2837F89200C5AA4F /* StoreKitWorkaroundsTests.swift */; };
FD20472A2CC19DCD00166727 /* OfferCodeRedemptionSheetPresenter.swift in Sources */ = {isa = PBXBuildFile; fileRef = FD2047292CC19DCD00166727 /* OfferCodeRedemptionSheetPresenter.swift */; };
FD2E6C9F2C480FF000CB4BD7 /* OHHTTPStubs in Frameworks */ = {isa = PBXBuildFile; productRef = FD2E6C9E2C480FF000CB4BD7 /* OHHTTPStubs */; };
FD2E6CA12C48100900CB4BD7 /* OHHTTPStubsSwift in Frameworks */ = {isa = PBXBuildFile; productRef = FD2E6CA02C48100900CB4BD7 /* OHHTTPStubsSwift */; };
FD43D2FC2C41864000077235 /* TimeInterval+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = FD43D2FA2C4185B700077235 /* TimeInterval+Extensions.swift */; };
Expand Down Expand Up @@ -2202,6 +2203,7 @@
F5FCD3E927DA0D0B003BDC04 /* PriceFormatterProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PriceFormatterProvider.swift; sourceTree = "<group>"; };
F5FCD3FB27DA2034003BDC04 /* PriceFormatterProviderTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PriceFormatterProviderTests.swift; sourceTree = "<group>"; };
FD18ED4D2837F89200C5AA4F /* StoreKitWorkaroundsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StoreKitWorkaroundsTests.swift; sourceTree = "<group>"; };
FD2047292CC19DCD00166727 /* OfferCodeRedemptionSheetPresenter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OfferCodeRedemptionSheetPresenter.swift; sourceTree = "<group>"; };
FD43D2FA2C4185B700077235 /* TimeInterval+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "TimeInterval+Extensions.swift"; sourceTree = "<group>"; };
FD43D2FD2C41867600077235 /* TimeInterval+ExtensionsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "TimeInterval+ExtensionsTests.swift"; sourceTree = "<group>"; };
FDAADFCA2BE2A5BF00BD1659 /* MockAllTransactionsProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockAllTransactionsProvider.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2472,6 +2474,7 @@
2D1015D9275959840086173F /* StoreTransaction.swift */,
2D1015DD275A57FC0086173F /* SubscriptionPeriod.swift */,
4F174F462B07EA7E00FE538E /* StorefrontProvider.swift */,
FD2047292CC19DCD00166727 /* OfferCodeRedemptionSheetPresenter.swift */,
);
path = StoreKitAbstractions;
sourceTree = "<group>";
Expand Down Expand Up @@ -5552,6 +5555,7 @@
4FBBC5682A61E42F0077281F /* NonEmptyStringDecodable.swift in Sources */,
2DDF41A324F6F331005BC22D /* PurchasesReceiptParser.swift in Sources */,
2CB8CF9327BF538F00C34DE3 /* PlatformInfo.swift in Sources */,
FD20472A2CC19DCD00166727 /* OfferCodeRedemptionSheetPresenter.swift in Sources */,
35D832F4262E606500E60AC5 /* HTTPResponse.swift in Sources */,
352B7D7927BD919B002A47DD /* DangerousSettings.swift in Sources */,
A56F9AB126990E9200AFC48F /* CustomerInfo.swift in Sources */,
Expand Down
51 changes: 51 additions & 0 deletions RevenueCat.xcworkspace/xcshareddata/swiftpm/Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"originHash" : "501760807f1881d92609a594586edb2ceb01ad8cc54331a3bde63278db5971bf",
fire-at-will marked this conversation as resolved.
Show resolved Hide resolved
"pins" : [
{
"identity" : "cwlcatchexception",
"kind" : "remoteSourceControl",
"location" : "https://github.com/mattgallagher/CwlCatchException.git",
"state" : {
"revision" : "3b123999de19bf04905bc1dfdb76f817b0f2cc00",
"version" : "2.1.2"
}
},
{
"identity" : "cwlpreconditiontesting",
"kind" : "remoteSourceControl",
"location" : "https://github.com/mattgallagher/CwlPreconditionTesting.git",
"state" : {
"revision" : "a23ded2c91df9156628a6996ab4f347526f17b6b",
"version" : "2.1.2"
}
},
{
"identity" : "nimble",
"kind" : "remoteSourceControl",
"location" : "https://github.com/quick/nimble",
"state" : {
"revision" : "1f3bde57bde12f5e7b07909848c071e9b73d6edc",
"version" : "10.0.0"
}
},
{
"identity" : "ohhttpstubs",
"kind" : "remoteSourceControl",
"location" : "https://github.com/AliSoftware/OHHTTPStubs.git",
"state" : {
"revision" : "12f19662426d0434d6c330c6974d53e2eb10ecd9",
"version" : "9.1.0"
}
},
{
"identity" : "swift-snapshot-testing",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-snapshot-testing",
"state" : {
"revision" : "26ed3a2b4a2df47917ca9b790a57f91285b923fb",
"version" : "1.12.0"
}
}
],
"version" : 3
}
37 changes: 31 additions & 6 deletions Sources/Purchasing/Purchases/Purchases.swift
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ public typealias StartPurchaseBlock = (@escaping PurchaseCompletedBlock) -> Void
fileprivate let systemInfo: SystemInfo
private let storeMessagesHelper: StoreMessagesHelperType?
private var customerInfoObservationDisposable: (() -> Void)?
private let offerCodeRedemptionSheetPresenter: OfferCodeRedemptionSheetPresenterType

private let syncAttributesAndOfferingsIfNeededRateLimiter = RateLimiter(maxCalls: 5, period: 60)

Expand Down Expand Up @@ -307,6 +308,7 @@ public typealias StartPurchaseBlock = (@escaping PurchaseCompletedBlock) -> Void

let purchasedProductsFetcher = OfflineCustomerInfoCreator.createPurchasedProductsFetcherIfAvailable()
let transactionFetcher = StoreKit2TransactionFetcher()
let offerCodeRedemptionSheetPresenter = OfferCodeRedemptionSheetPresenter()

let diagnosticsFileHandler: DiagnosticsFileHandlerType? = {
guard diagnosticsEnabled, #available(iOS 15.0, tvOS 15.0, macOS 12.0, watchOS 8.0, *) else { return nil }
Expand Down Expand Up @@ -570,7 +572,8 @@ public typealias StartPurchaseBlock = (@escaping PurchaseCompletedBlock) -> Void
purchasesOrchestrator: purchasesOrchestrator,
purchasedProductsFetcher: purchasedProductsFetcher,
trialOrIntroPriceEligibilityChecker: trialOrIntroPriceChecker,
storeMessagesHelper: storeMessagesHelper
storeMessagesHelper: storeMessagesHelper,
offerCodeRedemptionSheetPresenter: offerCodeRedemptionSheetPresenter
)
}

Expand Down Expand Up @@ -599,7 +602,8 @@ public typealias StartPurchaseBlock = (@escaping PurchaseCompletedBlock) -> Void
purchasesOrchestrator: PurchasesOrchestrator,
purchasedProductsFetcher: PurchasedProductsFetcherType?,
trialOrIntroPriceEligibilityChecker: CachingTrialOrIntroPriceEligibilityChecker,
storeMessagesHelper: StoreMessagesHelperType?
storeMessagesHelper: StoreMessagesHelperType?,
offerCodeRedemptionSheetPresenter: OfferCodeRedemptionSheetPresenterType
) {

if systemInfo.dangerousSettings.customEntitlementComputation {
Expand Down Expand Up @@ -647,6 +651,7 @@ public typealias StartPurchaseBlock = (@escaping PurchaseCompletedBlock) -> Void
self.purchasedProductsFetcher = purchasedProductsFetcher
self.trialOrIntroPriceEligibilityChecker = trialOrIntroPriceEligibilityChecker
self.storeMessagesHelper = storeMessagesHelper
self.offerCodeRedemptionSheetPresenter = offerCodeRedemptionSheetPresenter

super.init()

Expand Down Expand Up @@ -1068,13 +1073,33 @@ public extension Purchases {
@available(watchOS, unavailable)
@available(tvOS, unavailable)
@available(macOS, unavailable)
@available(macCatalyst, unavailable)
@objc func presentCodeRedemptionSheet() {
self.paymentQueueWrapper.paymentQueueWrapperType.presentCodeRedemptionSheet()
@available(macCatalyst 16.0, *)
@available(iOSApplicationExtension, unavailable)
@available(macCatalystApplicationExtension, unavailable)
@objc func presentCodeRedemptionSheet(
uiWindowScene: UIWindowScene? = nil
) {
let windowScene: UIWindowScene?

if let uiWindowScene {
windowScene = uiWindowScene
} else {
// Can't DI this since it's unavailable for iOS & macCatalyst app extensions
let uiWindowSceneFinder = UIWindowSceneFinder()
windowScene = uiWindowSceneFinder.attemptToGetActiveWindowScene()
}

if let windowScene {
self.offerCodeRedemptionSheetPresenter.presentCodeRedemptionSheet(
windowScene: windowScene
)
} else {
// TODO: Log message
}
}
#endif

#if !ENABLE_CUSTOM_ENTITLEMENT_COMPUTATION
#if !ENABLE_CUSTOM_ENTITLEMENT_COMPUTATION

@objc(getPromotionalOfferForProductDiscount:withProduct:withCompletion:)
func getPromotionalOffer(forProductDiscount discount: StoreProductDiscount,
Expand Down
6 changes: 4 additions & 2 deletions Sources/Purchasing/Purchases/PurchasesType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -746,11 +746,13 @@ public protocol PurchasesType: AnyObject {
* This is why RevenueCat's SDK makes it unavailable in mac catalyst.
*/
@available(iOS 14.0, *)
@available(iOSApplicationExtension, unavailable)
@available(watchOS, unavailable)
@available(tvOS, unavailable)
@available(macOS, unavailable)
@available(macCatalyst, unavailable)
func presentCodeRedemptionSheet()
@available(macCatalyst 16.0, *)
@available(macCatalystApplicationExtension, unavailable)
func presentCodeRedemptionSheet(uiWindowScene: UIWindowScene?)

#if os(iOS) || targetEnvironment(macCatalyst) || VISION_OS
/**
Expand Down
14 changes: 0 additions & 14 deletions Sources/Purchasing/StoreKit1/PaymentQueueWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,6 @@ protocol PaymentQueueWrapperType: AnyObject {
func showPriceConsentIfNeeded()
#endif

@available(iOS 14.0, *)
@available(macOS, unavailable)
@available(tvOS, unavailable)
@available(watchOS, unavailable)
@available(macCatalyst, unavailable)
func presentCodeRedemptionSheet()

var currentStorefront: Storefront? { get }

}
Expand Down Expand Up @@ -94,13 +87,6 @@ class PaymentQueueWrapper: NSObject, PaymentQueueWrapperType {
}
#endif

#if (os(iOS) && !targetEnvironment(macCatalyst)) || VISION_OS
@available(iOS 14.0, *)
func presentCodeRedemptionSheet() {
self.paymentQueue.presentCodeRedemptionSheetIfAvailable()
}
#endif

var currentStorefront: Storefront? {
return self.paymentQueue.storefront
.map(SK1Storefront.init)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
//
// Copyright RevenueCat Inc. All Rights Reserved.
//
// Licensed under the MIT License (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://opensource.org/licenses/MIT
//
// OfferCodeRedemptionSheetPresenter.swift
//
// Created by Will Taylor on 10/17/24.

import Foundation
import StoreKit

@objc protocol OfferCodeRedemptionSheetPresenterType {

@available(iOS 14.0, *)
@available(tvOS, unavailable)
@available(macOS, unavailable)
@available(macCatalyst 16.0, *)
func presentCodeRedemptionSheet(windowScene: UIWindowScene)
}

final internal class OfferCodeRedemptionSheetPresenter: OfferCodeRedemptionSheetPresenterType {

private var presentSheetTask: Task<Void, Never>?

private let paymentQueue: SKPaymentQueue

init(
paymentQueue: SKPaymentQueue = .default()
) {
self.paymentQueue = paymentQueue
}

@available(iOS 14.0, *)
@available(tvOS, unavailable)
@available(macOS, unavailable)
@available(macCatalyst 16.0, *)
func presentCodeRedemptionSheet(
windowScene: UIWindowScene
) {
#if os(iOS) && !targetEnvironment(macCatalyst)
if ProcessInfo().operatingSystemVersion.majorVersion < 16 {
// .presentOfferCodeRedeemSheet(in: windowScene) isn't available in iOS <16, so fall back
// to the SK1 implementation
self.sk1PresentCodeRedemptionSheet()
return
}
#endif

self.presentSheetTask = Task.detached { @MainActor in
if #available(iOSApplicationExtension 16.0, *), #available(iOS 16.0, *) {
try? await AppStore.presentOfferCodeRedeemSheet(in: windowScene)
} else {
// TODO: Log failure
}
}
}

@available(iOS 14.0, *)
@available(watchOS, unavailable)
@available(tvOS, unavailable)
@available(macOS, unavailable)
@available(macCatalyst, unavailable)
@available(macCatalystApplicationExtension, unavailable)
private func sk1PresentCodeRedemptionSheet() {
self.paymentQueue.presentCodeRedemptionSheet()
}
}

@available(iOSApplicationExtension, unavailable)
@available(macCatalystApplicationExtension, unavailable)
protocol UIWindowSceneFinderType {
func attemptToGetActiveWindowScene() -> UIWindowScene?
}

@available(iOSApplicationExtension, unavailable)
@available(macCatalystApplicationExtension, unavailable)
struct UIWindowSceneFinder: UIWindowSceneFinderType {
func attemptToGetActiveWindowScene() -> UIWindowScene? {
UIApplication.shared.connectedScenes
.first(where: { $0.activationState == .foregroundActive }) as? UIWindowScene
}
}