changeset 88:2704750d35a0

CloudKit implemented for logo api
author Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
date Wed, 27 Jan 2021 21:42:55 +0100
parents 104b643a8807
children 76b4a3be4d46
files LazyBear.xcodeproj/project.pbxproj LazyBear.xcodeproj/project.xcworkspace/xcuserdata/dennis.xcuserdatad/UserInterfaceState.xcuserstate lazybear/CloudKitManager.swift lazybear/GoogleApi.swift lazybear/LogoApi.swift lazybear/Supply views/Watchlist.swift lazybear/Supply views/WatchlistRow.swift lazybear/Tests/LoadImageTest.swift
diffstat 8 files changed, 66 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
--- a/LazyBear.xcodeproj/project.pbxproj	Wed Jan 27 19:59:58 2021 +0100
+++ b/LazyBear.xcodeproj/project.pbxproj	Wed Jan 27 21:42:55 2021 +0100
@@ -11,7 +11,7 @@
 		95078FD125BF4E640004FA75 /* CloudKitManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95078FD025BF4E640004FA75 /* CloudKitManager.swift */; };
 		950B79F625B1CB7A00E5DB5B /* CompanyList.swift in Sources */ = {isa = PBXBuildFile; fileRef = 950B79F525B1CB7A00E5DB5B /* CompanyList.swift */; };
 		952498B625BB47A700B00E22 /* LatestPriceModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 952498B525BB47A700B00E22 /* LatestPriceModel.swift */; };
-		9537923625BDF85D0001F82B /* GoogleApi.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9537923525BDF85D0001F82B /* GoogleApi.swift */; };
+		9537923625BDF85D0001F82B /* LogoApi.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9537923525BDF85D0001F82B /* LogoApi.swift */; };
 		9537924A25BDFCD70001F82B /* LoadImageTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9537924925BDFCD70001F82B /* LoadImageTest.swift */; };
 		954D992525A2123B001F7F60 /* HistoricalPricesModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 954D992425A2123B001F7F60 /* HistoricalPricesModel.swift */; };
 		954D996D25A2461B001F7F60 /* SwiftUICharts in Frameworks */ = {isa = PBXBuildFile; productRef = 954D996C25A2461B001F7F60 /* SwiftUICharts */; };
@@ -53,7 +53,7 @@
 		95078FD025BF4E640004FA75 /* CloudKitManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = CloudKitManager.swift; path = LazyBear/CloudKitManager.swift; sourceTree = SOURCE_ROOT; };
 		950B79F525B1CB7A00E5DB5B /* CompanyList.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CompanyList.swift; sourceTree = "<group>"; };
 		952498B525BB47A700B00E22 /* LatestPriceModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = LatestPriceModel.swift; path = lazybear/Models/LatestPriceModel.swift; sourceTree = SOURCE_ROOT; };
-		9537923525BDF85D0001F82B /* GoogleApi.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = GoogleApi.swift; path = lazybear/GoogleApi.swift; sourceTree = SOURCE_ROOT; };
+		9537923525BDF85D0001F82B /* LogoApi.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = LogoApi.swift; path = lazybear/LogoApi.swift; sourceTree = SOURCE_ROOT; };
 		9537924925BDFCD70001F82B /* LoadImageTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = LoadImageTest.swift; path = lazybear/Tests/LoadImageTest.swift; sourceTree = SOURCE_ROOT; };
 		954D992425A2123B001F7F60 /* HistoricalPricesModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = HistoricalPricesModel.swift; path = lazybear/Models/HistoricalPricesModel.swift; sourceTree = SOURCE_ROOT; };
 		95612C4F2598D48200F7698F /* SearchBar.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SearchBar.swift; sourceTree = "<group>"; };
@@ -204,7 +204,7 @@
 				95D1BF4825ADCF7700E5D063 /* Persistence.swift */,
 				95078FD025BF4E640004FA75 /* CloudKitManager.swift */,
 				95700BC525BD9D12009CEEFE /* IexApi.swift */,
-				9537923525BDF85D0001F82B /* GoogleApi.swift */,
+				9537923525BDF85D0001F82B /* LogoApi.swift */,
 				95B04EB225212369000AD27F /* LazyBearApp.swift */,
 				95B04EB425212369000AD27F /* ContentView.swift */,
 				95612C4D2598D48200F7698F /* Supply views */,
@@ -315,7 +315,7 @@
 				95B04EB325212369000AD27F /* LazyBearApp.swift in Sources */,
 				95AB4A7D259DCC0C0064C9C1 /* CompanyModel.swift in Sources */,
 				95700BC625BD9D12009CEEFE /* IexApi.swift in Sources */,
-				9537923625BDF85D0001F82B /* GoogleApi.swift in Sources */,
+				9537923625BDF85D0001F82B /* LogoApi.swift in Sources */,
 				9597CDFE25C1D484004DDFED /* TestStroke.swift in Sources */,
 				95C28AB625BC45CF0033D16A /* ChartStyle.swift in Sources */,
 				9537924A25BDFCD70001F82B /* LoadImageTest.swift in Sources */,
