2020-11-18 03:49:12 +01:00
|
|
|
//
|
|
|
|
// ArticleItemView.swift
|
|
|
|
// NetNewsWire Widget Extension
|
|
|
|
//
|
|
|
|
// Created by Stuart Breckenridge on 18/11/20.
|
|
|
|
// Copyright © 2020 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import SwiftUI
|
2021-01-30 02:02:42 +01:00
|
|
|
import RSWeb
|
2020-11-18 03:49:12 +01:00
|
|
|
|
|
|
|
struct ArticleItemView: View {
|
|
|
|
|
|
|
|
var article: LatestArticle
|
|
|
|
var deepLink: URL
|
2022-07-07 20:12:42 +02:00
|
|
|
@State private var iconImage: Image?
|
2020-11-18 03:49:12 +01:00
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
Link(destination: deepLink, label: {
|
|
|
|
HStack(alignment: .top, spacing: nil, content: {
|
|
|
|
// Feed Icon
|
2021-11-09 03:46:44 +01:00
|
|
|
if iconImage != nil {
|
2022-07-07 20:12:42 +02:00
|
|
|
iconImage!
|
2021-11-09 03:46:44 +01:00
|
|
|
.resizable()
|
|
|
|
.frame(width: 30, height: 30)
|
|
|
|
.cornerRadius(4)
|
|
|
|
}
|
2020-11-18 03:49:12 +01:00
|
|
|
|
|
|
|
// Title and Feed Name
|
|
|
|
VStack(alignment: .leading) {
|
|
|
|
Text(article.articleTitle ?? "Untitled")
|
2020-11-19 09:32:44 +01:00
|
|
|
.font(.footnote)
|
2020-11-18 03:49:12 +01:00
|
|
|
.bold()
|
|
|
|
.lineLimit(1)
|
|
|
|
.foregroundColor(.primary)
|
2020-11-19 09:32:44 +01:00
|
|
|
.padding(.top, -3)
|
2020-11-18 03:49:12 +01:00
|
|
|
|
|
|
|
HStack {
|
|
|
|
Text(article.feedTitle)
|
|
|
|
.font(.caption)
|
|
|
|
.lineLimit(1)
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
Spacer()
|
|
|
|
Text(pubDate(article.pubDate))
|
|
|
|
.font(.caption)
|
|
|
|
.lineLimit(1)
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2021-11-09 03:46:44 +01:00
|
|
|
}).onAppear {
|
2022-07-07 20:12:42 +02:00
|
|
|
iconImage = thumbnail(from: article.feedIconPath)
|
2021-11-09 03:46:44 +01:00
|
|
|
}
|
2020-11-18 03:49:12 +01:00
|
|
|
}
|
|
|
|
|
2022-07-07 20:12:42 +02:00
|
|
|
func thumbnail(from path: String?) -> Image? {
|
2022-07-29 01:33:05 +02:00
|
|
|
guard let imagePath = path else {
|
|
|
|
return Image(uiImage: UIImage(systemName: "globe")!)
|
|
|
|
}
|
|
|
|
|
|
|
|
let url = URL(fileURLWithPath: imagePath)
|
|
|
|
|
|
|
|
guard let data = try? Data(contentsOf: url),
|
|
|
|
let uiImage = UIImage(data: data) else {
|
|
|
|
return Image(uiImage: UIImage(systemName: "globe")!)
|
|
|
|
}
|
|
|
|
|
|
|
|
return Image(uiImage: uiImage)
|
2020-11-18 03:49:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func pubDate(_ dateString: String) -> String {
|
|
|
|
let dateFormatter = DateFormatter()
|
|
|
|
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
|
2021-04-13 06:17:31 +02:00
|
|
|
guard let date = dateFormatter.date(from: dateString) else {
|
|
|
|
return ""
|
|
|
|
}
|
2020-11-18 03:49:12 +01:00
|
|
|
|
|
|
|
let displayFormatter = DateFormatter()
|
|
|
|
displayFormatter.dateStyle = .medium
|
|
|
|
displayFormatter.timeStyle = .none
|
|
|
|
|
2021-04-13 06:17:31 +02:00
|
|
|
return displayFormatter.string(from: date)
|
2020-11-18 03:49:12 +01:00
|
|
|
}
|
|
|
|
}
|