comparison LazyBear/Views/Profile/ProfileView.swift @ 393:0a4c399170c4

Implementing WatchlistCreator
author Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
date Sun, 02 May 2021 12:41:20 +0200
parents 13f3578def61
children 4c90e5b18632
comparison
equal deleted inserted replaced
392:13f3578def61 393:0a4c399170c4
18 @State private var showCreateNewWatchlist = false 18 @State private var showCreateNewWatchlist = false
19 19
20 var body: some View { 20 var body: some View {
21 if profile.showView { 21 if profile.showView {
22 NavigationView { 22 NavigationView {
23
23 List { 24 List {
24 25
25 // Get Watchlist names -> Create rows for each watchlist -> in each row, show companies 26 // Get Watchlist names -> Create rows for each watchlist -> in each row, show companies
26 let watchlists = Set(watchlistCompanies.map { $0.watchlist }) // Set -> avoid duplicates names 27 let watchlists = Set(watchlistCompanies.map { $0.watchlist }) // Set -> avoid duplicates names
27 28
28 ForEach(Array(watchlists), id: \.self) { watchlist in 29 ForEach(Array(watchlists), id: \.self) { watchlist in
29 let symbols = watchlistCompanies.filter({ $0.watchlist == watchlist }).map { $0.symbol } 30 let symbols = watchlistCompanies.filter({ $0.watchlist == watchlist }).map { $0.symbol }
30 31
31 if let companies = profile.data.quotes { 32 if let companies = profile.data.quotes {
32 let filteredCompanies = companies.filter({ symbols.contains($0.key) }) 33 let filteredCompanies = companies.filter({ symbols.contains($0.key) })
33 StockRow(listName: watchlist, 34 StockRow(listName: watchlist,
34 list: filteredCompanies, 35 list: filteredCompanies,
35 intradayPrices: profile.data.intradayPrices, 36 intradayPrices: profile.data.intradayPrices,
36 addOnDelete: true 37 addOnDelete: true
37 ) 38 )
38 .onAppear { updateRows(symbols.count, filteredCompanies.count) }
39 .listRowInsets(EdgeInsets()) 39 .listRowInsets(EdgeInsets())
40 } 40 }
41 } 41 }
42 } 42 }
43 .navigationTitle("My profile") 43 .navigationTitle("My profile")
49 } 49 }
50 } 50 }
51 } 51 }
52 } 52 }
53 .fullScreenCover(isPresented: $showCreateNewWatchlist) { 53 .fullScreenCover(isPresented: $showCreateNewWatchlist) {
54 CreateNewWatchlist() 54 WatchlistCreator()
55 .environment(\.managedObjectContext, self.moc) 55 .environment(\.managedObjectContext, self.moc)
56 } 56 }
57 } else { 57 } else {
58 ProgressView() 58 ProgressView()
59 .onAppear { prepareUrl(isInitRequest: true) } 59 .onAppear { prepareUrl(isInitRequest: true) }
60 } 60 }
61 } 61 }
62 62
63 /*
64 Get symbols in watchlists -> request
65 */
63 private func prepareUrl(isInitRequest: Bool) { 66 private func prepareUrl(isInitRequest: Bool) {
64 if watchlistCompanies.isEmpty { 67 if watchlistCompanies.isEmpty {
65 profile.showView = true 68 profile.showView = true
66 } else { 69 } else {
67 let symbols = watchlistCompanies.map { $0.symbol } // Get symbols in watchlists 70 let symbols = watchlistCompanies.map { $0.symbol } // Get symbols in watchlists
68 var url = "https://api.lazybear.app/profile/type=init/symbols=" 71 var url = "https://api.lazybear.app/profile/type=init/symbols="
69 72
70 var counter = 0 73 var counter = 0
71 for symbol in symbols { 74 for symbol in symbols {
72 counter += 1 75 counter += 1
76
73 if counter == 1 { 77 if counter == 1 {
74 url += symbol 78 url += symbol
75 } else { 79 } else {
76 url += ",\(symbol)" 80 url += ",\(symbol)"
77 } 81 }
78 } 82 }
79 profile.request(url, isInitRequest: isInitRequest) 83 profile.request(url, isInitRequest: isInitRequest)
80 } 84 }
81 } 85 }
82
83 /*
84 If Core Data changes, companies is not updated because the API request is not called ->
85 Check if symbols.count (Core Data) is equal to filteredCompanies -> if not -> call API
86 */
87 private func updateRows(_ numberOfCoreDataCompanies: Int, _ numberOfApiRequestedCompanies: Int) {
88 if numberOfCoreDataCompanies != numberOfApiRequestedCompanies {
89 prepareUrl(isInitRequest: true) // Call API
90 }
91 }
92 } 86 }
93 87
94 struct ProfileView_Previews: PreviewProvider { 88 struct ProfileView_Previews: PreviewProvider {
95 static var previews: some View { 89 static var previews: some View {
96 ProfileView() 90 ProfileView()