comparison Simoleon/UI/RestoreButton.swift @ 156:84137052813d

Refactor code
author Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
date Sat, 28 Aug 2021 11:15:25 +0100
parents Simoleon/Helpers/RestoreButton.swift@c7b6249ab745
children
comparison
equal deleted inserted replaced
155:681f2cbe8c7f 156:84137052813d
1 //
2 // RestoreButton.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 RestoreButton: View {
12 @Binding var showingSubscriptionPaywall: Bool
13 @State private var alertTitle: LocalizedStringKey = ""
14 @State private var alertMessage: LocalizedStringKey = ""
15 @State private var restoringPurchases = false
16 @State private var showingAlert = false
17
18 var body: some View {
19 Button(action: restorePurchases) {
20 if restoringPurchases {
21 ProgressView()
22 } else {
23 Text("Restore purchases")
24 }
25 }
26 .alert(isPresented: $showingAlert) {
27 Alert(title: Text(alertTitle), message: Text(alertMessage), dismissButton: .default(Text("Ok")))
28 }
29 }
30
31 private func restorePurchases() {
32 restoringPurchases = true
33
34 Purchases.shared.restoreTransactions { purchaserInfo, error in
35 if purchaserInfo?.entitlements["all"]?.isActive == true {
36 showingSubscriptionPaywall = false
37 } else {
38 alertTitle = LocalizedStringKey("No subscriptions found")
39 alertMessage = LocalizedStringKey("You are not subscripted to Simoleon yet.")
40 restoringPurchases = false
41 showingAlert = true
42 }
43
44 if let error = error as NSError? {
45 alertTitle = LocalizedStringKey(error.localizedDescription)
46 alertMessage = LocalizedStringKey(error.localizedFailureReason ?? "")
47 showingAlert = true
48 }
49 }
50 }
51 }
52
53 struct RestoreButton_Previews: PreviewProvider {
54 static var previews: some View {
55 RestoreButton(showingSubscriptionPaywall: .constant(true))
56 }
57 }