Skip to content

Commit

Permalink
Fix trial notification
Browse files Browse the repository at this point in the history
  • Loading branch information
aromanov91 committed Jul 3, 2023
1 parent 1c984fe commit 9f6f6a2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ extension StoreViewModel {
}
}

func buy(product: Product, trialNotification: Bool = false) async -> Bool {
func buy(product: Product) async -> Bool {
isBuyLoading = true
do {
let result = try await storeKitService.purchase(product)
Expand All @@ -285,21 +285,6 @@ extension StoreViewModel {
isPremium = true
isPremiumActivated = true
isBuyLoading = false
if trialNotification, product.type == .autoRenewable, product.subscription?.introductoryOffer != nil {
let result = await localNotificationService.requestAccess()
if case let .success(status) = result, status, let trialDaysCount = product.trialDaysCount {
let timeInterval = TimeInterval((trialDaysCount - 2) * 24 * 60 * 60)
let notificationTime = Date().addingTimeInterval(timeInterval)
let dateComponents = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute], from: notificationTime)
await localNotificationService.schedule(localNotification: .init(
id: UUID(),
title: "Trial ends soon",
body: "Subscription ends in 2 days",
dateComponents: dateComponents,
repeats: false
))
}
}
return true
case .failure:
isBuyLoading = false
Expand All @@ -315,6 +300,24 @@ extension StoreViewModel {
return false
}
}

func addTrialNotification(product: Product) async {
if product.type == .autoRenewable, product.subscription?.introductoryOffer != nil {
let result = await localNotificationService.requestAccess()
if case let .success(status) = result, status, let trialDaysCount = product.trialDaysCount {
let timeInterval = TimeInterval((trialDaysCount - 2) * 24 * 60 * 60)
let notificationTime = Date().addingTimeInterval(timeInterval)
let dateComponents = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute], from: notificationTime)
await localNotificationService.schedule(localNotification: .init(
id: UUID(),
title: "Trial ends soon",
body: "Subscription ends in 2 days",
dateComponents: dateComponents,
repeats: false
))
}
}
}
}

extension StoreViewModel {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ struct StorePaymentButtonBar: View {
Button {
if let selectedProduct = viewModel.selectedProduct {
Task {
await viewModel.buy(product: selectedProduct, trialNotification: trialNotification)
let status = await viewModel.buy(product: selectedProduct)
if trialNotification, status {
await viewModel.addTrialNotification(product: selectedProduct)
}
}
}
} label: {
Expand Down

0 comments on commit 9f6f6a2

Please sign in to comment.