Mercurial > public > simoleon
comparison Simoleon/Helpers/ConversionBox.swift @ 21:c3dda63f50ed v1.1
Added Core Data and UI changes
- Implement Watchlist
- Change conversion design
- Improve UX
author | Dennis Concepción Martín <dennisconcepcionmartin@gmail.com> |
---|---|
date | Mon, 19 Jul 2021 19:27:12 +0100 |
parents | 94fd7ac93060 |
children | a8d76aa51da2 |
comparison
equal
deleted
inserted
replaced
20:f8aabe5b7251 | 21:c3dda63f50ed |
---|---|
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 Introspect | |
10 | 9 |
11 struct ConversionBox: View { | 10 struct ConversionBox: View { |
12 @Binding var mainCurrency: String | 11 @Binding var currencyPair: String |
13 @Binding var secondaryCurrency: String | |
14 @Binding var amountToConvert: String | 12 @Binding var amountToConvert: String |
15 @Binding var price: Double | 13 @Binding var price: Double |
16 @Binding var showingConversion: Bool | 14 @Binding var showingConversion: Bool |
17 @Binding var showingCurrencySelector: Bool | 15 @Binding var showingCurrencySelector: Bool |
18 @Binding var currencyPairNotFound: Bool | 16 @Binding var isEditing: Bool |
19 | |
20 @State private var showingCancelationButton = false | |
21 | 17 |
22 let currencyMetadata: [String: CurrencyMetadataModel] = parseJson("CurrencyMetadata.json") | 18 let currencyMetadata: [String: CurrencyMetadataModel] = parseJson("CurrencyMetadata.json") |
23 | 19 |
24 var body: some View { | 20 var body: some View { |
25 VStack(alignment: .leading) { | 21 VStack(alignment: .leading) { |
26 Text("\(currencyMetadata[mainCurrency]!.name) (\(mainCurrency))") | 22 let currencies = currencyPair.split(separator: "/") |
23 Text("\(currencyMetadata[String(currencies[0])]!.name) (\(String(currencies[0])))") | |
27 .font(.callout) | 24 .font(.callout) |
28 .fontWeight(.semibold) | 25 .fontWeight(.semibold) |
29 .padding(.top, 40) | 26 .padding(.top, 40) |
30 | 27 |
31 ZStack(alignment: .trailing) { | 28 ZStack(alignment: .trailing) { |
32 TextField("Enter amount", text: $amountToConvert) | 29 TextField("Enter amount", text: $amountToConvert) { startedEditing in |
33 .keyboardType(.decimalPad) | 30 if startedEditing { |
34 .font(Font.title.weight(.semibold)) | 31 withAnimation { |
35 .lineLimit(1) | 32 isEditing = true |
36 .padding(.bottom, 10) | 33 } |
37 .introspectTextField { textField in | 34 } |
38 if !showingCurrencySelector { | 35 } |
39 textField.becomeFirstResponder() | 36 onCommit: { |
40 } | 37 withAnimation { |
41 } | 38 isEditing = false |
39 } | |
40 } | |
41 .keyboardType(.decimalPad) | |
42 .font(Font.title.weight(.semibold)) | |
43 .lineLimit(1) | |
44 .padding(.bottom, 10) | |
42 } | 45 } |
43 | 46 |
44 Divider() | 47 Divider() |
45 | 48 |
46 Text("\(currencyMetadata[secondaryCurrency]!.name) (\(secondaryCurrency))") | 49 Text("\(currencyMetadata[String(currencies[1])]!.name) (\(String(currencies[1])))") |
47 .font(.callout) | 50 .font(.callout) |
48 .fontWeight(.semibold) | 51 .fontWeight(.semibold) |
49 .padding(.top, 10) | 52 .padding(.top, 10) |
50 | 53 |
51 if showingConversion { | 54 if showingConversion { |
52 Text("\(makeConversion(), specifier: "%.2f")") | 55 Text("\(makeConversion(), specifier: "%.2f")") |
53 .font(Font.title.weight(.semibold)) | 56 .font(Font.title.weight(.semibold)) |
54 .lineLimit(1) | 57 .lineLimit(1) |
55 .padding(.top, 5) | 58 .padding(.top, 5) |
56 } else { | 59 } else { |
57 if currencyPairNotFound { | 60 ProgressView() |
58 Text("The currency pair selected is not supported yet 😢") | 61 .padding(.top, 5) |
59 .padding(.top, 5) | |
60 } else { | |
61 ProgressView() | |
62 .padding(.top, 5) | |
63 } | |
64 } | 62 } |
65 } | 63 } |
66 } | 64 } |
67 | 65 |
68 | 66 |
78 } | 76 } |
79 | 77 |
80 | 78 |
81 struct ConversionBox_Previews: PreviewProvider { | 79 struct ConversionBox_Previews: PreviewProvider { |
82 static var previews: some View { | 80 static var previews: some View { |
83 ConversionBox(mainCurrency: .constant("USD"), secondaryCurrency: .constant("GBP"), amountToConvert: .constant("1000"), price: .constant(1), showingConversion: .constant(true), showingCurrencySelector: .constant(false), currencyPairNotFound: .constant(false)) | 81 ConversionBox(currencyPair: .constant("USD/GBP"), amountToConvert: .constant("1000"), price: .constant(1), showingConversion: .constant(false), showingCurrencySelector: .constant(false), isEditing: .constant(false)) |
84 } | 82 } |
85 } | 83 } |