Mercurial > public > lazybear
annotate LazyBear/Views/Profile/ProfileView.swift @ 391:8ec37b2baafd
Implementing CreateNewWatchlist
author | Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com> |
---|---|
date | Fri, 30 Apr 2021 20:25:52 +0200 |
parents | 6303385b3629 |
children | 13f3578def61 |
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> |
391
8ec37b2baafd
Implementing CreateNewWatchlist
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
390
diff
changeset
|
15 |
8ec37b2baafd
Implementing CreateNewWatchlist
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
390
diff
changeset
|
16 @State private var showCreateNewWatchlist = false |
377
d01859776fe6
ProfileView updated
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
375
diff
changeset
|
17 |
d01859776fe6
ProfileView updated
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
375
diff
changeset
|
18 var body: some View { |
378
6802c2393203
Implementing ProfileView (Watchlists)
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
377
diff
changeset
|
19 if profile.showView { |
6802c2393203
Implementing ProfileView (Watchlists)
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
377
diff
changeset
|
20 NavigationView { |
6802c2393203
Implementing ProfileView (Watchlists)
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
377
diff
changeset
|
21 List { |
391
8ec37b2baafd
Implementing CreateNewWatchlist
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
390
diff
changeset
|
22 |
8ec37b2baafd
Implementing CreateNewWatchlist
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
390
diff
changeset
|
23 // 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
|
24 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
|
25 |
378
6802c2393203
Implementing ProfileView (Watchlists)
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
377
diff
changeset
|
26 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
|
27 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
|
28 |
378
6802c2393203
Implementing ProfileView (Watchlists)
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
377
diff
changeset
|
29 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
|
30 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
|
31 StockRow(listName: watchlist, |
c206bd0bdb4e
Implementing RenameSheet.swift
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
379
diff
changeset
|
32 list: filteredCompanies, |
c206bd0bdb4e
Implementing RenameSheet.swift
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
379
diff
changeset
|
33 intradayPrices: profile.data.intradayPrices, |
c206bd0bdb4e
Implementing RenameSheet.swift
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
379
diff
changeset
|
34 addOnDelete: true |
c206bd0bdb4e
Implementing RenameSheet.swift
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
379
diff
changeset
|
35 ) |
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
|
36 .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
|
37 .listRowInsets(EdgeInsets()) |
378
6802c2393203
Implementing ProfileView (Watchlists)
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
377
diff
changeset
|
38 } |
6802c2393203
Implementing ProfileView (Watchlists)
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
377
diff
changeset
|
39 } |
6802c2393203
Implementing ProfileView (Watchlists)
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
377
diff
changeset
|
40 } |
6802c2393203
Implementing ProfileView (Watchlists)
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
377
diff
changeset
|
41 .navigationTitle("My profile") |
6802c2393203
Implementing ProfileView (Watchlists)
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
377
diff
changeset
|
42 .navigationBarTitleDisplayMode(.inline) |
388
79c39987aaa4
Implementing Watchlists in ProfileView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
387
diff
changeset
|
43 .toolbar { |
79c39987aaa4
Implementing Watchlists in ProfileView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
387
diff
changeset
|
44 ToolbarItem(placement: .navigationBarTrailing) { |
391
8ec37b2baafd
Implementing CreateNewWatchlist
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
390
diff
changeset
|
45 Button(action: { showCreateNewWatchlist = true }) { |
388
79c39987aaa4
Implementing Watchlists in ProfileView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
387
diff
changeset
|
46 Image(systemName: "plus") |
79c39987aaa4
Implementing Watchlists in ProfileView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
387
diff
changeset
|
47 } |
79c39987aaa4
Implementing Watchlists in ProfileView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
387
diff
changeset
|
48 } |
79c39987aaa4
Implementing Watchlists in ProfileView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
387
diff
changeset
|
49 } |
377
d01859776fe6
ProfileView updated
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
375
diff
changeset
|
50 } |
391
8ec37b2baafd
Implementing CreateNewWatchlist
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
390
diff
changeset
|
51 .sheet(isPresented: $showCreateNewWatchlist) { |
8ec37b2baafd
Implementing CreateNewWatchlist
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
390
diff
changeset
|
52 CreateNewWatchlist() |
8ec37b2baafd
Implementing CreateNewWatchlist
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
390
diff
changeset
|
53 } |
378
6802c2393203
Implementing ProfileView (Watchlists)
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
377
diff
changeset
|
54 } else { |
6802c2393203
Implementing ProfileView (Watchlists)
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
377
diff
changeset
|
55 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
|
56 .onAppear { prepareUrl(isInitRequest: true) } |
378
6802c2393203
Implementing ProfileView (Watchlists)
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
377
diff
changeset
|
57 } |
6802c2393203
Implementing ProfileView (Watchlists)
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
377
diff
changeset
|
58 } |
6802c2393203
Implementing ProfileView (Watchlists)
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
377
diff
changeset
|
59 |
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
|
60 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
|
61 if watchlistCompanies.isEmpty { |
79c39987aaa4
Implementing Watchlists in ProfileView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
387
diff
changeset
|
62 profile.showView = true |
79c39987aaa4
Implementing Watchlists in ProfileView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
387
diff
changeset
|
63 } else { |
79c39987aaa4
Implementing Watchlists in ProfileView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
387
diff
changeset
|
64 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
|
65 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
|
66 |
388
79c39987aaa4
Implementing Watchlists in ProfileView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
387
diff
changeset
|
67 var counter = 0 |
79c39987aaa4
Implementing Watchlists in ProfileView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
387
diff
changeset
|
68 for symbol in symbols { |
79c39987aaa4
Implementing Watchlists in ProfileView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
387
diff
changeset
|
69 counter += 1 |
79c39987aaa4
Implementing Watchlists in ProfileView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
387
diff
changeset
|
70 if counter == 1 { |
79c39987aaa4
Implementing Watchlists in ProfileView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
387
diff
changeset
|
71 url += symbol |
79c39987aaa4
Implementing Watchlists in ProfileView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
387
diff
changeset
|
72 } else { |
79c39987aaa4
Implementing Watchlists in ProfileView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
387
diff
changeset
|
73 url += ",\(symbol)" |
79c39987aaa4
Implementing Watchlists in ProfileView
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
387
diff
changeset
|
74 } |
377
d01859776fe6
ProfileView updated
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
375
diff
changeset
|
75 } |
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
|
76 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
|
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 } |
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 |
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 /* |
6303385b3629
Companies added to watchlists now are correctly updated
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
389
diff
changeset
|
81 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
|
82 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
|
83 */ |
6303385b3629
Companies added to watchlists now are correctly updated
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
389
diff
changeset
|
84 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
|
85 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
|
86 prepareUrl(isInitRequest: true) // Call API |
377
d01859776fe6
ProfileView updated
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
375
diff
changeset
|
87 } |
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 } |
d01859776fe6
ProfileView updated
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
375
diff
changeset
|
90 |
d01859776fe6
ProfileView updated
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
375
diff
changeset
|
91 struct ProfileView_Previews: PreviewProvider { |
d01859776fe6
ProfileView updated
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
375
diff
changeset
|
92 static var previews: some View { |
d01859776fe6
ProfileView updated
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
375
diff
changeset
|
93 ProfileView() |
d01859776fe6
ProfileView updated
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
375
diff
changeset
|
94 } |
d01859776fe6
ProfileView updated
Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
parents:
375
diff
changeset
|
95 } |