comparison Simoleon/Conversion.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 41a905e591e4
children 2eb05f396fcd
comparison
equal deleted inserted replaced
41:7703c122ce96 42:d25b02d439d4
4 // 4 //
5 // Created by Dennis Concepción Martín on 18/07/2021. 5 // Created by Dennis Concepción Martín on 18/07/2021.
6 // 6 //
7 7
8 import SwiftUI 8 import SwiftUI
9 import Purchases
9 10
10 struct Conversion: View { 11 struct Conversion: View {
11 var fetchUserSettings: Bool
12 @State var currencyPair: String 12 @State var currencyPair: String
13 @EnvironmentObject var subscriptionController: SubscriptionController
14
15 @State private var amountToConvert = "1000" 13 @State private var amountToConvert = "1000"
16 @State private var price: Double = 1.00 14 @State private var price: Double = 1.00
17 @State private var showingConversion = false 15 @State private var showingConversion = false
18 @State private var showingCurrencySelector = false 16 @State private var showingCurrencySelector = false
19 @State private var isEditing = false 17 @State private var amountIsEditing = false
20
21 @Environment(\.managedObjectContext) private var viewContext
22 @FetchRequest(sortDescriptors: []) private var defaultCurrency: FetchedResults<DefaultCurrency>
23 18
24 let currencyMetadata: [String: CurrencyMetadataModel] = parseJson("CurrencyMetadata.json") 19 let currencyMetadata: [String: CurrencyMetadataModel] = parseJson("CurrencyMetadata.json")
25 20
26 var body: some View { 21 var body: some View {
27 ScrollView(showsIndicators: false) { 22 ScrollView(showsIndicators: false) {
29 HStack { 24 HStack {
30 Button(action: { showingCurrencySelector = true }) { 25 Button(action: { showingCurrencySelector = true }) {
31 RoundedRectangle(cornerRadius: 15) 26 RoundedRectangle(cornerRadius: 15)
32 .foregroundColor(Color(.secondarySystemBackground)) 27 .foregroundColor(Color(.secondarySystemBackground))
33 .frame(height: 60) 28 .frame(height: 60)
34 .overlay(CurrencyRow(currencyPair: currencyPair).padding(.horizontal)) 29 .overlay(
30 CurrencyRow(currencyPair: currencyPair)
31 .padding(.horizontal)
32 )
35 } 33 }
36 34
37 FavouriteButton(currencyPair: currencyPair) 35 FavouriteButton(currencyPair: currencyPair)
38 } 36 }
39 37
40 ConversionBox( 38 ConversionBox(
41 currencyPair: $currencyPair, 39 currencyPair: $currencyPair,
42 amountToConvert: $amountToConvert, 40 amountToConvert: $amountToConvert,
43 price: $price, 41 price: $price,
44 showingConversion: $showingConversion, 42 showingConversion: $showingConversion,
45 showingCurrencySelector: $showingCurrencySelector, 43 amountIsEditing: $amountIsEditing
46 isEditing: $isEditing
47 ) 44 )
48 } 45 }
49 .padding() 46 .padding()
50 .onAppear {
51 if fetchUserSettings {
52 fetchingUserSettings()
53 }
54
55 request(currencyPair)
56 }
57 .onChange(of: showingCurrencySelector, perform: { showingCurrencySelector in
58 if !showingCurrencySelector {
59 request(currencyPair)
60 }
61 })
62 .sheet(isPresented: $showingCurrencySelector) { 47 .sheet(isPresented: $showingCurrencySelector) {
63 CurrencySelector(currencyPair: $currencyPair, showingCurrencySelector: $showingCurrencySelector) 48 CurrencySelector(currencyPair: $currencyPair, showingCurrencySelector: $showingCurrencySelector)
64 .environmentObject(subscriptionController)
65 } 49 }
66 } 50 }
51 .onAppear(perform: request)
67 .navigationTitle(Text("Convert", comment: "Navigation title")) 52 .navigationTitle(Text("Convert", comment: "Navigation title"))
68 .toolbar { 53 .toolbar {
69 ToolbarItem(placement: .cancellationAction) { 54 ToolbarItem(placement: .cancellationAction) {
70 if isEditing { 55 if amountIsEditing {
71 Button(action: { 56 Button(action: {
72 UIApplication.shared.dismissKeyboard() 57 UIApplication.shared.dismissKeyboard()
73 isEditing = false 58 amountIsEditing = false
74 }) { 59 }) {
75 Text("Cancel", comment: "Button to stop editing textfield") 60 Text("Cancel", comment: "Button to stop editing textfield")
76 } 61 }
77 } 62 }
78 } 63 }
79 } 64 }
80 .if(UIDevice.current.userInterfaceIdiom == .phone && fetchUserSettings) { content in 65 .if(UIDevice.current.userInterfaceIdiom == .phone) { content in
81 NavigationView { content } 66 NavigationView { content }
82 } 67 }
83 } 68 }
84 69
85 private func request(_ currencyPair: String) { 70 private func request() {
86 let url = "\(readConfig("API_URL")!)quotes?pairs=\(currencyPair)&api_key=\(readConfig("API_KEY")!)" 71 let url = "\(readConfig("API_URL")!)quotes?pairs=\(currencyPair)&api_key=\(readConfig("API_KEY")!)"
87 72
88 Simoleon.request(url: url, model: [CurrencyQuoteModel].self) { response in 73 Simoleon.request(url: url, model: [CurrencyQuoteModel].self) { response in
89 self.showingConversion = false 74 self.showingConversion = false
90 if let price = response.first?.price { 75 if let price = response.first?.price {
93 } else { 78 } else {
94 // Handle error 79 // Handle error
95 } 80 }
96 } 81 }
97 } 82 }
98
99 /*
100 1) Fetch default currency from User Settings
101 2) Change State var currencyPair
102 */
103 private func fetchingUserSettings() {
104 if let defaultCurrency = defaultCurrency.first {
105 self.currencyPair = defaultCurrency.pair ?? "USD/GBP"
106 }
107 }
108 } 83 }
109 84
110 85
111 struct Conversion_Previews: PreviewProvider { 86 struct Conversion_Previews: PreviewProvider {
112 static var previews: some View { 87 static var previews: some View {
113 Conversion(fetchUserSettings: true, currencyPair: "USD/GBP") 88 Conversion(currencyPair: "USD/GBP")
114 } 89 }
115 } 90 }