# HG changeset patch # User Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com> # Date 1617905728 -7200 # Node ID fde2b30c719e1dcded25fccb10760763a34a0906 # Parent 276e17f11c19a558d0b834e6bc868c17bafad404 Implementing Networking in ProfileView diff -r 276e17f11c19 -r fde2b30c719e LazyBear.xcodeproj/project.xcworkspace/xcuserdata/dennis.xcuserdatad/UserInterfaceState.xcuserstate Binary file LazyBear.xcodeproj/project.xcworkspace/xcuserdata/dennis.xcuserdatad/UserInterfaceState.xcuserstate has changed diff -r 276e17f11c19 -r fde2b30c719e LazyBear.xcodeproj/xcshareddata/xcschemes/LazyBear.xcscheme --- a/LazyBear.xcodeproj/xcshareddata/xcschemes/LazyBear.xcscheme Wed Apr 07 16:40:53 2021 +0200 +++ b/LazyBear.xcodeproj/xcshareddata/xcschemes/LazyBear.xcscheme Thu Apr 08 20:15:28 2021 +0200 @@ -31,7 +31,7 @@ (entityName: "WatchlistCompany") } - @NSManaged public var symbol: String? - @NSManaged public var name: String? - @NSManaged public var watchlist: String? + @NSManaged public var symbol: String + @NSManaged public var name: String + @NSManaged public var watchlist: String } diff -r 276e17f11c19 -r fde2b30c719e LazyBear/Views/Global Helpers/StockItem.swift --- a/LazyBear/Views/Global Helpers/StockItem.swift Wed Apr 07 16:40:53 2021 +0200 +++ b/LazyBear/Views/Global Helpers/StockItem.swift Thu Apr 08 20:15:28 2021 +0200 @@ -60,9 +60,9 @@ } } -//struct StockItem_Previews: PreviewProvider { -// static var previews: some View { -// StockItem(company: CompanyQuoteModel(companyName: "Akumin Inc", symbol: "AKU", latestPrice: 120.30, changePercent: 0.03)) -// -// } -//} +struct StockItem_Previews: PreviewProvider { + static var previews: some View { + StockItem(company: CompanyQuoteModel(companyName: "Akumin Inc", symbol: "AKU", latestPrice: 120.30, changePercent: 0.03), intradayPrices: [IntradayPricesResult(open: 130.3)]) + + } +} diff -r 276e17f11c19 -r fde2b30c719e LazyBear/Views/Global Helpers/StockRectangleRow.swift --- a/LazyBear/Views/Global Helpers/StockRectangleRow.swift Wed Apr 07 16:40:53 2021 +0200 +++ b/LazyBear/Views/Global Helpers/StockRectangleRow.swift Thu Apr 08 20:15:28 2021 +0200 @@ -43,8 +43,8 @@ } -//struct StockRectangleRow_Previews: PreviewProvider { -// static var previews: some View { -// StockRectangleRow(listType: "gainers", list: [CompanyQuoteModel(companyName: "apple inc", symbol: "aapl", latestPrice: 120.30, changePercent: 0.034)]) -// } -//} +struct StockRectangleRow_Previews: PreviewProvider { + static var previews: some View { + StockRectangleRow(listType: "gainers", list: [CompanyQuoteModel(companyName: "apple inc", symbol: "aapl", latestPrice: 120.30, changePercent: 0.034)], intradayPrices: ["aapl": [IntradayPricesResult(open: 130.3)]]) + } +} diff -r 276e17f11c19 -r fde2b30c719e LazyBear/Views/Profile/Networking/ProfileData.swift --- a/LazyBear/Views/Profile/Networking/ProfileData.swift Wed Apr 07 16:40:53 2021 +0200 +++ b/LazyBear/Views/Profile/Networking/ProfileData.swift Thu Apr 08 20:15:28 2021 +0200 @@ -8,31 +8,67 @@ import SwiftUI class ProfileData: ObservableObject { - @Published var companies = [CompanyQuoteModel]() @Published var showView = false - @FetchRequest(entity: WatchlistCompany.entity(), sortDescriptors: []) - var watchlistCompanies: FetchedResults + + @Published var watchlists = [String(): [CompanyQuoteModel]()] + @Published var intradayPrices = [String(): [IntradayPricesResult]()] private let baseUrl = Bundle.main.infoDictionary?["IEX_URL"] as? String ?? "Empty url" private let apiKey = Bundle.main.infoDictionary?["IEX_API"] as? String ?? "Empty key" - private func get() { - var url = "\(baseUrl)/stock/market/batch?symbols=" + func get(_ watchlistCompanies: FetchedResults) { + let dispatchGroup = DispatchGroup() + var url = String() + let watchlistSymbols = watchlistCompanies.map { $0.symbol } // Map watchlist symbols + + // 1. REQUEST LATEST PRICE OF THE COMPANIES IN THE WATCHLIST CORE DATA + url = "\(baseUrl)/stock/market/batch?symbols=" + + // If there are no companies in Core Data, do not request if !watchlistCompanies.isEmpty { - let symbols = watchlistCompanies.map { $0.symbol } - for symbol in symbols { - if symbols.firstIndex(of: symbol) == 0 { - url += symbol! + var counter = 0 + for symbol in watchlistSymbols { // Iterate watchlist symbols + counter += 1 + + // Append symbols to the URL to make the batch request + if counter == 1 { + url += symbol } else { - url += "\(symbol!)," + url += ",\(symbol)" } } - url = "&types=quote&token=\(apiKey)" - genericRequest(url: url, model: [String: CompanyQuoteBatch].self) { - print($0) - self.showView = true + url += "&types=quote&token=\(apiKey)" + genericRequest(url: url, model: [String: CompanyQuoteBatch].self) { dict in + // Iterate symbols in the Batch request (Keys of the dictionary response) + for symbol in dict.keys { + // Get index of the requested symbol in the watchlist symbol array + let index = watchlistSymbols.firstIndex(of: symbol) + + // Get watchlist name of that symbol + let watchlistName = watchlistCompanies[index!].watchlist + + // Append the CompanyQuote response to a dictionary with key as the watchlist name. Not trivial + // First create a object of the company I want to add to the dict + let newCompany = dict[symbol]?.quote + + // Second, get an array of the values in the watchlist key + if var companiesInWatchlist = self.watchlists[watchlistName] { + // Third, append the new value to that array + companiesInWatchlist.append(newCompany!) + + // Finally, append the updated array to the key dict + self.watchlists[watchlistName] = companiesInWatchlist + } else { + // If it's nil create the array and add it to dict + let initCompanyArray: [CompanyQuoteModel] = [newCompany!] + self.watchlists[watchlistName] = initCompanyArray + } + + print(self.watchlists) + + } } } else { self.showView = true diff -r 276e17f11c19 -r fde2b30c719e LazyBear/Views/Profile/ProfileView.swift --- a/LazyBear/Views/Profile/ProfileView.swift Wed Apr 07 16:40:53 2021 +0200 +++ b/LazyBear/Views/Profile/ProfileView.swift Thu Apr 08 20:15:28 2021 +0200 @@ -9,18 +9,50 @@ struct ProfileView: View { @ObservedObject var profileData = ProfileData() + @Environment(\.managedObjectContext) private var moc + @FetchRequest(entity: WatchlistCompany.entity(), sortDescriptors: []) + var watchlistCompanies: FetchedResults var body: some View { NavigationView { List { - ForEach(profileData.companies, id: \.self) { company in - Text("Hello company") - } + Text("Hello World") + Button("Save company", action: addCompany) + Button("Delete company", action: removeCompany) + Button("Request", action: {profileData.get(watchlistCompanies)}) + Button("Print watchlist", action: {print(watchlistCompanies.map {$0.symbol})}) } .navigationTitle("My profile") .navigationBarTitleDisplayMode(.inline) } } + + // REMOVE IN PRODUCTION + private func addCompany() { + let watchlistCompany = WatchlistCompany(context: moc) + watchlistCompany.symbol = "FB" + watchlistCompany.name = "Facebook Inc" + watchlistCompany.watchlist = "MyWatchlist" + do { + try moc.save() + print("Company saved") + } catch { + print(error.localizedDescription) + } + } + + private func removeCompany() { + let symbols = watchlistCompanies.map { $0.symbol } + let index = symbols.firstIndex(of: "aapl") + let company = watchlistCompanies[index!] + moc.delete(company) + do { + try moc.save() + print("Company deleted") + } catch { + // Error + } + } } struct ProfileView_Previews: PreviewProvider {