-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement auto-detection of active subscriptions
- Loading branch information
Showing
53 changed files
with
931 additions
and
67 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
...es/Flare/Classes/Extensions/Product.SubscriptionInfo.Status+ISubscriptionInfoStatus.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// | ||
// Flare | ||
// Copyright © 2024 Space Code. All rights reserved. | ||
// | ||
|
||
import struct StoreKit.Product | ||
|
||
// MARK: - ISubscriptionInfoStatus | ||
|
||
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, visionOS 1.0, *) | ||
extension Product.SubscriptionInfo.Status: ISubscriptionInfoStatus { | ||
var renewalState: RenewalState { | ||
RenewalState(self.state) | ||
} | ||
Check warning on line 14 in Sources/Flare/Classes/Extensions/Product.SubscriptionInfo.Status+ISubscriptionInfoStatus.swift Codecov / codecov/patchSources/Flare/Classes/Extensions/Product.SubscriptionInfo.Status+ISubscriptionInfoStatus.swift#L12-L14
|
||
|
||
var subscriptionRenewalInfo: VerificationResult<RenewalInfo> { | ||
switch self.renewalInfo { | ||
case let .verified(renewalInfo): | ||
return .verified(.init(renewalInfo: renewalInfo)) | ||
case let .unverified(renewalInfo, error): | ||
return .unverified(.init(renewalInfo: renewalInfo), error) | ||
} | ||
} | ||
Check warning on line 23 in Sources/Flare/Classes/Extensions/Product.SubscriptionInfo.Status+ISubscriptionInfoStatus.swift Codecov / codecov/patchSources/Flare/Classes/Extensions/Product.SubscriptionInfo.Status+ISubscriptionInfoStatus.swift#L16-L23
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
Sources/Flare/Classes/Listeners/TransactionListener/TransactionListenerDelegate.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// | ||
// Flare | ||
// Copyright © 2024 Space Code. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
protocol TransactionListenerDelegate: AnyObject { | ||
func transactionListener( | ||
_ transactionListener: ITransactionListener, | ||
transactionDidUpdate result: Result<StoreTransaction, IAPError> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// | ||
// Flare | ||
// Copyright © 2024 Space Code. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import StoreKit | ||
|
||
// MARK: - ExpirationReason | ||
|
||
public enum ExpirationReason { | ||
case autoRenewDisabled | ||
case billingError | ||
case didNotConsentToPriceIncrease | ||
case productUnavailable | ||
case unknown | ||
} | ||
|
||
extension ExpirationReason { | ||
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, visionOS 1.0, *) | ||
init(expirationReason: Product.SubscriptionInfo.RenewalInfo.ExpirationReason) { | ||
switch expirationReason { | ||
case .autoRenewDisabled: | ||
self = .autoRenewDisabled | ||
case .billingError: | ||
self = .billingError | ||
case .didNotConsentToPriceIncrease: | ||
self = .didNotConsentToPriceIncrease | ||
case .productUnavailable: | ||
self = .productUnavailable | ||
case .unknown: | ||
self = .unknown | ||
default: | ||
self = .unknown | ||
} | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
Sources/Flare/Classes/Models/Internal/Protocols/IRenewalInfo.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// | ||
// Flare | ||
// Copyright © 2024 Space Code. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import StoreKit | ||
|
||
public protocol IRenewalInfo { | ||
/// The JSON representation of the renewal information. | ||
var jsonRepresentation: Data { get } | ||
|
||
/// The original transaction identifier for the subscription group. | ||
var originalTransactionID: UInt64 { get } | ||
|
||
/// The currently active product identifier, or the most recently active product identifier if the | ||
/// subscription is expired. | ||
var currentProductID: String { get } | ||
|
||
/// Whether the subscription will auto renew at the end of the current billing period. | ||
var willAutoRenew: Bool { get } | ||
|
||
/// The product identifier the subscription will auto renew to at the end of the current billing period. | ||
/// | ||
/// If the user disabled auto renewing, this property will be `nil`. | ||
var autoRenewPreference: String? { get } | ||
|
||
/// The reason the subscription expired. | ||
var expirationReason: ExpirationReason? { get } | ||
|
||
/// The status of a price increase for the user. | ||
var priceIncreaseStatus: PriceIncreaseStatus { get } | ||
|
||
/// Whether the subscription is in a billing retry period. | ||
var isInBillingRetry: Bool { get } | ||
|
||
/// The date the billing grace period will expire. | ||
var gracePeriodExpirationDate: Date? { get } | ||
|
||
/// Identifies the offer that will be applied to the next billing period. | ||
/// | ||
/// If `offerType` is `promotional`, this will be the offer identifier. If `offerType` is | ||
/// `code`, this will be the offer code reference name. This will be `nil` for `introductory` | ||
/// offers and if there will be no offer applied for the next billing period. | ||
var offerID: String? { get } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
Sources/Flare/Classes/Models/Internal/Protocols/ISubscriptionInfo.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// | ||
// Flare | ||
// Copyright © 2024 Space Code. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
protocol ISubscriptionInfo { | ||
var subscriptionStatus: [SubscriptionInfoStatus] { get async throws } | ||
} |
11 changes: 11 additions & 0 deletions
11
Sources/Flare/Classes/Models/Internal/Protocols/ISubscriptionInfoStatus.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// | ||
// Flare | ||
// Copyright © 2024 Space Code. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
protocol ISubscriptionInfoStatus { | ||
var renewalState: RenewalState { get } | ||
var subscriptionRenewalInfo: VerificationResult<RenewalInfo> { get } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
Sources/Flare/Classes/Models/Internal/SK2RenewalInfo.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// | ||
// Flare | ||
// Copyright © 2024 Space Code. All rights reserved. | ||
// | ||
|
||
import StoreKit | ||
|
||
// MARK: - SK2RenewalInfo | ||
|
||
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, visionOS 1.0, *) | ||
struct SK2RenewalInfo { | ||
// MARK: Properties | ||
|
||
let underlyingRenewalInfo: Product.SubscriptionInfo.RenewalInfo | ||
|
||
// MARK: Initialization | ||
|
||
init(underlyingRenewalInfo: Product.SubscriptionInfo.RenewalInfo) { | ||
self.underlyingRenewalInfo = underlyingRenewalInfo | ||
} | ||
} | ||
|
||
// MARK: IRenewalInfo | ||
|
||
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, visionOS 1.0, *) | ||
extension SK2RenewalInfo: IRenewalInfo { | ||
var jsonRepresentation: Data { | ||
underlyingRenewalInfo.jsonRepresentation | ||
} | ||
|
||
var originalTransactionID: UInt64 { | ||
underlyingRenewalInfo.originalTransactionID | ||
} | ||
|
||
var willAutoRenew: Bool { | ||
underlyingRenewalInfo.willAutoRenew | ||
} | ||
|
||
var autoRenewPreference: String? { | ||
underlyingRenewalInfo.autoRenewPreference | ||
} | ||
|
||
var isInBillingRetry: Bool { | ||
underlyingRenewalInfo.isInBillingRetry | ||
} | ||
|
||
var gracePeriodExpirationDate: Date? { | ||
underlyingRenewalInfo.gracePeriodExpirationDate | ||
} | ||
|
||
var offerID: String? { | ||
underlyingRenewalInfo.offerID | ||
} | ||
|
||
var currentProductID: String { | ||
underlyingRenewalInfo.currentProductID | ||
} | ||
|
||
var expirationReason: ExpirationReason? { | ||
guard let expirationReason = self.underlyingRenewalInfo.expirationReason else { | ||
return nil | ||
} | ||
return ExpirationReason(expirationReason: expirationReason) | ||
} | ||
|
||
var priceIncreaseStatus: PriceIncreaseStatus { | ||
PriceIncreaseStatus(underlyingRenewalInfo.priceIncreaseStatus) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
Sources/Flare/Classes/Models/Internal/SK2SubscriptionInfo.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// | ||
// Flare | ||
// Copyright © 2024 Space Code. All rights reserved. | ||
// | ||
|
||
import StoreKit | ||
|
||
// MARK: - SK2SubscriptionInfo | ||
|
||
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, visionOS 1.0, *) | ||
struct SK2SubscriptionInfo { | ||
// MARK: Properties | ||
|
||
private let underlyingInfo: Product.SubscriptionInfo | ||
|
||
// MARK: Initialization | ||
|
||
init(underlyingInfo: Product.SubscriptionInfo) { | ||
self.underlyingInfo = underlyingInfo | ||
} | ||
} | ||
|
||
// MARK: ISubscriptionInfo | ||
|
||
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, visionOS 1.0, *) | ||
extension SK2SubscriptionInfo: ISubscriptionInfo { | ||
var subscriptionStatus: [SubscriptionInfoStatus] { | ||
get async throws { | ||
try await self.underlyingInfo.status.map { SubscriptionInfoStatus(underlyingStatus: $0) } | ||
} | ||
} | ||
} |
Oops, something went wrong.