comparison Simoleon/Settings.swift @ 154:8afba86ab8dd

Refactor code
author Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
date Wed, 25 Aug 2021 10:43:12 +0100
parents 2584fd74235a
children
comparison
equal deleted inserted replaced
153:2590ee472aa9 154:8afba86ab8dd
18 @State private var alertTitle = "" 18 @State private var alertTitle = ""
19 @State private var alertMessage = "" 19 @State private var alertMessage = ""
20 @State private var showingAlert = false 20 @State private var showingAlert = false
21 @State private var searchCurrency = "" 21 @State private var searchCurrency = ""
22 22
23 let fileController = FileController()
24
23 /* 25 /*
24 If searched currency string is empty: 26 If searched currency string is empty:
25 * Show all currencies 27 * Show all currencies
26 else: 28 else:
27 * Show filtered list of currencies containing searched currency string 29 * Show filtered list of currencies containing searched currency string
28 */ 30 */
29 var searchResults: [String] { 31 var searchResults: [String] {
30 let currencyPairsSupported: [String] = try! read(json: "CurrencyPairsSupported.json") 32 let currencyPairsSupported: [String] = try! fileController.read(json: "CurrencyPairsSupported.json")
31 if searchCurrency.isEmpty { 33 if searchCurrency.isEmpty {
32 return currencyPairsSupported.sorted() 34 return currencyPairsSupported.sorted()
33 } else { 35 } else {
34 return currencyPairsSupported.filter { $0.contains(searchCurrency.uppercased()) } 36 return currencyPairsSupported.filter { $0.contains(searchCurrency.uppercased()) }
35 } 37 }
112 * Set initial default curency for picker 114 * Set initial default curency for picker
113 else: 115 else:
114 * View is appearing after user selected another default currency 116 * View is appearing after user selected another default currency
115 * Save it to core data 117 * Save it to core data
116 */ 118 */
117 if selectedDefaultCurrency == "" { 119 // if selectedDefaultCurrency == "" {
118 selectedDefaultCurrency = defaultCurrency.first?.pair ?? "USD/GBP" 120 // selectedDefaultCurrency = defaultCurrency.first?.pair ?? "USD/GBP"
119 } else { 121 // } else {
120 setCoreData() 122 // setCoreData()
121 } 123 // }
122 } 124 }
123 .listStyle(InsetGroupedListStyle()) 125 .listStyle(InsetGroupedListStyle())
124 .navigationTitle("Settings") 126 .navigationTitle("Settings")
125 .sheet(isPresented: $showingSubscriptionPaywall, onDismiss: checkEntitlement) { 127 .sheet(isPresented: $showingSubscriptionPaywall, onDismiss: checkEntitlement) {
126 SubscriptionPaywall(showingSubscriptionPaywall: $showingSubscriptionPaywall) 128 SubscriptionPaywall(showingSubscriptionPaywall: $showingSubscriptionPaywall)
129 NavigationView { content } 131 NavigationView { content }
130 } 132 }
131 } 133 }
132 134
133 // Save default currency to core data 135 // Save default currency to core data
134 private func setCoreData() { 136 // private func setCoreData() {
135 if defaultCurrency.isEmpty { // If it's empty -> add record 137 // if defaultCurrency.isEmpty { // If it's empty -> add record
136 let defaultCurrency = DefaultCurrency(context: viewContext) 138 // let defaultCurrency = DefaultCurrency(context: viewContext)
137 defaultCurrency.pair = selectedDefaultCurrency 139 // defaultCurrency.pair = selectedDefaultCurrency
138 140 //
139 do { 141 // do {
140 try viewContext.save() 142 // try viewContext.save()
141 } catch { 143 // } catch {
142 print(error.localizedDescription) 144 // print(error.localizedDescription)
143 } 145 // }
144 } else { // If not, update record 146 // } else { // If not, update record
145 defaultCurrency.first?.pair = selectedDefaultCurrency 147 // defaultCurrency.first?.pair = selectedDefaultCurrency
146 try? viewContext.save() 148 // try? viewContext.save()
147 } 149 // }
148 } 150 // }
149 151
150 // Check if user subscription is active 152 // Check if user subscription is active
151 private func checkEntitlement() { 153 private func checkEntitlement() {
152 #if SCREENSHOTS 154 #if SCREENSHOTS
153 entitlementIsActive = true 155 entitlementIsActive = true