comparison LazyBear/Views/Profile/ProfileView.swift @ 394:4c90e5b18632

Fixes #46
author Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
date Fri, 07 May 2021 11:00:53 +0200
parents 0a4c399170c4
children 5c99883c7964
comparison
equal deleted inserted replaced
393:0a4c399170c4 394:4c90e5b18632
8 import SwiftUI 8 import SwiftUI
9 import CoreData 9 import CoreData
10 10
11 struct ProfileView: View { 11 struct ProfileView: View {
12 @ObservedObject var profile = Profile() 12 @ObservedObject var profile = Profile()
13
14 @Environment(\.managedObjectContext) private var moc 13 @Environment(\.managedObjectContext) private var moc
15 @FetchRequest(entity: WatchlistCompany.entity(), sortDescriptors: []) 14 @FetchRequest(entity: WatchlistCompany.entity(), sortDescriptors: [])
16 var watchlistCompanies: FetchedResults<WatchlistCompany> 15 var watchlistCompanies: FetchedResults<WatchlistCompany>
17 16
18 @State private var showCreateNewWatchlist = false 17 @State private var showCreateNewWatchlist = false
19 18
20 var body: some View { 19 var body: some View {
21 if profile.showView { 20 if profile.showView {
22 NavigationView { 21 NavigationView {
23
24 List { 22 List {
25
26 // Get Watchlist names -> Create rows for each watchlist -> in each row, show companies 23 // Get Watchlist names -> Create rows for each watchlist -> in each row, show companies
27 let watchlists = Set(watchlistCompanies.map { $0.watchlist }) // Set -> avoid duplicates names 24 let watchlists = Set(watchlistCompanies.map { $0.watchlist }) // Set -> avoid duplicates names
28 25
29 ForEach(Array(watchlists), id: \.self) { watchlist in 26 ForEach(Array(watchlists).sorted(), id: \.self) { listName in
30 let symbols = watchlistCompanies.filter({ $0.watchlist == watchlist }).map { $0.symbol } 27 let symbols = watchlistCompanies.filter({ $0.watchlist == listName }).map { $0.symbol }
31 28
32 if let companies = profile.data.quotes { 29 if let companies = profile.data.quotes {
33 let filteredCompanies = companies.filter({ symbols.contains($0.key) }) 30 // Select from API requested companies only the ones withing the watchlist
34 StockRow(listName: watchlist, 31 let list = companies.filter({ symbols.contains($0.key) })
35 list: filteredCompanies, 32 StockRow(listName: listName, list: list, intradayPrices: profile.data.intradayPrices, addOnDelete: true)
36 intradayPrices: profile.data.intradayPrices,
37 addOnDelete: true
38 )
39 .listRowInsets(EdgeInsets())
40 } 33 }
34 }
35 .listRowInsets(EdgeInsets())
36 .onAppear { // Refresh API requested companies when Core Data changes
37 refreshList()
41 } 38 }
42 } 39 }
43 .navigationTitle("My profile") 40 .navigationTitle("My profile")
44 .navigationBarTitleDisplayMode(.inline) 41 .navigationBarTitleDisplayMode(.inline)
45 .toolbar { 42 .toolbar {
59 .onAppear { prepareUrl(isInitRequest: true) } 56 .onAppear { prepareUrl(isInitRequest: true) }
60 } 57 }
61 } 58 }
62 59
63 /* 60 /*
64 Get symbols in watchlists -> request 61 Get symbols in watchlists -> Prepare url -> Request
65 */ 62 */
66 private func prepareUrl(isInitRequest: Bool) { 63 private func prepareUrl(isInitRequest: Bool) {
67 if watchlistCompanies.isEmpty { 64 if watchlistCompanies.isEmpty {
68 profile.showView = true 65 profile.showView = true
69 } else { 66 } else {
70 let symbols = watchlistCompanies.map { $0.symbol } // Get symbols in watchlists 67 let symbols = watchlistCompanies.map { $0.symbol }
71 var url = "https://api.lazybear.app/profile/type=init/symbols=" 68 var url = "https://api.lazybear.app/profile/type=init/symbols="
72 69
73 var counter = 0 70 var counter = 0
74 for symbol in symbols { 71 for symbol in symbols {
75 counter += 1 72 counter += 1
81 } 78 }
82 } 79 }
83 profile.request(url, isInitRequest: isInitRequest) 80 profile.request(url, isInitRequest: isInitRequest)
84 } 81 }
85 } 82 }
83
84 /*
85 When a company is added to a watchlist or a new watchlist is created -> call function
86 to make the API request and refresh correctly the list
87 */
88 private func refreshList() {
89 print("Companies in watchlist -> \(watchlistCompanies.count)")
90 print("Companies requested -> \(profile.data.quotes!.count)")
91
92 if profile.data.quotes!.count < watchlistCompanies.count {
93 prepareUrl(isInitRequest: true)
94 }
95 }
86 } 96 }
87 97
88 struct ProfileView_Previews: PreviewProvider { 98 struct ProfileView_Previews: PreviewProvider {
89 static var previews: some View { 99 static var previews: some View {
90 ProfileView() 100 ProfileView()