Mercurial > public > lazybear
comparison 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 |
comparison
equal
deleted
inserted
replaced
345:fde2b30c719e | 346:80bfa88c6b0f |
---|---|
6 // | 6 // |
7 | 7 |
8 import SwiftUI | 8 import SwiftUI |
9 | 9 |
10 struct HomeView: View { | 10 struct HomeView: View { |
11 @ObservedObject var homeData = HomeData() | 11 @ObservedObject var home = Home() |
12 @State private var showTradingDates = false | 12 @State private var showTradingDates = false |
13 | 13 |
14 // Set recurrent price request | 14 // Set recurrent price request |
15 @State private var timer = Timer.publish(every: 10, on: .main, in: .common).autoconnect() | 15 @State private var timer = Timer.publish(every: 10, on: .main, in: .common).autoconnect() |
16 | 16 |
21 }() | 21 }() |
22 | 22 |
23 let dueDate = Date() | 23 let dueDate = Date() |
24 | 24 |
25 var body: some View { | 25 var body: some View { |
26 if homeData.showView { | 26 if home.showView { |
27 NavigationView { | 27 NavigationView { |
28 List { | 28 List { |
29 SectorRow(sectorPerformance: homeData.sectorPerformance) | 29 if let sectorPerformance = home.data.sectorPerformance { |
30 SectorRow(sectorPerformance: sectorPerformance) | |
31 .listRowInsets(EdgeInsets()) | |
32 } | |
33 | |
34 if let lists = home.data.lists { | |
35 ForEach(Array(lists.keys.sorted()), id: \.self) { listName in | |
36 if let intradayPrices = home.data.intradayPrices { | |
37 StockRectangleRow(listName: listName, list: lists[listName]!, nestedIntradayPrices: intradayPrices) | |
38 } else { | |
39 StockRectangleRow(listName: listName, list: lists[listName]!, nestedIntradayPrices: nil) | |
40 } | |
41 } | |
30 .listRowInsets(EdgeInsets()) | 42 .listRowInsets(EdgeInsets()) |
31 | |
32 // Get keys of the dictionary list | |
33 let listTypes = ["mostactive", "losers", "gainers"] | |
34 ForEach(listTypes.sorted(), id: \.self) { listType in | |
35 if let list = homeData.topLists[listType] { | |
36 StockRectangleRow(listType: listType, list: list, intradayPrices: homeData.intradayPrices) | |
37 | |
38 } | |
39 } | 43 } |
40 .listRowInsets(EdgeInsets()) | |
41 } | 44 } |
42 .onReceive(timer) { _ in homeData.get() } | 45 .onReceive(timer) { _ in home.request("https://api.lazybear.app/home/streaming") } |
43 .onDisappear { self.timer.upstream.connect().cancel() } // Stop timer | 46 .onDisappear { self.timer.upstream.connect().cancel() } // Stop timer |
44 .navigationTitle("\(dueDate, formatter: Self.taskDateFormat)") | 47 .navigationTitle("\(dueDate, formatter: Self.taskDateFormat)") |
45 .navigationBarTitleDisplayMode(.inline) | 48 .navigationBarTitleDisplayMode(.inline) |
46 .navigationViewStyle(StackNavigationViewStyle()) | 49 .navigationViewStyle(StackNavigationViewStyle()) |
47 .toolbar { | 50 .toolbar { |
51 } | 54 } |
52 } | 55 } |
53 } | 56 } |
54 } | 57 } |
55 .sheet(isPresented: $showTradingDates) { | 58 .sheet(isPresented: $showTradingDates) { |
56 TradingDates() | 59 if let dates = home.data.tradingDates { |
60 TradingDates(dates: dates) | |
61 } | |
57 } | 62 } |
58 } else { | 63 } else { |
59 ProgressView() | 64 ProgressView() |
60 .onAppear { | 65 .onAppear { |
61 homeData.get() | 66 home.request("https://api.lazybear.app/home/init") |
62 // Restart timer | 67 // Restart timer |
63 self.timer = Timer.publish(every: 10, on: .main, in: .common).autoconnect() | 68 self.timer = Timer.publish(every: 10, on: .main, in: .common).autoconnect() |
64 } | 69 } |
65 | 70 |
66 } | 71 } |