comparison Simoleon/Settings.swift @ 42:d25b02d439d4

Minor updates subscription and legal requirements
author Dennis Concepción Martín <dennisconcepcionmartin@gmail.com>
date Mon, 26 Jul 2021 15:35:06 +0100
parents 7703c122ce96
children 2eb05f396fcd
comparison
equal deleted inserted replaced
41:7703c122ce96 42:d25b02d439d4
7 7
8 import SwiftUI 8 import SwiftUI
9 import Purchases 9 import Purchases
10 10
11 struct Settings: View { 11 struct Settings: View {
12 @EnvironmentObject var subscriptionController: SubscriptionController
13 @Environment(\.managedObjectContext) private var viewContext 12 @Environment(\.managedObjectContext) private var viewContext
14 @FetchRequest(sortDescriptors: []) private var defaultCurrency: FetchedResults<DefaultCurrency> 13 @FetchRequest(sortDescriptors: []) private var defaultCurrency: FetchedResults<DefaultCurrency>
15 14
16 @State private var selectedDefaultCurrency = "" 15 @State private var selectedDefaultCurrency = ""
17 @State private var showingSubscriptionPaywall = false 16 @State private var showingSubscriptionPaywall = false
17 @State private var entitlementIsActive = false
18 18
19 let currencyPairs: [String] = parseJson("CurrencyPairs.json") 19 let currencyPairs: [String] = parseJson("CurrencyPairs.json")
20 20
21 var body: some View { 21 var body: some View {
22 List { 22 List {
23 Section(header: Text("Subscription", comment: "Section header in settings")) {
24 if subscriptionController.isActive {
25 NavigationLink(destination: SubscriberInfo()) {
26 Text("Information", comment: "Button to show subscription information in settings")
27 }
28 } else {
29 Button(action: { showingSubscriptionPaywall = true }) {
30 Text("Subscribe", comment: "Button to suscribe in settings")
31 }
32 }
33 }
34
35 Section(header: Text("Preferences", comment: "Section header in settings")) { 23 Section(header: Text("Preferences", comment: "Section header in settings")) {
36 if subscriptionController.isActive { 24 if entitlementIsActive {
37 Picker(selection: $selectedDefaultCurrency, label: Text("Default currency", comment: "Picker to select default currency"), content: { 25 Picker(selection: $selectedDefaultCurrency, label: Text("Default currency", comment: "Picker to select default currency"), content: {
38 ForEach(currencyPairs.sorted(), id: \.self) { currencyPair in 26 ForEach(currencyPairs.sorted(), id: \.self) { currencyPair in
39 Text(currencyPair) 27 Text(currencyPair)
40 } 28 }
41 }) 29 })
81 Section(header: Text("About")) { 69 Section(header: Text("About")) {
82 Link(destination: URL(string: "https://dennistech.io")!) { 70 Link(destination: URL(string: "https://dennistech.io")!) {
83 Text("Website", comment: "Button to go to Dennis Tech website") 71 Text("Website", comment: "Button to go to Dennis Tech website")
84 } 72 }
85 73
86 Link(destination: URL(string: "https://dennistech.io")!) { 74 Link(destination: URL(string: "https://dennistech.io/privacy-policy")!) {
87 Text("Privacy Policy", comment: "Button to go to app privacy policy") 75 Text("Privacy Policy", comment: "Button to go to app privacy policy")
88 } 76 }
89 77
90 Link(destination: URL(string: "https://dennistech.io/terms-of-use")!) { 78 Link(destination: URL(string: "https://dennistech.io/terms-of-use")!) {
91 Text("Terms of Use", comment: "Button to go to app terms of use") 79 Text("Terms of Use", comment: "Button to go to app terms of use")
92 } 80 }
93 } 81 }
94 } 82 }
95 .onAppear(perform: onAppear) 83 .onAppear {
84 /*
85 if selectedDefaultCurrency is empty -> view is appearing for the first time -> set initial default curency for picker
86 else -> view is appearing after user selected another default currency -> save it to core data
87 */
88 if selectedDefaultCurrency == "" {
89 self.selectedDefaultCurrency = defaultCurrency.first?.pair ?? "USD/GBP"
90 } else {
91 setCoreData()
92 }
93 }
96 .listStyle(InsetGroupedListStyle()) 94 .listStyle(InsetGroupedListStyle())
97 .navigationTitle(Text("Settings", comment: "Navigation title")) 95 .navigationTitle(Text("Settings", comment: "Navigation title"))
98 .sheet(isPresented: $showingSubscriptionPaywall) { 96 .sheet(isPresented: $showingSubscriptionPaywall, onDismiss: checkEntitlement) {
99 Subscription(showingSubscriptionPaywall: $showingSubscriptionPaywall) 97 SubscriptionPaywall(showingSubscriptionPaywall: $showingSubscriptionPaywall)
100 .environmentObject(subscriptionController)
101 } 98 }
102 .if(UIDevice.current.userInterfaceIdiom == .phone) { content in 99 .if(UIDevice.current.userInterfaceIdiom == .phone) { content in
103 NavigationView { content } 100 NavigationView { content }
104 } 101 }
105 } 102 }
106 103
107 private func onAppear() { 104 /*
108 // Set initial value of the picker 105 Save default currency to core data
109 if selectedDefaultCurrency == "" { 106 */
110 self.selectedDefaultCurrency = defaultCurrency.first?.pair ?? "USD/GBP"
111 } else {
112 setCoreData()
113 }
114 }
115
116 private func setCoreData() { 107 private func setCoreData() {
117 if self.defaultCurrency.isEmpty { // If it's empty -> add record 108 if self.defaultCurrency.isEmpty { // If it's empty -> add record
118 let defaultCurrency = DefaultCurrency(context: viewContext) 109 let defaultCurrency = DefaultCurrency(context: viewContext)
119 defaultCurrency.pair = selectedDefaultCurrency 110 defaultCurrency.pair = selectedDefaultCurrency
120 111
126 } else { // If not, update record 117 } else { // If not, update record
127 self.defaultCurrency.first?.pair = selectedDefaultCurrency 118 self.defaultCurrency.first?.pair = selectedDefaultCurrency
128 try? viewContext.save() 119 try? viewContext.save()
129 } 120 }
130 } 121 }
122
123 /*
124 Check if user subscription is active
125 */
126 private func checkEntitlement() {
127 Purchases.shared.purchaserInfo { (purchaserInfo, error) in
128 if purchaserInfo?.entitlements["all"]?.isActive == true {
129 entitlementIsActive = true
130 print("Entitlement is active")
131 } else {
132 entitlementIsActive = false
133 print("Entitlement is NOT active")
134 }
135 }
136 }
131 } 137 }
132 138
133 struct Settings_Previews: PreviewProvider { 139 struct Settings_Previews: PreviewProvider {
134 static var previews: some View { 140 static var previews: some View {
135 Settings() 141 Settings()
136 .environmentObject(SubscriptionController())
137 .environment(\.locale, .init(identifier: "es")) 142 .environment(\.locale, .init(identifier: "es"))
138 } 143 }
139 } 144 }