Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instantiating an Interface Listener in Kotlin

I cannot, for the life of me, instantiate an interface outside of a fragment in Kotlin or Kotlin for Android. It was standard procedure in Java to say something like:

MyInterface mInterfaceListener = new MyInterface(this);
mInterfaceListener.invokeSomeGenericMethod();

Note that mInterfaceListener is referring to an Interface, not an onCLickListener or anything like that

How are interfaces instantiated in Kotlin? How do I make a "listener" and trigger an interface's functions?

Below are some attempts in a very simple app I am doing for learning purposes. Notice the variable mPresenterListener which is an Interface

    class QuoteActivity : QuoteContract.ViewOps, AppCompatActivity() {

    private lateinit var vText: TextView
    private lateinit var vFab: FloatingActionButton
    private lateinit var mPresenterListener: QuoteContract.PresenterOperations

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        mPresenterListener = this.mPresenterListener
        vText=findViewById(R.id.main_quote)
        vFab=findViewById(R.id.main_fab)
        vFab.setOnClickListener{
            mPresenterListener.onQuoteRequested()
        }
    }

    override fun displayQuote(quote: String) {
        vText.text = quote
    }

}

And my presenter:

    class QuotePresenter(private val viewListener: QuoteContract.ViewOps): QuoteContract.PresenterOperations {

    private lateinit var modelListener: QuoteContract.ModelOperations

    init {
        modelListener = this.modelListener
    }


    override fun onQuoteRequested() {
        modelListener.generateQuote()
    }

    override fun onQuoteGenerated(quote: String) {
        viewListener.displayQuote(quote)
    }


}

The interface:

interface QuoteContract {



//Methods available to Presenter (Presenter -> View)
interface ViewOps{
    fun displayQuote(quote: String)
}


//Ops offered from presenter to view (Presenter->View)

interface PresenterOperations {
    //Presenter->View
    fun onQuoteRequested()
    //Presenter->Model
    fun onQuoteGenerated(quote: String)
}

//Ops offered from Model to Presenter (Model -> Presenter)
interface ModelOperations {
    fun generateQuote()
}

}

like image 951
Josh Ribeiro Avatar asked Nov 30 '17 03:11

Josh Ribeiro


1 Answers

You can do watchers/listeners like this:

val textView: TextView = this.findViewById(R.id.amountEdit)
val watcher = object : TextWatcher {
    override fun afterTextChanged(p0: Editable?) {
        val inputAmount = textView.text.toString
        val amount = if (!inputAmount.isEmpty()) inputAmount.toDouble() else 0.0
        conversionViewModel?.convert(amount)
        }

    override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
        println("before text changed called..")
    }

    override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
        println("text changed..")
    }
}

and then:

textView.addTextChangedListener(watcher)

Notice the object in declaring the watcher. Can do the same with a listener.

like image 156
Rob Avatar answered Nov 04 '22 13:11

Rob