Load embed web view only on tap (for privacy)
This commit is contained in:
parent
5932d00f2f
commit
7944ec6399
|
@ -26,6 +26,23 @@ public final class StatusCardControl: UIControl {
|
||||||
private let imageView = UIImageView()
|
private let imageView = UIImageView()
|
||||||
private let titleLabel = UILabel()
|
private let titleLabel = UILabel()
|
||||||
private let linkLabel = UILabel()
|
private let linkLabel = UILabel()
|
||||||
|
private lazy var showEmbedButton: UIButton = {
|
||||||
|
if #available(iOS 15.0, *) {
|
||||||
|
var configuration = UIButton.Configuration.gray()
|
||||||
|
configuration.background.visualEffect = UIBlurEffect(style: .systemUltraThinMaterial)
|
||||||
|
configuration.baseBackgroundColor = .clear
|
||||||
|
configuration.cornerStyle = .capsule
|
||||||
|
configuration.buttonSize = .large
|
||||||
|
return UIButton(configuration: configuration, primaryAction: UIAction { [weak self] _ in
|
||||||
|
self?.showWebView()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return UIButton(type: .system, primaryAction: UIAction { [weak self] _ in
|
||||||
|
self?.showWebView()
|
||||||
|
})
|
||||||
|
}()
|
||||||
|
private var html = ""
|
||||||
|
|
||||||
private static let cardContentPool = WKProcessPool()
|
private static let cardContentPool = WKProcessPool()
|
||||||
private var webView: WKWebView?
|
private var webView: WKWebView?
|
||||||
|
@ -95,14 +112,23 @@ public final class StatusCardControl: UIControl {
|
||||||
containerStackView.isUserInteractionEnabled = false
|
containerStackView.isUserInteractionEnabled = false
|
||||||
containerStackView.distribution = .fill
|
containerStackView.distribution = .fill
|
||||||
|
|
||||||
|
showEmbedButton.setImage(UIImage(systemName: "play.fill"), for: .normal)
|
||||||
|
|
||||||
addSubview(containerStackView)
|
addSubview(containerStackView)
|
||||||
addSubview(highlightView)
|
addSubview(highlightView)
|
||||||
|
addSubview(showEmbedButton)
|
||||||
|
|
||||||
containerStackView.translatesAutoresizingMaskIntoConstraints = false
|
containerStackView.translatesAutoresizingMaskIntoConstraints = false
|
||||||
highlightView.translatesAutoresizingMaskIntoConstraints = false
|
highlightView.translatesAutoresizingMaskIntoConstraints = false
|
||||||
|
showEmbedButton.translatesAutoresizingMaskIntoConstraints = false
|
||||||
|
|
||||||
containerStackView.pinToParent()
|
containerStackView.pinToParent()
|
||||||
highlightView.pinToParent()
|
highlightView.pinToParent()
|
||||||
|
NSLayoutConstraint.activate([
|
||||||
|
showEmbedButton.widthAnchor.constraint(equalTo: showEmbedButton.heightAnchor),
|
||||||
|
showEmbedButton.centerXAnchor.constraint(equalTo: imageView.centerXAnchor),
|
||||||
|
showEmbedButton.centerYAnchor.constraint(equalTo: imageView.centerYAnchor),
|
||||||
|
])
|
||||||
}
|
}
|
||||||
|
|
||||||
required init?(coder: NSCoder) {
|
required init?(coder: NSCoder) {
|
||||||
|
@ -133,12 +159,13 @@ public final class StatusCardControl: UIControl {
|
||||||
}
|
}
|
||||||
|
|
||||||
if let html = card.html, !html.isEmpty {
|
if let html = card.html, !html.isEmpty {
|
||||||
let webView = setupWebView()
|
showEmbedButton.isHidden = false
|
||||||
webView.loadHTMLString("<meta name='viewport' content='width=device-width,user-scalable=no'><style>body { margin: 0; color-scheme: light dark; } body > :only-child { width: 100vw !important; height: 100vh !important }</style>" + html, baseURL: nil)
|
self.html = html
|
||||||
addSubview(webView)
|
|
||||||
} else {
|
} else {
|
||||||
webView?.removeFromSuperview()
|
webView?.removeFromSuperview()
|
||||||
webView = nil
|
webView = nil
|
||||||
|
showEmbedButton.isHidden = true
|
||||||
|
self.html = ""
|
||||||
}
|
}
|
||||||
|
|
||||||
updateConstraints(for: card.layout)
|
updateConstraints(for: card.layout)
|
||||||
|
@ -190,10 +217,6 @@ public final class StatusCardControl: UIControl {
|
||||||
dividerConstraint = dividerView.widthAnchor.constraint(equalToConstant: pixelSize).activate()
|
dividerConstraint = dividerView.widthAnchor.constraint(equalToConstant: pixelSize).activate()
|
||||||
}
|
}
|
||||||
|
|
||||||
if let webView {
|
|
||||||
layoutConstraints += webView.pinTo(to: imageView)
|
|
||||||
}
|
|
||||||
|
|
||||||
NSLayoutConstraint.activate(layoutConstraints)
|
NSLayoutConstraint.activate(layoutConstraints)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -214,6 +237,15 @@ public final class StatusCardControl: UIControl {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extension StatusCardControl {
|
||||||
|
fileprivate func showWebView() {
|
||||||
|
let webView = setupWebView()
|
||||||
|
webView.loadHTMLString("<meta name='viewport' content='width=device-width,user-scalable=no'><style>body { margin: 0; color-scheme: light dark; } body > :only-child { width: 100vw !important; height: 100vh !important }</style>" + html, baseURL: nil)
|
||||||
|
addSubview(webView)
|
||||||
|
webView.pinTo(to: imageView)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
extension StatusCardControl: WKNavigationDelegate, WKUIDelegate {
|
extension StatusCardControl: WKNavigationDelegate, WKUIDelegate {
|
||||||
fileprivate func setupWebView() -> WKWebView {
|
fileprivate func setupWebView() -> WKWebView {
|
||||||
if let webView { return webView }
|
if let webView { return webView }
|
||||||
|
|
Loading…
Reference in New Issue