Coder Social home page Coder Social logo

Comments (14)

bribar avatar bribar commented on July 26, 2024

shouldUnregister does include the filter string in manager.currentSession when removing the prefix and string

image

from inputbaraccessoryview.

nathantannar4 avatar nathantannar4 commented on July 26, 2024

Use the properties passed by the delegates function, it explicitly passes the filter text

Sent with GitHawk

from inputbaraccessoryview.

bribar avatar bribar commented on July 26, 2024

func autocompleteManager(_ manager: AutocompleteManager, shouldComplete prefix: String, with text: String) -> Bool { print("shouldComplete") print(prefix) print(text) print(manager.currentSession as Any) return true }

Ya it's showing empty

from inputbaraccessoryview.

bribar avatar bribar commented on July 26, 2024

hey @nathantannar4

After more investigation, I found this worked with the autocomplete method

guard delegate?.autocompleteManager(self, shouldComplete: session.prefix, with: (session.completion?.text)!) != false else { return }

session.filter is always empty, therefore passing on text as empty to the delegate.

I'm not a Swift expert, so maybe this is incorrect, but I can at least capture the text within the ExampleViewController.

from inputbaraccessoryview.

bribar avatar bribar commented on July 26, 2024

For better reference:

open func autocomplete(with session: AutocompleteSession) {
    
    guard let textView = inputTextView else { return }
    
    // Replace the attributedText with a modified version
    let autocomplete = session.completion?.text ?? ""
    
    guard delegate?.autocompleteManager(self, shouldComplete: session.prefix, with: autocomplete) != false else { return }
    
    // Create a range that overlaps the prefix
    let prefixLength = session.prefix.utf16.count
    let insertionRange = NSRange(
        location: session.range.location + (keepPrefixOnCompletion ? prefixLength : 0),
        length: session.filter.utf16.count + (!keepPrefixOnCompletion ? prefixLength : 0)
    )
    
    // Transform range
    guard let range = Range(insertionRange, in: textView.text) else { return }
    let nsrange = NSRange(range, in: textView.text)
    
    insertAutocomplete(autocomplete, at: session, for: nsrange)
    
    // Move Cursor to the end of the inserted text
    let selectedLocation = insertionRange.location + autocomplete.utf16.count + (appendSpaceOnCompletion ? 1 : 0)
    textView.selectedRange = NSRange(
        location: selectedLocation,
        length: 0
    )
    
    // End the session
    unregisterCurrentSession()
}

from inputbaraccessoryview.

nathantannar4 avatar nathantannar4 commented on July 26, 2024

@bribar I believe you misunderstand the function. Have you looked at the documentation for the method?

func autocompleteManager(_ manager: AutocompleteManager, shouldComplete prefix: String, with text: String) -> Bool

The text is the String that the user has already typed, ie: the string to "autocomplete", so yes it can be empty if all they have entered is a prefix.

If you want the text that the user selected, look to AutocompleteCompletion, each AutocompleteSession has once as a completion property. AutocompleteCompletion can specify the text to autocomplete too and an optional displayText (useful for things like emojis or if you want to style a users name differently)

Your original question:

Shouldn't text be the autocomplete I selected?

No that is the text they have typed which filters available autocompletes

If you are still confused, please leave a more detailed reply as to what you are trying to do.

from inputbaraccessoryview.

bribar avatar bribar commented on July 26, 2024

I copy that... I'm just trying to add the completion text without the prefix to a collection, if they remove the text then I remove it from the collection.

var mentions : [String] = [String]()

Example, user types: "Hey @ninja and @avatar, lets jump on a conference call."

"Ninja" and "Avatar" get added to mentions

If user removes @ninja, I remove it from mentions, so now mentions has ["Avatar"]


In the autocomplete method, if I print(session) it shows this

AutocompleteSession(prefix: "@", range: {9, 3}, filter: "ni", completion: Optional(InputBarAccessoryView.AutocompleteCompletion(text: "Ninja", displayText: nil)))

but in shouldComplete of ExampleViewController

manager.currentSession.completion is nil

Do I need to build on what you have or is this functionality default?

from inputbaraccessoryview.

nathantannar4 avatar nathantannar4 commented on July 26, 2024

Hmm ok well the completion property is nil up until the point where a user taps the cell. Have you overridden the didSelectRowAt? It gets set there so it shouldn't be nil. It cant be nil if the autocomplete works because it uses that property in the session data

Sent with GitHawk

from inputbaraccessoryview.

nathantannar4 avatar nathantannar4 commented on July 26, 2024

In any case I haven't made a method that would alert you when someone has deleted an autocomplete, so while you could get the name to append to a string array, it would never be removed.

Sent with GitHawk

from inputbaraccessoryview.

bribar avatar bribar commented on July 26, 2024

I haven't overridden didSelectRowAt.

Yes autocomplete I can print(session) and its there.

open func autocomplete(with session: AutocompleteSession) {
    print(session)
    guard let textView = inputTextView else { return }
    guard delegate?.autocompleteManager(self, shouldComplete: session.prefix, with: session.filter) != false else { return }
    
    // Create a range that overlaps the prefix
    let prefixLength = session.prefix.utf16.count
    let insertionRange = NSRange(
        location: session.range.location + (keepPrefixOnCompletion ? prefixLength : 0),
        length: session.filter.utf16.count + (!keepPrefixOnCompletion ? prefixLength : 0)
    )
    
    // Transform range
    guard let range = Range(insertionRange, in: textView.text) else { return }
    let nsrange = NSRange(range, in: textView.text)
    
    // Replace the attributedText with a modified version
    let autocomplete = session.completion?.text ?? ""
    insertAutocomplete(autocomplete, at: session, for: nsrange)
    
    // Move Cursor to the end of the inserted text
    let selectedLocation = insertionRange.location + autocomplete.utf16.count + (appendSpaceOnCompletion ? 1 : 0)
    textView.selectedRange = NSRange(
        location: selectedLocation,
        length: 0
    )
    
    // End the session
    unregisterCurrentSession()
}

But in

func autocompleteManager(_ manager: AutocompleteManager, shouldUnregister prefix: String) -> Bool {
    print("shouldUnregister")
    print(manager.currentSession as Any)
    
    return true
}

it's nil

from inputbaraccessoryview.

nathantannar4 avatar nathantannar4 commented on July 26, 2024

Ok, I don't ever update the currentSession which would be why it's still nil since it's a struct, I never made it with the intention of using the completion property.

  • I will fix that in the next release, but I still would advise that you only check for all the @mentions when someone is done typing, rather than as they are typically because you won’t be able to remove it from your array without making some custom modifications to my code

Sent with GitHawk

from inputbaraccessoryview.

bribar avatar bribar commented on July 26, 2024

Sounds good @nathantannar4. Is v2.0.0 available on pods? It only installs v1.5.4.

from inputbaraccessoryview.

nathantannar4 avatar nathantannar4 commented on July 26, 2024

Hmm it should be available, try pod repo update your local pods might be out of date

Sent with GitHawk

from inputbaraccessoryview.

nathantannar4 avatar nathantannar4 commented on July 26, 2024

This should be resolved in the latest version

from inputbaraccessoryview.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.