Mercurial > public > simoleon
comparison Simoleon/UI/SubscribeButton.swift @ 156:84137052813d
Refactor code
author | Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com> |
---|---|
date | Sat, 28 Aug 2021 11:15:25 +0100 |
parents | Simoleon/Helpers/SubscribeButton.swift@3133bf6f6deb |
children |
comparison
equal
deleted
inserted
replaced
155:681f2cbe8c7f | 156:84137052813d |
---|---|
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 @State private var price = "" | |
14 @State private var alertTitle = "" | |
15 @State private var alertMessage = "" | |
16 @State private var showingAlert = false | |
17 @State private var showingPrice = false | |
18 | |
19 var body: some View { | |
20 Button(action: purchaseMonthlySubscription) { | |
21 RoundedRectangle(cornerRadius: 15) | |
22 .frame(height: 60) | |
23 .overlay( | |
24 VStack { | |
25 if showingPrice { | |
26 Text("Subscribe for \(price) / month") | |
27 .foregroundColor(.white) | |
28 .fontWeight(.semibold) | |
29 } else { | |
30 ProgressView() | |
31 } | |
32 } | |
33 ) | |
34 } | |
35 .onAppear(perform: fetchMonthlySubscription) | |
36 .alert(isPresented: $showingAlert) { | |
37 Alert(title: Text(alertTitle), message: Text(alertMessage), dismissButton: .default(Text("Ok"))) | |
38 } | |
39 } | |
40 | |
41 private func fetchMonthlySubscription() { | |
42 Purchases.shared.offerings { (offerings, error) in | |
43 if let product = offerings?.current?.monthly?.product { | |
44 price = formatCurrency(product.priceLocale, product.price) | |
45 showingPrice = true | |
46 } | |
47 | |
48 if let error = error as NSError? { | |
49 alertTitle = error.localizedDescription | |
50 alertMessage = error.localizedFailureReason ?? "" | |
51 price = "-" | |
52 showingPrice = true | |
53 showingAlert = true | |
54 } | |
55 } | |
56 } | |
57 | |
58 private func purchaseMonthlySubscription() { | |
59 showingPrice = false | |
60 | |
61 Purchases.shared.offerings { (offerings, error) in | |
62 if let package = offerings?.current?.monthly { | |
63 | |
64 Purchases.shared.purchasePackage(package) { (transaction, purchaserInfo, error, userCancelled) in | |
65 if purchaserInfo?.entitlements["all"]?.isActive == true { | |
66 showingPrice = true | |
67 showingSubscriptionPaywall = false | |
68 } | |
69 | |
70 if let error = error as NSError? { | |
71 alertTitle = error.localizedDescription | |
72 alertMessage = error.localizedFailureReason ?? "" | |
73 showingPrice = true | |
74 showingAlert = true | |
75 } | |
76 } | |
77 | |
78 if let error = error as NSError? { | |
79 alertTitle = error.localizedDescription | |
80 alertMessage = error.localizedFailureReason ?? "" | |
81 showingPrice = true | |
82 showingAlert = true | |
83 } | |
84 } | |
85 } | |
86 } | |
87 | |
88 private func formatCurrency(_ locale: Locale, _ amount: NSDecimalNumber) -> String { | |
89 let formatter = NumberFormatter() | |
90 formatter.locale = locale | |
91 formatter.numberStyle = .currency | |
92 | |
93 // It won't fail. Check unit test | |
94 let formattedAmount = formatter.string(from: amount as NSNumber)! | |
95 | |
96 return formattedAmount | |
97 } | |
98 } | |
99 | |
100 struct SubscribeButton_Previews: PreviewProvider { | |
101 static var previews: some View { | |
102 SubscribeButton(showingSubscriptionPaywall: .constant(true)) | |
103 } | |
104 } |