Handle results from article actions in swift

This commit is contained in:
Jackson Harper
2022-03-22 15:25:02 -07:00
parent 7f15f42358
commit 7b6c381ce2
4 changed files with 41 additions and 3 deletions

View File

@ -60,6 +60,8 @@ struct WebReader: UIViewRepresentable {
webView.configuration.userContentController.add(webView, name: "viewerAction")
webView.configuration.userContentController.addScriptMessageHandler(context.coordinator, contentWorld: .page, name: "articleAction")
context.coordinator.linkHandler = openLinkAction
context.coordinator.webViewActionHandler = webViewActionHandler
context.coordinator.updateNavBarVisibilityRatio = navBarVisibilityRatioUpdater

View File

@ -38,6 +38,40 @@ extension WebReaderCoordinator: WKScriptMessageHandler {
}
}
extension WebReaderCoordinator: WKScriptMessageHandlerWithReply {
func userContentController(_: WKUserContentController,
didReceive message: WKScriptMessage,
replyHandler: @escaping (Any?, String?) -> Void)
{
guard let messageBody = message.body as? [String: Any] else { return }
guard let actionID = messageBody["actionID"] as? String else { return }
print("handling message", actionID, messageBody)
switch actionID {
case "deleteHighlight":
// TODO: make API call here, web expects a boolean result.
// we pass results back to JS as the `result` property.
// We are just passing true as an example here. It should
// be false if the API has an error.
replyHandler(["result": true], nil)
// TODO:
// case "createHighlight":
// break
// case "mergeHighlightMutation":
// break
// case "updateHighlightMutation":
// break
// case "articleReadingProgressMutation":
// break
default:
replyHandler(nil, "Unknown actionID: \(actionID)")
}
}
}
extension WebReaderCoordinator: WKNavigationDelegate {
// swiftlint:disable:next line_length
func webView(_: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {

File diff suppressed because one or more lines are too long

View File

@ -6,11 +6,13 @@ import { applyStoredTheme } from '@omnivore/web/lib/themeUpdater'
import '@omnivore/web/styles/globals.css'
import '@omnivore/web/styles/articleInnerStyling.css'
const mutation = (name, input) => {
window?.webkit?.messageHandlers.viewerAction?.postMessage({
const mutation = async (name, input) => {
const result = await window?.webkit?.messageHandlers.articleAction?.postMessage({
actionID: name,
...input
})
console.log('action result', result, result.result)
return result.result
}
const App = () => {