Mercurial > public > simoleon
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Simoleon/UI/RestoreButton.swift Sat Aug 28 11:15:25 2021 +0100 @@ -0,0 +1,57 @@ +// +// 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)) + } +}