feat(ios): add intelligents button (#9281)

Co-authored-by: 砍砍 <git@qaq.wiki>
This commit is contained in:
EYHN
2024-12-24 15:51:11 +08:00
committed by GitHub
parent fbe3e08769
commit 3cf4bcf651
78 changed files with 3283 additions and 114 deletions
@@ -0,0 +1,113 @@
import Capacitor
import Intelligents
import UIKit
class AFFiNEViewController: CAPBridgeViewController {
override func viewDidLoad() {
super.viewDidLoad()
// disable by default, enable manually when there is a "back" button in page-header
webView?.allowsBackForwardNavigationGestures = false
navigationController?.navigationBar.isHidden = true
extendedLayoutIncludesOpaqueBars = false
edgesForExtendedLayout = []
let intelligentsButton = installIntelligentsButton()
intelligentsButton.delegate = self
dismissIntelligentsButton()
}
override func capacitorDidLoad() {
let plugins: [CAPPlugin] = [
CookiePlugin(),
HashcashPlugin(),
NavigationGesturePlugin(),
IntelligentsPlugin(representController: self),
]
plugins.forEach { bridge?.registerPluginInstance($0) }
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
navigationController?.setNavigationBarHidden(false, animated: animated)
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
}
}
extension AFFiNEViewController: IntelligentsButtonDelegate, IntelligentsFocusApertureViewDelegate {
func onIntelligentsButtonTapped(_ button: IntelligentsButton) {
guard let webView else {
assertionFailure() // ? wdym ?
return
}
button.beginProgress()
let script = "return await window.getCurrentDocContentInMarkdown();"
webView.callAsyncJavaScript(
script,
arguments: [:],
in: nil,
in: .page
) { result in
button.stopProgress()
webView.resignFirstResponder()
if case let .failure(error) = result {
print("[?] \(self) script error: \(error.localizedDescription)")
}
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
if case let .success(content) = result,
let res = content as? String
{
print("[*] \(self) received document with \(res.count) characters")
DispatchQueue.main.async {
self.openIntelligentsSheet(withContext: res)
}
} else {
DispatchQueue.main.async {
self.openSimpleChat()
}
}
}
}
}
func openIntelligentsSheet(withContext context: String) {
guard let view = webView?.subviews.first else {
assertionFailure()
return
}
assert(view is UIScrollView)
_ = context
let focus = IntelligentsFocusApertureView()
focus.prepareAnimationWith(
capturingTargetContentView: view,
coveringRootViewController: self
)
focus.delegate = self
focus.executeAnimationKickIn()
dismissIntelligentsButton()
}
func openSimpleChat() {
let targetController = IntelligentsChatController()
presentIntoCurrentContext(withTargetController: targetController)
}
func focusApertureRequestAction(actionType: IntelligentsFocusApertureViewActionType) {
switch actionType {
case .translateTo:
fatalError("not implemented")
case .summary:
fatalError("not implemented")
case .chatWithAI:
let controller = IntelligentsChatController()
presentIntoCurrentContext(withTargetController: controller)
case .dismiss:
presentIntelligentsButton()
}
}
}