changeset 74:ceb92c8d8e84

Update stock view
author Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
date Sat, 23 Jan 2021 00:18:07 +0100
parents 9c135509f82b
children 883cac235c16
files LazyBear.xcodeproj/project.pbxproj LazyBear.xcodeproj/project.xcworkspace/xcuserdata/dennis.xcuserdatad/UserInterfaceState.xcuserstate lazybear/Company.swift lazybear/Functions/HistoricalPrices.swift lazybear/Functions/LatestPrice.swift lazybear/Models/IntradayPricesModel.swift lazybear/Models/LatestPriceModel.swift lazybear/Supply views/CurrentPrice.swift lazybear/Supply views/Stock.swift
diffstat 9 files changed, 56 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/LazyBear.xcodeproj/project.pbxproj	Fri Jan 22 23:02:27 2021 +0100
+++ b/LazyBear.xcodeproj/project.pbxproj	Sat Jan 23 00:18:07 2021 +0100
@@ -12,6 +12,7 @@
 		952498B325BB381300B00E22 /* CurrentPrice.swift in Sources */ = {isa = PBXBuildFile; fileRef = 952498B225BB381300B00E22 /* CurrentPrice.swift */; };
 		952498B625BB47A700B00E22 /* LatestPriceModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 952498B525BB47A700B00E22 /* LatestPriceModel.swift */; };
 		952498B925BB481100B00E22 /* LatestPrice.swift in Sources */ = {isa = PBXBuildFile; fileRef = 952498B825BB481100B00E22 /* LatestPrice.swift */; };
+		95493BA425BB8C8100761301 /* IntradayPricesModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95493BA325BB8C8100761301 /* IntradayPricesModel.swift */; };
 		954D992225A2105F001F7F60 /* companies.json in Resources */ = {isa = PBXBuildFile; fileRef = 954D992125A2105F001F7F60 /* companies.json */; };
 		954D992525A2123B001F7F60 /* HistoricalPricesModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 954D992425A2123B001F7F60 /* HistoricalPricesModel.swift */; };
 		954D996D25A2461B001F7F60 /* SwiftUICharts in Frameworks */ = {isa = PBXBuildFile; productRef = 954D996C25A2461B001F7F60 /* SwiftUICharts */; };
@@ -51,6 +52,7 @@
 		952498B225BB381300B00E22 /* CurrentPrice.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CurrentPrice.swift; sourceTree = "<group>"; };
 		952498B525BB47A700B00E22 /* LatestPriceModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = LatestPriceModel.swift; path = lazybear/Models/LatestPriceModel.swift; sourceTree = SOURCE_ROOT; };
 		952498B825BB481100B00E22 /* LatestPrice.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = LatestPrice.swift; path = lazybear/Functions/LatestPrice.swift; sourceTree = SOURCE_ROOT; };
+		95493BA325BB8C8100761301 /* IntradayPricesModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = IntradayPricesModel.swift; path = lazybear/Models/IntradayPricesModel.swift; sourceTree = SOURCE_ROOT; };
 		954D992125A2105F001F7F60 /* companies.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; name = companies.json; path = lazybear/Data/companies.json; sourceTree = SOURCE_ROOT; };
 		954D992425A2123B001F7F60 /* HistoricalPricesModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = HistoricalPricesModel.swift; path = lazybear/Models/HistoricalPricesModel.swift; sourceTree = SOURCE_ROOT; };
 		954D997025A253A9001F7F60 /* Config.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = Config.swift; path = lazybear/Config.swift; sourceTree = SOURCE_ROOT; };
@@ -151,6 +153,7 @@
 				95E0287A25B88F3C00020CF2 /* FormDescription.swift */,
 				95F6C30025BAEC8B003CF389 /* ShowingCompany.swift */,
 				952498B525BB47A700B00E22 /* LatestPriceModel.swift */,
+				95493BA325BB8C8100761301 /* IntradayPricesModel.swift */,
 			);
 			path = Models;
 			sourceTree = "<group>";
@@ -290,6 +293,7 @@
 			buildActionMask = 2147483647;
 			files = (
 				95AB4A7A259DCBAE0064C9C1 /* ReadJson.swift in Sources */,
+				95493BA425BB8C8100761301 /* IntradayPricesModel.swift in Sources */,
 				95E0287B25B88F3C00020CF2 /* FormDescription.swift in Sources */,
 				952498B625BB47A700B00E22 /* LatestPriceModel.swift in Sources */,
 				95F6C30525BAF599003CF389 /* CompanyHeader.swift in Sources */,
Binary file LazyBear.xcodeproj/project.xcworkspace/xcuserdata/dennis.xcuserdatad/UserInterfaceState.xcuserstate has changed
--- a/lazybear/Company.swift	Fri Jan 22 23:02:27 2021 +0100
+++ b/lazybear/Company.swift	Sat Jan 23 00:18:07 2021 +0100
@@ -17,6 +17,7 @@
             VStack(alignment: .leading) {
                 Stock(name: self.name, symbol: self.symbol)
                 Spacer()
+                //Insiders()
             }
         }
     }
--- a/lazybear/Functions/HistoricalPrices.swift	Fri Jan 22 23:02:27 2021 +0100
+++ b/lazybear/Functions/HistoricalPrices.swift	Sat Jan 23 00:18:07 2021 +0100
@@ -43,6 +43,7 @@
                     // everything is good, so we can exit
                     return
                 }
+                
             }
 
             // if we're still here it means there was a problem
