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

[iOS] - Add Free Trial to Leo on iOS #26001

Merged
merged 3 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
9 changes: 8 additions & 1 deletion ios/brave-ios/Sources/AIChat/AIChatStrings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ extension Strings {
"aichat.paywallYearlySubscriptionDescription",
tableName: "BraveLeo",
bundle: .module,
value: "SAVE UP TO 25%",
value: "BEST VALUE",
comment:
"The description indicating yearly subscription that show how much user is saving percentage"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment needs an update

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

)
Expand Down Expand Up @@ -397,6 +397,13 @@ extension Strings {
value: "Upgrade Now",
comment: "The title of the button for action triggering purchase"
)
public static let paywallPurchaseActionIntroOfferTitle = NSLocalizedString(
"aichat.paywallPurchaseActionIntroOfferTitle",
tableName: "BraveLeo",
bundle: .module,
value: "Try 7 Days Free",
comment: "The title of the button for action triggering purchase"
)
public static let paywallPremiumUpsellTitle = NSLocalizedString(
"aichat.paywallPremiumUpsellTitle",
tableName: "BraveLeo",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,20 @@ struct AIChatPaywallView: View {
private var selectedTierType: AIChatSubscriptionTier = .monthly

@State
private var availableTierTypes: [AIChatSubscriptionTier] = [.monthly]
private var availableTierTypes: [AIChatSubscriptionTier] = [.monthly, .yearly]

@ObservedObject
private(set) var storeSDK = BraveStoreSDK.shared

@State
private var paymentStatus: AIChatPaymentStatus = .success

@State
private var isMonthlyIntroOfferAvailable: Bool = false

@State
private var isYearlyIntroOfferAvailable: Bool = false

@State
private var isShowingPurchaseAlert = false

Expand Down Expand Up @@ -151,6 +157,9 @@ struct AIChatPaywallView: View {
.onDisappear {
iapRestoreTimer?.cancel()
}
.task {
await fetchIntroOfferStatus()
}
}

private var tierSelection: some View {
Expand Down Expand Up @@ -203,10 +212,19 @@ struct AIChatPaywallView: View {
.tint(Color.white)
.padding()
} else {
Text(Strings.AIChat.paywallPurchaseActionTitle)
.font(.body.weight(.semibold))
.foregroundColor(Color(.white))
.padding()
if (selectedTierType == .monthly && isMonthlyIntroOfferAvailable)
|| (selectedTierType == .yearly && isYearlyIntroOfferAvailable)
{
Text(Strings.AIChat.paywallPurchaseActionIntroOfferTitle)
.font(.body.weight(.semibold))
.foregroundColor(Color(.white))
.padding()
} else {
Text(Strings.AIChat.paywallPurchaseActionTitle)
.font(.body.weight(.semibold))
.foregroundColor(Color(.white))
.padding()
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to create a conditional view here, just swap text instead:

let isIntroOfferAvailable = 
  (selectedTierType == .monthly && isMonthlyIntroOfferAvailable) || 
  (selectedTierType == .yearly && isYearlyIntroOfferAvailable)
Text(isIntroOfferAvailable ? 
    Strings.AIChat.paywallPurchaseActionIntroOfferTitle) :
    Strings.AIChat.paywallPurchaseActionTitle)
  .font(.body.weight(.semibold))
  .foregroundColor(Color(.white))
  .padding()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

}
}
.frame(maxWidth: .infinity)
Expand Down Expand Up @@ -282,6 +300,20 @@ struct AIChatPaywallView: View {
isShowingPurchaseAlert = true
}
}

private func fetchIntroOfferStatus() async {
paymentStatus = .ongoing

isMonthlyIntroOfferAvailable = await storeSDK.isIntroOfferAvailable(
for: BraveStoreProduct.leoMonthly
)

isYearlyIntroOfferAvailable = await storeSDK.isIntroOfferAvailable(
for: BraveStoreProduct.leoYearly
)

paymentStatus = .success
}
}

