comparison Simoleon/Helpers/SubscribeButton.swift @ 28:4f862c618b44

Implemented RevenueCat
author Dennis Concepción Martín <dennisconcepcionmartin@gmail.com>
date Thu, 22 Jul 2021 19:06:01 +0100
parents
children f76d0e26c178
comparison
equal deleted inserted replaced
27:d95582268b44 28:4f862c618b44
1 //
2 // SubscribeButton.swift
3 // Simoleon
4 //
5 // Created by Dennis Concepción Martín on 22/07/2021.
6 //
7
8 import SwiftUI
9 import Purchases
10
11 struct SubscribeButton: View {
12 @Binding var showingSubscriptionPaywall: Bool
13 @EnvironmentObject var subscriptionController: SubscriptionController
14
15 @State private var subscribeButtonText = ""
16 @State private var showingPrice = false
17 @State private var alertTitle = ""
18 @State private var alertMessage = ""
19 @State private var showingAlert = false
20
21 var body: some View {
22 Button(action: purchaseMonthlySubscription) {
23 RoundedRectangle(cornerRadius: 15)
24 .frame(height: 60)
25 .overlay(
26 VStack {
27 if showingPrice {
28 Text(subscribeButtonText)
29 .foregroundColor(.white)
30 .fontWeight(.semibold)
31 } else {
32 ProgressView()
33 }
34 }
35 )
36 }
37 .onAppear(perform: fetchMonthlySubscription)
38 .alert(isPresented: $showingAlert) {
39 Alert(title: Text(alertTitle), message: Text(alertMessage), dismissButton: .default(Text("Ok")))
40 }
41 }
42
43 private func fetchMonthlySubscription() {
44 Purchases.shared.offerings { (offerings, error) in
45 if let product = offerings?.current?.monthly?.product {
46 let price = formatCurrency(product.priceLocale, product.price)
47 subscribeButtonText = "Subscribe for \(price) / month"
48 showingPrice = true
49 }
50
51 if let error = error as NSError? {
52 alertTitle = error.localizedDescription
53 alertMessage = error.localizedFailureReason ?? "If the problem persists send an email to dmartin@dennistech.io"
54 subscribeButtonText = "-"
55 showingPrice = true
56 showingAlert = true
57 }
58 }
59 }
60
61 private func purchaseMonthlySubscription() {
62 showingPrice = false
63
64 Purchases.shared.offerings { (offerings, error) in
65 if let package = offerings?.current?.monthly {
66
67 Purchases.shared.purchasePackage(package) { (transaction, purchaserInfo, error, userCancelled) in
68 if purchaserInfo?.entitlements["all"]?.isActive == true {
69 showingPrice = true
70 subscriptionController.isActive = true
71 showingSubscriptionPaywall = false
72 }
73
74 if let error = error as NSError? {
75 alertTitle = error.localizedDescription
76 alertMessage = error.localizedFailureReason ?? "If the problem persists send an email to dmartin@dennistech.io"
77 showingPrice = true
78 showingAlert = true
79 }
80 }
81
82 if let error = error as NSError? {
83 alertTitle = error.localizedDescription
84 alertMessage = error.localizedFailureReason ?? "If the problem persists send an email to dmartin@dennistech.io"
85 showingPrice = true
86 showingAlert = true
87 }
88 }
89 }
90 }
91
92 private func formatCurrency(_ locale: Locale, _ amount: NSDecimalNumber) -> String {
93 let formatter = NumberFormatter()
94 formatter.locale = locale
95 formatter.numberStyle = .currency
96
97 if let formattedAmount = formatter.string(from: amount as NSNumber) {
98 return formattedAmount
99 } else {
100 return "\(amount)\(locale.currencySymbol!)"
101 }
102 }
103 }
104
105 struct SubscribeButton_Previews: PreviewProvider {
106 static var previews: some View {
107 SubscribeButton(showingSubscriptionPaywall: .constant(true))
108 }
109 }