Mercurial > public > lazybear
comparison LazyBear/Views/Profile/ProfileView.swift @ 390:6303385b3629
Companies added to watchlists now are correctly updated
author | Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com> |
---|---|
date | Sun, 25 Apr 2021 19:52:04 +0200 |
parents | db8bc3ed526a |
children | 8ec37b2baafd |
comparison
equal
deleted
inserted
replaced
389:db8bc3ed526a | 390:6303385b3629 |
---|---|
10 | 10 |
11 struct ProfileView: View { | 11 struct ProfileView: View { |
12 @ObservedObject var profile = Profile() | 12 @ObservedObject var profile = Profile() |
13 @FetchRequest(entity: WatchlistCompany.entity(), sortDescriptors: []) | 13 @FetchRequest(entity: WatchlistCompany.entity(), sortDescriptors: []) |
14 var watchlistCompanies: FetchedResults<WatchlistCompany> | 14 var watchlistCompanies: FetchedResults<WatchlistCompany> |
15 | |
16 // Refresh view when watchlistCompanies change | |
17 @State private var refreshing = false | |
18 private var didSave = NotificationCenter.default.publisher(for: .NSManagedObjectContextDidSave) | |
19 | 15 |
20 var body: some View { | 16 var body: some View { |
21 if profile.showView { | 17 if profile.showView { |
22 NavigationView { | 18 NavigationView { |
19 // Get Watchlist names -> Create rows for each watchlist -> in each row, show companies | |
23 List { | 20 List { |
24 // Take all the different watchlist created | |
25 let watchlists = Set(watchlistCompanies.map { $0.watchlist }) // Set -> avoid duplicates names | 21 let watchlists = Set(watchlistCompanies.map { $0.watchlist }) // Set -> avoid duplicates names |
22 | |
26 ForEach(Array(watchlists), id: \.self) { watchlist in | 23 ForEach(Array(watchlists), id: \.self) { watchlist in |
27 | |
28 // Get all the symbols of this watchlist | |
29 let symbols = watchlistCompanies.filter({ $0.watchlist == watchlist }).map { $0.symbol } | 24 let symbols = watchlistCompanies.filter({ $0.watchlist == watchlist }).map { $0.symbol } |
30 | 25 |
31 if let companies = profile.data.quotes { | 26 if let companies = profile.data.quotes { |
32 let filteredCompanies = companies.filter({ symbols.contains($0.key) }) | 27 let filteredCompanies = companies.filter({ symbols.contains($0.key) }) |
33 StockRow(listName: watchlist, | 28 StockRow(listName: watchlist, |
34 list: filteredCompanies, | 29 list: filteredCompanies, |
35 intradayPrices: profile.data.intradayPrices, | 30 intradayPrices: profile.data.intradayPrices, |
36 addOnDelete: true | 31 addOnDelete: true |
37 ) | 32 ) |
33 .onAppear { updateRows(symbols.count, filteredCompanies.count) } | |
38 .listRowInsets(EdgeInsets()) | 34 .listRowInsets(EdgeInsets()) |
39 } | 35 } |
40 } | |
41 // The listener for refresh the view | |
42 .onReceive(self.didSave) { _ in | |
43 self.refreshing.toggle() | |
44 } | 36 } |
45 } | 37 } |
46 .navigationTitle("My profile") | 38 .navigationTitle("My profile") |
47 .navigationBarTitleDisplayMode(.inline) | 39 .navigationBarTitleDisplayMode(.inline) |
48 .toolbar { | 40 .toolbar { |
53 } | 45 } |
54 } | 46 } |
55 } | 47 } |
56 } else { | 48 } else { |
57 ProgressView() | 49 ProgressView() |
58 .onAppear { prepareUrl() } | 50 .onAppear { prepareUrl(isInitRequest: true) } |
59 } | 51 } |
60 } | 52 } |
61 | 53 |
62 private func prepareUrl() { | 54 private func prepareUrl(isInitRequest: Bool) { |
63 if watchlistCompanies.isEmpty { | 55 if watchlistCompanies.isEmpty { |
64 profile.showView = true | 56 profile.showView = true |
65 } else { | 57 } else { |
66 let symbols = watchlistCompanies.map { $0.symbol } // Get symbols in watchlists | 58 let symbols = watchlistCompanies.map { $0.symbol } // Get symbols in watchlists |
67 var url = "https://api.lazybear.app/profile/type=init/symbols=" | 59 var url = "https://api.lazybear.app/profile/type=init/symbols=" |
73 url += symbol | 65 url += symbol |
74 } else { | 66 } else { |
75 url += ",\(symbol)" | 67 url += ",\(symbol)" |
76 } | 68 } |
77 } | 69 } |
78 profile.request(url) | 70 profile.request(url, isInitRequest: isInitRequest) |
71 } | |
72 } | |
73 | |
74 /* | |
75 If Core Data changes, companies is not updated because the API request is not called -> | |
76 Check if symbols.count (Core Data) is equal to filteredCompanies -> if not -> call API | |
77 */ | |
78 private func updateRows(_ numberOfCoreDataCompanies: Int, _ numberOfApiRequestedCompanies: Int) { | |
79 if numberOfCoreDataCompanies != numberOfApiRequestedCompanies { | |
80 prepareUrl(isInitRequest: true) // Call API | |
79 } | 81 } |
80 } | 82 } |
81 } | 83 } |
82 | 84 |
83 struct ProfileView_Previews: PreviewProvider { | 85 struct ProfileView_Previews: PreviewProvider { |