comparison LazyBear/Views/Search/Helpers/SearchedCompanyItem.swift @ 417:5f21f7c23c5e

Add comments and clean code
author Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
date Fri, 11 Jun 2021 11:37:42 +0200
parents 4c90e5b18632
children 4effac4733b0
comparison
equal deleted inserted replaced
416:1662a41e2c1a 417:5f21f7c23c5e
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 watchlistCompany: FetchedResults<WatchlistCompany>
16 16
17 @State private var showingActionSheet = false 17 @State private var showingWatchlistSelector = false
18 18
19 var body: some View { 19 var body: some View {
20 let watchlistSymbols = watchlistCompany.map { $0.symbol }
21 HStack { 20 HStack {
22 Button(action: { self.showingActionSheet = true }) { 21 Button(action: { self.showingWatchlistSelector = true }) {
22 let watchlistSymbols = watchlistCompany.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 {
47 .fontWeight(.semibold) 47 .fontWeight(.semibold)
48 48
49 Text(company.region!) 49 Text(company.region!)
50 } 50 }
51 } 51 }
52 .actionSheet(isPresented: $showingActionSheet) { 52 .actionSheet(isPresented: $showingWatchlistSelector) {
53 ActionSheet(title: Text("Add to watchlist"), message: Text("Select"), buttons: generateButtons()) 53 ActionSheet(title: Text("Add to watchlist"), message: Text("Select"), buttons: generateButtons())
54 } 54 }
55 } 55 }
56 56
57 // Get watchlist names -> generate buttons 57 /*
58 Generate buttons for each watchlist to let the user selects to which watchlist
59 he wants to add the company
60 */
58 private func generateButtons() -> [ActionSheet.Button] { 61 private func generateButtons() -> [ActionSheet.Button] {
59 var actionButtons = [ActionSheet.Button]() 62 var actionButtons = [ActionSheet.Button]()
60 let watchlists = Set(watchlistCompany.map { $0.watchlist }) 63 let watchlists = Set(watchlistCompany.map { $0.watchlist })
61 64
62 for watchlistName in watchlists { 65 for watchlistName in watchlists {
70 actionButtons.append(.cancel()) 73 actionButtons.append(.cancel())
71 74
72 return actionButtons 75 return actionButtons
73 } 76 }
74 77
75 // Add to watchlist 78 /*
79 When the user taps the watchlist -> save the company to CoreData
80 */
76 private func addCompany(_ symbol: String, _ name: String, _ watchlist: String) { 81 private func addCompany(_ symbol: String, _ name: String, _ watchlist: String) {
77 let watchlistCompany = WatchlistCompany(context: moc) 82 let watchlistCompany = WatchlistCompany(context: moc)
78 watchlistCompany.symbol = symbol 83 watchlistCompany.symbol = symbol
79 watchlistCompany.name = name 84 watchlistCompany.name = name
80 watchlistCompany.watchlist = watchlist 85 watchlistCompany.watchlist = watchlist