private struct AIChatPremiumTierSelectionView: View {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,22 @@ public class AppStoreSDK: ObservableObject {
(try? await subscription(for: product)?.subscription?.status) ?? []
}

/// Retrieves a product's renewable subscription trial status
/// - Parameter product: The product whose subscription trial status to retrieve
/// - Returns: The renewable subscription's trial status
@MainActor
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are all these function @MainActor isolated? None of it seems to require it

Copy link
Contributor Author

@Brandon-T Brandon-T Oct 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh you're right. I got used to applying MainActor to everything because of BraveSkusSDK using Brave-Core functions. But in reality only the BraveSkusSDK needs it. So I removed it from all the AppStoreSDK functions :)

public func isIntroOfferAvailable(for product: any AppStoreProduct) async -> Bool {
guard let subscription = await subscription(for: product)?.subscription else {
return false
}

if subscription.introductoryOffer == nil {
return false
}

return await subscription.isEligibleForIntroOffer == true
}

/// Retrieves a product's renewable subscription renewal information
/// - Parameter product: The product whose renewal to retrieve
/// - Returns: The renewable subscription's renewal information
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ public enum BraveStoreProduct: String, AppStoreProduct, CaseIterable {

switch self {
case .vpnMonthly, .leoMonthly: return "\(prefix)\(productId).monthly"
case .vpnYearly, .leoYearly: return "\(prefix)\(productId).yearly"
case .vpnYearly: return "\(prefix)\(productId).yearly"
case .leoYearly: return "\(prefix)\(productId).yearly.2"
}
}
}
Expand Down
44 changes: 33 additions & 11 deletions ios/brave-ios/Sources/BraveStore/Subscription/StoreKit.storekit
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
{
"appPolicies" : {
"eula" : "",
"policies" : [
{
"locale" : "en_US",
"policyText" : "",
"policyURL" : ""
}
]
},
"identifier" : "9019C18F",
"nonRenewingSubscriptions" : [

Expand Down Expand Up @@ -89,7 +99,10 @@
"recurringSubscriptionPeriod" : "P1M",
"referenceName" : "Brave Leo Monthly",
"subscriptionGroupID" : "AD92D716",
"type" : "RecurringSubscription"
"type" : "RecurringSubscription",
"winbackOffers" : [

]
},
{
"adHocOffers" : [
Expand All @@ -98,7 +111,7 @@
"codeOffers" : [

],
"displayPrice" : "150.00",
"displayPrice" : "149.99",
"familyShareable" : false,
"groupNumber" : 1,
"internalID" : "377962C3",
Expand All @@ -110,11 +123,14 @@
"locale" : "en_US"
}
],
"productID" : "braveleo.yearly",
"productID" : "braveleo.yearly.2",
"recurringSubscriptionPeriod" : "P1Y",
"referenceName" : "Brave Leo Yearly",
"subscriptionGroupID" : "AD92D716",
"type" : "RecurringSubscription"
"type" : "RecurringSubscription",
"winbackOffers" : [

]
}
]
},
Expand All @@ -132,9 +148,9 @@
"codeOffers" : [
{
"eligibility" : [
"expired",
"existing",
"new"
"new",
"expired"
],
"internalID" : "5CF353FA",
"isStackable" : true,
Expand All @@ -146,9 +162,9 @@
{
"displayPrice" : "7.99",
"eligibility" : [
"new",
"expired",
"existing",
"new"
"existing"
],
"internalID" : "3D43C72B",
"isStackable" : true,
Expand Down Expand Up @@ -183,7 +199,10 @@
"recurringSubscriptionPeriod" : "P1M",
"referenceName" : "Brave VPN Monthly",
"subscriptionGroupID" : "E6F28F94",
"type" : "RecurringSubscription"
"type" : "RecurringSubscription",
"winbackOffers" : [

]
},
{
"adHocOffers" : [
Expand Down Expand Up @@ -217,13 +236,16 @@
"recurringSubscriptionPeriod" : "P1Y",
"referenceName" : "Brave VPN Yearly",
"subscriptionGroupID" : "E6F28F94",
"type" : "RecurringSubscription"
"type" : "RecurringSubscription",
"winbackOffers" : [

]
}
]
}
],
"version" : {
"major" : 3,
"major" : 4,
"minor" : 0
}
}
Loading