--- a/lazybear/Functions/LatestPrice.swift	Fri Jan 22 23:02:27 2021 +0100
+++ b/lazybear/Functions/LatestPrice.swift	Sat Jan 23 00:18:07 2021 +0100
@@ -8,7 +8,10 @@
 import SwiftUI
 
 class LatestPrice: ObservableObject {
-    @Published var result = [LatestPriceModel]()
+    @Published var latestPrice = Double()
+    @Published var changePercent = Double()
+    //@Published var isUSMarketOpen = Bool()
+    
     @Published var showingView = false
     @Published var showingAlert = false
     
@@ -21,23 +24,16 @@
         let request = URLRequest(url: url)
         URLSession.shared.dataTask(with: request) { data, response, error in
             if let data = data {
-                if let decodedResponse = try? JSONDecoder().decode([LatestPriceModel].self, from: data) {
+                if let decodedResponse = try? JSONDecoder().decode(LatestPriceModel.self, from: data) {
                     // we have good data – go back to the main thread
                     DispatchQueue.main.async {
                         // update our UI
-                        self.result = decodedResponse
+                        self.latestPrice = decodedResponse.latestPrice
+                        self.changePercent = decodedResponse.changePercent
+                        //self.isUSMarketOpen = decodedResponse.isUSMarketOpen
+                        self.showingView = true
                         print("API request ok")
-                        
-                        // Check if data is empty
-                        if self.result.isEmpty || self.result.count <= 1 {
-                            print("Data is empty")
-                            self.showingView = false
-                            self.showingAlert = true
-                        } else {
-                            print("Showing view...")
-                            self.showingView = true
-                        }
-                        
+                        print("Showing view...")
                     }
 
                     // everything is good, so we can exit
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lazybear/Models/IntradayPricesModel.swift	Sat Jan 23 00:18:07 2021 +0100
@@ -0,0 +1,12 @@
+//
+//  IntradayPriceModel.swift
+//  LazyBear
+//
+//  Created by Dennis Concepción Martín on 22/1/21.
+//
+
+import SwiftUI
+
+struct IntradayPricesModel: Codable {
+    var open: Double
+}
--- a/lazybear/Models/LatestPriceModel.swift	Fri Jan 22 23:02:27 2021 +0100
+++ b/lazybear/Models/LatestPriceModel.swift	Sat Jan 23 00:18:07 2021 +0100
@@ -8,5 +8,7 @@
 import SwiftUI
 
 struct LatestPriceModel: Codable {
-    var companyName: String
+    var latestPrice: Double
+    var changePercent: Double
+    //var isUSMarketOpen: String
 }
--- a/lazybear/Supply views/CurrentPrice.swift	Fri Jan 22 23:02:27 2021 +0100
+++ b/lazybear/Supply views/CurrentPrice.swift	Sat Jan 23 00:18:07 2021 +0100
@@ -8,28 +8,28 @@
 import SwiftUI
 
 struct CurrentPrice: View {
-    @ObservedObject var latestPrice = LatestPrice()
+    @State var price: Double
+    @State var change: Double
+    @State var marketIsOpen: Bool
+    
     var body: some View {
         HStack {
-            Text("320.30")
+            Text("\(price, specifier: "%.2f")")
                 .font(.headline)
                 .padding(.trailing)
             
-            Text("+1.67%")
+            Text("\(change, specifier: "%.2f")%")
                 .foregroundColor(.green)
             
             Spacer()
             AddWatchlist()
         }
         .padding([.leading, .trailing])
-        .onAppear {
-            latestPrice.request(symbol: "AAPL", sandbox: true)
-        }
     }
 }
 
 struct CurrentPrice_Previews: PreviewProvider {
     static var previews: some View {
-        CurrentPrice()
+        CurrentPrice(price: 100.50, change: 1.35, marketIsOpen: true)
     }
 }
--- a/lazybear/Supply views/Stock.swift	Fri Jan 22 23:02:27 2021 +0100
+++ b/lazybear/Supply views/Stock.swift	Sat Jan 23 00:18:07 2021 +0100
@@ -10,6 +10,8 @@
 
 struct Stock: View {
     @ObservedObject var historicalPrices = HistoricalPrices()
+    @ObservedObject var latestPrice = LatestPrice()
+    
     var periods = ["1D", "1W", "1M", "3M", "6M", "1Y", "2Y", "5Y"]
     @State var selectedPeriod = 3
     var name: String
@@ -18,7 +20,20 @@
     var body: some View {
         VStack {
             Divider()
-            CurrentPrice()
+            
+            let price = latestPrice.latestPrice
+            let change = latestPrice.changePercent*100
+            //let marketIsOpen = latestPrice.isUSMarketOpen
+            
+            if latestPrice.showingView {
+            CurrentPrice(price: price, change: change, marketIsOpen: false)
+            } else {
+                HStack {
+                ProgressView()
+                    .padding(.leading)
+                    Spacer()
+                }
+            }
             Divider()
             DateSelection(selectedperiod: $selectedPeriod)
                 .onChange(of: selectedPeriod, perform: { value in
@@ -32,13 +47,13 @@
                 LineView(data: scalate(prices: prices), title: "", style: chartStyle())
                     .padding([.leading, .trailing])
                     .offset(y: -40)
-            }
-            else {
+            } else {
                 ProgressView()
             }
         }
         .onAppear {
             historicalPrices.request(symbol: self.symbol, period: periods[selectedPeriod], sandbox: true)
+            latestPrice.request(symbol: self.symbol, sandbox: true)
         }
     }