comparison LazyBear/Views/Profile/ProfileView.swift @ 401:f843c6382529

Add Enumeration to Networks files
author Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
date Thu, 20 May 2021 21:04:49 +0200
parents 5c99883c7964
children fd8df65927e9
comparison
equal deleted inserted replaced
400:6055a867d2b6 401:f843c6382529
39 .onAppear { // Refresh API requested companies when Core Data changes 39 .onAppear { // Refresh API requested companies when Core Data changes
40 refreshList() 40 refreshList()
41 } 41 }
42 } 42 }
43 .onAppear { self.timer = Timer.publish(every: 10, on: .main, in: .common).autoconnect() } // Start timer 43 .onAppear { self.timer = Timer.publish(every: 10, on: .main, in: .common).autoconnect() } // Start timer
44 .onReceive(timer) { _ in prepareUrl(isInitRequest: false) } 44 .onReceive(timer) { _ in prepareUrl(.streaming) }
45 .onDisappear { self.timer.upstream.connect().cancel() } // Stop timer 45 .onDisappear { self.timer.upstream.connect().cancel() } // Stop timer
46 .navigationTitle("My profile") 46 .navigationTitle("My profile")
47 .navigationBarTitleDisplayMode(.inline) 47 .navigationBarTitleDisplayMode(.inline)
48 .toolbar { 48 .toolbar {
49 ToolbarItem(placement: .navigationBarTrailing) { 49 ToolbarItem(placement: .navigationBarTrailing) {
57 WatchlistCreator() 57 WatchlistCreator()
58 .environment(\.managedObjectContext, self.moc) 58 .environment(\.managedObjectContext, self.moc)
59 } 59 }
60 } else { 60 } else {
61 ProgressView() 61 ProgressView()
62 .onAppear { prepareUrl(isInitRequest: true) } 62 .onAppear { prepareUrl(.initial) }
63 } 63 }
64 } 64 }
65 65
66 /* 66 /*
67 Get symbols in watchlists -> Prepare url -> Request 67 Get symbols in watchlists -> Prepare url -> Request
68 */ 68 */
69 private func prepareUrl(isInitRequest: Bool) { 69 private func prepareUrl(_ requestType: RequestType) {
70 if watchlistCompanies.isEmpty { 70 let symbols = watchlistCompanies.map { $0.symbol }
71 profile.showView = true 71
72 } else { 72 var symbolString = ""
73 let symbols = watchlistCompanies.map { $0.symbol } 73 for (index, symbol) in symbols.enumerated() {
74 var typeRequest = "streaming" 74 if index == 0 {
75 if isInitRequest { typeRequest = "init" } 75 symbolString += symbol
76 var url = "https://api.lazybear.app/profile/type=\(typeRequest)/symbols=" 76 } else {
77 77 symbolString += ",\(symbol)"
78 var counter = 0
79 for symbol in symbols {
80 counter += 1
81
82 if counter == 1 {
83 url += symbol
84 } else {
85 url += ",\(symbol)"
86 }
87 } 78 }
88 profile.request(url, isInitRequest: isInitRequest) 79 }
80
81 switch requestType {
82 case .initial:
83 let url = "https://api.lazybear.app/profile/type=init/symbols=\(symbolString)"
84 profile.request(url, .initial)
85
86 default:
87 let url = "https://api.lazybear.app/profile/type=streaming/symbols=\(symbolString)"
88 profile.request(url, .streaming)
89 } 89 }
90 } 90 }
91
91 92
92 /* 93 /*
93 When a company is added to a watchlist or a new watchlist is created -> call function 94 When a company is added to a watchlist or a new watchlist is created -> call function
94 to make the API request and refresh correctly the list 95 to make the API request and refresh correctly the list
95 */ 96 */
96 private func refreshList() { 97 private func refreshList() {
97 print("Companies in watchlist -> \(watchlistCompanies.count)") 98 print("Companies in watchlist -> \(watchlistCompanies.count)")
98 print("Companies requested -> \(profile.data.quotes!.count)") 99 print("Companies requested -> \(profile.data.quotes!.count)")
99 100
100 if profile.data.quotes!.count < watchlistCompanies.count { 101 if profile.data.quotes!.count < watchlistCompanies.count {
101 prepareUrl(isInitRequest: true) 102 prepareUrl(.initial)
102 } 103 }
103 } 104 }
104 } 105 }
105 106
106 struct ProfileView_Previews: PreviewProvider { 107 struct ProfileView_Previews: PreviewProvider {