Reputation: 51
I'm working on a keyboard extension for iOS, and I've encountered an issue with processing the context before the input in my replaceDocumentText function. The function is supposed to process both the text before and after the input cursor, but I'm noticing that the loop for processing the context before the input (documentContextBeforeInput) only executes after I dismiss and reopen the keyboard.
I am using KeyboardKit for this extension.
`
func replaceDocumentText(textProxy: UITextDocumentProxy, suggestionsRequired: Bool=false)
async -> (context: String, count: Int) {
var startingOffset = 0
var context = ""
var forwardContextCount = 0
//show loading only if suggestions required
if suggestionsRequired {
self.loadingState = .loading
}
while let previousContext = textProxy.documentContextBeforeInput, !previousContext.isEmpty {
/// Check for the context before the cursor
context = previousContext + context
startingOffset += previousContext.count
textProxy.adjustTextPosition(byCharacterOffset: -previousContext.count)
try? await Task.sleep(nanoseconds: 50_000_000)
}
// Adjust cursor from where it started
textProxy.adjustTextPosition(byCharacterOffset: context.count)
try? await Task.sleep(nanoseconds: 1000_000_000)
while let nextContext = textProxy.documentContextAfterInput, !nextContext.isEmpty {
/// Check for the context After the cursor
forwardContextCount += 1
context += nextContext
textProxy.adjustTextPosition(byCharacterOffset: nextContext.count)
try? await Task.sleep(nanoseconds: 100_000_000)
}
self.promptText = context
self.promptCount = context.count
return await withCheckedContinuation({ continuation in
if suggestionsRequired {
Task{
await self.getSuggestions()
}
}
continuation.resume(returning: (self.promptText, self.promptCount))
})
}``
When I have text in the textField I press the button to read the context but this line will never executes I believe its because of ".isEmpty" condition
while let previousContext = textProxy.documentContextBeforeInput, !previousContext.isEmpty
But I have the text in the textField as soon I close my keyboard extension and reopens it and now on the press of button everything will work smoothly. Kindly help me with this.
I tried some text cases but not able to figure out the issue
Upvotes: 0
Views: 117
Reputation: 11
func replaceDocumentText(textProxy: UITextDocumentProxy, suggestionsRequired: Bool = false) async -> (context: String, count: Int) {
var context = ""
var forwardContextCount = 0
//show loading only if suggestions required
if suggestionsRequired {
self.loadingState = .loading
}
// Combine previous and next context directly without adjusting text position
let previousContext = textProxy.documentContextBeforeInput ?? ""
let nextContext = textProxy.documentContextAfterInput ?? ""
context = previousContext + nextContext
self.promptText = context
self.promptCount = context.count
return await withCheckedContinuation({ continuation in
if suggestionsRequired {
Task {
await self.getSuggestions()
}
}
continuation.resume(returning: (self.promptText, self.promptCount))
})
}
Upvotes: 0