changeset 207:53b47dcc1b6c

Add DateSelection
author Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
date Sat, 27 Feb 2021 12:43:08 +0000
parents e965cd71bc3b
children b7601724cf82
files LazyBear/UI/DateSelection.swift LazyBear/UI/HistoricalPriceView.swift
diffstat 2 files changed, 32 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LazyBear/UI/DateSelection.swift	Sat Feb 27 12:43:08 2021 +0000
@@ -0,0 +1,20 @@
+//
+//  DateSelection.swift
+//  LazyBear
+//
+//  Created by Dennis Concepción Martín on 27/2/21.
+//
+
+import SwiftUI
+
+struct DateSelection: View {
+    var body: some View {
+        Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/)
+    }
+}
+
+struct DateSelection_Previews: PreviewProvider {
+    static var previews: some View {
+        DateSelection()
+    }
+}
--- a/LazyBear/UI/HistoricalPriceView.swift	Fri Feb 26 22:08:03 2021 +0000
+++ b/LazyBear/UI/HistoricalPriceView.swift	Sat Feb 27 12:43:08 2021 +0000
@@ -12,9 +12,17 @@
     var symbol: String
     var chartHeight: CGFloat
     @State private var historicalPrices = [HistoricalPriceModel]()
+    var period = ["1W", "1M", "3M", "6M", "1Y", "2Y", "5Y"]
+    @State var selectedPeriod = 2
     
     var body: some View {
         VStack {
+            DateSelection(period: period, selectedperiod: $selectedPeriod)
+                .padding(.horizontal)
+                .onChange(of: selectedPeriod, perform: { (value) in
+                    request(url: getUrl(range: period[value]), model: [HistoricalPriceModel].self) { self.historicalPrices = $0 }
+                })
+            
             let (colour, prices) = prepareChart()
             Chart(data: prices)
                 .frame(height: chartHeight)
@@ -24,15 +32,15 @@
                                                            endPoint: .bottom)))
         }
         .onAppear {
-            request(url: getUrl(), model: [HistoricalPriceModel].self) { self.historicalPrices = $0 }
+            request(url: getUrl(range: "3m"), model: [HistoricalPriceModel].self) { self.historicalPrices = $0 }
         }
     }
     
-    private func getUrl() -> String {
+    private func getUrl(range: String) -> String {
         let baseUrl = Bundle.main.infoDictionary?["IEX_URL"] as? String ?? "Empty url"
         let apiKey = Bundle.main.infoDictionary?["IEX_API"] as? String ?? "Empty key"
-        let url = "\(baseUrl)/stock/\(symbol)/chart/1m?chartCloseOnly=true&token=\(apiKey)"
-        
+        let url = "\(baseUrl)/stock/\(symbol)/chart/\(range)?chartCloseOnly=true&token=\(apiKey)"
+
         return url
     }