changeset 111:a9b7ea478c25

Clean code
author Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
date Mon, 01 Feb 2021 19:00:48 +0100
parents 9f02fcaf9ed1
children cd6177f15ded
files LazyBear.xcodeproj/project.pbxproj LazyBear.xcodeproj/project.xcworkspace/xcuserdata/dennis.xcuserdatad/UserInterfaceState.xcuserstate lazybear/Jobs/Normalize.swift lazybear/Jobs/NormalizeData.swift lazybear/Views/Price.swift lazybear/Views/Stock.swift lazybear/Views/WatchlistRow.swift
diffstat 7 files changed, 54 insertions(+), 49 deletions(-) [+]
line wrap: on
line diff
--- a/LazyBear.xcodeproj/project.pbxproj	Mon Feb 01 18:39:28 2021 +0100
+++ b/LazyBear.xcodeproj/project.pbxproj	Mon Feb 01 19:00:48 2021 +0100
@@ -17,7 +17,7 @@
 		954DDF0425C456E800848A4B /* QuoteModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 954DDF0325C456E800848A4B /* QuoteModel.swift */; };
 		95612C512598D48200F7698F /* SearchBar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95612C4F2598D48200F7698F /* SearchBar.swift */; };
 		95621AD925BF2EDB00BB17FC /* CloudKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 95621AD825BF2EDB00BB17FC /* CloudKit.framework */; };
-		95825AFC25C7255600465409 /* Normalize.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95825AFB25C7255600465409 /* Normalize.swift */; };
+		95825AFC25C7255600465409 /* NormalizeData.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95825AFB25C7255600465409 /* NormalizeData.swift */; };
 		958B678525C42B2400BF9F89 /* ApiAccess.swift in Sources */ = {isa = PBXBuildFile; fileRef = 958B678425C42B2400BF9F89 /* ApiAccess.swift */; };
 		9597CE0125C1DC0A004DDFED /* LogoModifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9597CE0025C1DC0A004DDFED /* LogoModifier.swift */; };
 		9597CE0425C1DFE7004DDFED /* LogoPlaceholder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9597CE0325C1DFE7004DDFED /* LogoPlaceholder.swift */; };
@@ -60,7 +60,7 @@
 		95612C4F2598D48200F7698F /* SearchBar.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SearchBar.swift; sourceTree = "<group>"; };
 		95621AD725BF2EC500BB17FC /* LazyBear.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = LazyBear.entitlements; sourceTree = "<group>"; };
 		95621AD825BF2EDB00BB17FC /* CloudKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CloudKit.framework; path = System/Library/Frameworks/CloudKit.framework; sourceTree = SDKROOT; };
-		95825AFB25C7255600465409 /* Normalize.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = Normalize.swift; path = lazybear/Jobs/Normalize.swift; sourceTree = SOURCE_ROOT; };
+		95825AFB25C7255600465409 /* NormalizeData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = NormalizeData.swift; path = lazybear/Jobs/NormalizeData.swift; sourceTree = SOURCE_ROOT; };
 		958B678425C42B2400BF9F89 /* ApiAccess.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = ApiAccess.swift; path = lazybear/ApiAccess.swift; sourceTree = SOURCE_ROOT; };
 		9597CE0025C1DC0A004DDFED /* LogoModifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LogoModifier.swift; sourceTree = "<group>"; };
 		9597CE0325C1DFE7004DDFED /* LogoPlaceholder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LogoPlaceholder.swift; sourceTree = "<group>"; };
@@ -161,7 +161,7 @@
 			isa = PBXGroup;
 			children = (
 				95AB4A79259DCBAE0064C9C1 /* ReadJson.swift */,
-				95825AFB25C7255600465409 /* Normalize.swift */,
+				95825AFB25C7255600465409 /* NormalizeData.swift */,
 			);
 			path = Jobs;
 			sourceTree = "<group>";
