changeset 336:6f904b166564

Implementing TradingDaysView
author Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
date Wed, 31 Mar 2021 20:42:44 +0200
parents 2dad5828ccf6
children 31f2838b2de7
files LazyBear.xcodeproj/project.pbxproj LazyBear.xcodeproj/project.xcworkspace/xcuserdata/dennis.xcuserdatad/UserInterfaceState.xcuserstate LazyBear/Views/Home/Helpers/TopStockItem.swift LazyBear/Views/Home/Helpers/TopStockRow.swift LazyBear/Views/Home/Helpers/TradingDatesItem.swift LazyBear/Views/Home/HomeView.swift LazyBear/Views/Home/TradingDates.swift LazyBear/Views/Networking/HomeData.swift LazyBear/Views/Networking/TradingDatesModel.swift
diffstat 9 files changed, 87 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/LazyBear.xcodeproj/project.pbxproj	Wed Mar 31 17:06:57 2021 +0200
+++ b/LazyBear.xcodeproj/project.pbxproj	Wed Mar 31 20:42:44 2021 +0200
@@ -29,6 +29,7 @@
 		95672B9625DDA54700DCBE4A /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 95672B9525DDA54700DCBE4A /* Preview Assets.xcassets */; };
 		95672B9825DDA54700DCBE4A /* Persistence.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95672B9725DDA54700DCBE4A /* Persistence.swift */; };
 		95672B9B25DDA54800DCBE4A /* LazyBear.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = 95672B9925DDA54800DCBE4A /* LazyBear.xcdatamodeld */; };
+		9585FE782614E5E60022CFD8 /* TradingDatesModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9585FE772614E5E60022CFD8 /* TradingDatesModel.swift */; };
 		95893DC92613C421003698C5 /* HomeData.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95893DC82613C421003698C5 /* HomeData.swift */; };
 		95893DCE2613C46B003698C5 /* SectorPerformanceModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95893DCD2613C46B003698C5 /* SectorPerformanceModel.swift */; };
 		958A735225E0170900FD7ECA /* CloudKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 958A735125E0170900FD7ECA /* CloudKit.framework */; };
@@ -65,6 +66,7 @@
 		95672B9725DDA54700DCBE4A /* Persistence.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Persistence.swift; sourceTree = "<group>"; };
 		95672B9A25DDA54800DCBE4A /* LazyBear.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = LazyBear.xcdatamodel; sourceTree = "<group>"; };
 		95672B9C25DDA54800DCBE4A /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
+		9585FE772614E5E60022CFD8 /* TradingDatesModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TradingDatesModel.swift; sourceTree = "<group>"; };
 		95893DC82613C421003698C5 /* HomeData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeData.swift; sourceTree = "<group>"; };
 		95893DCD2613C46B003698C5 /* SectorPerformanceModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SectorPerformanceModel.swift; sourceTree = "<group>"; };
 		958A734E25E016FD00FD7ECA /* LazyBear.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = LazyBear.entitlements; sourceTree = "<group>"; };
@@ -201,6 +203,7 @@
 			children = (
 				95893DC82613C421003698C5 /* HomeData.swift */,
 				95893DCD2613C46B003698C5 /* SectorPerformanceModel.swift */,
+				9585FE772614E5E60022CFD8 /* TradingDatesModel.swift */,
 			);
 			path = Networking;
 			sourceTree = "<group>";
@@ -341,6 +344,7 @@
 				95893DC92613C421003698C5 /* HomeData.swift in Sources */,
 				95893DCE2613C46B003698C5 /* SectorPerformanceModel.swift in Sources */,
 				95672B8F25DDA54700DCBE4A /* LazyBearApp.swift in Sources */,
+				9585FE782614E5E60022CFD8 /* TradingDatesModel.swift in Sources */,
 				95ECCA5D2612169200A67EFA /* LineShape.swift in Sources */,
 				9550444C26111FED000E0BCB /* TopStockItem.swift in Sources */,
 				951566EA2613A37C007C0F36 /* TradingDatesItem.swift in Sources */,
