Mercurial > public > geoquiz
comparison GeoQuiz/Logic/StoreKitController.swift @ 19:f140bb277c96
refactor code
author | Dennis C. M. <dennis@denniscm.com> |
---|---|
date | Sun, 23 Oct 2022 00:11:38 +0100 |
parents | GeoQuiz/Logic/StoreKitRCClass.swift@136928bae534 |
children | b145c408f791 |
comparison
equal
deleted
inserted
replaced
18:d20cf93c9812 | 19:f140bb277c96 |
---|---|
1 // | |
2 // StoreKitController.swift | |
3 // GeoQuiz | |
4 // | |
5 // Created by Dennis Concepción Martín on 9/10/22. | |
6 // | |
7 | |
8 import Foundation | |
9 import RevenueCat | |
10 | |
11 class StoreKitController: ObservableObject { | |
12 @Published var errorAlertTitle = "" | |
13 @Published var errorAlertMessage = "" | |
14 | |
15 @Published var showingErrorAlert = false | |
16 @Published var showingSuccessAlert = false | |
17 @Published var showingActivityAlert = false | |
18 | |
19 @Published var offerings: Offerings? = nil | |
20 @Published var customerInfo: CustomerInfo? { | |
21 didSet { | |
22 premiumIsActive = customerInfo?.entitlements["Premium"]?.isActive == true | |
23 } | |
24 } | |
25 | |
26 @Published var premiumIsActive = false | |
27 | |
28 init() { | |
29 Purchases.shared.getCustomerInfo { (customerInfo, error) in | |
30 self.customerInfo = customerInfo | |
31 } | |
32 } | |
33 | |
34 func buy(_ package: Package) { | |
35 showingActivityAlert = true | |
36 | |
37 Purchases.shared.purchase(package: package) { (transaction, customerInfo, error, userCancelled) in | |
38 if customerInfo?.entitlements["Premium"]?.isActive == true { | |
39 self.showingSuccessAlert = true | |
40 } | |
41 | |
42 if let error = error as? RevenueCat.ErrorCode { | |
43 switch error { | |
44 case .purchaseCancelledError: | |
45 self.errorAlertTitle = "Purchase cancelled" | |
46 self.errorAlertMessage = "" | |
47 self.showingErrorAlert = true | |
48 default: | |
49 self.errorAlertTitle = "The purchase failed" | |
50 self.errorAlertMessage = "If the problem persists, contact me at dmartin@dennistech.io" | |
51 self.showingErrorAlert = true | |
52 } | |
53 } | |
54 | |
55 self.customerInfo = customerInfo | |
56 self.showingActivityAlert = false | |
57 } | |
58 } | |
59 | |
60 func restorePurchase() { | |
61 showingActivityAlert = true | |
62 | |
63 Purchases.shared.restorePurchases { customerInfo, error in | |
64 if customerInfo?.entitlements["Premium"]?.isActive == true { | |
65 self.showingSuccessAlert = true | |
66 } else { | |
67 self.errorAlertTitle = "Opps!" | |
68 self.errorAlertMessage = "You don't have GeoQuiz Premium unlocked." | |
69 self.showingErrorAlert = true | |
70 } | |
71 | |
72 if let _ = error { | |
73 self.errorAlertTitle = "The purchase couldn't be restored" | |
74 self.errorAlertMessage = "If the problem persists, contact me at dmartin@dennistech.io" | |
75 self.showingErrorAlert = true | |
76 } | |
77 | |
78 self.customerInfo = customerInfo | |
79 self.showingActivityAlert = false | |
80 } | |
81 } | |
82 | |
83 func fetchOfferings() { | |
84 Purchases.shared.getOfferings { (offerings, error) in | |
85 if let _ = error { | |
86 self.errorAlertTitle = "The product couldn't be fetched" | |
87 self.errorAlertMessage = "If the problem persists, contact me at dmartin@dennistech.io" | |
88 self.showingErrorAlert = true | |
89 } | |
90 | |
91 self.offerings = offerings | |
92 } | |
93 } | |
94 } |