comparison Simoleon/UI/ConversionBox.swift @ 160:0c589138a6f3

Implement Conversion Box
author Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
date Sun, 29 Aug 2021 19:04:34 +0100
parents 84137052813d
children 3913aff613e8
comparison
equal deleted inserted replaced
159:35628bac01f5 160:0c589138a6f3
1 ////
2 //// ConversionBox.swift
3 //// Simoleon
4 ////
5 //// Created by Dennis Concepción Martín on 18/07/2021.
6 ////
7 // 1 //
8 //import SwiftUI 2 // ConversionBox.swift
3 // Simoleon
9 // 4 //
10 //struct ConversionBox: View { 5 // Created by Dennis Concepción Martín on 18/07/2021.
11 // var currencyDetails: CurrencyDetailsModel
12 // @State var currencyPair: CurrencyPairModel
13 //
14 // var body: some View {
15 // VStack(alignment: .leading) {
16 // Text("\(baseName) (\(currencyPair.baseSymbol))")
17 // .font(.callout)
18 // .fontWeight(.semibold)
19 // .padding(.top, 40)
20 //
21 // ZStack(alignment: .trailing) {
22 // TextField("Enter amount", text: $amount) { startedEditing in
23 // if startedEditing {
24 // withAnimation {
25 // amountIsEditing = true
26 // }
27 // }
28 // }
29 // onCommit: {
30 // withAnimation {
31 // amountIsEditing = false
32 // }
33 // }
34 // .keyboardType(.decimalPad)
35 // .font(Font.title.weight(.semibold))
36 // .lineLimit(1)
37 // .accessibilityIdentifier("ConversionTextField")
38 // }
39 //
40 // Divider()
41 // 6 //
42 // let quoteName = currencyDetails[currencyPair.quoteSymbol]!.name 7
43 // Text("\(quoteName) (\(currencyPair.quoteSymbol))") 8 import SwiftUI
44 // .font(.callout) 9
45 // .fontWeight(.semibold) 10 struct ConversionBox: View {
46 // .padding(.top, 10) 11 @ObservedObject var currencyPair: CurrencyPair
47 // 12 @State private var amount = ""
48 // if showingConversion { 13 @State private var isEditing = false
49 // Text("\(makeConversion(), specifier: "%.2f")") 14 @State private var showingConversion = false
50 // .font(Font.title.weight(.semibold)) 15 @State private var currencyQuote = CurrencyQuoteModel()
51 // .lineLimit(1) 16 @State private var showingAlert = false
52 // .padding(.top, 5) 17
53 // } else { 18 let networkHelper = NetworkHelper()
54 // ProgressView() 19 let currencyDetails: [String: CurrencyModel] = try! readJson(from: "Currencies.json")
55 // .padding(.top, 5) 20
56 // } 21 var body: some View {
57 // } 22 VStack(alignment: .leading) {
58 // .onAppear(perform: request) 23 let baseCurrencyName = currencyDetails[currencyPair.baseSymbol]!.name
59 // } 24 Text("\(baseCurrencyName) (\(currencyPair.baseSymbol))")
60 // 25 .font(.callout)
61 // /* 26 .fontWeight(.semibold)
62 // if the amount can be converted to Double: 27
63 // * Return amount 28 ConversionTextfield(amount: $amount, isEditing: $isEditing)
64 // else: 29 Divider()
65 // * Return zero 30
66 // */ 31 let quoteCurrencyName = currencyDetails[currencyPair.quoteSymbol]!.name
67 // func makeConversion() -> Double { 32 Text("\(quoteCurrencyName) (\(currencyPair.quoteSymbol))")
68 // if let amountToConvert = Double(amount) { 33 .font(.callout)
69 // return amountToConvert * price // Conversion 34 .fontWeight(.semibold)
70 // } else { 35
71 // return 0 36 if showingConversion {
72 // } 37 let conversion = convert()
73 // } 38 Text("\(conversion, specifier: "%.2f")")
74 //} 39 .font(Font.title.weight(.semibold))
75 // 40 .lineLimit(1)
76 // 41 } else {
77 //struct ConversionBox_Previews: PreviewProvider { 42 ProgressView()
78 // static var previews: some View { 43 }
79 // let fileController = File() 44 }
80 // let currencyDetails: [String: CurrencyDetailsModel] = try! fileController.read(json: "CurrencyDetails.json") 45 .toolbar {
81 // ConversionBox(currencyPair: CurrencyPair(), currencyDetails: currencyDetails) 46 ToolbarItem(placement: .navigationBarTrailing) {
82 // } 47 if isEditing {
83 //} 48 Button(action: {
49 UIApplication.shared.dismissKeyboard()
50 isEditing = false
51 }) {
52 Text("Done")
53 }
54 }
55 }
56 }
57
58 .onAppear {
59 showingConversion = false
60 let pair = "\(currencyPair.baseSymbol)/\(currencyPair.quoteSymbol)"
61 let apiKey = readConfig(withKey: "API_KEY")!
62 let url = "https://api.1forge.com/quotes?pairs=\(pair)&api_key=\(apiKey)"
63 try? networkHelper.httpRequest(url: url, model: [CurrencyQuoteModel].self) { response in
64 if let currencyQuote = response.first {
65 self.currencyQuote = currencyQuote
66 } else {
67 showingAlert = true
68 }
69
70 showingConversion = true
71 }
72 }
73 .alert(isPresented: $showingAlert) {
74 Alert(
75 title: Text("Currencies not supported."),
76 message: Text("Currently, we are unable to convert from \(currencyPair.baseSymbol) to \(currencyPair.quoteSymbol)."),
77 dismissButton: .default(Text("Dismiss")
78 )
79 )
80 }
81 }
82
83 private func convert() -> Double {
84 guard let amount = Double(amount) else { return 0 }
85 guard let price = currencyQuote.price else { return 0 }
86
87 return amount * price
88 }
89 }
90
91 struct ConversionBox_Previews: PreviewProvider {
92 static var previews: some View {
93 ConversionBox(currencyPair: CurrencyPair())
94 }
95 }