deep-linking-sample/Apps/Wikipedia/Wikipedia/Code/WKWebView+EditSelectionJavascript.swift
Javier Cicchelli 9bcdaa697b [Setup] Basic project structure (#1)
This PR contains all the work related to setting up this project as required to implement the [Assignment](https://repo.rock-n-code.com/rock-n-code/deep-linking-assignment/wiki/Assignment) on top, as intended.

To summarise this work:
- [x] created a new **Xcode** project;
- [x] cloned the `Wikipedia` app and inserted it into the **Xcode** project;
- [x] created the `Locations` app and also, its `Libraries` package;
- [x] created the `Shared` package to share dependencies between the apps;
- [x] added a `Makefile` file and implemented some **environment** and **help** commands.

Co-authored-by: Javier Cicchelli <javier@rock-n-code.com>
Reviewed-on: rock-n-code/deep-linking-assignment#1
2023-04-08 18:37:13 +00:00

72 lines
3.6 KiB
Swift

import WebKit
import CocoaLumberjackSwift
extension WKWebView {
private func selectedTextEditInfo(from dictionary: [String: Any]) -> SelectedTextEditInfo? {
guard
let selectedAndAdjacentTextDict = dictionary["selectedAndAdjacentText"] as? [String: Any],
let selectedText = selectedAndAdjacentTextDict["selectedText"] as? String,
let textBeforeSelectedText = selectedAndAdjacentTextDict["textBeforeSelectedText"] as? String,
let textAfterSelectedText = selectedAndAdjacentTextDict["textAfterSelectedText"] as? String,
let isSelectedTextInTitleDescription = dictionary["isSelectedTextInTitleDescription"] as? Bool,
let sectionID = dictionary["sectionID"] as? Int
else {
DDLogError("Error converting dictionary to SelectedTextEditInfo")
return nil
}
let descriptionSource: ArticleDescriptionSource?
if let descriptionSourceString = dictionary["descriptionSource"] as? String {
descriptionSource = ArticleDescriptionSource(rawValue: descriptionSourceString)
} else {
descriptionSource = nil
}
let selectedAndAdjacentText = SelectedAndAdjacentText(selectedText: selectedText, textAfterSelectedText: textAfterSelectedText, textBeforeSelectedText: textBeforeSelectedText)
return SelectedTextEditInfo(selectedAndAdjacentText: selectedAndAdjacentText, isSelectedTextInTitleDescription: isSelectedTextInTitleDescription, sectionID: sectionID, descriptionSource: descriptionSource)
}
@objc func wmf_getSelectedTextEditInfo(completionHandler: ((SelectedTextEditInfo?, Error?) -> Void)? = nil) {
evaluateJavaScript("window.wmf.editTextSelection.getSelectedTextEditInfo()") { [weak self] (result, error) in
guard let error = error else {
guard let completionHandler = completionHandler else {
return
}
guard
let resultDict = result as? [String: Any],
let selectedTextEditInfo = self?.selectedTextEditInfo(from: resultDict)
else {
DDLogError("Error handling 'getSelectedTextEditInfo()' dictionary response")
return
}
completionHandler(selectedTextEditInfo, nil)
return
}
DDLogError("Error when evaluating javascript on fetch and transform: \(error)")
}
}
}
class SelectedAndAdjacentText {
public let selectedText: String
public let textAfterSelectedText: String
public let textBeforeSelectedText: String
init(selectedText: String, textAfterSelectedText: String, textBeforeSelectedText: String) {
self.selectedText = selectedText
self.textAfterSelectedText = textAfterSelectedText
self.textBeforeSelectedText = textBeforeSelectedText
}
}
@objcMembers class SelectedTextEditInfo: NSObject {
public let selectedAndAdjacentText: SelectedAndAdjacentText
public let isSelectedTextInTitleDescription: Bool
public let sectionID: Int
public let descriptionSource: ArticleDescriptionSource?
init(selectedAndAdjacentText: SelectedAndAdjacentText, isSelectedTextInTitleDescription: Bool, sectionID: Int, descriptionSource: ArticleDescriptionSource?) {
self.selectedAndAdjacentText = selectedAndAdjacentText
self.isSelectedTextInTitleDescription = isSelectedTextInTitleDescription
self.sectionID = sectionID
self.descriptionSource = descriptionSource
}
}