changeset 401:f843c6382529

Add Enumeration to Networks files
author Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
date Thu, 20 May 2021 21:04:49 +0200
parents 6055a867d2b6
children 8357b101df67
files LazyBear.xcodeproj/project.pbxproj LazyBear.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved LazyBear.xcodeproj/project.xcworkspace/xcuserdata/dennis.xcuserdatad/UserInterfaceState.xcuserstate LazyBear/Global Models/HistoricalPricesModel.swift LazyBear/LazyBearApp.swift LazyBear/Views/Company/Chart.swift LazyBear/Views/Company/Networking/ChartResponse.swift LazyBear/Views/Company/Networking/Company.swift LazyBear/Views/Global Helpers/RequestType.swift LazyBear/Views/Home/HomeView.swift LazyBear/Views/Home/Networking/Home.swift LazyBear/Views/Profile/Networking/Profile.swift LazyBear/Views/Profile/ProfileView.swift
diffstat 13 files changed, 93 insertions(+), 51 deletions(-) [+]
line wrap: on
line diff
--- a/LazyBear.xcodeproj/project.pbxproj	Sat May 15 19:54:20 2021 +0200
+++ b/LazyBear.xcodeproj/project.pbxproj	Thu May 20 21:04:49 2021 +0200
@@ -47,6 +47,7 @@
 		95721DB4262787EF00EC527B /* ExtensiveList.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95721DB3262787EF00EC527B /* ExtensiveList.swift */; };
 		95721DB826278EC100EC527B /* CurrencyListItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95721DB726278EC100EC527B /* CurrencyListItem.swift */; };
 		958A735225E0170900FD7ECA /* CloudKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 958A735125E0170900FD7ECA /* CloudKit.framework */; };
+		9594F0402651355B00CFA8D4 /* HistoricalPricesModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9594F03F2651355B00CFA8D4 /* HistoricalPricesModel.swift */; };
 		95A07F5D26305A8F009865AA /* IntradayPricesModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95A07F5C26305A8F009865AA /* IntradayPricesModel.swift */; };
 		95A07F6226305A9B009865AA /* CurrencyModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95A07F6126305A9B009865AA /* CurrencyModel.swift */; };
 		95A07F6C26305AC6009865AA /* QuoteModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95A07F6B26305AC6009865AA /* QuoteModel.swift */; };
@@ -70,6 +71,7 @@
 		95E31C142647363800106B98 /* Chart.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95E31C132647363800106B98 /* Chart.swift */; };
 		95E31C16264736BE00106B98 /* DatePicker.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95E31C15264736BE00106B98 /* DatePicker.swift */; };
 		95E31C1826473A4D00106B98 /* CompanyHeader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95E31C1726473A4D00106B98 /* CompanyHeader.swift */; };
+		95E8BAA32656D86E0016AE72 /* RequestType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95E8BAA22656D86E0016AE72 /* RequestType.swift */; };
 		95FBE0DC2619CA7200440386 /* ProfileView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95FBE0DB2619CA7200440386 /* ProfileView.swift */; };
 /* End PBXBuildFile section */
 
@@ -138,6 +140,7 @@
 		95721DB726278EC100EC527B /* CurrencyListItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CurrencyListItem.swift; sourceTree = "<group>"; };
 		958A734E25E016FD00FD7ECA /* LazyBear.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = LazyBear.entitlements; sourceTree = "<group>"; };
 		958A735125E0170900FD7ECA /* CloudKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CloudKit.framework; path = System/Library/Frameworks/CloudKit.framework; sourceTree = SDKROOT; };
+		9594F03F2651355B00CFA8D4 /* HistoricalPricesModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HistoricalPricesModel.swift; sourceTree = "<group>"; };
 		95A07F5C26305A8F009865AA /* IntradayPricesModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IntradayPricesModel.swift; sourceTree = "<group>"; };
 		95A07F6126305A9B009865AA /* CurrencyModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CurrencyModel.swift; sourceTree = "<group>"; };
 		95A07F6B26305AC6009865AA /* QuoteModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QuoteModel.swift; sourceTree = "<group>"; };
@@ -160,6 +163,7 @@
 		95E31C132647363800106B98 /* Chart.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Chart.swift; sourceTree = "<group>"; };
 		95E31C15264736BE00106B98 /* DatePicker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DatePicker.swift; sourceTree = "<group>"; };
 		95E31C1726473A4D00106B98 /* CompanyHeader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CompanyHeader.swift; sourceTree = "<group>"; };
+		95E8BAA22656D86E0016AE72 /* RequestType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RequestType.swift; sourceTree = "<group>"; };
 		95FBE0DB2619CA7200440386 /* ProfileView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProfileView.swift; sourceTree = "<group>"; };
 /* End PBXFileReference section */
 
