/
CurrencyRowContainerView.swift
114 lines (104 loc) · 3.94 KB
/
CurrencyRowContainerView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//
// CurrencyRowData.swift
// CurrencyRowData
//
// Created by Andrew Morgan on 26/08/2021.
//
import SwiftUI
import RealmSwift
struct CurrencyRowContainerView: View {
@ObservedResults(Rate.self) var rates
var isBase = false
let baseSymbol: String
@Binding var baseAmount: Double
let symbol: String
var refreshToggle: Bool
var action: (_: String) -> Void = {_ in }
@State private var previousToggle = false
var rate: Rate? {
rates.where { $0.query.from == baseSymbol && $0.query.to == symbol }.first
}
var body: some View {
if let rate = rate {
HStack {
CurrencyRowDataView(rate: rate, isBase: isBase, baseAmount: $baseAmount, action: action)
if refreshToggle != previousToggle {
Image(systemName: "arrow.clockwise.icloud")
.onAppear(perform: refreshData)
}
}
} else {
Text("Loading Data...")
.onAppear(perform: loadData)
}
}
private func refreshData() {
guard let url = URL(string: "https://api.exchangerate.host/convert?from=\(baseSymbol)&to=\(symbol)") else {
print("Invalid URL")
previousToggle = refreshToggle
return
}
let request = URLRequest(url: url)
URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data else {
print("Error fetching data: \(error?.localizedDescription ?? "Unknown error")")
DispatchQueue.main.async { previousToggle = refreshToggle }
return
}
if let decodedResponse = try? JSONDecoder().decode(Rate.self, from: data) {
DispatchQueue.main.async {
if let existingRate = rate {
do {
let realm = try Realm()
try realm.write() {
guard let thawedrate = existingRate.thaw() else {
print("Couldn't thaw existingRate")
return
}
thawedrate.date = decodedResponse.date
thawedrate.result = decodedResponse.result
}
} catch {
print("Unable to update existing rate in Realm")
}
}
previousToggle = refreshToggle
}
} else {
print("No data received")
DispatchQueue.main.async { previousToggle = refreshToggle }
}
}
.resume()
}
private func loadData() {
guard let url = URL(string: "https://api.exchangerate.host/convert?from=\(baseSymbol)&to=\(symbol)") else {
print("Invalid URL")
return
}
let request = URLRequest(url: url)
print("Network request: \(url.description)")
URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data else {
print("Error fetching data: \(error?.localizedDescription ?? "Unknown error")")
return
}
if let decodedResponse = try? JSONDecoder().decode(Rate.self, from: data) {
DispatchQueue.main.async {
$rates.append(decodedResponse)
}
} else {
print("No data received")
}
}
.resume()
}
}
struct CurrencyRowData_Previews: PreviewProvider {
static var previews: some View {
CurrencyRowContainerView(baseSymbol: "GBP",
baseAmount: .constant(1.0),
symbol: "USD",
refreshToggle: false)
}
}