@@ -328,7 +328,7 @@
 				954D992525A2123B001F7F60 /* HistoricalPricesModel.swift in Sources */,
 				95FE646725C2DC580052832E /* WatchlistCompany+CoreDataClass.swift in Sources */,
 				95E411A725BEE03000A9C23F /* Watchlist.swift in Sources */,
-				95825AFC25C7255600465409 /* Normalize.swift in Sources */,
+				95825AFC25C7255600465409 /* NormalizeData.swift in Sources */,
 				95363CDC25C8741900B74131 /* Request.swift in Sources */,
 				95F7CAF625ADC7B7009E0E7C /* LazyBear.xcdatamodeld in Sources */,
 				95FE646825C2DC580052832E /* WatchlistCompany+CoreDataProperties.swift in Sources */,
Binary file LazyBear.xcodeproj/project.xcworkspace/xcuserdata/dennis.xcuserdatad/UserInterfaceState.xcuserstate has changed
--- a/lazybear/Jobs/Normalize.swift	Mon Feb 01 18:39:28 2021 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,21 +0,0 @@
-//
-//  normalize.swift
-//  LazyBear
-//
-//  Created by Dennis Concepción Martín on 31/1/21.
-//
-
-import SwiftUI
-
-func normalize(_ data: [Double]) -> [DataPoint] {
-    var normalData = [DataPoint]()
-    let min = data.min()!
-    let max = data.max()!
-    
-    for value in data {
-        let normal = (value - min) / (max - min)
-        normalData.append(DataPoint(value: normal))
-    }
-    
-    return normalData
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lazybear/Jobs/NormalizeData.swift	Mon Feb 01 19:00:48 2021 +0100
@@ -0,0 +1,21 @@
+//
+//  normalize.swift
+//  LazyBear
+//
+//  Created by Dennis Concepción Martín on 31/1/21.
+//
+
+import SwiftUI
+
+func normalize(_ data: [Double]) -> [DataPoint] {
+    var normalData = [DataPoint]()
+    let min = data.min()!
+    let max = data.max()!
+    
+    for value in data {
+        let normal = (value - min) / (max - min)
+        normalData.append(DataPoint(value: normal))
+    }
+    
+    return normalData
+}
--- a/lazybear/Views/Price.swift	Mon Feb 01 18:39:28 2021 +0100
+++ b/lazybear/Views/Price.swift	Mon Feb 01 19:00:48 2021 +0100
@@ -11,18 +11,18 @@
 struct Price: View {
     @State var symbol: String
     @State var showVertical: Bool
+    @EnvironmentObject var apiAccess: ApiAccess
     
+    // <--------- API Job --------->
     @State private var url = String() { didSet { requestPrice() }}
     @State private var showingView = false
-    
     @State var latestPrice = Float()
     @State var changePercent = Double() { didSet { self.showingView = true }}
     @State private var negativeChange = false
+    // <--------- API Job --------->
     
-    let iexApi = IexApi()  // Request api function
-    let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()  // Set recurrent price request
-    
-    @EnvironmentObject var apiAccess: ApiAccess
+    // Set recurrent price request. Real-time prices
+    let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
     
     var body: some View {
         VStack(alignment: .trailing) {
@@ -42,27 +42,29 @@
             }
         }
         .onAppear { getUrl() }
-        //.onReceive(timer) { _ in giveMePrices() }
+        //.onReceive(timer) { _ in requestPrice() }
         .onDisappear { self.timer.upstream.connect().cancel() }  // Stop timer
     }
     
      private func getUrl() {
-        let baseUrl = apiAccess.results[1].url ?? ""  // 1 -> Sandbox / 2 -> Production
+        // 1 -> Sandbox / 2 -> Production
+        let baseUrl = apiAccess.results[1].url ?? ""
         let token = apiAccess.results[1].key ?? ""
-        let path = iexApi.getPath(version: .stable, stock: .symbol(company: symbol), endpoint: .quote, range: nil, parameters: nil)
+        let path = "/stable/stock/\(symbol)/quote?token="
         
         self.url = baseUrl + path + token
 
     }
     
     private func requestPrice() {
-        iexApi.request(url: url, model: QuoteModel.self) { result in
+        request(url: url, model: QuoteModel.self) { result in
             self.latestPrice = result.latestPrice
             if self.changePercent >= 0 { self.negativeChange = true }
             self.changePercent = result.changePercent
         }
     }
 }
+
 // Wrap content if some condition is satisfied
 extension View {
    @ViewBuilder
--- a/lazybear/Views/Stock.swift	Mon Feb 01 18:39:28 2021 +0100
+++ b/lazybear/Views/Stock.swift	Mon Feb 01 19:00:48 2021 +0100
@@ -11,21 +11,26 @@
     var name: String
     var symbol: String
     var lineChartHeight: CGFloat
-    
+    @EnvironmentObject var apiAccess: ApiAccess
+
+    // <--------- Picker --------->
     var period = ["1W", "1M", "3M", "6M", "1Y", "2Y", "5Y"]
     @State var selectedPeriod = 2
+    // <--------- Picker --------->
     
-    @State private var url = String() { didSet { requestHistorical() }}
+    // <--------- API Job --------->
+    @State private var url = String() {
+        didSet { request(url: url, model: [HistoricalPricesModel].self) { self.data = $0 } }}
+    
     @State private var data = [HistoricalPricesModel]() { didSet { self.showingLineChart = true }}
     @State private var showingLineChart = false
-    
-    let iexApi = IexApi()  // Request api function
+    // <--------- API Job --------->
     
-    @EnvironmentObject var apiAccess: ApiAccess
-    
+    // <--------- Core Data --------->
     @Environment(\.managedObjectContext) private var viewContext
     @FetchRequest(entity: WatchlistCompany.entity(), sortDescriptors: [])
-    var companies: FetchedResults<WatchlistCompany>  // Fetch core data
+    var companies: FetchedResults<WatchlistCompany>
+    // <--------- Core Data --------->
     
     var body: some View {
         VStack {
@@ -57,17 +62,15 @@
     }
     
     private func getUrl(range: String) {
-        let baseUrl = apiAccess.results[1].url ?? ""  // 1 -> Sandbox / 2 -> Production
+        // 1 -> Sandbox / 2 -> Production
+        let baseUrl = apiAccess.results[1].url ?? ""
         let token = apiAccess.results[1].key ?? ""
-        let path = iexApi.getPath(version: .stable, stock: .symbol(company: symbol), endpoint: .historicalPrices, range: .period(range: range), parameters: .chartCloseOnly)
+        let path = "/stable/stock/\(symbol)/chart/\(range)?chartCloseOnly=true&includeToday=false&token="
         
         self.url = baseUrl + path + token
+        print(self.url)
    }
     
-    private func requestHistorical() {
-        iexApi.request(url: url, model: [HistoricalPricesModel].self) { self.data = $0 }
-    }
-    
     private func colorLineChart(prices: [Double]) -> Bool {
         let startPrice = prices.first ?? 0
         let endPrice = prices.last ?? 1
--- a/lazybear/Views/WatchlistRow.swift	Mon Feb 01 18:39:28 2021 +0100
+++ b/lazybear/Views/WatchlistRow.swift	Mon Feb 01 19:00:48 2021 +0100
@@ -19,7 +19,7 @@
         Button(action: { companyView.isShowing.toggle() }) {
             HStack {
                 let url = apiAccess.results[0].url
-                let path = LogoApi.URL.company(symbol: company.symbol ?? "").path
+                let path = "/iex/api/logos/\(company.symbol!).png"
                 let endpoint = url! + path
                 WebImage(url: URL(string: endpoint))
                     .resizable()
@@ -36,7 +36,7 @@
                 }
                 
                 Spacer()
-                if self.editMode?.wrappedValue.isEditing ?? true { } else { // If is not editing -> show prices
+                if self.editMode?.wrappedValue.isEditing ?? true { } else { // If EditButton() is not clicked -> show prices
                     Price(symbol: company.symbol ?? "", showVertical: false)
                 }
             }