@@ -281,6 +285,7 @@
 				95A07F7026305AD5009865AA /* SectorPerformanceModel.swift */,
 				95A07F7526305AE3009865AA /* TradingDatesModel.swift */,
 				95613ADE264FC6FD00D4CE8F /* LatestNewsModel.swift */,
+				9594F03F2651355B00CFA8D4 /* HistoricalPricesModel.swift */,
 			);
 			path = "Global Models";
 			sourceTree = "<group>";
@@ -378,6 +383,7 @@
 				9550444826111FC9000E0BCB /* StockRow.swift */,
 				9550444B26111FED000E0BCB /* StockItem.swift */,
 				95BD2FB226341D36008B6752 /* BlurBackground.swift */,
+				95E8BAA22656D86E0016AE72 /* RequestType.swift */,
 			);
 			path = "Global Helpers";
 			sourceTree = "<group>";
@@ -654,6 +660,7 @@
 				9550443A26111B2B000E0BCB /* HomeView.swift in Sources */,
 				95672B9825DDA54700DCBE4A /* Persistence.swift in Sources */,
 				95A7C0742616409D003E2EC1 /* ParseJSON.swift in Sources */,
+				95E8BAA32656D86E0016AE72 /* RequestType.swift in Sources */,
 				954D7EA8260BBA6600A13C50 /* WatchlistCompany+CoreDataProperties.swift in Sources */,
 				951566E72613A2B6007C0F36 /* TradingDates.swift in Sources */,
 				955E73392623568F005652FF /* Home.swift in Sources */,
@@ -681,6 +688,7 @@
 				95D308F82624B3A400A39F77 /* CurrencyItem.swift in Sources */,
 				95E31C0D26472CA000106B98 /* CompanyView.swift in Sources */,
 				95A07F7126305AD5009865AA /* SectorPerformanceModel.swift in Sources */,
+				9594F0402651355B00CFA8D4 /* HistoricalPricesModel.swift in Sources */,
 				9550444626111EE5000E0BCB /* SectorItem.swift in Sources */,
 				954D7EA7260BBA6600A13C50 /* WatchlistCompany+CoreDataClass.swift in Sources */,
 				95672B9B25DDA54800DCBE4A /* LazyBear.xcdatamodeld in Sources */,
