Mercurial > public > lazybear
changeset 175:be5f5cabf789
Implement NewsView
author | Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com> |
---|---|
date | Sat, 20 Feb 2021 19:29:08 +0100 |
parents | 730a6f0959fe |
children | 8ed956c01a54 |
files | LazyBear/Models/NewsModel.swift LazyBear/UI/CompanyRow.swift LazyBear/UI/NewsRow.swift LazyBear/UI/NewsView.swift LazyBear/UI/PriceView.swift LazyBear/UI/Row.swift |
diffstat | 6 files changed, 180 insertions(+), 37 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LazyBear/Models/NewsModel.swift Sat Feb 20 19:29:08 2021 +0100 @@ -0,0 +1,17 @@ +// +// NewsModel.swift +// LazyBear +// +// Created by Dennis Concepción Martín on 20/2/21. +// + +import SwiftUI + +struct NewsModel: Codable, Hashable { + var datetime: Int? + var headline: String? + var source: String? + var url: String? + var summary: String? + var image: String? +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LazyBear/UI/CompanyRow.swift Sat Feb 20 19:29:08 2021 +0100 @@ -0,0 +1,37 @@ +// +// Row.swift +// LazyBear +// +// Created by Dennis Concepción Martín on 19/2/21. +// + +import SwiftUI + +struct CompanyRow: View { + var baseText: String + var subText: String? + var lefView: AnyView? + var rightView: AnyView? + + var body: some View { + HStack { + lefView + VStack(alignment: .leading) { + Text(baseText.uppercased()) + .fontWeight(.semibold) + + Text(subText?.capitalized ?? "") + .lineLimit(1) + } + + Spacer() + rightView + } + } +} + +struct Row_Previews: PreviewProvider { + static var previews: some View { + CompanyRow(baseText: "aapl", subText: "apple inc") + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LazyBear/UI/NewsRow.swift Sat Feb 20 19:29:08 2021 +0100 @@ -0,0 +1,66 @@ +// +// NewsRow.swift +// LazyBear +// +// Created by Dennis Concepción Martín on 20/2/21. +// + +import SwiftUI + +struct NewsRow: View { + var new: NewsModel + + var body: some View { + HStack { + VStack(alignment: .leading) { + if let source = new.source { + Text(source.uppercased()) + .font(.caption) + .opacity(0.5) + + } + + if let headline = new.headline { + Text(headline) + .font(.headline) + .lineLimit(4) + } + + if let summary = new.summary { + Text(summary) + .opacity(0.5) + .font(.subheadline) + .lineLimit(2) + .padding(.bottom, 5) + } + + if (new.datetime != nil) { + let humanDate = convertDate() + Text("\(humanDate) ago") + .font(.caption2) + .opacity(0.5) + } + } + } + .padding(.horizontal) + } + + // Cover Epoch time to human date + 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 ?? 0)/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: NewsModel(datetime: 1613838000000, headline: "As Facebook, Microsoft, Apple, Uber, Amazon, and others play larger roles in OpenStreetMap, hobbyists fear private sector will overshadow their work (Corey Dickinson/Bloomberg)", source: "Techmeme", url: "https://cloud.iexapis.com/v1/news/article/d760b0bc-42f0-4ed0-a721-a7691eeaa132", summary: "Corey Dickinson / Bloomberg : As Facebook, Microsoft, Apple, Uber, Amazon, and others play larger roles in OpenStreetMap, hobbyists fear private sector will overshadow their work — What do Lyft, Facebook, the International Red Cross, the U.N., the government of Nepal and Pokémon Go have in common?", image: "https://cloud.iexapis.com/v1/news/image/d760b0bc-42f0-4ed0-a721-a7691eeaa132")) + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LazyBear/UI/NewsView.swift Sat Feb 20 19:29:08 2021 +0100 @@ -0,0 +1,40 @@ +// +// NewsView.swift +// LazyBear +// +// Created by Dennis Concepción Martín on 20/2/21. +// + +import SwiftUI + +struct NewsView: View { + @State var news = [NewsModel]() + var symbol: String + + var body: some View { + VStack(alignment: .leading) { + ForEach(news, id: \.self) { new in + NewsRow(new: new) + Divider() + } + } + .onAppear { + request(url: getUrl(), model: [NewsModel].self) { self.news = $0 } + } + } + + private func getUrl() -> String { + let baseUrl = Bundle.main.infoDictionary?["IEX_URL"] as? String ?? "Empty url" + let apiKey = Bundle.main.infoDictionary?["IEX_API"] as? String ?? "Empty key" + let url = "\(baseUrl)/stock/\(symbol)/news/last/10?token=\(apiKey)" + + return url + } +} + + +struct NewsView_Previews: PreviewProvider { + static var previews: some View { + NewsView(symbol: "aapl") + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LazyBear/UI/PriceView.swift Sat Feb 20 19:29:08 2021 +0100 @@ -0,0 +1,20 @@ +// +// PriceView.swift +// LazyBear +// +// Created by Dennis Concepción Martín on 20/2/21. +// + +import SwiftUI + +struct PriceView: View { + var body: some View { + Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/) + } +} + +struct PriceView_Previews: PreviewProvider { + static var previews: some View { + PriceView() + } +}
--- a/LazyBear/UI/Row.swift Sat Feb 20 13:13:14 2021 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,37 +0,0 @@ -// -// Row.swift -// LazyBear -// -// Created by Dennis Concepción Martín on 19/2/21. -// - -import SwiftUI - -struct Row: View { - var baseText: String - var subText: String? - var lefView: AnyView? - var rightView: AnyView? - - var body: some View { - HStack { - lefView - VStack(alignment: .leading) { - Text(baseText.uppercased()) - .fontWeight(.semibold) - - Text(subText?.capitalized ?? "") - .lineLimit(1) - } - - Spacer() - rightView - } - } -} - -struct Row_Previews: PreviewProvider { - static var previews: some View { - Row(baseText: "aapl", subText: "apple inc") - } -}