comparison Simoleon/ConversionView.swift @ 186:1ebd1c5dd302

finish ConversionView
author Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
date Thu, 23 Dec 2021 11:30:38 +0100
parents 2fc95efcb1ee
children 13d5a8deb6c2
comparison
equal deleted inserted replaced
185:2fc95efcb1ee 186:1ebd1c5dd302
19 // CurrencyTextfield variables 19 // CurrencyTextfield variables
20 @State private var amount = "1" 20 @State private var amount = "1"
21 21
22 // CurrencyConversion variables 22 // CurrencyConversion variables
23 @State private var showConversion = false 23 @State private var showConversion = false
24 @State private var conversion = CurrencyConversionResponse(message: [CurrencyConversionResult]()) 24 @State private var latestRate = CurrencyLatestRateResponse(message: [CurrencyLatestRateResult]())
25
26 // Update currency rates
27 @State private var timer = Timer.publish(every: 3, on: .main, in: .common).autoconnect()
25 28
26 var body: some View { 29 var body: some View {
27 ScrollView(showsIndicators: false) { 30 ScrollView(showsIndicators: false) {
28 VStack(alignment: .leading, spacing: 20) { 31 VStack(alignment: .leading, spacing: 20) {
29 // MARK: - Currency selector 32 // MARK: - Currency selector
46 Text("\(baseCurrency.code) - \(baseCurrency.name)") 49 Text("\(baseCurrency.code) - \(baseCurrency.name)")
47 .font(.callout) 50 .font(.callout)
48 .fontWeight(.semibold) 51 .fontWeight(.semibold)
49 52
50 CurrencyTextfield(currencyCode: baseCurrency.code, amount: $amount) 53 CurrencyTextfield(currencyCode: baseCurrency.code, amount: $amount)
51 .onChange(of: amount) { _ in
52 showConversion = false
53 getConversion()
54 }
55 54
56 Divider() 55 Divider()
57 Text("\(quoteCurrency.code) - \(quoteCurrency.name)") 56 Text("\(quoteCurrency.code) - \(quoteCurrency.name)")
58 .font(.callout) 57 .font(.callout)
59 .fontWeight(.semibold) 58 .fontWeight(.semibold)
60 59
61 CurrencyConversion( 60 CurrencyConversion(
62 conversion: conversion, 61 latestRate: latestRate,
63 currencyCode: quoteCurrency.code, 62 currencyCode: quoteCurrency.code,
64 showConversion: $showConversion 63 amount: $amount
65 ) 64 )
66 } 65 }
67 .padding() 66 .padding()
68 .sheet(isPresented: $showingCurrencyList) { 67 .sheet(isPresented: $showingCurrencyList) {
69 CurrencyList(baseCurrency: $baseCurrency, quoteCurrency: $quoteCurrency, selecting: selecting) 68 CurrencyList(baseCurrency: $baseCurrency, quoteCurrency: $quoteCurrency, selecting: selecting)
70 } 69 }
71 } 70 }
72 .onAppear(perform: getConversion) 71 .onAppear {
72 getConversion()
73 timer = Timer.publish(every: 3, on: .main, in: .common).autoconnect()
74 }
75 .onReceive(timer) { _ in
76 getConversion()
77 }
78 .onDisappear {
79 timer.upstream.connect().cancel()
80 }
73 .navigationTitle("Convert") 81 .navigationTitle("Convert")
74 .if(UIDevice.current.userInterfaceIdiom == .phone && showNavigationView ?? true) { content in 82 .if(UIDevice.current.userInterfaceIdiom == .phone && showNavigationView ?? true) { content in
75 NavigationView { content } 83 NavigationView { content }
76 } 84 }
77 } 85 }
82 showingCurrencyList.toggle() 90 showingCurrencyList.toggle()
83 } 91 }
84 92
85 // Request conversion 93 // Request conversion
86 private func getConversion() { 94 private func getConversion() {
87 guard let amount = Float(amount) else {
88 amount = ""
89 showConversion = true
90 return
91 }
92
93 let currencyPair = "\(baseCurrency.code)\(quoteCurrency.code)" 95 let currencyPair = "\(baseCurrency.code)\(quoteCurrency.code)"
94 let url = "https://api.simoleon.app/fx/convert?symbols=\(currencyPair)&amount=\(amount)" 96 let url = "https://api.simoleon.app/fx/latest?symbols=\(currencyPair)"
95 httpRequest(url: url, model: CurrencyConversionResponse.self) { response in 97 httpRequest(url: url, model: CurrencyLatestRateResponse.self) { response in
96 conversion = response 98 latestRate = response
97 if conversion.message.isEmpty { 99 print(latestRate.message.first!.timestamp)
100 if latestRate.message.isEmpty {
98 // Handle exception 101 // Handle exception
99 } else { 102 } else {
100 showConversion = true 103 showConversion = true
101 } 104 }
102 } 105 }