comparison LazyBear/Views/Profile/ProfileView.swift @ 399:5c99883c7964

Implementing networking in CompanyView
author Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
date Sat, 15 May 2021 12:31:40 +0200
parents 4c90e5b18632
children f843c6382529
comparison
equal deleted inserted replaced
398:933546fa5651 399:5c99883c7964
13 @Environment(\.managedObjectContext) private var moc 13 @Environment(\.managedObjectContext) private var moc
14 @FetchRequest(entity: WatchlistCompany.entity(), sortDescriptors: []) 14 @FetchRequest(entity: WatchlistCompany.entity(), sortDescriptors: [])
15 var watchlistCompanies: FetchedResults<WatchlistCompany> 15 var watchlistCompanies: FetchedResults<WatchlistCompany>
16 16
17 @State private var showCreateNewWatchlist = false 17 @State private var showCreateNewWatchlist = false
18
19 // Set recurrent price request
20 @State private var timer = Timer.publish(every: 10, on: .main, in: .common).autoconnect()
18 21
19 var body: some View { 22 var body: some View {
20 if profile.showView { 23 if profile.showView {
21 NavigationView { 24 NavigationView {
22 List { 25 List {
35 .listRowInsets(EdgeInsets()) 38 .listRowInsets(EdgeInsets())
36 .onAppear { // Refresh API requested companies when Core Data changes 39 .onAppear { // Refresh API requested companies when Core Data changes
37 refreshList() 40 refreshList()
38 } 41 }
39 } 42 }
43 .onAppear { self.timer = Timer.publish(every: 10, on: .main, in: .common).autoconnect() } // Start timer
44 .onReceive(timer) { _ in prepareUrl(isInitRequest: false) }
45 .onDisappear { self.timer.upstream.connect().cancel() } // Stop timer
40 .navigationTitle("My profile") 46 .navigationTitle("My profile")
41 .navigationBarTitleDisplayMode(.inline) 47 .navigationBarTitleDisplayMode(.inline)
42 .toolbar { 48 .toolbar {
43 ToolbarItem(placement: .navigationBarTrailing) { 49 ToolbarItem(placement: .navigationBarTrailing) {
44 Button(action: { showCreateNewWatchlist = true }) { 50 Button(action: { showCreateNewWatchlist = true }) {
63 private func prepareUrl(isInitRequest: Bool) { 69 private func prepareUrl(isInitRequest: Bool) {
64 if watchlistCompanies.isEmpty { 70 if watchlistCompanies.isEmpty {
65 profile.showView = true 71 profile.showView = true
66 } else { 72 } else {
67 let symbols = watchlistCompanies.map { $0.symbol } 73 let symbols = watchlistCompanies.map { $0.symbol }
68 var url = "https://api.lazybear.app/profile/type=init/symbols=" 74 var typeRequest = "streaming"
75 if isInitRequest { typeRequest = "init" }
76 var url = "https://api.lazybear.app/profile/type=\(typeRequest)/symbols="
69 77
70 var counter = 0 78 var counter = 0
71 for symbol in symbols { 79 for symbol in symbols {
72 counter += 1 80 counter += 1
73 81