Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
Adding The capability for type hiding promotion subscription store
Browse files Browse the repository at this point in the history
  • Loading branch information
soner-yuksel committed Nov 15, 2023
1 parent 0bd71e1 commit 6a898f4
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
59 changes: 57 additions & 2 deletions Sources/BraveVPN/BraveVPN.swift
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,29 @@ public class BraveVPN {
helper.mainCredential?.hostnameDisplayValue
}

/// Type of vpnsubscription
public enum SubscriptionType: Equatable {
case monthly, yearly, other
}

/// Type of the active purchased vpn plan
public static var activeSubscriptionType: SubscriptionType {
guard let credential = GRDSubscriberCredential.current() else {
logAndStoreError("subscriptionName: failed to retrieve subscriber credentials")
return .other
}
let productId = credential.subscriptionType

switch productId {
case VPNProductInfo.ProductIdentifiers.monthlySub:
return .monthly
case VPNProductInfo.ProductIdentifiers.yearlySub:
return .yearly
default:
return .other
}
}

/// Name of the purchased vpn plan.
public static var subscriptionName: String {
guard let credential = GRDSubscriberCredential.current() else {
Expand Down Expand Up @@ -652,7 +675,7 @@ public class BraveVPN {

// MARK: - Promotion

/// Editing product promotiuon order first yearly and monthly after
/// Editing product promotion order first monthly and yearly after
@MainActor public static func updateStorePromotionOrder() async {
let storePromotionController = SKProductStorePromotionController.default()
// Fetch Products
Expand All @@ -664,12 +687,44 @@ public class BraveVPN {

// Update the order
do {
try await storePromotionController.update(promotionOrder: [yearlyProduct, monthlyProduct])
try await storePromotionController.update(promotionOrder: [monthlyProduct, yearlyProduct])
} catch {
Logger.module.debug("Error while opdating product promotion order ")
}
}

/// Hiding Store pormotion if the active subscription for the type
@MainActor public static func hideActiveStorePromotion() async {
let storePromotionController = SKProductStorePromotionController.default()

// Fetch Products
guard let yearlyProduct = VPNProductInfo.yearlySubProduct,
let monthlyProduct = VPNProductInfo.monthlySubProduct else {
Logger.module.debug("Found empty while fetching SKProducts for promotion order")
return
}

// Hide the promotion
let activeSubscriptionType = BraveVPN.activeSubscriptionType

switch activeSubscriptionType {
case .monthly:
await hideSubscriptionType(monthlyProduct)
case .yearly:
await hideSubscriptionType(yearlyProduct)
default:
break
}

func hideSubscriptionType(_ product: SKProduct) async {
do {
try await storePromotionController.update(promotionVisibility: .hide, for: product)
} catch {
Logger.module.debug("Error while opdating product promotion order ")
}
}
}

public static func activatePaymentTypeForStoredPromotion(savedPayment: SKPayment?) {
if let payment = savedPayment {
SKPaymentQueue.default().add(payment)
Expand Down
2 changes: 0 additions & 2 deletions Sources/BraveVPN/BraveVPNPreferences.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import Preferences
extension Preferences {
final public class VPN {
public static let popupShowed = Option<Bool>(key: "vpn.popup-showed", default: false)
/// We get it from Guardian's servers.
public static let lastPurchaseProductId = Option<String?>(key: "vpn.last-purchase-id", default: nil)
/// When the current subscription plan expires. It is nil if the user has not bought any vpn plan yet.
/// In case of receipt expiration this date might be set to some old date(like year 1970)
/// to make sure vpn expiration logic will be called.
Expand Down

0 comments on commit 6a898f4

Please sign in to comment.