Mercurial > public > simoleon
view Simoleon/Helpers/RestoreButton.swift @ 150:6eac99e99b96
Add error handling to read json function
author | Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com> |
---|---|
date | Thu, 19 Aug 2021 19:12:56 +0100 |
parents | c7b6249ab745 |
children |
line wrap: on
line source
// // RestoreButton.swift // Simoleon // // Created by Dennis Concepción Martín on 22/07/2021. // import SwiftUI import Purchases struct RestoreButton: View { @Binding var showingSubscriptionPaywall: Bool @State private var alertTitle: LocalizedStringKey = "" @State private var alertMessage: LocalizedStringKey = "" @State private var restoringPurchases = false @State private var showingAlert = false var body: some View { Button(action: restorePurchases) { if restoringPurchases { ProgressView() } else { Text("Restore purchases") } } .alert(isPresented: $showingAlert) { Alert(title: Text(alertTitle), message: Text(alertMessage), dismissButton: .default(Text("Ok"))) } } private func restorePurchases() { restoringPurchases = true Purchases.shared.restoreTransactions { purchaserInfo, error in if purchaserInfo?.entitlements["all"]?.isActive == true { showingSubscriptionPaywall = false } else { alertTitle = LocalizedStringKey("No subscriptions found") alertMessage = LocalizedStringKey("You are not subscripted to Simoleon yet.") restoringPurchases = false showingAlert = true } if let error = error as NSError? { alertTitle = LocalizedStringKey(error.localizedDescription) alertMessage = LocalizedStringKey(error.localizedFailureReason ?? "") showingAlert = true } } } } struct RestoreButton_Previews: PreviewProvider { static var previews: some View { RestoreButton(showingSubscriptionPaywall: .constant(true)) } }