diff Simoleon/Helpers/CurrencySelector.swift @ 77:1069c33d3a42

Added new method to show unlocked content
author Dennis Concepción Martín <dennisconcepcionmartin@gmail.com>
date Sat, 31 Jul 2021 17:09:58 +0100
parents b6f8661300f2
children 529feb1fc8d5
line wrap: on
line diff
--- a/Simoleon/Helpers/CurrencySelector.swift	Sat Jul 31 16:36:01 2021 +0100
+++ b/Simoleon/Helpers/CurrencySelector.swift	Sat Jul 31 17:09:58 2021 +0100
@@ -12,6 +12,7 @@
     @Binding var currencyPair: String
     @Binding var showingCurrencySelector: Bool
     
+    @State private var entitlementIsActive = false
     @State private var searchCurrency = ""
     @State private var showingSubscriptionPaywall = false
     @State private var alertTitle = ""
@@ -40,18 +41,24 @@
                 SearchBar(placeholder: "Search...", text: $searchCurrency)
                     .padding()
                 
+                if entitlementIsActive {
                     List(searchResults, id: \.self) { currencyPair in
-                        Button(action: { select(currencyPair.name) }) {
+                        Button(action: {
+                            self.currencyPair = currencyPair.name
+                            showingCurrencySelector = false
+                        }) {
                             CurrencyRow(currencyPairName: currencyPair.name)
                         }
                     }
-                .id(UUID())
-                .listStyle(PlainListStyle())
-                .gesture(DragGesture()
-                            .onChanged({ _ in
-                                UIApplication.shared.dismissKeyboard()
-                            })
-                )
+                    .listStyle()
+                } else {
+                    List(searchResults, id: \.self) { currencyPair in
+                        Button(action: { select(currencyPair) }) {
+                            CurrencyRow(currencyPairName: currencyPair.name, isLocked: currencyPair.isLocked)
+                        }
+                    }
+                    .listStyle()
+                }
             }
             .navigationTitle("Currencies")
             .navigationBarTitleDisplayMode(.inline)
@@ -63,10 +70,11 @@
                 }
             }
         }
+        .onAppear(perform: checkEntitlement)
         .alert(isPresented: $showingAlert) {
             Alert(title: Text(alertTitle), message: Text(alertMessage), dismissButton: .default(Text("Ok")))
         }
-        .sheet(isPresented: $showingSubscriptionPaywall) {
+        .sheet(isPresented: $showingSubscriptionPaywall, onDismiss: checkEntitlement) {
             SubscriptionPaywall(showingSubscriptionPaywall: $showingSubscriptionPaywall)
         }
     }
@@ -77,13 +85,20 @@
      else:
      * Show subscription paywall
      */
-    private func select(_ currencyPair: String) {
+    private func select(_ currencyPair: CurrencyPairModel) {
+        if currencyPair.isLocked {
+            showingSubscriptionPaywall = true
+        } else {
+            self.currencyPair = currencyPair.name
+            showingCurrencySelector = false
+        }
+    }
+    
+    // Check if user subscription is active
+    private func checkEntitlement() {
         Purchases.shared.purchaserInfo { (purchaserInfo, error) in
             if purchaserInfo?.entitlements["all"]?.isActive == true {
-                self.currencyPair = currencyPair
-                showingCurrencySelector = false
-            } else {
-                showingSubscriptionPaywall = true
+                entitlementIsActive = true
             }
             
             if let error = error as NSError? {
@@ -94,6 +109,11 @@
         }
     }
 }
+extension View {
+    func listStyle() -> some View {
+        self.modifier(ListModifier())
+    }
+}
 
 
 struct CurrencySelector_Previews: PreviewProvider {