comparison LazyBear/Views/Company/Chart.swift @ 399:5c99883c7964

Implementing networking in CompanyView
author Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
date Sat, 15 May 2021 12:31:40 +0200
parents
children 6055a867d2b6
comparison
equal deleted inserted replaced
398:933546fa5651 399:5c99883c7964
1 //
2 // Chart.swift
3 // LazyBear
4 //
5 // Created by Dennis Concepción Martín on 8/5/21.
6 //
7
8 import SwiftUI
9 import StockCharts
10
11 struct Chart: View {
12 var chartData: ChartResponse
13 var symbol: String
14
15 // Date picker
16 var ranges = ["1D", "5D", "1M", "3M", "6M", "1Y", "5Y"]
17 @State private var selectedRange = "3M"
18
19
20 var body: some View {
21 let priceViewStyle = PriceViewStyle(
22 alignment: .leading,
23 priceFont: .headline,
24 priceFontWeight: .semibold,
25 percentFont: .body,
26 percentFontWeight: .medium
27 )
28
29 VStack {
30 DatePicker(ranges: ranges, selectedRange: $selectedRange)
31
32 RoundedRectangle(cornerRadius: 15)
33 .foregroundColor(Color(.secondarySystemBackground))
34 .frame(height: 270)
35 .overlay(
36 VStack {
37 HStack {
38 if let quote = chartData.quote![symbol.uppercased()] {
39 let latestPrice = quote.latestPrice ?? 0
40 let changePercent = quote.changePercent ?? 0
41 PriceView(latestPrice: latestPrice, changePercent: changePercent, style: priceViewStyle)
42 }
43 Spacer()
44 }
45 .padding([.top, .leading, .trailing])
46
47 if let intradayPrices = chartData.intradayPrices![symbol.uppercased()] {
48 if let prices = intradayPrices.compactMap { $0.open } {
49 LineChartView(data: prices,
50 dates: nil,
51 hours: nil,
52 dragGesture: true
53 )
54 .padding(.bottom)
55 }
56 }
57 }
58 )
59
60 if let latestNews = chartData.latestNews {
61 ForEach(latestNews, id: \.self) { new in
62 NewsRow(new: new)
63 }
64 }
65 }
66 }
67 }
68
69 struct Chart_Previews: PreviewProvider {
70 static var previews: some View {
71 Chart(
72 chartData: ChartResponse(
73 intradayPrices: ["AAPL": [IntradayPriceModel(open: 120.3)]],
74 quote: ["aapl": QuoteModel(changePercent: 0.03, companyName: "Apple Inc", latestPrice: 120.3)],
75 latestNews: [LatestNewsModel(
76 datetime: 1621037430000,
77 headline: "Chaos Monkeys' author calls Apple's statement on his departure defamatory",
78 image: "https://cloud.iexapis.com/v1/news/image/99abeb99-6d9e-47c8-ae7b-53404eacccec",
79 source: "Investing.com",
80 summary: "https://www.investing.com/news/stock-market-news",
81 url: "https://cloud.iexapis.com/v1/news/article/99abeb99-6d9e-47c8-ae7b-53404eacccec")]),
82
83 symbol: "aapl"
84 )
85 }
86 }