Mercurial > public > simoleon
comparison Simoleon/Settings.swift @ 27:d95582268b44
Fix bug CoreData and minor UI changes
author | Dennis Concepción Martín <dennisconcepcionmartin@gmail.com> |
---|---|
date | Wed, 21 Jul 2021 12:36:10 +0100 |
parents | 933d9ab04374 |
children | 4f862c618b44 |
comparison
equal
deleted
inserted
replaced
26:337816652bfe | 27:d95582268b44 |
---|---|
7 | 7 |
8 import SwiftUI | 8 import SwiftUI |
9 | 9 |
10 struct Settings: View { | 10 struct Settings: View { |
11 @Environment(\.managedObjectContext) private var viewContext | 11 @Environment(\.managedObjectContext) private var viewContext |
12 @FetchRequest(sortDescriptors: []) private var userSettings: FetchedResults<UserSettings> | 12 @FetchRequest(sortDescriptors: []) private var defaultCurrency: FetchedResults<DefaultCurrency> |
13 @State private var selectedCurrencyPair = "USD/GBP" | 13 @State private var selectedDefaultCurrency = "" |
14 let currencyPairs: [String] = parseJson("CurrencyPairs.json") | 14 let currencyPairs: [String] = parseJson("CurrencyPairs.json") |
15 | 15 |
16 var body: some View { | 16 var body: some View { |
17 List { | 17 List { |
18 Section(header: Text("Preferences")) { | 18 Section(header: Text("Preferences")) { |
19 Picker("Default currency", selection: $selectedCurrencyPair) { | 19 Picker("Default currency", selection: $selectedDefaultCurrency) { |
20 ForEach(currencyPairs.sorted(), id: \.self) { currencyPair in | 20 ForEach(currencyPairs.sorted(), id: \.self) { currencyPair in |
21 Text(currencyPair) | 21 Text(currencyPair) |
22 } | 22 } |
23 } | 23 } |
24 } | 24 } |
58 Section(header: Text("About")) { | 58 Section(header: Text("About")) { |
59 Link("Website", destination: URL(string: "https://dennistech.io")!) | 59 Link("Website", destination: URL(string: "https://dennistech.io")!) |
60 Link("Privacy Policy", destination: URL(string: "https://dennistech.io")!) | 60 Link("Privacy Policy", destination: URL(string: "https://dennistech.io")!) |
61 } | 61 } |
62 } | 62 } |
63 .onChange(of: selectedCurrencyPair, perform: { selectedCurrencyPair in | 63 .onAppear(perform: setCurrency) |
64 setDefaultCurrency() | |
65 }) | |
66 .onAppear(perform: fetchUserSettings) | |
67 .listStyle(InsetGroupedListStyle()) | 64 .listStyle(InsetGroupedListStyle()) |
68 .navigationTitle("Settings") | 65 .navigationTitle("Settings") |
69 .if(UIDevice.current.userInterfaceIdiom == .phone) { content in | 66 .if(UIDevice.current.userInterfaceIdiom == .phone) { content in |
70 NavigationView { content } | 67 NavigationView { content } |
71 } | 68 } |
72 } | 69 } |
73 | 70 |
74 /* | 71 private func setCurrency() { |
75 1) Fetch default currency from User Settings | 72 if selectedDefaultCurrency == "" { |
76 2) Change State var currencyPair | 73 self.selectedDefaultCurrency = defaultCurrency.first?.pair ?? "USD/GBP" |
77 */ | 74 } else { |
78 private func fetchUserSettings() { | 75 setCoreData() |
79 if let userSettings = userSettings.first { | |
80 self.selectedCurrencyPair = userSettings.defaultCurrency ?? "USD/GBP" | |
81 } | 76 } |
82 } | 77 } |
83 | 78 |
84 private func setDefaultCurrency() { | 79 private func setCoreData() { |
85 if self.userSettings.isEmpty { /// If it's empty -> add record | 80 if self.defaultCurrency.isEmpty { // If it's empty -> add record |
86 let userSettings = UserSettings(context: viewContext) | 81 let defaultCurrency = DefaultCurrency(context: viewContext) |
87 userSettings.defaultCurrency = selectedCurrencyPair | 82 defaultCurrency.pair = selectedDefaultCurrency |
88 | 83 |
89 do { | 84 do { |
90 try viewContext.save() | 85 try viewContext.save() |
91 } catch { | 86 } catch { |
92 print(error.localizedDescription) | 87 print(error.localizedDescription) |
93 } | 88 } |
94 } else { /// If not, update record | 89 } else { // If not, update record |
95 self.userSettings.first?.defaultCurrency = selectedCurrencyPair | 90 self.defaultCurrency.first?.pair = selectedDefaultCurrency |
96 try? viewContext.save() | 91 try? viewContext.save() |
97 } | 92 } |
98 } | 93 } |
99 } | 94 } |
100 | 95 |