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