comparison Simoleon/Settings.swift @ 23:699b5bb619db

UserSettings CoreData implemented
author Dennis Concepción Martín <dennisconcepcionmartin@gmail.com>
date Tue, 20 Jul 2021 09:54:41 +0100
parents 3596690dda73
children bda6a55d027a
comparison
equal deleted inserted replaced
22:3596690dda73 23:699b5bb619db
6 // 6 //
7 7
8 import SwiftUI 8 import SwiftUI
9 9
10 struct Settings: View { 10 struct Settings: View {
11 @Environment(\.managedObjectContext) private var viewContext
12 @FetchRequest(sortDescriptors: []) private var userSettings: FetchedResults<UserSettings>
13 @State private var selectedCurrencyPair = "USD/GBP"
11 let currencyPairs: [String] = parseJson("CurrencyPairs.json") 14 let currencyPairs: [String] = parseJson("CurrencyPairs.json")
12 @State private var selectedCurrencyPair = "USD/GBP"
13 15
14 var body: some View { 16 var body: some View {
15 List { 17 List {
16 Section(header: Text("Preferences")) { 18 Section(header: Text("Preferences")) {
17 Picker("Default currency", selection: $selectedCurrencyPair) { 19 Picker("Default currency", selection: $selectedCurrencyPair) {
18 ForEach(currencyPairs.sorted(), id: \.self) { currencyPair in 20 ForEach(currencyPairs.sorted(), id: \.self) { currencyPair in
19 Text(currencyPair) 21 Text(currencyPair)
20 } 22 }
21 } 23 }
24 .onChange(of: selectedCurrencyPair, perform: { selectedCurrencyPair in
25 setDefaultCurrency()
26 })
22 } 27 }
23 28
24 Section(header: Text("Stay in touch")) { 29 Section(header: Text("Stay in touch")) {
25 HStack { 30 Link(destination: URL(string: "https://itunes.apple.com/app/id1576390953?action=write-review")!) {
26 Image(systemName: "heart.fill") 31 HStack {
27 .foregroundColor(Color(.systemRed)) 32 Image(systemName: "heart.fill")
28 .imageScale(.large) 33 .foregroundColor(Color(.systemRed))
29 34 .imageScale(.large)
30 Text("Rate Simoleon") 35
36 Text("Rate Simoleon")
37 }
31 } 38 }
32 39
33 Link(destination: URL(string: "https://dennistech.io")!) { 40 Link(destination: URL(string: "https://dennistech.io")!) {
34 HStack { 41 HStack {
35 Image("TwitterLogo") 42 Image("TwitterLogo")
52 } 59 }
53 60
54 Section(header: Text("About")) { 61 Section(header: Text("About")) {
55 Link("Website", destination: URL(string: "https://dennistech.io")!) 62 Link("Website", destination: URL(string: "https://dennistech.io")!)
56 Link("Privacy Policy", destination: URL(string: "https://dennistech.io")!) 63 Link("Privacy Policy", destination: URL(string: "https://dennistech.io")!)
64 Link("Developer's Twitter", destination: URL(string: "https://twitter.com/dennisconcep")!)
57 } 65 }
58 } 66 }
67 .onAppear(perform: fetchUserSettings)
59 .listStyle(InsetGroupedListStyle()) 68 .listStyle(InsetGroupedListStyle())
60 .navigationTitle("Settings") 69 .navigationTitle("Settings")
61 .if(UIDevice.current.userInterfaceIdiom == .phone) { content in 70 .if(UIDevice.current.userInterfaceIdiom == .phone) { content in
62 NavigationView { content } 71 NavigationView { content }
72 }
73 }
74
75 /*
76 1) Fetch default currency from User Settings
77 2) Change State var currencyPair
78 */
79 private func fetchUserSettings() {
80 if let userSettings = userSettings.first {
81 self.selectedCurrencyPair = userSettings.defaultCurrency ?? "USD/GBP"
82 }
83 }
84
85 private func setDefaultCurrency() {
86 if self.userSettings.isEmpty { /// If it's empty -> add record
87 let userSettings = UserSettings(context: viewContext)
88 userSettings.defaultCurrency = selectedCurrencyPair
89
90 do {
91 try viewContext.save()
92 } catch {
93 print(error.localizedDescription)
94 }
95 } else { /// If not, update record
96 self.userSettings.first?.defaultCurrency = selectedCurrencyPair
97 try? viewContext.save()
63 } 98 }
64 } 99 }
65 } 100 }
66 101
67 struct Settings_Previews: PreviewProvider { 102 struct Settings_Previews: PreviewProvider {