Make sure image loaders don't loop if onAppear gets repeatedly called

This commit is contained in:
Maurice Parker 2020-07-01 19:50:40 -05:00
parent e7e3a0d5d7
commit ee4d8b34b1
3 changed files with 23 additions and 19 deletions

View File

@ -23,6 +23,7 @@ final class ArticleIconImageLoader: ObservableObject {
}
func loadImage(for article: Article) {
guard image == nil else { return }
self.article = article
image = article.iconImage()
}
@ -50,7 +51,7 @@ private extension ArticleIconImageLoader {
for author in authors {
if author.avatarURL == avatarURL {
image = article.iconImage()
return
}
}

View File

@ -21,8 +21,27 @@ final class FeedIconImageLoader: ObservableObject {
}
func loadImage(for feed: Feed) {
guard image == nil else { return }
self.feed = feed
fetchImage()
}
}
private extension FeedIconImageLoader {
@objc func faviconDidBecomeAvailable(_ note: Notification) {
fetchImage()
}
@objc func webFeedIconDidBecomeAvailable(_ note: Notification) {
guard let feed = feed as? WebFeed, let noteFeed = note.userInfo?[UserInfoKey.webFeed] as? WebFeed, feed == noteFeed else {
return
}
fetchImage()
}
func fetchImage() {
if let webFeed = feed as? WebFeed {
if let feedIconImage = appDelegate.webFeedIconDownloader.icon(for: webFeed) {
image = feedIconImage
@ -40,19 +59,3 @@ final class FeedIconImageLoader: ObservableObject {
}
}
private extension FeedIconImageLoader {
@objc func faviconDidBecomeAvailable(_ note: Notification) {
guard let feed = feed else { return }
loadImage(for: feed)
}
@objc func webFeedIconDidBecomeAvailable(_ note: Notification) {
guard let feed = feed as? WebFeed, let noteFeed = note.userInfo?[UserInfoKey.webFeed] as? WebFeed, feed == noteFeed else {
return
}
loadImage(for: feed)
}
}

View File

@ -24,7 +24,7 @@ struct TimelineItemView: View {
VStack {
Text(verbatim: timelineItem.article.title ?? "N/A")
.fontWeight(.semibold)
.lineLimit(3)
.lineLimit(AppDefaults.timelineNumberOfLines)
.frame(maxWidth: .infinity, alignment: .leading)
.padding(.trailing, 4)
Spacer()