changeset 282:d85f6dde3284

Minor PriceView update
author Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
date Fri, 19 Mar 2021 19:22:30 +0100
parents 6835e2885aa6
children 2313c44f6c8e
files LazyBear/Models/PriceModel.swift LazyBear/UI/PriceView.swift
diffstat 2 files changed, 9 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/LazyBear/Models/PriceModel.swift	Fri Mar 19 19:22:16 2021 +0100
+++ b/LazyBear/Models/PriceModel.swift	Fri Mar 19 19:22:30 2021 +0100
@@ -8,6 +8,6 @@
 import SwiftUI
 
 struct PriceModel: Codable {
-    var latestPrice: Float
-    var changePercent: Double
+    var latestPrice: Float?
+    var changePercent: Double?
 }
--- a/LazyBear/UI/PriceView.swift	Fri Mar 19 19:22:16 2021 +0100
+++ b/LazyBear/UI/PriceView.swift	Fri Mar 19 19:22:30 2021 +0100
@@ -10,21 +10,21 @@
 struct PriceView: View {
     var symbol: String
     var showVertical: Bool
-    @State private var latestPrice = Float()
-    @State private var changePercent = Double()
-    @State private var negativeChange = false
+    @State private var price = PriceModel()
     @State private var timer = Timer.publish(every: 10, on: .main, in: .common).autoconnect()  // Set recurrent price request
     
     var body: some View {
+        let latestPrice = price.latestPrice ?? 0
+        let changePercent = price.changePercent ?? 0
         VStack {
             if showVertical {
                 VStack(alignment: .trailing) {
                     Text("\(latestPrice, specifier: "%.2f")")
                         .fontWeight(.semibold)
                         .padding(.horizontal)
-
+                    
                     Text("\(changePercent*100, specifier: "%.2f")%")
-                        .foregroundColor(negativeChange ? Color(.systemRed) : Color(.systemGreen))
+                        .foregroundColor(changePercent < 0 ? Color(.systemRed) : Color(.systemGreen))
                         .padding(.trailing)
                 }
             } else {
@@ -36,7 +36,7 @@
 
                     Text("\(changePercent*100, specifier: "%.2f")%")
                         .font(.headline)
-                        .foregroundColor(negativeChange ? Color(.systemRed) : Color(.systemGreen))
+                        .foregroundColor(changePercent < 0 ? Color(.systemRed) : Color(.systemGreen))
                         .padding(.trailing)
                     
                     Spacer()
@@ -54,12 +54,7 @@
     private func call() {
         let url = getUrl(endpoint: .quote, symbol: symbol)
         request(url: url, model: PriceModel.self) { result in
-            self.latestPrice = result.latestPrice
-            if result.changePercent < 0 {
-                self.negativeChange  = true
-            }
-            
-            self.changePercent = result.changePercent
+            self.price = result
         }
     }
 }