Show favicon in detail view when available and when there’s no user avatar or feed icon.

This commit is contained in:
Brent Simmons 2017-12-29 20:04:43 -08:00
parent 599b42b9df
commit d2d7369d6a
3 changed files with 37 additions and 15 deletions

View File

@ -19,6 +19,7 @@ class ArticleRenderer {
let article: Article
let articleStyle: ArticleStyle
static var faviconImgTagCache = [Feed: String]()
lazy var longDateFormatter: DateFormatter = {
let dateFormatter = DateFormatter()
@ -169,21 +170,11 @@ class ArticleRenderer {
d["newsitem_description"] = body
d["avatars"] = ""
var didAddAvatar = false
if let avatar = avatarToUse() {
let avatarHTML = avatar.html(dimension: 64)
d["avatars"] = avatarHTML
// var ix = 0
// let ct = avatars.count
// for avatar in avatars {
// avatarHTML += avatar.html(dimension: 64)
// if ix < ct - 1 {
// avatarHTML += "&nbsp;"
// }
// ix += 1
// }
// if !avatarHTML.isEmpty {
// d["avatars"] = avatarHTML
// }
didAddAvatar = true
}
var feedLink = ""
@ -196,6 +187,13 @@ class ArticleRenderer {
d["feedlink"] = feedLink
d["feedlink_withfavicon"] = feedLink
d["favicon"] = ""
if !didAddAvatar, let feed = article.feed {
if let favicon = faviconImgTag(forFeed: feed) {
d["favicon"] = favicon
}
}
let longDate = longDateFormatter.string(from: article.logicalDatePublished)
let mediumDate = mediumDateFormatter.string(from: article.logicalDatePublished)
let shortDate = shortDateFormatter.string(from: article.logicalDatePublished)
@ -230,6 +228,30 @@ class ArticleRenderer {
}
}
private func faviconImgTag(forFeed feed: Feed) -> String? {
if let cachedImgTag = ArticleRenderer.faviconImgTagCache[feed] {
return cachedImgTag
}
if let favicon = appDelegate.faviconDownloader.favicon(for: feed) {
if let s = base64String(forImage: favicon) {
let imgTag = "<img src=\"data:image/tiff;base64, " + s + "\" height=16 width=16 />"
ArticleRenderer.faviconImgTagCache[feed] = imgTag
return imgTag
}
}
return nil
}
private func base64String(forImage image: NSImage) -> String? {
let d = image.tiffRepresentation
return d?.base64EncodedString()
}
private func singleArticleSpecifiedAuthor() -> Author? {
// The author of this article, if just one.

View File

@ -68,7 +68,7 @@ a:hover {
.headerTable {
widtdh: 100%;
}
.headerContainer img {
.avatar img {
border-radius: 7px;
margin-right: 8px;
}

View File

@ -3,8 +3,8 @@
<div class="headerContainer">
<table cellpadding=0 cellspacing=0 border=0 class="headerTable">
<tr>
<td class="header leftAlign">[[avatars]]</td>
<td class="header leftAlign"><b>[[feedlink]]</b><br />[[byline]]</td>
<td class="header leftAlign avatar">[[avatars]]</td>
<td class="header leftAlign">[[favicon]] <b>[[feedlink]]</b><br />[[byline]]</td>
</tr>
</table>
</div>