2019-11-07 21:29:16 +01:00
|
|
|
//
|
2020-01-27 20:58:32 +01:00
|
|
|
// ArticleIconSchemeHandler.swift
|
2019-11-07 21:29:16 +01:00
|
|
|
// NetNewsWire-iOS
|
|
|
|
//
|
2020-01-27 20:58:32 +01:00
|
|
|
// Created by Maurice Parker on 1/27/20.
|
|
|
|
// Copyright © 2020 Ranchero Software. All rights reserved.
|
2019-11-07 21:29:16 +01:00
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import WebKit
|
|
|
|
import Articles
|
|
|
|
|
|
|
|
class ArticleIconSchemeHandler: NSObject, WKURLSchemeHandler {
|
|
|
|
|
2020-01-30 20:53:27 +01:00
|
|
|
weak var coordinator: SceneCoordinator?
|
2020-01-27 20:58:32 +01:00
|
|
|
|
|
|
|
init(coordinator: SceneCoordinator) {
|
|
|
|
self.coordinator = coordinator
|
|
|
|
}
|
2019-11-07 21:29:16 +01:00
|
|
|
|
|
|
|
func webView(_ webView: WKWebView, start urlSchemeTask: WKURLSchemeTask) {
|
|
|
|
|
2020-01-30 20:53:27 +01:00
|
|
|
guard let url = urlSchemeTask.request.url, let coordinator = coordinator else {
|
2020-01-27 20:58:32 +01:00
|
|
|
urlSchemeTask.didFailWithError(URLError(.fileDoesNotExist))
|
|
|
|
return
|
|
|
|
}
|
2020-02-09 02:21:55 +01:00
|
|
|
|
|
|
|
guard let components = URLComponents(url: url, resolvingAgainstBaseURL: false) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let articleID = components.path
|
2020-01-27 20:58:32 +01:00
|
|
|
guard let iconImage = coordinator.articleFor(articleID)?.iconImage() else {
|
2019-11-07 22:00:43 +01:00
|
|
|
urlSchemeTask.didFailWithError(URLError(.fileDoesNotExist))
|
|
|
|
return
|
2019-11-07 21:29:16 +01:00
|
|
|
}
|
2019-11-07 22:00:43 +01:00
|
|
|
|
|
|
|
let iconView = IconView(frame: CGRect(x: 0, y: 0, width: 48, height: 48))
|
|
|
|
iconView.iconImage = iconImage
|
|
|
|
let renderedImage = iconView.asImage()
|
|
|
|
|
|
|
|
guard let data = renderedImage.dataRepresentation() else {
|
|
|
|
urlSchemeTask.didFailWithError(URLError(.fileDoesNotExist))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let headerFields = ["Cache-Control": "no-cache"]
|
2020-01-27 20:58:32 +01:00
|
|
|
if let response = HTTPURLResponse(url: url, statusCode: 200, httpVersion: nil, headerFields: headerFields) {
|
2019-11-07 22:00:43 +01:00
|
|
|
urlSchemeTask.didReceive(response)
|
|
|
|
urlSchemeTask.didReceive(data)
|
|
|
|
urlSchemeTask.didFinish()
|
|
|
|
}
|
|
|
|
|
2019-11-07 21:29:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func webView(_ webView: WKWebView, stop urlSchemeTask: WKURLSchemeTask) {
|
|
|
|
urlSchemeTask.didFailWithError(URLError(.unknown))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-01-27 20:58:32 +01:00
|
|
|
|