comparison LazyBear/Views/Profile/ProfileView.swift @ 427:e707dbfc3115

Fixing weird animation .onDelete
author Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
date Wed, 16 Jun 2021 13:46:40 +0200
parents 6dd97877f575
children 3ca32ff79630
comparison
equal deleted inserted replaced
426:4ec307505b3a 427:e707dbfc3115
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 List { 23 List {
24 /* 24 if let apiCompanies = profile.data.quotes {
25 Get Watchlist names -> Create rows for each watchlist -> in each row, show companies 25 let watchlistsNames = Array(Set(watchlistCompanies.map { $0.watchlistName })).sorted() /// Get watchlistsNames in Core Data
26 */ 26 ForEach(watchlistsNames, id: \.self) { watchlistName in
27 let watchlists = Set(watchlistCompanies.map { $0.watchlist }) /// Set -> avoid duplicates names 27 let companies = createWatchlistRow(apiCompanies, watchlistCompanies, watchlistName)
28 28 StockRow(listName: watchlistName, companies: companies, showWatchlistSheet: true)
29 ForEach(Array(watchlists).sorted(), id: \.self) { listName in
30 let symbols = watchlistCompanies.filter({ $0.watchlist == listName }).map { $0.symbol } /// Get symbols contained in specified watchlist (Core Data)
31
32 if let companies = profile.data.quotes {
33 let list = companies.filter({ symbols.contains($0.key) }) /// From API response select the companies within the specified watchlist
34 StockRow(list: [listName: list], intradayPrices: profile.data.intradayPrices)
35 } 29 }
36 } 30 .listRowInsets(EdgeInsets())
37 .listRowInsets(EdgeInsets()) 31 .onAppear { /// Request API again when Core Data changes to update the list
38 .onAppear { /// Request API again when Core Data changes to update the list 32 refreshList()
39 refreshList() 33 }
40 } 34 }
41 } 35 }
42 .onAppear { self.timer = Timer.publish(every: 10, on: .main, in: .common).autoconnect() } /// Start timer 36 .onAppear { self.timer = Timer.publish(every: 10, on: .main, in: .common).autoconnect() } /// Start timer
43 .onDisappear { self.timer.upstream.connect().cancel() } /// Stop timer 37 .onDisappear { self.timer.upstream.connect().cancel() } /// Stop timer
44 .onReceive(timer) { _ in 38 .onReceive(timer) { _ in
65 .onAppear { prepareUrl(.initial) } 59 .onAppear { prepareUrl(.initial) }
66 } 60 }
67 } 61 }
68 62
69 /* 63 /*
64 At this point, we have the API response with the watchlist companies data requested and received. Now, we have to extract from the API response
65 the companies within the selected watchlist. To do that, we should do the following:
66 1) Get an array of all the symbols within the specified watchlist.
67 2) Iterate over watchlistSymbols and return the company (QuoteModel object) from apiCompanies that matches.
68 3) Append this symbol to a new array.
69 */
70 private func createWatchlistRow(_ apiCompanies: [CompanyModel], _ watchlistCompanies: FetchedResults<WatchlistCompany>, _ watchlistName: String) -> [CompanyModel] {
71 let watchlistSymbols = watchlistCompanies.filter({ $0.watchlistName == watchlistName }).map { $0.symbol } /// Get symbols contained in watchlistsName (Core Data)
72
73 var companies = [CompanyModel]()
74 for watchlistSymbol in watchlistSymbols {
75 let company = apiCompanies.first(where: { $0.symbol == watchlistSymbol })
76 companies.append(company!)
77 }
78
79 return companies
80 }
81
82 /*
83 When a company is added to a watchlist or a new watchlist is created -> call function
84 to make the API request and refresh correctly the list
85 */
86 private func refreshList() {
87 if profile.data.quotes!.count < watchlistCompanies.count {
88 prepareUrl(.initial)
89 }
90 }
91
92 /*
70 Get symbols in watchlists (Core Data) -> Prepare url -> Request 93 Get symbols in watchlists (Core Data) -> Prepare url -> Request
71 */ 94 */
72 private func prepareUrl(_ requestType: RequestType) { 95 private func prepareUrl(_ requestType: RequestType) {
73 let symbols = watchlistCompanies.map { $0.symbol } 96 let symbols = watchlistCompanies.map { $0.symbol }
74 97
89 default: 112 default:
90 let url = "https://api.lazybear.app/profile/type=streaming/symbols=\(symbolString)" 113 let url = "https://api.lazybear.app/profile/type=streaming/symbols=\(symbolString)"
91 profile.request(url, .streaming) 114 profile.request(url, .streaming)
92 } 115 }
93 } 116 }
94
95
96 /*
97 When a company is added to a watchlist or a new watchlist is created -> call function
98 to make the API request and refresh correctly the list
99 */
100 private func refreshList() {
101 // print("Companies in watchlist -> \(watchlistCompanies.count)")
102 // print("Companies requested -> \(profile.data.quotes!.count)")
103
104 if profile.data.quotes!.count < watchlistCompanies.count {
105 prepareUrl(.initial)
106 }
107 }
108 } 117 }
109 118
110 struct ProfileView_Previews: PreviewProvider { 119 struct ProfileView_Previews: PreviewProvider {
111 static var previews: some View { 120 static var previews: some View {
112 ProfileView() 121 ProfileView()