Binary file LazyBear.xcodeproj/project.xcworkspace/xcuserdata/dennis.xcuserdatad/UserInterfaceState.xcuserstate has changed
--- a/lazybear/CloudKitManager.swift	Wed Jan 27 19:59:58 2021 +0100
+++ b/lazybear/CloudKitManager.swift	Wed Jan 27 21:42:55 2021 +0100
@@ -9,11 +9,11 @@
 import CloudKit
 
 class CloudKitManager {
-    func query(recordType: String, recordName: String) -> CKRecord {  // recordType = "Database", e.g "API" / recordName = "Name", e.g iexProduction
+    func query(recordType: String, recordName: String, completition: @escaping ([CKRecord]) -> Void) {  // recordType = "Database", e.g "API"
         let publicDatabase = CKContainer.default().publicCloudDatabase
         
         // Query arguments
-        let recordID = CKRecord.ID(recordName: recordName)
+        let recordID = CKRecord.ID(recordName: recordName)  // e.g iexApiProduction
         let predicate = NSPredicate(format: "recordID = %@", recordID)
         let query = CKQuery(recordType: recordType, predicate: predicate)
         
@@ -21,13 +21,20 @@
         
         // Query begins
         publicDatabase.perform(query, inZoneWith: nil) { (results, error) in
-            guard error != nil else {  // If error is nil, then print results
-                print("Successfull CloudKit query")
+            if error != nil {
+                print("CloudKit query went wrong")
+                print(error!.localizedDescription)
+                
+                completition([CKRecord]())  // Return empty if error
 
+            } else {
+
+                if let results = results {
+                    print("Succesfull CloudKit query")
+                    
+                    completition(results)  // Return value for key ...
+                }
             }
-            
-            print("CloudKit query went wrong")
-            print(error!.localizedDescription)
         }
     }
 }
--- a/lazybear/GoogleApi.swift	Wed Jan 27 19:59:58 2021 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,21 +0,0 @@
-//
-//  GoogleApi.swift
-//  LazyBear
-//
-//  Created by Dennis Concepción Martín on 24/1/21.
-//
-
-import SwiftUI
-
-struct GoogleApi {
-    enum URL {
-        case company(symbol: String)
-        
-        var path: String {
-            switch self {
-            case let .company(symbol):
-                return "/iex/api/logos/\(symbol.uppercased()).png"
-            }
-        }
-    }
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lazybear/LogoApi.swift	Wed Jan 27 21:42:55 2021 +0100
@@ -0,0 +1,21 @@
+//
+//  GoogleApi.swift
+//  LazyBear
+//
+//  Created by Dennis Concepción Martín on 24/1/21.
+//
+
+import SwiftUI
+
+struct LogoApi {
+    enum URL {
+        case company(symbol: String)
+        
+        var path: String {
+            switch self {
+            case let .company(symbol):
+                return "/iex/api/logos/\(symbol.uppercased()).png"
+            }
+        }
+    }
+}
--- a/lazybear/Supply views/Watchlist.swift	Wed Jan 27 19:59:58 2021 +0100
+++ b/lazybear/Supply views/Watchlist.swift	Wed Jan 27 21:42:55 2021 +0100
@@ -6,23 +6,38 @@
 //
 
 import SwiftUI
+import CloudKit
 
 struct Watchlist: View {
     @Environment(\.managedObjectContext) private var viewContext  // Core data
     @FetchRequest(entity: WatchlistCompany.entity(), sortDescriptors: [])  // Core data
     var companies: FetchedResults<WatchlistCompany>  // Fetch core data
+    
     let cloud = CloudKitManager()
+    @State private var showingView = false
+    @State var cloudResults = [CKRecord]() {
+        didSet {
+            self.showingView = true
+        }
+    }
     
     var body: some View {
-        List {
-            EditButton()
-            let url = cloud.query(recordType: "API", recordName: "iexLogo")
-            ForEach(companies) { company in
-                WatchlistRow(company: company, url: "")
-                
+        if self.showingView {
+            List {
+                EditButton()
+                let url = cloudResults[0].object(forKey: "url") as! String
+                ForEach(companies) { company in
+                    WatchlistRow(company: company, url: url)
+                    
+                }
+                // Delete from persistent storage
+                .onDelete { indexSet in deleteWatchlist(indexSet: indexSet) }
             }
-            // Delete from persistent storage
-            .onDelete { indexSet in deleteWatchlist(indexSet: indexSet) }
+        } else {
+            Spacer()
+                .onAppear {
+                    cloud.query(recordType: "API", recordName: "iexLogo") { self.cloudResults = $0 }
+                }
         }
     }
     
--- a/lazybear/Supply views/WatchlistRow.swift	Wed Jan 27 19:59:58 2021 +0100
+++ b/lazybear/Supply views/WatchlistRow.swift	Wed Jan 27 21:42:55 2021 +0100
@@ -17,8 +17,9 @@
     var body: some View {
         Button(action: { companyView.isShowing.toggle() }) {
             HStack {
-                let path = GoogleApi.URL.company(symbol: company.symbol!).path
-                WebImage(url: URL(string: url + path))
+                let path = LogoApi.URL.company(symbol: company.symbol!).path
+                let endpoint = url + path
+                WebImage(url: URL(string: endpoint))
                     .resizable()
                     .placeholder { Rectangle().foregroundColor(.gray) }
                     .indicator(.activity) // Activity Indicator
--- a/lazybear/Tests/LoadImageTest.swift	Wed Jan 27 19:59:58 2021 +0100
+++ b/lazybear/Tests/LoadImageTest.swift	Wed Jan 27 21:42:55 2021 +0100
@@ -10,7 +10,7 @@
 
 struct LoadImageTest: View {
     var body: some View {
-        let url = GoogleApi.URL.company(symbol: "AMZN").path
+        let url = LogoApi.URL.company(symbol: "AMZN").path
         WebImage(url: URL(string: url))
             .indicator(.activity) // Activity Indicator
     }