Mercurial > public > lazybear
view LazyBear/UI/NewsRow.swift @ 271:e1610b54015d
Minor updates
author | Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com> |
---|---|
date | Sun, 14 Mar 2021 13:25:21 +0100 |
parents | f9d6a324e3d2 |
children |
line wrap: on
line source
// // NewsRow.swift // LazyBear // // Created by Dennis Concepción Martín on 20/2/21. // import SwiftUI import SDWebImageSwiftUI struct NewsRow: View { var new: NewsModel @State private var showingSafari = false @Environment(\.colorScheme) var colorScheme // Detect dark mode var body: some View { Button(action: { self.showingSafari = true }) { VStack(alignment: .leading) { // If iPad show Image if UIDevice.current.userInterfaceIdiom == .pad { WebImage(url: URL(string: new.image ?? "")) .resizable() .placeholder { } .indicator(.activity) .aspectRatio(contentMode: .fit) .cornerRadius(UIDevice.current.userInterfaceIdiom == .pad ? 10: 0) } if let source = new.source { Text(source.uppercased()) .font(.caption) .opacity(0.5) } if let headline = new.headline { Text(headline) .font(.headline) .lineLimit(UIDevice.current.userInterfaceIdiom == .pad ? nil: 4) } if let summary = new.summary { Text(summary) .opacity(0.5) .font(.subheadline) .lineLimit(UIDevice.current.userInterfaceIdiom == .pad ? 10: 1) .padding(.bottom, 5) } if (new.datetime != nil) { let humanDate = convertDate() Text("\(humanDate) ago") .font(.caption2) .opacity(0.5) } } } .foregroundColor(colorScheme == .dark ? .white: .black) .sheet(isPresented: $showingSafari) { SafariView(url:URL(string: new.url ?? "")!) } } // Convert 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")) } }