comparison LazyBear/Views/Home/TradingDates.swift @ 339:e81c18164afb

Fixing backend API Requests
author Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
date Sat, 03 Apr 2021 13:02:40 +0200
parents 31f2838b2de7
children 80bfa88c6b0f
comparison
equal deleted inserted replaced
338:71a9c0e61783 339:e81c18164afb
5 // Created by Dennis Concepción Martín on 30/3/21. 5 // Created by Dennis Concepción Martín on 30/3/21.
6 // 6 //
7 7
8 import SwiftUI 8 import SwiftUI
9 9
10 enum DateType {
11 case holidays, trading
12 }
13
10 struct TradingDates: View { 14 struct TradingDates: View {
11 var stringDates: [TradingDatesModel] 15 @State private var dates = [TradingDatesModel]()
16 @State private var showingView = false
17
18 private let baseUrl = Bundle.main.infoDictionary?["IEX_URL"] as? String ?? "Empty url"
19 private let apiKey = Bundle.main.infoDictionary?["IEX_API"] as? String ?? "Empty key"
12 20
13 var body: some View { 21 var body: some View {
14 NavigationView { 22 NavigationView {
15 ScrollView { 23 ScrollView {
16 VStack(spacing: 20) { 24 VStack(spacing: 20) {
18 TradingDatesItem(date: date) 26 TradingDatesItem(date: date)
19 } 27 }
20 } 28 }
21 .padding() 29 .padding()
22 } 30 }
31 .onAppear { requestDates(.holidays) }
23 .navigationTitle("Holiday dates") 32 .navigationTitle("Holiday dates")
33 .navigationBarTitleDisplayMode(.inline)
24 } 34 }
25 } 35 }
26 36
37 private func requestDates(_ dateType: DateType) {
38 switch dateType {
39 case .trading:
40 let tradingUrl = "\(baseUrl)/ref-data/us/dates/trade/next/10?token=\(apiKey)"
41 genericRequest(url: tradingUrl, model: [TradingDatesModel].self) {
42 self.dates = $0
43 self.showingView = true
44 }
45 case.holidays:
46 let holidaysUrl = "\(baseUrl)/ref-data/us/dates/holiday/next/10?token=\(apiKey)"
47 genericRequest(url: holidaysUrl, model: [TradingDatesModel].self) {
48 self.dates = $0
49 self.showingView = true
50 }
51 }
52 }
27 53
28 private func getArrayOfDates() -> [Date] { 54 private func getArrayOfDates() -> [Date] {
29 // Get array of the string dates 55 // Get array of the string dates
30 let stringDates = self.stringDates.map { $0.date } 56 let stringDates = self.dates.map { $0.date }
31 57
32 // Convert string to date 58 // Convert string to date
33 let dateFormatter = DateFormatter() 59 let dateFormatter = DateFormatter()
34 dateFormatter.dateFormat = "yyyy-MM-dd" 60 dateFormatter.dateFormat = "yyyy-MM-dd"
35 61
44 } 70 }
45 71
46 struct TradingDate_Previews: PreviewProvider { 72 struct TradingDate_Previews: PreviewProvider {
47 static var previews: some View { 73 static var previews: some View {
48 // Format is YYY-MM-DD 74 // Format is YYY-MM-DD
49 TradingDates(stringDates: [TradingDatesModel(date: "2020-04-01")]) 75 TradingDates()
50 } 76 }
51 } 77 }