Mercurial > public > lazybear
annotate 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 |
rev | line source |
---|---|
341
4e6c47a81b80
Testing UserProfile
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
diff
changeset
|
1 // |
4e6c47a81b80
Testing UserProfile
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
diff
changeset
|
2 // ProfileView.swift |
4e6c47a81b80
Testing UserProfile
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
diff
changeset
|
3 // LazyBear |
4e6c47a81b80
Testing UserProfile
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
diff
changeset
|
4 // |
4e6c47a81b80
Testing UserProfile
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
diff
changeset
|
5 // Created by Dennis Concepción Martín on 4/4/21. |
4e6c47a81b80
Testing UserProfile
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
diff
changeset
|
6 // |
4e6c47a81b80
Testing UserProfile
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
diff
changeset
|
7 |
4e6c47a81b80
Testing UserProfile
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
diff
changeset
|
8 import SwiftUI |
378
6802c2393203
Implementing ProfileView (Watchlists)
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
377
diff
changeset
|
9 import CoreData |
341
4e6c47a81b80
Testing UserProfile
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
diff
changeset
|
10 |
377
d01859776fe6
ProfileView updated
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
375
diff
changeset
|
11 struct ProfileView: View { |
d01859776fe6
ProfileView updated
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
375
diff
changeset
|
12 @ObservedObject var profile = Profile() |
378
6802c2393203
Implementing ProfileView (Watchlists)
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
377
diff
changeset
|
13 @FetchRequest(entity: WatchlistCompany.entity(), sortDescriptors: []) |
6802c2393203
Implementing ProfileView (Watchlists)
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
377
diff
changeset
|
14 var watchlistCompanies: FetchedResults<WatchlistCompany> |
377
d01859776fe6
ProfileView updated
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
375
diff
changeset
|
15 |
d01859776fe6
ProfileView updated
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
375
diff
changeset
|
16 var body: some View { |
378
6802c2393203
Implementing ProfileView (Watchlists)
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
377
diff
changeset
|
17 if profile.showView { |
6802c2393203
Implementing ProfileView (Watchlists)
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
377
diff
changeset
|
18 NavigationView { |
390
6303385b3629
Companies added to watchlists now are correctly updated
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
389
diff
changeset
|
19 // Get Watchlist names -> Create rows for each watchlist -> in each row, show companies |
378
6802c2393203
Implementing ProfileView (Watchlists)
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
377
diff
changeset
|
20 List { |
6802c2393203
Implementing ProfileView (Watchlists)
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
377
diff
changeset
|
21 let watchlists = Set(watchlistCompanies.map { $0.watchlist }) // Set -> avoid duplicates names |
390
6303385b3629
Companies added to watchlists now are correctly updated
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
389
diff
changeset
|
22 |
378
6802c2393203
Implementing ProfileView (Watchlists)
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
377
diff
changeset
|
23 ForEach(Array(watchlists), id: \.self) { watchlist in |
6802c2393203
Implementing ProfileView (Watchlists)
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
377
diff
changeset
|
24 let symbols = watchlistCompanies.filter({ $0.watchlist == watchlist }).map { $0.symbol } |
390
6303385b3629
Companies added to watchlists now are correctly updated
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
389
diff
changeset
|
25 |
378
6802c2393203
Implementing ProfileView (Watchlists)
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
377
diff
changeset
|
26 if let companies = profile.data.quotes { |
6802c2393203
Implementing ProfileView (Watchlists)
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
377
diff
changeset
|
27 let filteredCompanies = companies.filter({ symbols.contains($0.key) }) |
387
c206bd0bdb4e
Implementing RenameSheet.swift
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
379
diff
changeset
|
28 StockRow(listName: watchlist, |
c206bd0bdb4e
Implementing RenameSheet.swift
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
379
diff
changeset
|
29 list: filteredCompanies, |
c206bd0bdb4e
Implementing RenameSheet.swift
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
379
diff
changeset
|
30 intradayPrices: profile.data.intradayPrices, |
c206bd0bdb4e
Implementing RenameSheet.swift
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
379
diff
changeset
|
31 addOnDelete: true |
c206bd0bdb4e
Implementing RenameSheet.swift
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
379
diff
changeset
|
32 ) |
390
6303385b3629
Companies added to watchlists now are correctly updated
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
389
diff
changeset
|
33 .onAppear { updateRows(symbols.count, filteredCompanies.count) } |
387
c206bd0bdb4e
Implementing RenameSheet.swift
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
379
diff
changeset
|
34 .listRowInsets(EdgeInsets()) |
378
6802c2393203
Implementing ProfileView (Watchlists)
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
377
diff
changeset
|
35 } |
6802c2393203
Implementing ProfileView (Watchlists)
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
377
diff
changeset
|
36 } |
6802c2393203
Implementing ProfileView (Watchlists)
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
377
diff
changeset
|
37 } |
6802c2393203
Implementing ProfileView (Watchlists)
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
377
diff
changeset
|
38 .navigationTitle("My profile") |
6802c2393203
Implementing ProfileView (Watchlists)
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
377
diff
changeset
|
39 .navigationBarTitleDisplayMode(.inline) |
388
79c39987aaa4
Implementing Watchlists in ProfileView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
387
diff
changeset
|
40 .toolbar { |
79c39987aaa4
Implementing Watchlists in ProfileView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
387
diff
changeset
|
41 ToolbarItem(placement: .navigationBarTrailing) { |
79c39987aaa4
Implementing Watchlists in ProfileView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
387
diff
changeset
|
42 Button(action: {}) { |
79c39987aaa4
Implementing Watchlists in ProfileView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
387
diff
changeset
|
43 Image(systemName: "plus") |
79c39987aaa4
Implementing Watchlists in ProfileView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
387
diff
changeset
|
44 } |
79c39987aaa4
Implementing Watchlists in ProfileView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
387
diff
changeset
|
45 } |
79c39987aaa4
Implementing Watchlists in ProfileView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
387
diff
changeset
|
46 } |
377
d01859776fe6
ProfileView updated
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
375
diff
changeset
|
47 } |
378
6802c2393203
Implementing ProfileView (Watchlists)
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
377
diff
changeset
|
48 } else { |
6802c2393203
Implementing ProfileView (Watchlists)
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
377
diff
changeset
|
49 ProgressView() |
390
6303385b3629
Companies added to watchlists now are correctly updated
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
389
diff
changeset
|
50 .onAppear { prepareUrl(isInitRequest: true) } |
378
6802c2393203
Implementing ProfileView (Watchlists)
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
377
diff
changeset
|
51 } |
6802c2393203
Implementing ProfileView (Watchlists)
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
377
diff
changeset
|
52 } |
6802c2393203
Implementing ProfileView (Watchlists)
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
377
diff
changeset
|
53 |
390
6303385b3629
Companies added to watchlists now are correctly updated
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
389
diff
changeset
|
54 private func prepareUrl(isInitRequest: Bool) { |
388
79c39987aaa4
Implementing Watchlists in ProfileView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
387
diff
changeset
|
55 if watchlistCompanies.isEmpty { |
79c39987aaa4
Implementing Watchlists in ProfileView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
387
diff
changeset
|
56 profile.showView = true |
79c39987aaa4
Implementing Watchlists in ProfileView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
387
diff
changeset
|
57 } else { |
79c39987aaa4
Implementing Watchlists in ProfileView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
387
diff
changeset
|
58 let symbols = watchlistCompanies.map { $0.symbol } // Get symbols in watchlists |
79c39987aaa4
Implementing Watchlists in ProfileView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
387
diff
changeset
|
59 var url = "https://api.lazybear.app/profile/type=init/symbols=" |
378
6802c2393203
Implementing ProfileView (Watchlists)
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
377
diff
changeset
|
60 |
388
79c39987aaa4
Implementing Watchlists in ProfileView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
387
diff
changeset
|
61 var counter = 0 |
79c39987aaa4
Implementing Watchlists in ProfileView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
387
diff
changeset
|
62 for symbol in symbols { |
79c39987aaa4
Implementing Watchlists in ProfileView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
387
diff
changeset
|
63 counter += 1 |
79c39987aaa4
Implementing Watchlists in ProfileView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
387
diff
changeset
|
64 if counter == 1 { |
79c39987aaa4
Implementing Watchlists in ProfileView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
387
diff
changeset
|
65 url += symbol |
79c39987aaa4
Implementing Watchlists in ProfileView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
387
diff
changeset
|
66 } else { |
79c39987aaa4
Implementing Watchlists in ProfileView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
387
diff
changeset
|
67 url += ",\(symbol)" |
79c39987aaa4
Implementing Watchlists in ProfileView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
387
diff
changeset
|
68 } |
377
d01859776fe6
ProfileView updated
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
375
diff
changeset
|
69 } |
390
6303385b3629
Companies added to watchlists now are correctly updated
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
389
diff
changeset
|
70 profile.request(url, isInitRequest: isInitRequest) |
6303385b3629
Companies added to watchlists now are correctly updated
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
389
diff
changeset
|
71 } |
6303385b3629
Companies added to watchlists now are correctly updated
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
389
diff
changeset
|
72 } |
6303385b3629
Companies added to watchlists now are correctly updated
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
389
diff
changeset
|
73 |
6303385b3629
Companies added to watchlists now are correctly updated
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
389
diff
changeset
|
74 /* |
6303385b3629
Companies added to watchlists now are correctly updated
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
389
diff
changeset
|
75 If Core Data changes, companies is not updated because the API request is not called -> |
6303385b3629
Companies added to watchlists now are correctly updated
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
389
diff
changeset
|
76 Check if symbols.count (Core Data) is equal to filteredCompanies -> if not -> call API |
6303385b3629
Companies added to watchlists now are correctly updated
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
389
diff
changeset
|
77 */ |
6303385b3629
Companies added to watchlists now are correctly updated
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
389
diff
changeset
|
78 private func updateRows(_ numberOfCoreDataCompanies: Int, _ numberOfApiRequestedCompanies: Int) { |
6303385b3629
Companies added to watchlists now are correctly updated
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
389
diff
changeset
|
79 if numberOfCoreDataCompanies != numberOfApiRequestedCompanies { |
6303385b3629
Companies added to watchlists now are correctly updated
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
389
diff
changeset
|
80 prepareUrl(isInitRequest: true) // Call API |
377
d01859776fe6
ProfileView updated
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
375
diff
changeset
|
81 } |
d01859776fe6
ProfileView updated
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
375
diff
changeset
|
82 } |
d01859776fe6
ProfileView updated
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
375
diff
changeset
|
83 } |
d01859776fe6
ProfileView updated
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
375
diff
changeset
|
84 |
d01859776fe6
ProfileView updated
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
375
diff
changeset
|
85 struct ProfileView_Previews: PreviewProvider { |
d01859776fe6
ProfileView updated
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
375
diff
changeset
|
86 static var previews: some View { |
d01859776fe6
ProfileView updated
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
375
diff
changeset
|
87 ProfileView() |
d01859776fe6
ProfileView updated
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
375
diff
changeset
|
88 } |
d01859776fe6
ProfileView updated
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
375
diff
changeset
|
89 } |