public struct FriendlyTextField: View, BeFriend {
public let eternalId: String
let title: String
@Binding var text: String
@State var focused: Bool = false
@StateObject var speechManager = SpeechManager.shared
@StateObject var positionManager = PositionManager.shared
public init(_ id: String, _ title: String, text: Binding<String>) {
self.eternalId = id
self.title = title
self._text = text
}
public var body: some View {
FriendlyWrappedView(eternalId) {
TextField(title, text: $text)
.onAppear {
focused = true
print(focused)
}
}
.onRight {
focused = true
if !focused {
speechManager.startRecord()
speechManager.onRecord = eternalId
} else {
speechManager.stopRecord()
speechManager.onRecord = ""
}
}
.onChange(of: self.focused) {
if !$0 {
speechManager.stopRecord()
speechManager.onRecord = ""
}
}
.onChange(of: speechManager.text) { _ in
if speechManager.onRecord == eternalId {
text = speechManager.lastText + speechManager.text
}
}
.onChange(of: speechManager.lastText) { _ in
if speechManager.onRecord == eternalId {
text = speechManager.lastText + speechManager.text
}
}
.onChange(of: positionManager.focus) { newValue in
if newValue != eternalId {
focused = false
}
}
}
}
When this view appears, it prints false
, but I set focused
to true
.
FriendlyWrappedView
is just adding some modifiers to the view.
Also StateObject
doesn't work. The published value was changed, but onChange
isn't called.
And when I change text
's value, it works well.
If I change focused
to a binding value, it works again
My Xcode version: 13.3 My iPad version: 15.4
Other views work well.