Skip to content

Commit

Permalink
add onClose and onReplacementAction
Browse files Browse the repository at this point in the history
  • Loading branch information
mozzius committed May 18, 2024
1 parent 7263777 commit 9be4231
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ public class ExpoBlueskyTranslateModule: Module {
public func definition() -> ModuleDefinition {
Name("ExpoBlueskyTranslate")
View(ExpoBlueskyTranslateView.self) {
Events("onEvent")
Events("onClose")
Events("onReplacementAction")
Prop("text") { (view: ExpoBlueskyTranslateView, text: String) in
view.props.text = text
}
Expand Down
25 changes: 17 additions & 8 deletions modules/expo-bluesky-translate/ios/ExpoBlueskyTranslateView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,35 @@ import Foundation
import SwiftUI

class TranslateViewProps: ObservableObject {
@Published var text: String = ""
@Published var isPresented: Bool = false
@Published var children: [UIView]?
@Published var onEvent: EventDispatcher
init(onEvent: EventDispatcher) {
self.onEvent = onEvent
var text: String = ""
@Published var isPresented: Bool = false {
didSet {
if !isPresented {
self.onClose()
}
}
}
var children: [UIView]?
var onClose: EventDispatcher
var onReplacementAction: EventDispatcher
init(onClose: EventDispatcher, onReplacementAction: EventDispatcher) {
self.onClose = onClose
self.onReplacementAction = onReplacementAction
}
}

class ExpoBlueskyTranslateView: ExpoView {
let props: TranslateViewProps
let onEvent = EventDispatcher()
let onClose = EventDispatcher()
let onReplacementAction = EventDispatcher()

override func didUpdateReactSubviews() {
let subChildren = self.reactSubviews()
props.children = subChildren
}

required init(appContext: AppContext? = nil) {
props = TranslateViewProps(onEvent: onEvent)
props = TranslateViewProps(onClose: onClose, onReplacementAction: onReplacementAction)
let hostingController = UIHostingController(rootView: TranslateView(props: props))
super.init(appContext: appContext)
setupHostingController(hostingController)
Expand Down
4 changes: 3 additions & 1 deletion modules/expo-bluesky-translate/ios/TranslateView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ struct TranslateView: View {
.translationPresentation(
isPresented: $props.isPresented,
text: props.text
)
) { translation in
props.onReplacementAction(["text": translation])
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ export type ExpoBlueskyTranslateProps = {
text: string
isPresented?: boolean
children: React.ReactNode
onClose?: () => void
onReplacementAction?: (event: {nativeEvent: {text: string}}) => void
}
6 changes: 5 additions & 1 deletion src/view/com/post-thread/PostThreadItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -645,13 +645,17 @@ function ExpandedPostDetails({
openLink(translatorUrl)
}
}, [openLink, translatorUrl])

return (
<View style={[s.flexRow, s.mt2, s.mb10]}>
<Text style={pal.textLight}>{niceDate(post.indexedAt)}</Text>
{needsTranslation && (
<>
<Text style={pal.textLight}> &middot; </Text>
<ExpoBlueskyTranslateView text={text} isPresented={presented}>
<ExpoBlueskyTranslateView
text={text}
isPresented={presented}
onClose={() => setPresented(false)}>
<Text
style={pal.link}
title={_(msg`Translate`)}
Expand Down

0 comments on commit 9be4231

Please sign in to comment.