Mercurial > public > lazybear
changeset 114:4c172ff9af20
Add optional values to all the models
author | Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com> |
---|---|
date | Thu, 04 Feb 2021 20:07:29 +0100 |
parents | 16f8514cc5e6 |
children | 33e03e2863f6 |
files | LazyBear.xcodeproj/project.xcworkspace/xcuserdata/dennis.xcuserdatad/UserInterfaceState.xcuserstate lazybear/Models/HistoricalPricesModel.swift lazybear/Models/QuoteModel.swift lazybear/Views/InsiderTransactions.swift lazybear/Views/Price.swift lazybear/Views/Stock.swift |
diffstat | 6 files changed, 11 insertions(+), 16 deletions(-) [+] |
line wrap: on
line diff
Binary file LazyBear.xcodeproj/project.xcworkspace/xcuserdata/dennis.xcuserdatad/UserInterfaceState.xcuserstate has changed
--- a/lazybear/Models/HistoricalPricesModel.swift Mon Feb 01 20:19:32 2021 +0100 +++ b/lazybear/Models/HistoricalPricesModel.swift Thu Feb 04 20:07:29 2021 +0100 @@ -8,8 +8,8 @@ import Foundation struct HistoricalPricesModel: Codable { - var date: String - var close: Double + var date: String? + var close: Double? var volume: Float? var change: Double? var changePercent: Double?
--- a/lazybear/Models/QuoteModel.swift Mon Feb 01 20:19:32 2021 +0100 +++ b/lazybear/Models/QuoteModel.swift Thu Feb 04 20:07:29 2021 +0100 @@ -8,7 +8,7 @@ import Foundation struct QuoteModel: Codable { - var latestPrice: Float - var changePercent: Double + var latestPrice: Float? + var changePercent: Double? }
--- a/lazybear/Views/InsiderTransactions.swift Mon Feb 01 20:19:32 2021 +0100 +++ b/lazybear/Views/InsiderTransactions.swift Thu Feb 04 20:07:29 2021 +0100 @@ -39,7 +39,6 @@ let path = "/stable/stock/\(symbol)/insider-transactions?token=" self.url = baseUrl + path + token - print(url) } }
--- a/lazybear/Views/Price.swift Mon Feb 01 20:19:32 2021 +0100 +++ b/lazybear/Views/Price.swift Thu Feb 04 20:07:29 2021 +0100 @@ -16,9 +16,7 @@ // <--------- API Job ---------> @State private var url = String() { didSet { requestPrice() }} @State private var showingView = false - @State var latestPrice = Float() - @State var changePercent = Double() { didSet { self.showingView = true }} - @State private var negativeChange = false + @State private var data = [QuoteModel]() { didSet { self.showingView = true }} // <--------- API Job ---------> // Set recurrent price request. Real-time prices @@ -28,13 +26,13 @@ VStack(alignment: .trailing) { if self.showingView { Group { - Text("\(latestPrice, specifier: "%.2f")") + Text("\(data[0].latestPrice ?? 0, specifier: "%.2f")") .fontWeight(.semibold) - Text("\(changePercent*100, specifier: "%.2f")%") + Text("\(data[0].changePercent ?? 0 * 100, specifier: "%.2f")%") .font(.subheadline) .foregroundColor(.white) - .background(Color(negativeChange ? .red : .green).cornerRadius(5)) + //.background(Color(negativeChange ? .red : .green).cornerRadius(5)) } .if(showVertical) { content in HStack { content } @@ -58,13 +56,10 @@ private func requestPrice() { request(url: url, model: QuoteModel.self) { result in - self.latestPrice = result.latestPrice - if self.changePercent >= 0 { self.negativeChange = true } - self.changePercent = result.changePercent + self.data = [QuoteModel(latestPrice: result.latestPrice, changePercent: result.changePercent)] } } } - // Wrap content if some condition is satisfied extension View { @ViewBuilder @@ -77,6 +72,7 @@ } } + struct Price_Previews: PreviewProvider { static var previews: some View { Price(symbol: "AAPL", showVertical: false)
--- a/lazybear/Views/Stock.swift Mon Feb 01 20:19:32 2021 +0100 +++ b/lazybear/Views/Stock.swift Thu Feb 04 20:07:29 2021 +0100 @@ -50,7 +50,7 @@ getUrl(range: period[selectedPeriod]) }) - let prices = data.map { $0.close } + let prices = data.map { $0.close ?? 0 } if showingLineChart { let normalPrices = normalize(prices) LineChart(dataPoints: normalPrices, lineColor: colorLineChart(prices: prices) ? .green : .red, lineWidth: 2)