Binary file LazyBear.xcodeproj/project.xcworkspace/xcuserdata/dennis.xcuserdatad/UserInterfaceState.xcuserstate has changed
--- a/LazyBear/Views/Home/Helpers/TopStockItem.swift	Wed Mar 31 17:06:57 2021 +0200
+++ b/LazyBear/Views/Home/Helpers/TopStockItem.swift	Wed Mar 31 20:42:44 2021 +0200
@@ -55,9 +55,9 @@
     }
 }
 
-//struct TopStockItem_Previews: PreviewProvider {
-//    static var previews: some View {
-//        TopStockItem(company: CompanyRowModel(symbol: "aapl", companyName: "apple inc", latestPrice: 120.30, changePercent: 0.03), intradayPrices: <#IntradayPricesArray#>)
-//
-//    }
-//}
+struct TopStockItem_Previews: PreviewProvider {
+    static var previews: some View {
+        TopStockItem(company: CompanyRowModel(symbol: "aapl", companyName: "apple inc", latestPrice: 120.30, changePercent: 0.03), intradayPricesArray: IntradayPricesArray(intradayPrices: [IntradayPricesModel(open: 120.00)]))
+
+    }
+}
--- a/LazyBear/Views/Home/Helpers/TopStockRow.swift	Wed Mar 31 17:06:57 2021 +0200
+++ b/LazyBear/Views/Home/Helpers/TopStockRow.swift	Wed Mar 31 20:42:44 2021 +0200
@@ -46,9 +46,11 @@
     }
 }
 
-//
-//struct TopStockRow_Previews: PreviewProvider {
-//    static var previews: some View {
-//        TopStockRow(key: "Gainers", list:[CompanyRowModel](), intradayPrices: <#[String : IntradayPricesArray]#>)
-//    }
-//}
+
+struct TopStockRow_Previews: PreviewProvider {
+    static var previews: some View {
+        let list = [CompanyRowModel(symbol: "aapl", companyName: "apple inc", latestPrice: 120.30, changePercent: 0.03)]
+        let intradayPricesArray = IntradayPricesArray(intradayPrices: [IntradayPricesModel(open: 120.00)])
+        TopStockRow(key: "Gainers", list: list, intradayPricesDict: ["AAPL": intradayPricesArray])
+    }
+}
--- a/LazyBear/Views/Home/Helpers/TradingDatesItem.swift	Wed Mar 31 17:06:57 2021 +0200
+++ b/LazyBear/Views/Home/Helpers/TradingDatesItem.swift	Wed Mar 31 20:42:44 2021 +0200
@@ -8,6 +8,7 @@
 import SwiftUI
 
 struct TradingDatesItem: View {
+    var date: Date
     @Environment(\.colorScheme) var colorScheme
     
     var body: some View {
@@ -21,10 +22,10 @@
                         .foregroundColor(Color("default"))
                         
                     VStack {
-                        Text("April")
+                        Text(get(.month))
                             .fontWeight(.semibold)
                         
-                        Text("20")
+                        Text(get(.day))
                             .font(.title)
                             .fontWeight(.semibold)
                             .foregroundColor(Color("default"))
@@ -47,10 +48,32 @@
                 .clipShape(RoundedRectangle(cornerRadius: 20))
             )
     }
+    
+    private enum Components {
+        case day, month, year
+    }
+    
+    private func get(_ components: Components) -> String {
+        let dateComponents = Calendar.current.dateComponents([.year, .month, .day], from: date)
+        
+        switch components {
+        case .year:
+            return "\(dateComponents.year ?? 2020)"
+        case .day:
+            return "\(dateComponents.day ?? 1)"
+        case .month:
+            let dateFormatter = DateFormatter()
+            let monthNumber = dateComponents.month ?? 1
+            let monthLetters = dateFormatter.shortMonthSymbols[monthNumber-1]
+            
+            return "\(monthLetters)"
+        }
+        
+    }
 }
 
 struct TradingDatesItem_Previews: PreviewProvider {
     static var previews: some View {
-        TradingDatesItem()
+        TradingDatesItem(date: Date())
     }
 }