--- a/LazyBear.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved	Sat May 15 19:54:20 2021 +0200
+++ b/LazyBear.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved	Thu May 20 21:04:49 2021 +0200
@@ -6,8 +6,8 @@
         "repositoryURL": "https://github.com/denniscm190/StockCharts.git",
         "state": {
           "branch": null,
-          "revision": "cd83064a16d419160722f8c57bcc90bfb907ff24",
-          "version": "1.1.3"
+          "revision": "e9baa9118dfe956e8d9e34816f9c0059ad0d4fda",
+          "version": "1.1.4"
         }
       },
       {
Binary file LazyBear.xcodeproj/project.xcworkspace/xcuserdata/dennis.xcuserdatad/UserInterfaceState.xcuserstate has changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LazyBear/Global Models/HistoricalPricesModel.swift	Thu May 20 21:04:49 2021 +0200
@@ -0,0 +1,14 @@
+//
+//  HistoricalPricesModel.swift
+//  LazyBear
+//
+//  Created by Dennis Concepción Martín on 16/5/21.
+//
+
+import SwiftUI
+
+struct HistoricalPricesModel: Codable {
+    var close: Double
+    var date: String
+    var minute: String?
+}
--- a/LazyBear/LazyBearApp.swift	Sat May 15 19:54:20 2021 +0200
+++ b/LazyBear/LazyBearApp.swift	Thu May 20 21:04:49 2021 +0200
@@ -13,7 +13,7 @@
 
     var body: some Scene {
         WindowGroup {
-            CompanyView(symbol: "aapl")
+            ContentView()
                 .environment(\.managedObjectContext, persistenceController.container.viewContext)
         }
     }
--- a/LazyBear/Views/Company/Chart.swift	Sat May 15 19:54:20 2021 +0200
+++ b/LazyBear/Views/Company/Chart.swift	Thu May 20 21:04:49 2021 +0200
@@ -23,8 +23,9 @@
         if company.showChartView {
             VStack {
                 DatePicker(ranges: ranges, selectedRange: $selectedRange)
-                    .onChange(of: selectedRange, perform: { value in
-                        print(value.lowercased())
+                    .onChange(of: selectedRange, perform: { range in
+//                        let url = "https://api.lazybear.app/company/chart/type=init/symbol=\(symbol)/range=\(range)"
+//                        company.request(url, .refresh, "chart")
                     })
                 
                 RoundedRectangle(cornerRadius: 15)
@@ -49,8 +50,8 @@
                             }
                             .padding([.top, .leading, .trailing])
                             
-                            if let intradayPrices = company.chartData.intradayPrices![symbol.uppercased()] {
-                                if let prices = intradayPrices.compactMap { $0.open } {  // Map without nil
+                            if let historicalPrices = company.chartData.historicalPrices {
+                                if let prices = historicalPrices.compactMap { $0.close } {  // Map without nil
                                     LineChartView(data: prices, dates: nil, hours: nil, dragGesture: true)
                                         .padding(.bottom)
                                 }
@@ -68,12 +69,12 @@
             .onDisappear { self.timer.upstream.connect().cancel() }  // Stop timer
             .onReceive(timer) { _ in
                 let url = "https://api.lazybear.app/company/chart/type=streaming/symbol=\(symbol)"
-                company.request(url, isInitRequest: false, "chart") }  // Receive timer notification
+                company.request(url, .streaming, "chart") }  // Receive timer notification
         } else {
             ProgressView()
                 .onAppear {
                     let url = "https://api.lazybear.app/company/chart/type=init/symbol=\(symbol)"
-                    company.request(url, isInitRequest: true, "chart")
+                    company.request(url, .initial, "chart")
                 }
         }
     }
--- a/LazyBear/Views/Company/Networking/ChartResponse.swift	Sat May 15 19:54:20 2021 +0200
+++ b/LazyBear/Views/Company/Networking/ChartResponse.swift	Thu May 20 21:04:49 2021 +0200
@@ -8,12 +8,12 @@
 import SwiftUI
 
 struct ChartResponse: Codable {
-    var intradayPrices: [String: [IntradayPriceModel]]?
+    var historicalPrices: [HistoricalPricesModel]?
     var quote: [String: QuoteModel]?
     var latestNews: [LatestNewsModel]?
     
     private enum CodingKeys : String, CodingKey {
-        case intradayPrices = "intraday_prices"
+        case historicalPrices = "historical_prices"
         case quote
         case latestNews = "latest_news"
     }
--- a/LazyBear/Views/Company/Networking/Company.swift	Sat May 15 19:54:20 2021 +0200
+++ b/LazyBear/Views/Company/Networking/Company.swift	Thu May 20 21:04:49 2021 +0200
@@ -11,11 +11,18 @@
     @Published var showChartView = false
     @Published var chartData = ChartResponse()
     
-    func request(_ url: String, isInitRequest: Bool, _ view: String) {
+    func request(_ url: String, _ requestType: RequestType, _ view: String) {
         if view == "chart" {
             genericRequest(url: url, model: ChartResponse.self) { response in
-                if isInitRequest { self.chartData = response }  // If is the first request -> init()
-                else { self.chartData.quote = response.quote }  // If not, request streaming data (without intradayPrices)
+                switch requestType {
+                case .initial:
+                    self.chartData = response
+                case .refresh:
+                    print("refresh")
+                case .streaming:
+                    self.chartData.quote = response.quote
+                }
+                
                 self.showChartView = true
             }
         }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LazyBear/Views/Global Helpers/RequestType.swift	Thu May 20 21:04:49 2021 +0200
@@ -0,0 +1,14 @@
+//
+//  RequestType.swift
+//  LazyBear
+//
+//  Created by Dennis Concepción Martín on 20/5/21.
+//
+
+import SwiftUI
+
+enum RequestType {
+    case initial
+    case streaming
+    case refresh
+}
--- a/LazyBear/Views/Home/HomeView.swift	Sat May 15 19:54:20 2021 +0200
+++ b/LazyBear/Views/Home/HomeView.swift	Thu May 20 21:04:49 2021 +0200
@@ -51,7 +51,7 @@
                     }
                 }
                 .onAppear { self.timer = Timer.publish(every: 10, on: .main, in: .common).autoconnect() }  // Start timer
-                .onReceive(timer) { _ in home.request("https://api.lazybear.app/home/type=streaming", isInitRequest: false) }  // Receive timer notification
+                .onReceive(timer) { _ in home.request("https://api.lazybear.app/home/type=streaming", .streaming) }  // Receive timer notification
                 .onDisappear { self.timer.upstream.connect().cancel() }  // Stop timer
                 .navigationTitle("\(dueDate, formatter: Self.taskDateFormat)")
                 .navigationBarTitleDisplayMode(.inline)
@@ -72,7 +72,7 @@
         } else {
             ProgressView()
                 .onAppear {
-                    home.request("https://api.lazybear.app/home/type=init", isInitRequest: true)
+                    home.request("https://api.lazybear.app/home/type=init", .initial)
                 }
 
         }
--- a/LazyBear/Views/Home/Networking/Home.swift	Sat May 15 19:54:20 2021 +0200
+++ b/LazyBear/Views/Home/Networking/Home.swift	Thu May 20 21:04:49 2021 +0200
@@ -11,17 +11,16 @@
     @Published var showView = false
     @Published var data = HomeResponse()
     
-    func request(_ url: String, isInitRequest: Bool) {
+    func request(_ url: String, _ requestType: RequestType) {
         genericRequest(url: url, model: HomeResponse.self) { response in
-            
-            // If is the first request -> init()
-            if isInitRequest {
+            switch requestType {
+            case .initial:
                 self.data = response
-            } else {
-                // If not, request streaming data (without intradayPrices and latestCurrencies)
+            default:
                 self.data.lists = response.lists
                 self.data.sectorPerformance = response.sectorPerformance
             }
+            
             self.showView = true
         }
     }
--- a/LazyBear/Views/Profile/Networking/Profile.swift	Sat May 15 19:54:20 2021 +0200
+++ b/LazyBear/Views/Profile/Networking/Profile.swift	Thu May 20 21:04:49 2021 +0200
@@ -11,17 +11,15 @@
     @Published var showView = false
     @Published var data = ProfileResponse()
     
-    func request(_ url: String, isInitRequest: Bool) {
+    func request(_ url: String, _ requestType: RequestType) {
         genericRequest(url: url, model: ProfileResponse.self) { response in
-
-            // If is the first request -> init()
-            if isInitRequest {
+            switch requestType {
+            case .initial:
                 self.data = response
-            } else {
-                 // If not, request streaming data (without intradayPrices)
+            default:
                 self.data.quotes = response.quotes
             }
-
+            
             self.showView = true
         }
     }
--- a/LazyBear/Views/Profile/ProfileView.swift	Sat May 15 19:54:20 2021 +0200
+++ b/LazyBear/Views/Profile/ProfileView.swift	Thu May 20 21:04:49 2021 +0200
@@ -41,7 +41,7 @@
                     }
                 }
                 .onAppear { self.timer = Timer.publish(every: 10, on: .main, in: .common).autoconnect() }  // Start timer
-                .onReceive(timer) { _ in prepareUrl(isInitRequest: false) }
+                .onReceive(timer) { _ in prepareUrl(.streaming) }
                 .onDisappear { self.timer.upstream.connect().cancel() }  // Stop timer
                 .navigationTitle("My profile")
                 .navigationBarTitleDisplayMode(.inline)
@@ -59,35 +59,36 @@
             }
         } else {
             ProgressView()
-                .onAppear { prepareUrl(isInitRequest: true) }
+                .onAppear { prepareUrl(.initial) }
         }
     }
     
     /*
      Get symbols in watchlists -> Prepare url -> Request
      */
-    private func prepareUrl(isInitRequest: Bool) {
-        if watchlistCompanies.isEmpty {
-            profile.showView = true
-        } else {
-            let symbols = watchlistCompanies.map { $0.symbol }
-            var typeRequest = "streaming"
-            if isInitRequest { typeRequest = "init" }
-            var url = "https://api.lazybear.app/profile/type=\(typeRequest)/symbols="
-
-            var counter = 0
-            for symbol in symbols {
-                counter += 1
-                
-                if counter == 1 {
-                    url += symbol
-                } else {
-                    url += ",\(symbol)"
-                }
+    private func prepareUrl(_ requestType: RequestType) {
+        let symbols = watchlistCompanies.map { $0.symbol }
+        
+        var symbolString = ""
+        for (index, symbol) in symbols.enumerated() {
+            if index == 0 {
+                symbolString += symbol
+            } else {
+                symbolString += ",\(symbol)"
             }
-            profile.request(url, isInitRequest: isInitRequest)
+        }
+        
+        switch requestType {
+        case .initial:
+            let url = "https://api.lazybear.app/profile/type=init/symbols=\(symbolString)"
+            profile.request(url, .initial)
+            
+        default:
+            let url = "https://api.lazybear.app/profile/type=streaming/symbols=\(symbolString)"
+            profile.request(url, .streaming)
         }
     }
+        
     
     /*
      When a company is added to a watchlist or a new watchlist is created -> call function
@@ -98,7 +99,7 @@
         print("Companies requested -> \(profile.data.quotes!.count)")
         
         if profile.data.quotes!.count < watchlistCompanies.count {
-            prepareUrl(isInitRequest: true)
+            prepareUrl(.initial)
         }
     }
 }