Learn Courses My Dashboard

Problem with RevenueCat & View Not Updating

Hello,

I have started using RevenueCat. I was able to sign up for sandbox testing. However, during testing, my view was not updating. During debugging, I discovered that model.userPurchases[productID!] remained nil. I have been trying for a few days to find out what the problem was but was not successful. Please help.

My Model

import Foundation

struct fetusInfo: Identifiable {
var id:UUID?
var gestationalAge:Int?
var EFW: Int?
var zscore: Double?
var growthPercentile: Double?

}
struct subscription: Identifiable{

var id:UUID?
var productId: String?

static func subscriptionData () -> [subscription] {
    
    var array = [subscription]()
    
    var s1 = subscription()
    s1.productId = "fpg_89.99_4ever"
    array.append(s1)
    
    var s2 = subscription()
    s2.productId = "fgp_099_1m"
    array.append(s2)
    
    var s3 = subscription()
    s3.productId = "fgp_999_1yr"
    array.append(s3)
    
    return array
}

}

My View Model

import Foundation
import Firebase
import SwiftUI

class fetusInfoModel: ObservableObject, Identifiable {

@Published var fetuses = [fetusInfo]()
@Published var subscriptions = [subscription]()
@Published var loggedIn = false
@Published var isOnBoardingNotShowing = false
@Published var today = Date()
@Published var userPurchases = [String:Bool]()

init() {
    self.subscriptions = subscription.subscriptionData()
}

func makePurchase(subscription: subscription) {
    PurchaseService.purchase(productId: subscription.productId){
        if subscription.productId != nil {
             self.userPurchases[subscription.productId!] = true
        }
    }
}

My View

if model.userPurchases[model.subscriptions[0].productId!] != true

                            {
                            HStack{
                                Spacer()

                                Button ("Upgrade to View Weight Projection") {
                                    model.makePurchase(subscription: model.subscriptions[0])
                                }
                                .buttonStyle(CustomButtonStyle())
                                .frame(width: 264)
                                Spacer()
                            }
                            }
                            else
                            {
                            Text("• "+stringPercentileWHO + " Percentile at 39 Weeks:\n     ") + Text("\(Int(EFWatThirtyNineWeeksWHO)) grams or \(projectedPoundsWHO) lb \(projectedOuncesWHO) oz").bold() + Text("\n")
                            }

Thanks