Mercurial > public > lazybear
comparison LazyBear/Views/Search/Helpers/SearchedCompanyItem.swift @ 425:4effac4733b0
Changing keys from API responses
author | Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com> |
---|---|
date | Wed, 16 Jun 2021 13:46:01 +0200 |
parents | 5f21f7c23c5e |
children | c6913f0ce46e |
comparison
equal
deleted
inserted
replaced
424:6dd97877f575 | 425:4effac4733b0 |
---|---|
10 struct SearchedCompanyItem: View { | 10 struct SearchedCompanyItem: View { |
11 var company: SearchResponse | 11 var company: SearchResponse |
12 | 12 |
13 @Environment(\.managedObjectContext) private var moc | 13 @Environment(\.managedObjectContext) private var moc |
14 @FetchRequest(entity: WatchlistCompany.entity(), sortDescriptors: []) | 14 @FetchRequest(entity: WatchlistCompany.entity(), sortDescriptors: []) |
15 var watchlistCompany: FetchedResults<WatchlistCompany> | 15 var watchlistCompanies: FetchedResults<WatchlistCompany> |
16 | 16 |
17 @State private var showingWatchlistSelector = false | 17 @State private var showingWatchlistSelector = false |
18 | 18 |
19 var body: some View { | 19 var body: some View { |
20 HStack { | 20 HStack { |
21 Button(action: { self.showingWatchlistSelector = true }) { | 21 Button(action: { self.showingWatchlistSelector = true }) { |
22 let watchlistSymbols = watchlistCompany.map { $0.symbol } | 22 let watchlistSymbols = watchlistCompanies.map { $0.symbol } |
23 if watchlistSymbols.contains(company.symbol!) { | 23 if watchlistSymbols.contains(company.symbol!) { |
24 Image(systemName: "star.fill") | 24 Image(systemName: "star.fill") |
25 .foregroundColor(.yellow) | 25 .foregroundColor(.yellow) |
26 .imageScale(.large) | 26 .imageScale(.large) |
27 } else { | 27 } else { |
58 Generate buttons for each watchlist to let the user selects to which watchlist | 58 Generate buttons for each watchlist to let the user selects to which watchlist |
59 he wants to add the company | 59 he wants to add the company |
60 */ | 60 */ |
61 private func generateButtons() -> [ActionSheet.Button] { | 61 private func generateButtons() -> [ActionSheet.Button] { |
62 var actionButtons = [ActionSheet.Button]() | 62 var actionButtons = [ActionSheet.Button]() |
63 let watchlists = Set(watchlistCompany.map { $0.watchlist }) | 63 let watchlists = Set(watchlistCompanies.map { $0.watchlistName }) |
64 | 64 |
65 for watchlistName in watchlists { | 65 for watchlistName in watchlists { |
66 actionButtons.append( | 66 actionButtons.append( |
67 .default(Text(watchlistName)) { | 67 .default(Text(watchlistName)) { |
68 addCompany(company.symbol!, company.securityName!, watchlistName) | 68 addCompany(company.symbol!, company.securityName!, watchlistName) |
76 } | 76 } |
77 | 77 |
78 /* | 78 /* |
79 When the user taps the watchlist -> save the company to CoreData | 79 When the user taps the watchlist -> save the company to CoreData |
80 */ | 80 */ |
81 private func addCompany(_ symbol: String, _ name: String, _ watchlist: String) { | 81 private func addCompany(_ symbol: String, _ name: String, _ watchlistName: String) { |
82 let watchlistCompany = WatchlistCompany(context: moc) | 82 let watchlistCompany = WatchlistCompany(context: moc) |
83 watchlistCompany.symbol = symbol | 83 watchlistCompany.symbol = symbol |
84 watchlistCompany.name = name | 84 watchlistCompany.name = name |
85 watchlistCompany.watchlist = watchlist | 85 watchlistCompany.watchlistName = watchlistName |
86 do { | 86 do { |
87 try moc.save() | 87 try moc.save() |
88 print("Company saved") | 88 print("Company saved") |
89 } catch { | 89 } catch { |
90 print(error.localizedDescription) | 90 print(error.localizedDescription) |