--- a/LazyBear/Views/Home/HomeView.swift	Wed Mar 31 17:06:57 2021 +0200
+++ b/LazyBear/Views/Home/HomeView.swift	Wed Mar 31 20:42:44 2021 +0200
@@ -46,7 +46,7 @@
         .onAppear { homeData.get() }
         .onReceive(timer) {_ in homeData.get() }
         .sheet(isPresented: $showTradingDates) {
-            TradingDates()
+            TradingDates(stringDates: homeData.holidayDates)
         }
     }
 }
--- a/LazyBear/Views/Home/TradingDates.swift	Wed Mar 31 17:06:57 2021 +0200
+++ b/LazyBear/Views/Home/TradingDates.swift	Wed Mar 31 20:42:44 2021 +0200
@@ -8,24 +8,44 @@
 import SwiftUI
 
 struct TradingDates: View {
+    var stringDates: [TradingDatesModel]
     
     var body: some View {
         NavigationView {
             ScrollView {
                 VStack(spacing: 30) {
-                    ForEach((1..<10)) { index in
-                        TradingDatesItem()
+                    ForEach(getArrayOfDates(), id: \.self) { date in
+                        TradingDatesItem(date: date)
                     }
                 }
                 .padding()
             }
-            .navigationTitle("Trading dates")
+            .navigationTitle("Holiday dates")
         }
     }
+    
+    
+    private func getArrayOfDates() -> [Date] {
+        // Get array of the string dates
+        let stringDates = self.stringDates.map { $0.date }
+        
+        // Convert string to date
+        let dateFormatter = DateFormatter()
+        dateFormatter.dateFormat = "yyyy-MM-dd"
+        
+        // Append dates to a Date array
+        var dates = [Date]()
+        for stringDate in stringDates {
+            dates.append(dateFormatter.date(from: stringDate)!)
+        }
+        
+        return dates
+    }
 }
 
 struct TradingDate_Previews: PreviewProvider {
     static var previews: some View {
-        TradingDates()
+        // Format is YYY-MM-DD
+        TradingDates(stringDates: [TradingDatesModel(date: "2020-04-01")])
     }
 }
--- a/LazyBear/Views/Networking/HomeData.swift	Wed Mar 31 17:06:57 2021 +0200
+++ b/LazyBear/Views/Networking/HomeData.swift	Wed Mar 31 20:42:44 2021 +0200
@@ -11,6 +11,7 @@
     @Published var sectorPerformance = [SectorPerformanceModel]()
     @Published var list = ["mostactive": [CompanyRowModel](), "gainers": [CompanyRowModel](), "losers": [CompanyRowModel]()]
     @Published var intradayPrices = [String(): IntradayPricesArray(intradayPrices: [IntradayPricesModel(open: Double())])]
+    @Published var holidayDates = [TradingDatesModel]()
     
     private let baseUrl = Bundle.main.infoDictionary?["IEX_URL"] as? String ?? "Empty url"
     private let apiKey = Bundle.main.infoDictionary?["IEX_API"] as? String ?? "Empty key"
@@ -56,5 +57,9 @@
                 }
             }
         }
+        
+        // 4. Request trading and holiday days
+        url = "\(baseUrl)/ref-data/us/dates/holiday/next/30?token=\(apiKey)"
+        request(url: url, model: [TradingDatesModel].self) { self.holidayDates = $0 }
     }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LazyBear/Views/Networking/TradingDatesModel.swift	Wed Mar 31 20:42:44 2021 +0200
@@ -0,0 +1,13 @@
+//
+//  TradingDatesModel.swift
+//  LazyBear
+//
+//  Created by Dennis Concepción Martín on 31/3/21.
+//
+
+import SwiftUI
+
+struct TradingDatesModel: Codable {
+    var date: String
+
+}