2022-04-13 14:43:16 +02:00
|
|
|
//
|
|
|
|
// NewsView+Configuration.swift
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// Created by MainasuK on 2022-4-13.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
2022-05-11 12:39:34 +02:00
|
|
|
import Combine
|
2022-04-13 14:43:16 +02:00
|
|
|
import MastodonSDK
|
|
|
|
import MastodonLocalization
|
|
|
|
import AlamofireImage
|
2022-04-18 11:14:49 +02:00
|
|
|
import FaviconFinder
|
2022-04-13 14:43:16 +02:00
|
|
|
|
|
|
|
extension NewsView {
|
|
|
|
public func configure(link: Mastodon.Entity.Link) {
|
2022-04-18 11:14:49 +02:00
|
|
|
let faviconPlaceholder = UIImage(systemName: "network")
|
|
|
|
providerFaviconImageView.image = faviconPlaceholder
|
|
|
|
if let url = URL(string: link.url) {
|
|
|
|
let token = providerFaviconImageView.tag
|
|
|
|
FaviconFinder(url: url).downloadFavicon { [weak self] result in
|
|
|
|
guard let self = self else { return }
|
|
|
|
switch result {
|
|
|
|
case .success(let favicon):
|
|
|
|
DispatchQueue.main.async { [weak self] in
|
|
|
|
guard let self = self else { return }
|
|
|
|
guard self.providerFaviconImageView.tag == token else { return }
|
|
|
|
self.providerFaviconImageView.image = favicon.image
|
|
|
|
}
|
|
|
|
case .failure:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-13 14:43:16 +02:00
|
|
|
providerNameLabel.text = link.providerName
|
|
|
|
headlineLabel.text = link.title
|
2022-04-15 11:22:30 +02:00
|
|
|
footnoteLabel.text = L10n.Plural.peopleTalking(link.talkingPeopleCount ?? 0)
|
2022-04-13 14:43:16 +02:00
|
|
|
|
|
|
|
let configuration = MediaView.Configuration(
|
|
|
|
info: .image(info: .init(
|
|
|
|
aspectRadio: CGSize(width: link.width, height: link.height),
|
|
|
|
assetURL: link.image
|
|
|
|
)),
|
|
|
|
blurhash: link.blurhash
|
|
|
|
)
|
|
|
|
imageView.setup(configuration: configuration)
|
|
|
|
|
|
|
|
if let previewURL = configuration.previewURL,
|
|
|
|
let url = URL(string: previewURL)
|
|
|
|
{
|
|
|
|
let placeholder = UIImage.placeholder(color: .systemGray6)
|
|
|
|
let request = URLRequest(url: url)
|
|
|
|
ImageDownloader.default.download(request, completion: { response in
|
|
|
|
switch response.result {
|
|
|
|
case .success(let image):
|
|
|
|
configuration.previewImage = image
|
|
|
|
case .failure:
|
|
|
|
configuration.previewImage = placeholder
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
} // end func
|
|
|
|
}
|