Mercurial > public > lazybear
changeset 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 | 933546fa5651 |
children | 6055a867d2b6 |
files | LazyBear.xcodeproj/project.pbxproj LazyBear.xcodeproj/project.xcworkspace/xcuserdata/dennis.xcuserdatad/UserInterfaceState.xcuserstate LazyBear/Global Models/LatestNewsModel.swift LazyBear/Views/Company/Chart.swift LazyBear/Views/Company/CompanyView.swift LazyBear/Views/Company/Helpers/Chart.swift LazyBear/Views/Company/Helpers/CompanyHeader.swift LazyBear/Views/Company/Helpers/NewsRow.swift LazyBear/Views/Company/Helpers/ViewSelector.swift LazyBear/Views/Company/Helpers/ViewType.swift LazyBear/Views/Company/Networking/ChartResponse.swift LazyBear/Views/Company/Networking/Company.swift LazyBear/Views/Home/HomeView.swift LazyBear/Views/Profile/ProfileView.swift |
diffstat | 14 files changed, 306 insertions(+), 100 deletions(-) [+] |
line wrap: on
line diff
--- a/LazyBear.xcodeproj/project.pbxproj Sun May 09 00:07:44 2021 +0200 +++ b/LazyBear.xcodeproj/project.pbxproj Sat May 15 12:31:40 2021 +0200 @@ -31,6 +31,10 @@ 955E73392623568F005652FF /* Home.swift in Sources */ = {isa = PBXBuildFile; fileRef = 955E73382623568F005652FF /* Home.swift */; }; 955E733C262356F3005652FF /* HomeResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 955E733B262356F3005652FF /* HomeResponse.swift */; }; 95606D082647005B0072C02C /* StockCharts in Frameworks */ = {isa = PBXBuildFile; productRef = 95606D072647005B0072C02C /* StockCharts */; }; + 95613AD9264FC5A900D4CE8F /* Company.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95613AD8264FC5A900D4CE8F /* Company.swift */; }; + 95613ADD264FC6A200D4CE8F /* ChartResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95613ADC264FC6A200D4CE8F /* ChartResponse.swift */; }; + 95613ADF264FC6FD00D4CE8F /* LatestNewsModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95613ADE264FC6FD00D4CE8F /* LatestNewsModel.swift */; }; + 95613AE1264FD34100D4CE8F /* NewsRow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95613AE0264FD34100D4CE8F /* NewsRow.swift */; }; 9562404C263C766D00C6C511 /* WatchlistCreator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9562404B263C766D00C6C511 /* WatchlistCreator.swift */; }; 9562404E263C7D8800C6C511 /* WatchlistCreatorClass.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9562404D263C7D8800C6C511 /* WatchlistCreatorClass.swift */; }; 95629DA02645298E007AF020 /* Profile.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95629D9F2645298E007AF020 /* Profile.swift */; }; @@ -66,7 +70,7 @@ 95E31C142647363800106B98 /* Chart.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95E31C132647363800106B98 /* Chart.swift */; }; 95E31C16264736BE00106B98 /* DatePicker.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95E31C15264736BE00106B98 /* DatePicker.swift */; }; 95E31C1826473A4D00106B98 /* CompanyHeader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95E31C1726473A4D00106B98 /* CompanyHeader.swift */; }; - 95E31C1C26473B5100106B98 /* ViewType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95E31C1B26473B5100106B98 /* ViewType.swift */; }; + 95E31C1C26473B5100106B98 /* ViewSelector.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95E31C1B26473B5100106B98 /* ViewSelector.swift */; }; 95FBE0DC2619CA7200440386 /* ProfileView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95FBE0DB2619CA7200440386 /* ProfileView.swift */; }; /* End PBXBuildFile section */ @@ -115,6 +119,10 @@ 9550444B26111FED000E0BCB /* StockItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StockItem.swift; sourceTree = "<group>"; }; 955E73382623568F005652FF /* Home.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Home.swift; sourceTree = "<group>"; }; 955E733B262356F3005652FF /* HomeResponse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeResponse.swift; sourceTree = "<group>"; }; + 95613AD8264FC5A900D4CE8F /* Company.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Company.swift; sourceTree = "<group>"; }; + 95613ADC264FC6A200D4CE8F /* ChartResponse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChartResponse.swift; sourceTree = "<group>"; }; + 95613ADE264FC6FD00D4CE8F /* LatestNewsModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LatestNewsModel.swift; sourceTree = "<group>"; }; + 95613AE0264FD34100D4CE8F /* NewsRow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NewsRow.swift; sourceTree = "<group>"; }; 9562404A263C72E800C6C511 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; }; 9562404B263C766D00C6C511 /* WatchlistCreator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WatchlistCreator.swift; sourceTree = "<group>"; }; 9562404D263C7D8800C6C511 /* WatchlistCreatorClass.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WatchlistCreatorClass.swift; sourceTree = "<group>"; }; @@ -153,7 +161,7 @@ 95E31C132647363800106B98 /* Chart.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Chart.swift; sourceTree = "<group>"; }; 95E31C15264736BE00106B98 /* DatePicker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DatePicker.swift; sourceTree = "<group>"; }; 95E31C1726473A4D00106B98 /* CompanyHeader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CompanyHeader.swift; sourceTree = "<group>"; }; - 95E31C1B26473B5100106B98 /* ViewType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewType.swift; sourceTree = "<group>"; }; + 95E31C1B26473B5100106B98 /* ViewSelector.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewSelector.swift; sourceTree = "<group>"; }; 95FBE0DB2619CA7200440386 /* ProfileView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProfileView.swift; sourceTree = "<group>"; }; /* End PBXFileReference section */ @@ -274,6 +282,7 @@ 95A07F6B26305AC6009865AA /* QuoteModel.swift */, 95A07F7026305AD5009865AA /* SectorPerformanceModel.swift */, 95A07F7526305AE3009865AA /* TradingDatesModel.swift */, + 95613ADE264FC6FD00D4CE8F /* LatestNewsModel.swift */, ); path = "Global Models"; sourceTree = "<group>"; @@ -433,6 +442,7 @@ isa = PBXGroup; children = ( 95E31C0C26472CA000106B98 /* CompanyView.swift */, + 95E31C132647363800106B98 /* Chart.swift */, 95E31C0E26472CB600106B98 /* Helpers */, 95E31C0F26472CC000106B98 /* Networking */, ); @@ -442,10 +452,10 @@ 95E31C0E26472CB600106B98 /* Helpers */ = { isa = PBXGroup; children = ( - 95E31C1B26473B5100106B98 /* ViewType.swift */, + 95E31C1B26473B5100106B98 /* ViewSelector.swift */, 95E31C1726473A4D00106B98 /* CompanyHeader.swift */, - 95E31C132647363800106B98 /* Chart.swift */, 95E31C15264736BE00106B98 /* DatePicker.swift */, + 95613AE0264FD34100D4CE8F /* NewsRow.swift */, ); path = Helpers; sourceTree = "<group>"; @@ -453,6 +463,8 @@ 95E31C0F26472CC000106B98 /* Networking */ = { isa = PBXGroup; children = ( + 95613AD8264FC5A900D4CE8F /* Company.swift */, + 95613ADC264FC6A200D4CE8F /* ChartResponse.swift */, ); path = Networking; sourceTree = "<group>"; @@ -633,11 +645,14 @@ files = ( 950C36E3260FB6180081CF53 /* HapticsManager.swift in Sources */, 95FBE0DC2619CA7200440386 /* ProfileView.swift in Sources */, - 95E31C1C26473B5100106B98 /* ViewType.swift in Sources */, + 95E31C1C26473B5100106B98 /* ViewSelector.swift in Sources */, 95A5186A26185AAB0002D27C /* GenericRequest.swift in Sources */, 95A5188626186F590002D27C /* PriceView.swift in Sources */, + 95613AE1264FD34100D4CE8F /* NewsRow.swift in Sources */, + 95613ADD264FC6A200D4CE8F /* ChartResponse.swift in Sources */, 9550444926111FC9000E0BCB /* StockRow.swift in Sources */, 9550444326111E7A000E0BCB /* SectorRow.swift in Sources */, + 95613AD9264FC5A900D4CE8F /* Company.swift in Sources */, 95721DB826278EC100EC527B /* CurrencyListItem.swift in Sources */, 950272CD2635AACD003E779D /* ToolbarMenu.swift in Sources */, 9550443A26111B2B000E0BCB /* HomeView.swift in Sources */, @@ -659,6 +674,7 @@ 95E31C1826473A4D00106B98 /* CompanyHeader.swift in Sources */, 95A07F6226305A9B009865AA /* CurrencyModel.swift in Sources */, 9529947E2629CA3E005F0AB0 /* Search.swift in Sources */, + 95613ADF264FC6FD00D4CE8F /* LatestNewsModel.swift in Sources */, 950D0E292618AEC800D17AD7 /* SearchedCompanyItem.swift in Sources */, 955E733C262356F3005652FF /* HomeResponse.swift in Sources */, 95BD2FB326341D36008B6752 /* BlurBackground.swift in Sources */,
Binary file LazyBear.xcodeproj/project.xcworkspace/xcuserdata/dennis.xcuserdatad/UserInterfaceState.xcuserstate has changed
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LazyBear/Global Models/LatestNewsModel.swift Sat May 15 12:31:40 2021 +0200 @@ -0,0 +1,17 @@ +// +// LatestNewsModel.swift +// LazyBear +// +// Created by Dennis Concepción Martín on 15/5/21. +// + +import SwiftUI + +struct LatestNewsModel: Codable, Hashable { + var datetime: Int + var headline: String + var image: String + var source: String + var summary: String + var url: String +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LazyBear/Views/Company/Chart.swift Sat May 15 12:31:40 2021 +0200 @@ -0,0 +1,86 @@ +// +// Chart.swift +// LazyBear +// +// Created by Dennis Concepción Martín on 8/5/21. +// + +import SwiftUI +import StockCharts + +struct Chart: View { + var chartData: ChartResponse + var symbol: String + + // Date picker + var ranges = ["1D", "5D", "1M", "3M", "6M", "1Y", "5Y"] + @State private var selectedRange = "3M" + + + var body: some View { + let priceViewStyle = PriceViewStyle( + alignment: .leading, + priceFont: .headline, + priceFontWeight: .semibold, + percentFont: .body, + percentFontWeight: .medium + ) + + VStack { + DatePicker(ranges: ranges, selectedRange: $selectedRange) + + RoundedRectangle(cornerRadius: 15) + .foregroundColor(Color(.secondarySystemBackground)) + .frame(height: 270) + .overlay( + VStack { + HStack { + if let quote = chartData.quote![symbol.uppercased()] { + let latestPrice = quote.latestPrice ?? 0 + let changePercent = quote.changePercent ?? 0 + PriceView(latestPrice: latestPrice, changePercent: changePercent, style: priceViewStyle) + } + Spacer() + } + .padding([.top, .leading, .trailing]) + + if let intradayPrices = chartData.intradayPrices![symbol.uppercased()] { + if let prices = intradayPrices.compactMap { $0.open } { + LineChartView(data: prices, + dates: nil, + hours: nil, + dragGesture: true + ) + .padding(.bottom) + } + } + } + ) + + if let latestNews = chartData.latestNews { + ForEach(latestNews, id: \.self) { new in + NewsRow(new: new) + } + } + } + } +} + +struct Chart_Previews: PreviewProvider { + static var previews: some View { + Chart( + chartData: ChartResponse( + intradayPrices: ["AAPL": [IntradayPriceModel(open: 120.3)]], + quote: ["aapl": QuoteModel(changePercent: 0.03, companyName: "Apple Inc", latestPrice: 120.3)], + latestNews: [LatestNewsModel( + datetime: 1621037430000, + headline: "Chaos Monkeys' author calls Apple's statement on his departure defamatory", + image: "https://cloud.iexapis.com/v1/news/image/99abeb99-6d9e-47c8-ae7b-53404eacccec", + source: "Investing.com", + summary: "https://www.investing.com/news/stock-market-news", + url: "https://cloud.iexapis.com/v1/news/article/99abeb99-6d9e-47c8-ae7b-53404eacccec")]), + + symbol: "aapl" + ) + } +}
--- a/LazyBear/Views/Company/CompanyView.swift Sun May 09 00:07:44 2021 +0200 +++ b/LazyBear/Views/Company/CompanyView.swift Sat May 15 12:31:40 2021 +0200 @@ -11,20 +11,37 @@ struct CompanyView: View { var symbol: String - // Date picker - var ranges = ["1D", "5D", "1M", "3M", "6M", "1Y", "5Y"] - @State private var selectedRange = "Red" - + @ObservedObject var company = Company() @ObservedObject var viewSelector = ViewSelector() @State private var showViewSelector = false + // Set recurrent price request + @State private var timer = Timer.publish(every: 10, on: .main, in: .common).autoconnect() + var body: some View { NavigationView { ScrollView { VStack { CompanyHeader(symbol: symbol, showViewSelector: $showViewSelector) - DatePicker(ranges: ranges, selectedRange: $selectedRange) - Chart() + + // <--- Chart View ---> + if viewSelector.views["chart"]! { + let url = "https://api.lazybear.app/company/chart/type=init/symbol=\(symbol)" + + if company.showChartView { + Chart(chartData: company.chartData, symbol: symbol) + .onAppear { self.timer = Timer.publish(every: 10, on: .main, in: .common).autoconnect() } // Start timer + .onDisappear { self.timer.upstream.connect().cancel() } // Stop timer + .onReceive(timer) { _ in + let url = "https://api.lazybear.app/company/chart/type=streaming/symbol=\(symbol)" + company.request(url, isInitRequest: false, "chart") } // Receive timer notification + } + else { + ProgressView() + .onAppear { company.request(url, isInitRequest: true, "chart") } } + + // ---> Chart View <--- + } } .padding() } @@ -32,8 +49,8 @@ .navigationBarTitleDisplayMode(.inline) } .actionSheet(isPresented: $showViewSelector) { - ActionSheet(title: Text("Change background"), message: Text("Select a new color"), buttons: [ - .default(Text("Chart")) { viewSelector.showView(.chart) }, + ActionSheet(title: Text("Select an option"), buttons: [ + .default(Text("Chart & News")) { viewSelector.showView(.chart) }, .cancel() ]) }
--- a/LazyBear/Views/Company/Helpers/Chart.swift Sun May 09 00:07:44 2021 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,46 +0,0 @@ -// -// Chart.swift -// LazyBear -// -// Created by Dennis Concepción Martín on 8/5/21. -// - -import SwiftUI -import StockCharts - -struct Chart: View { - var body: some View { - let priceViewStyle = PriceViewStyle( - alignment: .leading, - priceFont: .title2, - priceFontWeight: .semibold, - percentFont: .title3, - percentFontWeight: .semibold - ) - - RoundedRectangle(cornerRadius: 15) - .foregroundColor(Color(.secondarySystemBackground)) - .frame(height: 300) - .overlay( - VStack { - HStack { - PriceView(latestPrice: 120.30, changePercent: 0.03, style: priceViewStyle) - Spacer() - } - .padding([.top, .leading, .trailing]) - - LineChartView(data: [120.3, 120.4, 121.0, 122.0], - dates: nil, - hours: nil, - dragGesture: true) - .padding(.bottom) - } - ) - } -} - -struct Chart_Previews: PreviewProvider { - static var previews: some View { - Chart() - } -}
--- a/LazyBear/Views/Company/Helpers/CompanyHeader.swift Sun May 09 00:07:44 2021 +0200 +++ b/LazyBear/Views/Company/Helpers/CompanyHeader.swift Sat May 15 12:31:40 2021 +0200 @@ -19,7 +19,10 @@ Spacer() - Button("Views", action: { showViewSelector = true }) + Button(action: { showViewSelector = true }) { + Text("Views ") + Image(systemName: "chevron.down") + } } } }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LazyBear/Views/Company/Helpers/NewsRow.swift Sat May 15 12:31:40 2021 +0200 @@ -0,0 +1,63 @@ +// +// NewsRow.swift +// LazyBear +// +// Created by Dennis Concepción Martín on 15/5/21. +// + +import SwiftUI + +struct NewsRow: View { + var new: LatestNewsModel + + var body: some View { + VStack(alignment: .leading) { + Text(new.source.uppercased()) + .font(.caption) + .opacity(0.5) + + Text(new.headline) + .font(.headline) + + Text(new.summary) + .opacity(0.5) + .font(.subheadline) + .lineLimit(1) + .padding(.bottom, 5) + + let humanDate = convertDate() + Text("\(humanDate) ago") + .font(.caption2) + .opacity(0.5) + + Divider() + } + } + + /* + Convert Epoch time to human readable + */ + private func convertDate() -> String { + let now = Date() // Current date + // Time when the article was published. Divide new.datetime by 1,000 because + // TimeInterval() function must be in seconds, not in miliseconds + let articlePublished = Date(timeIntervalSince1970: TimeInterval(new.datetime)/1000) + let formatter = DateComponentsFormatter() + formatter.unitsStyle = .full + let humanDate = formatter.string(from: articlePublished, to: now)! + + return humanDate + } +} + +struct NewsRow_Previews: PreviewProvider { + static var previews: some View { + NewsRow(new: LatestNewsModel( + datetime: 1621037430000, + headline: "Chaos Monkeys' author calls Apple's statement on his departure defamatory", + image: "https://cloud.iexapis.com/v1/news/image/99abeb99-6d9e-47c8-ae7b-53404eacccec", + source: "Investing.com", + summary: "https://www.investing.com/news/stock-market-news", + url: "https://cloud.iexapis.com/v1/news/article/99abeb99-6d9e-47c8-ae7b-53404eacccec")) + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LazyBear/Views/Company/Helpers/ViewSelector.swift Sat May 15 12:31:40 2021 +0200 @@ -0,0 +1,36 @@ +// +// ViewType.swift +// LazyBear +// +// Created by Dennis Concepción Martín on 8/5/21. +// + +import SwiftUI + + +class ViewSelector: ObservableObject { + @Published var views = [ + "chart": true + ] + + enum ViewType { + case chart + } + + func showView(_ viewType: ViewType) { + switch viewType { + case .chart: + toogleVariables() + views["chart"] = true + } + } + + /* + Change to false views + */ + private func toogleVariables() { + for view in views.keys { + views[view] = false + } + } +}
--- a/LazyBear/Views/Company/Helpers/ViewType.swift Sun May 09 00:07:44 2021 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,35 +0,0 @@ -// -// ViewType.swift -// LazyBear -// -// Created by Dennis Concepción Martín on 8/5/21. -// - -import SwiftUI - - -class ViewSelector: ObservableObject { - @Published var showChartView = false - - enum ViewType { - case chart - } - - func showView(_ viewType: ViewType) { - switch viewType { - case .chart: - toogleVariables() - } - } - - private func toogleVariables() { - // Testing this //// - let mirror = Mirror(reflecting: self) - - for child in mirror.children { -// let value = child.value -// child.label = value - } - //// - } -}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LazyBear/Views/Company/Networking/ChartResponse.swift Sat May 15 12:31:40 2021 +0200 @@ -0,0 +1,20 @@ +// +// ChartResponse.swift +// LazyBear +// +// Created by Dennis Concepción Martín on 15/5/21. +// + +import SwiftUI + +struct ChartResponse: Codable { + var intradayPrices: [String: [IntradayPriceModel]]? + var quote: [String: QuoteModel]? + var latestNews: [LatestNewsModel]? + + private enum CodingKeys : String, CodingKey { + case intradayPrices = "intraday_prices" + case quote + case latestNews = "latest_news" + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LazyBear/Views/Company/Networking/Company.swift Sat May 15 12:31:40 2021 +0200 @@ -0,0 +1,23 @@ +// +// Company.swift +// LazyBear +// +// Created by Dennis Concepción Martín on 15/5/21. +// + +import SwiftUI + +class Company: ObservableObject { + @Published var showChartView = false + @Published var chartData = ChartResponse() + + func request(_ url: String, isInitRequest: Bool, _ view: String) { + if view == "chart" { + genericRequest(url: url, model: ChartResponse.self) { response in + if isInitRequest { self.chartData = response } // If is the first request -> init() + else { self.chartData.quote = response.quote } // If not, request streaming data (without intradayPrices) + self.showChartView = true + } + } + } +}
--- a/LazyBear/Views/Home/HomeView.swift Sun May 09 00:07:44 2021 +0200 +++ b/LazyBear/Views/Home/HomeView.swift Sat May 15 12:31:40 2021 +0200 @@ -50,7 +50,8 @@ .listRowInsets(EdgeInsets()) } } - .onReceive(timer) { _ in home.request("https://api.lazybear.app/home/type=streaming", isInitRequest: false) } + .onAppear { self.timer = Timer.publish(every: 10, on: .main, in: .common).autoconnect() } // Start timer + .onReceive(timer) { _ in home.request("https://api.lazybear.app/home/type=streaming", isInitRequest: false) } // Receive timer notification .onDisappear { self.timer.upstream.connect().cancel() } // Stop timer .navigationTitle("\(dueDate, formatter: Self.taskDateFormat)") .navigationBarTitleDisplayMode(.inline) @@ -72,9 +73,6 @@ ProgressView() .onAppear { home.request("https://api.lazybear.app/home/type=init", isInitRequest: true) - - // Restart timer - self.timer = Timer.publish(every: 10, on: .main, in: .common).autoconnect() } }
--- a/LazyBear/Views/Profile/ProfileView.swift Sun May 09 00:07:44 2021 +0200 +++ b/LazyBear/Views/Profile/ProfileView.swift Sat May 15 12:31:40 2021 +0200 @@ -15,6 +15,9 @@ var watchlistCompanies: FetchedResults<WatchlistCompany> @State private var showCreateNewWatchlist = false + + // Set recurrent price request + @State private var timer = Timer.publish(every: 10, on: .main, in: .common).autoconnect() var body: some View { if profile.showView { @@ -37,6 +40,9 @@ refreshList() } } + .onAppear { self.timer = Timer.publish(every: 10, on: .main, in: .common).autoconnect() } // Start timer + .onReceive(timer) { _ in prepareUrl(isInitRequest: false) } + .onDisappear { self.timer.upstream.connect().cancel() } // Stop timer .navigationTitle("My profile") .navigationBarTitleDisplayMode(.inline) .toolbar { @@ -65,7 +71,9 @@ profile.showView = true } else { let symbols = watchlistCompanies.map { $0.symbol } - var url = "https://api.lazybear.app/profile/type=init/symbols=" + var typeRequest = "streaming" + if isInitRequest { typeRequest = "init" } + var url = "https://api.lazybear.app/profile/type=\(typeRequest)/symbols=" var counter = 0 for symbol in symbols {