-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathFeedbackPaywallViewModel.swift
64 lines (51 loc) · 2.14 KB
/
FeedbackPaywallViewModel.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//
// FeedbackPaywallViewModel.swift
// Lockdown
//
// Created by Fabian Mistoiu on 10.10.2024.
// Copyright © 2024 Confirmed Inc. All rights reserved.
//
import Foundation
import StoreKit
class FeedbackPaywallViewModel {
struct PaywallPlan {
let id: String
let name: String
let price: String
let pricePeriod: String?
let promo: String?
}
@Published var paywallPlans: [PaywallPlan] = []
@Published var selectedPlanIndex: Int = 0
private let products: FeedbackProducts
private let subscriptionInfo: [InternalSubscription]
var onCloseHandler: ((UIViewController) -> Void)? = nil
var onPurchaseHandler: ((UIViewController, String) -> Void)? = nil
init(products: FeedbackProducts, subscriptionInfo: [InternalSubscription]) {
self.products = products
self.subscriptionInfo = subscriptionInfo
createPaywallPlans()
}
public func selectPlan(at index: Int) {
selectedPlanIndex = index
}
private func createPaywallPlans() {
let currencyFormatter = NumberFormatter()
currencyFormatter.usesGroupingSeparator = true
currencyFormatter.numberStyle = .currency
currencyFormatter.locale = subscriptionInfo.first?.priceLocale
guard let yearlyPlan = subscriptionInfo.first(where: { $0.productId == products.yearly}),
let weeklyPlan = subscriptionInfo.first(where: { $0.productId == products.weekly}),
let yearlyPrice = currencyFormatter.string(from: yearlyPlan.price),
let weeklyPrice = currencyFormatter.string(from: weeklyPlan.price) else {
return
}
let yearlyPricePerWeek = yearlyPlan.price.dividing(by: 52)
let saving = 100 - Int(Double(truncating: yearlyPricePerWeek) / Double(truncating: weeklyPlan.price)*100)
paywallPlans = [
PaywallPlan(id: products.yearly, name: "Yearly Plan", price: yearlyPrice, pricePeriod: nil, promo: "SAVE \(saving)%"),
PaywallPlan(id: products.weekly, name: "Weekly Plan", price: weeklyPrice, pricePeriod: "per week", promo: nil)
]
selectedPlanIndex = 0
}
}