comparison Simoleon/Settings.swift @ 28:4f862c618b44

Implemented RevenueCat
author Dennis Concepción Martín <dennisconcepcionmartin@gmail.com>
date Thu, 22 Jul 2021 19:06:01 +0100
parents d95582268b44
children c52966834f83
comparison
equal deleted inserted replaced
27:d95582268b44 28:4f862c618b44
4 // 4 //
5 // Created by Dennis Concepción Martín on 19/07/2021. 5 // Created by Dennis Concepción Martín on 19/07/2021.
6 // 6 //
7 7
8 import SwiftUI 8 import SwiftUI
9 import Purchases
9 10
10 struct Settings: View { 11 struct Settings: View {
12 @EnvironmentObject var subscriptionController: SubscriptionController
11 @Environment(\.managedObjectContext) private var viewContext 13 @Environment(\.managedObjectContext) private var viewContext
12 @FetchRequest(sortDescriptors: []) private var defaultCurrency: FetchedResults<DefaultCurrency> 14 @FetchRequest(sortDescriptors: []) private var defaultCurrency: FetchedResults<DefaultCurrency>
15
13 @State private var selectedDefaultCurrency = "" 16 @State private var selectedDefaultCurrency = ""
17 @State private var showingSubscriptionPaywall = false
18
14 let currencyPairs: [String] = parseJson("CurrencyPairs.json") 19 let currencyPairs: [String] = parseJson("CurrencyPairs.json")
15 20
16 var body: some View { 21 var body: some View {
17 List { 22 List {
23 Section(header: Text("Subscription")) {
24 NavigationLink("Information", destination: SubscriberInfo())
25 if !subscriptionController.isActive {
26 Text("Subscribe")
27 .onTapGesture { showingSubscriptionPaywall = true }
28 }
29 }
30
18 Section(header: Text("Preferences")) { 31 Section(header: Text("Preferences")) {
19 Picker("Default currency", selection: $selectedDefaultCurrency) { 32 if subscriptionController.isActive {
20 ForEach(currencyPairs.sorted(), id: \.self) { currencyPair in 33 Picker("Default currency", selection: $selectedDefaultCurrency) {
21 Text(currencyPair) 34 ForEach(currencyPairs.sorted(), id: \.self) { currencyPair in
35 Text(currencyPair)
36 }
22 } 37 }
38 } else {
39 LockedCurrencyPicker()
40 .contentShape(Rectangle())
41 .onTapGesture { showingSubscriptionPaywall = true }
23 } 42 }
24 } 43 }
25 44
26 Section(header: Text("Stay in touch")) { 45 Section(header: Text("Stay in touch")) {
27 Link(destination: URL(string: "https://itunes.apple.com/app/id1576390953?action=write-review")!) { 46 Link(destination: URL(string: "https://itunes.apple.com/app/id1576390953?action=write-review")!) {
58 Section(header: Text("About")) { 77 Section(header: Text("About")) {
59 Link("Website", destination: URL(string: "https://dennistech.io")!) 78 Link("Website", destination: URL(string: "https://dennistech.io")!)
60 Link("Privacy Policy", destination: URL(string: "https://dennistech.io")!) 79 Link("Privacy Policy", destination: URL(string: "https://dennistech.io")!)
61 } 80 }
62 } 81 }
63 .onAppear(perform: setCurrency) 82 .onAppear(perform: onAppear)
64 .listStyle(InsetGroupedListStyle()) 83 .listStyle(InsetGroupedListStyle())
65 .navigationTitle("Settings") 84 .navigationTitle("Settings")
85 .sheet(isPresented: $showingSubscriptionPaywall) {
86 Subscription(showingSubscriptionPaywall: $showingSubscriptionPaywall)
87 .environmentObject(subscriptionController)
88 }
66 .if(UIDevice.current.userInterfaceIdiom == .phone) { content in 89 .if(UIDevice.current.userInterfaceIdiom == .phone) { content in
67 NavigationView { content } 90 NavigationView { content }
68 } 91 }
69 } 92 }
70 93
71 private func setCurrency() { 94 private func onAppear() {
95 // Set initial value of the picker
72 if selectedDefaultCurrency == "" { 96 if selectedDefaultCurrency == "" {
73 self.selectedDefaultCurrency = defaultCurrency.first?.pair ?? "USD/GBP" 97 self.selectedDefaultCurrency = defaultCurrency.first?.pair ?? "USD/GBP"
74 } else { 98 } else {
75 setCoreData() 99 setCoreData()
76 } 100 }
94 } 118 }
95 119
96 struct Settings_Previews: PreviewProvider { 120 struct Settings_Previews: PreviewProvider {
97 static var previews: some View { 121 static var previews: some View {
98 Settings() 122 Settings()
123 .environmentObject(SubscriptionController())
99 } 124 }
100 } 125 }