diff LazyBear/Views/Home/HomeView.swift @ 346:80bfa88c6b0f

Implementing Prop API
author Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
date Sun, 11 Apr 2021 19:55:47 +0200
parents ab909fc9ce55
children 5ccceb527178
line wrap: on
line diff
--- a/LazyBear/Views/Home/HomeView.swift	Thu Apr 08 20:15:28 2021 +0200
+++ b/LazyBear/Views/Home/HomeView.swift	Sun Apr 11 19:55:47 2021 +0200
@@ -8,7 +8,7 @@
 import SwiftUI
 
 struct HomeView: View {
-    @ObservedObject var homeData = HomeData()
+    @ObservedObject var home = Home()
     @State private var showTradingDates = false
     
     // Set recurrent price request
@@ -23,23 +23,26 @@
         let dueDate = Date()
     
     var body: some View {
-        if homeData.showView {
+        if home.showView {
             NavigationView {
                 List {
-                    SectorRow(sectorPerformance: homeData.sectorPerformance)
+                    if let sectorPerformance = home.data.sectorPerformance {
+                        SectorRow(sectorPerformance: sectorPerformance)
+                            .listRowInsets(EdgeInsets())
+                    }
+                    
+                    if let lists = home.data.lists {
+                        ForEach(Array(lists.keys.sorted()), id: \.self) { listName in
+                            if let intradayPrices = home.data.intradayPrices {
+                                StockRectangleRow(listName: listName, list: lists[listName]!, nestedIntradayPrices: intradayPrices)
+                            } else {
+                                StockRectangleRow(listName: listName, list: lists[listName]!, nestedIntradayPrices: nil)
+                            }
+                        }
                         .listRowInsets(EdgeInsets())
-                        
-                    // Get keys of the dictionary list
-                    let listTypes = ["mostactive", "losers", "gainers"]
-                    ForEach(listTypes.sorted(), id: \.self) { listType in
-                        if let list = homeData.topLists[listType] {
-                            StockRectangleRow(listType: listType, list: list, intradayPrices: homeData.intradayPrices)
-                                
-                        }
                     }
-                    .listRowInsets(EdgeInsets())
                 }
-                .onReceive(timer) { _ in homeData.get() }
+                .onReceive(timer) { _ in home.request("https://api.lazybear.app/home/streaming") }
                 .onDisappear { self.timer.upstream.connect().cancel() }  // Stop timer
                 .navigationTitle("\(dueDate, formatter: Self.taskDateFormat)")
                 .navigationBarTitleDisplayMode(.inline)
@@ -53,12 +56,14 @@
                 }
             }
             .sheet(isPresented: $showTradingDates) {
-                TradingDates()
+                if let dates = home.data.tradingDates {
+                    TradingDates(dates: dates)
+                }
             }
         } else {
             ProgressView()
                 .onAppear {
-                    homeData.get()
+                    home.request("https://api.lazybear.app/home/init")
                     // Restart timer
                     self.timer = Timer.publish(every: 10, on: .main, in: .common).autoconnect()
                 }