Untitled

 avatar
unknown
swift
2 years ago
5.8 kB
11
Indexable
//
//  PaywallView.swift
//  CustomTextsLockScreen
//
//  Created by Doğancan Mavideniz on 25.09.2022.
//

import SwiftUI
import RevenueCat

struct PaywallView: View {

    @State var currentOffering: Offering?
    @Binding var isPremiumView: Bool
    @Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
    var body: some View {
        ZStack {
            Color.black
                .ignoresSafeArea()
            ScrollView {
                VStack(alignment: .leading) {
                    HStack {
                        Text("Upgrade Premium").font(.custom("Figtree-bold", size: 42)).foregroundColor(.white)
                        Spacer()
                        Button {
                            presentationMode.wrappedValue.dismiss()
                        } label: {
                            Image(systemName: "multiply")
                                .resizable()
                                .frame(width: 25, height: 25)
                                .foregroundColor(.white)
                        }
                    }
                    Text("\nUnlock all features!").font(.custom("Figtree-bold", size: 34)).foregroundColor(.white)

                    Text("\nUnlock all features including special fonts, emoji & symbol features, and supports the developer. Also remove all ad.").font(.custom("Figtree-bold", size: 16)).foregroundColor(.white)
                    if currentOffering != nil {
                        ForEach(currentOffering!.availablePackages) { pkg in
                            Button {
                                Purchases.shared.purchase(package: pkg) { (transaction, customerInfo, error, userCancelled) in

                                    if customerInfo?.entitlements.all["pro"]?.isActive == true {
                                        // Unlock that great "pro" content
                                        isPremiumView = true
                                        presentationMode.wrappedValue.dismiss()
                                    }
                                }
                            } label: {
                                ZStack {
                                    Rectangle()
                                        .cornerRadius(20)
                                        .foregroundColor(.white)
                                    HStack {
                                        Text("\(pkg.storeProduct.subscriptionPeriod!.periodTitle)")
                                            .font(.custom("Figtree-Bold", size: 25)).foregroundColor(.black)
                                        Spacer()
                                        Text("\(pkg.storeProduct.localizedPriceString)")
                                            .font(.custom("Figtree-Bold", size: 35)).foregroundColor(.black)
                                    }.padding()
                                }.frame(height: UIScreen.main.bounds.height * 0.11).padding()
                            }
                        }

                    }

                    HStack {
                        Spacer()
                        Button {
                            Purchases.shared.restorePurchases { (customerInfo, error) in
                                //... check customerInfo to see if entitlement is now active
                                isPremiumView = customerInfo?.entitlements.all["pro"]?.isActive == true
                            }
                        } label: {
                            Text("Restore purchases").foregroundColor(.gray).font(.caption2)
                        }
                        Spacer()
                    }

                    HStack(spacing: 20) {
                        Spacer()
                        Link("Privacy Policy", destination: URL(string: "https://docs.google.com/document/d/1YPf27zlxIVGL4C5PEmXXhHvPd_1_oYwIOHaSy6BVrs0/edit?usp=sharing")!)
                        Link("Terms & Conditions", destination: URL(string: "https://docs.google.com/document/d/1roOcpR1-BrJoUEeOImWwyFjwCoz4PM_600LUrQAvYck/edit?usp=sharing")!)
                        Spacer()
                    }.font(.system(size: 10)).foregroundColor(.gray).padding()
                    VStack {
                        Text("Payment will be charged to your iTunes account at confirmation of purchase. Subscriptions will automatically renew unless auto-renew is turned off at least 24 hours before the end of current period. Your account will be charged according to your plan for renewal within 24 hours prior to the end of the current period. You can manage or turn off auto-renew in your Apple ID account settings at any time after purchase.").font(.system(size: 12))
                            .multilineTextAlignment(.center)
                            .padding(.leading, 30).padding(.trailing, 30)
                        Spacer(minLength: 50)
                    }.foregroundColor(.gray)
                }.padding()
            }
        }.navigationBarBackButtonHidden(true)
            .onAppear() {
            Purchases.shared.getOfferings { offerings, error in
                if let offer = offerings?.current, error == nil {
                    currentOffering = offer
                }
            }
        }
    }
}

extension SubscriptionPeriod {
    var durationTitle: String {
        switch self.unit {
        case .day: return "day"
        case .week: return "week"
        case .month: return "month"
        case .year: return "year"
        @unknown default: return "Unknown"
        }
    }

    var periodTitle: String {
        let periodString = "\(self.value) \(self.durationTitle)"
        let pluralized = self.value > 1 ? periodString + "s" : periodString
        return pluralized
    }
}
Editor is loading...