Mercurial > public > lazybear
comparison LazyBear/Views/Profile/ProfileView.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 | e24c9ca71824 |
children | 6dd97877f575 |
comparison
equal
deleted
inserted
replaced
416:1662a41e2c1a | 417:5f21f7c23c5e |
---|---|
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 watchlistCompanies: FetchedResults<WatchlistCompany> | 15 var watchlistCompanies: FetchedResults<WatchlistCompany> |
16 | 16 |
17 @State private var showCreateNewWatchlist = false | 17 @State private var showCreateNewWatchlist = false |
18 | 18 @State private var timer = Timer.publish(every: 10, on: .main, in: .common).autoconnect() /// Set recurrent price request |
19 // Set recurrent price request | |
20 @State private var timer = Timer.publish(every: 10, on: .main, in: .common).autoconnect() | |
21 | 19 |
22 var body: some View { | 20 var body: some View { |
23 if profile.showView { | 21 if profile.showView { |
24 NavigationView { | 22 NavigationView { |
25 List { | 23 List { |
26 // Get Watchlist names -> Create rows for each watchlist -> in each row, show companies | 24 /* |
27 let watchlists = Set(watchlistCompanies.map { $0.watchlist }) // Set -> avoid duplicates names | 25 Get Watchlist names -> Create rows for each watchlist -> in each row, show companies |
28 | 26 */ |
27 let watchlists = Set(watchlistCompanies.map { $0.watchlist }) /// Set -> avoid duplicates names | |
29 ForEach(Array(watchlists).sorted(), id: \.self) { listName in | 28 ForEach(Array(watchlists).sorted(), id: \.self) { listName in |
30 let symbols = watchlistCompanies.filter({ $0.watchlist == listName }).map { $0.symbol } | 29 let symbols = watchlistCompanies.filter({ $0.watchlist == listName }).map { $0.symbol } |
31 | |
32 if let companies = profile.data.quotes { | 30 if let companies = profile.data.quotes { |
33 // Select from API requested companies only the ones withing the watchlist | 31 let list = companies.filter({ symbols.contains($0.key) }) /// From API response select the companies within the specified watchlist |
34 let list = companies.filter({ symbols.contains($0.key) }) | |
35 StockRow(listName: listName, list: list, intradayPrices: profile.data.intradayPrices, addOnDelete: true) | 32 StockRow(listName: listName, list: list, intradayPrices: profile.data.intradayPrices, addOnDelete: true) |
36 } | 33 } |
37 } | 34 } |
38 .listRowInsets(EdgeInsets()) | 35 .listRowInsets(EdgeInsets()) |
39 .onAppear { // Refresh API requested companies when Core Data changes | 36 .onAppear { /// Request API again when Core Data changes to update the list |
40 refreshList() | 37 refreshList() |
41 } | 38 } |
42 } | 39 } |
43 .onAppear { self.timer = Timer.publish(every: 10, on: .main, in: .common).autoconnect() } // Start timer | 40 .onAppear { self.timer = Timer.publish(every: 10, on: .main, in: .common).autoconnect() } // Start timer |
44 .onDisappear { self.timer.upstream.connect().cancel() } // Stop timer | 41 .onDisappear { self.timer.upstream.connect().cancel() } // Stop timer |
66 .onAppear { prepareUrl(.initial) } | 63 .onAppear { prepareUrl(.initial) } |
67 } | 64 } |
68 } | 65 } |
69 | 66 |
70 /* | 67 /* |
71 Get symbols in watchlists -> Prepare url -> Request | 68 Get symbols in watchlists (Core Data) -> Prepare url -> Request |
72 */ | 69 */ |
73 private func prepareUrl(_ requestType: RequestType) { | 70 private func prepareUrl(_ requestType: RequestType) { |
74 let symbols = watchlistCompanies.map { $0.symbol } | 71 let symbols = watchlistCompanies.map { $0.symbol } |
75 | 72 |
76 var symbolString = "" | 73 var symbolString = "" |