Mercurial > public > lazybear
changeset 91:9138585de5ea
Implement environment object to api request
author | Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com> |
---|---|
date | Fri, 29 Jan 2021 13:29:52 +0100 |
parents | c59c01f70a55 |
children | 9abe4b6433f9 |
files | LazyBear.xcodeproj/project.pbxproj LazyBear.xcodeproj/project.xcworkspace/xcuserdata/dennis.xcuserdatad/UserInterfaceState.xcuserstate lazybear/ApiAccess.swift lazybear/ContentView.swift lazybear/IexApi.swift lazybear/LazyBearApp.swift lazybear/Supply views/Price.swift lazybear/Supply views/Watchlist.swift lazybear/Supply views/WatchlistRow.swift |
diffstat | 9 files changed, 81 insertions(+), 48 deletions(-) [+] |
line wrap: on
line diff
--- a/LazyBear.xcodeproj/project.pbxproj Thu Jan 28 16:43:27 2021 +0100 +++ b/LazyBear.xcodeproj/project.pbxproj Fri Jan 29 13:29:52 2021 +0100 @@ -16,6 +16,7 @@ 95612C512598D48200F7698F /* SearchBar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95612C4F2598D48200F7698F /* SearchBar.swift */; }; 95621AD925BF2EDB00BB17FC /* CloudKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 95621AD825BF2EDB00BB17FC /* CloudKit.framework */; }; 95700BC625BD9D12009CEEFE /* IexApi.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95700BC525BD9D12009CEEFE /* IexApi.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 */; }; 95AB4A7A259DCBAE0064C9C1 /* ReadJson.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95AB4A79259DCBAE0064C9C1 /* ReadJson.swift */; }; @@ -56,6 +57,7 @@ 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; }; 95700BC525BD9D12009CEEFE /* IexApi.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = IexApi.swift; path = lazybear/IexApi.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>"; }; 95AB4A79259DCBAE0064C9C1 /* ReadJson.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = ReadJson.swift; path = lazybear/Functions/ReadJson.swift; sourceTree = SOURCE_ROOT; }; @@ -199,6 +201,7 @@ 95078FD025BF4E640004FA75 /* CloudKitManager.swift */, 95700BC525BD9D12009CEEFE /* IexApi.swift */, 9537923525BDF85D0001F82B /* LogoApi.swift */, + 958B678425C42B2400BF9F89 /* ApiAccess.swift */, 95B04EB225212369000AD27F /* LazyBearApp.swift */, 95B04EB425212369000AD27F /* ContentView.swift */, 95612C4D2598D48200F7698F /* Supply views */, @@ -304,6 +307,7 @@ 95F6C2F025BAE2ED003CF389 /* Company.swift in Sources */, 95F6F46125C20E63002AC66A /* ListHeader.swift in Sources */, 95D1BF4925ADCF7700E5D063 /* Persistence.swift in Sources */, + 958B678525C42B2400BF9F89 /* ApiAccess.swift in Sources */, 95E4119225BEC56F00A9C23F /* SuperTitle.swift in Sources */, 95B04EB325212369000AD27F /* LazyBearApp.swift in Sources */, 95AB4A7D259DCC0C0064C9C1 /* CompanyModel.swift in Sources */,
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/ApiAccess.swift Fri Jan 29 13:29:52 2021 +0100 @@ -0,0 +1,14 @@ +// +// EnvEndpoints.swift +// LazyBear +// +// Created by Dennis Concepción Martín on 29/1/21. +// + +import SwiftUI +import CloudKit + +class ApiAccess: ObservableObject { + @Published var results = [ApiModel]() + @Published var showingView = false +}
--- a/lazybear/ContentView.swift Thu Jan 28 16:43:27 2021 +0100 +++ b/lazybear/ContentView.swift Fri Jan 29 13:29:52 2021 +0100 @@ -7,26 +7,52 @@ import SwiftUI import CoreData +import CloudKit struct ContentView: View { @State var searchedCompany = String() @State public var showingSearch: Bool = false let persistenceController = PersistenceController.shared + + let cloud = CloudKitManager() + @State var cloudFetch = [CKRecord]() { didSet { cloudValues() }} // On change, call function + @EnvironmentObject var apiAccess: ApiAccess var body: some View { VStack(alignment: .leading) { - SuperTitle(name: "Home") - SearchBar(searchedText: $searchedCompany, showingSearch: $showingSearch) + if apiAccess.showingView { + SuperTitle(name: "Home") + SearchBar(searchedText: $searchedCompany, showingSearch: $showingSearch) - if showingSearch { - if searchedCompany.count > 2 { - CompanyList(searchedCompany: $searchedCompany) + if showingSearch { + if searchedCompany.count > 2 { + CompanyList(searchedCompany: $searchedCompany) + } + Spacer() + } else { + Watchlist() } - Spacer() - } else { - Watchlist() } } + .onAppear { cloud.query(recordType: "API") { self.cloudFetch = $0 } } // Request CloudKit + } + + // Assign values to the model + private func cloudValues() { + var results = [ApiModel]() + cloudFetch.forEach({ (result) in + let key = result.object(forKey: "key") as? String + let name = result.object(forKey: "name") as? String + let url = result.object(forKey: "url") as? String + + let value = ApiModel(key: key, name: name, url: url) + results.append(value) + }) + // Main thread + DispatchQueue.main.async { + apiAccess.results = results + apiAccess.showingView = true + } } } @@ -34,5 +60,6 @@ struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() + .environmentObject(ApiAccess()) } }
--- a/lazybear/IexApi.swift Thu Jan 28 16:43:27 2021 +0100 +++ b/lazybear/IexApi.swift Fri Jan 29 13:29:52 2021 +0100 @@ -28,13 +28,16 @@ } } } - + enum Endpoint { case historicalPrices + case quote var path: String { switch self { case .historicalPrices: return "/chart" + case .quote: + return "/quote?" } } } @@ -61,14 +64,14 @@ // Create URL - func getURL(version: Version, stock: Stock, endpoint: Endpoint, range: Range, parameters: Parameters) -> String { + func getURL(version: Version, stock: Stock, endpoint: Endpoint, range: Range?, parameters: Parameters?) -> String { let version = version.path let stock = stock.path let endpoint = endpoint.path - let range = range.path - let parameters = parameters.path + let range = range?.path + let parameters = parameters?.path - let path = "\(version)\(stock)\(endpoint)\(range)\(parameters)&token=" + let path = "\(version)\(stock)\(endpoint)\(range ?? "")\(parameters ?? "")token=" return path
--- a/lazybear/LazyBearApp.swift Thu Jan 28 16:43:27 2021 +0100 +++ b/lazybear/LazyBearApp.swift Fri Jan 29 13:29:52 2021 +0100 @@ -10,11 +10,13 @@ @main struct LazyBearApp: App { let persistenceController = PersistenceController.shared + var apiAccess = ApiAccess() var body: some Scene { WindowGroup { ContentView() .environment(\.managedObjectContext, persistenceController.container.viewContext) + .environmentObject(apiAccess) // Api info (url and token) } } }
--- a/lazybear/Supply views/Price.swift Thu Jan 28 16:43:27 2021 +0100 +++ b/lazybear/Supply views/Price.swift Fri Jan 29 13:29:52 2021 +0100 @@ -9,8 +9,17 @@ import CloudKit struct Price: View { + let iexApi = IexApi() + var body: some View { Text("Price") + .onAppear { + //let url = api[1].url! as String + //let token = api[1].key! as String + //let path = iexApi.getURL(version: .stable, stock: .symbol(company: "AAPL"), endpoint: .quote, range: nil, parameters: nil) + //let endpoint = url + path + token + //print(endpoint) + } } }
--- a/lazybear/Supply views/Watchlist.swift Thu Jan 28 16:43:27 2021 +0100 +++ b/lazybear/Supply views/Watchlist.swift Fri Jan 29 13:29:52 2021 +0100 @@ -13,28 +13,13 @@ @FetchRequest(entity: WatchlistCompany.entity(), sortDescriptors: []) var companies: FetchedResults<WatchlistCompany> // Fetch core data - let cloud = CloudKitManager() - @State private var cloudFetch = [CKRecord]() { didSet { self.cloudValues() }} - @State private var cloudResults = [ApiModel]() - @State private var showingView = false - - var body: some View { - if self.showingView { - ListHeader(header: "Watchlist") - List { - ForEach(companies) { company in - //WatchlistRow(company: company, url: url) - } - .onDelete { indexSet in deleteWatchlist(indexSet: indexSet) } // Delete from persistent storage + ListHeader(header: "Watchlist") + List { + ForEach(companies) { company in + WatchlistRow(company: company) } - .onAppear { - print(cloudResults) - } - - } else { - Spacer() - .onAppear { cloud.query(recordType: "API") { self.cloudFetch = $0 } } + .onDelete { indexSet in deleteWatchlist(indexSet: indexSet) } // Delete from persistent storage } } @@ -49,18 +34,6 @@ print(error.localizedDescription) } } - - private func cloudValues() { - cloudFetch.forEach({ (result) in - let key = result.object(forKey: "key") as? String - let name = result.object(forKey: "name") as? String - let url = result.object(forKey: "url") as? String - - let value = ApiModel(key: key, name: name, url: url) - self.cloudResults.append(value) - }) - self.showingView = true - } } struct Watchlist_Previews: PreviewProvider {
--- a/lazybear/Supply views/WatchlistRow.swift Thu Jan 28 16:43:27 2021 +0100 +++ b/lazybear/Supply views/WatchlistRow.swift Fri Jan 29 13:29:52 2021 +0100 @@ -10,16 +10,17 @@ import SDWebImageSwiftUI struct WatchlistRow: View { + @EnvironmentObject var apiAccess: ApiAccess @ObservedObject var companyView = CompanyView() @Environment(\.editMode) var editMode // EditButton list var company: WatchlistCompany - var url: String var body: some View { Button(action: { companyView.isShowing.toggle() }) { HStack { + let url = apiAccess.results[0].url let path = LogoApi.URL.company(symbol: company.symbol ?? "").path - let endpoint = url + path + let endpoint = url! + path WebImage(url: URL(string: endpoint)) .resizable() .placeholder { LogoPlaceholder() } // If there is no logo @@ -55,6 +56,6 @@ let watchlistCompany = WatchlistCompany(context: moc) watchlistCompany.name = "apple inc" watchlistCompany.symbol = "aapl" - return WatchlistRow(company: watchlistCompany, url: "") + return WatchlistRow(company: watchlistCompany) } }