F
F
foonfyrick2020-09-03 12:22:05
Android
foonfyrick, 2020-09-03 12:22:05

Is the MVP implemented correctly?

I don’t quite understand if I’m doing everything right, and when rewriting the application, I couldn’t figure out where to write Thread ...
I read that it connects to the model somewhere, receives data from there, does something with them, that is, the model has methods: get data, process data, give data.
View responds to events on the screen by showing messages.
The presenter reacts to the called methods in the view, taking data from the view for processing into the model and giving the data already processed by the model.

Trying to implement something like this, I got this picture:
VIEW

class MainActivity : AppCompatActivity(),TIView {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val tpresenter=Presenter(this)
        tpresenter.giveResult()
    }
    override fun showResult(message: String) {
        runOnUiThread {
            tvText.setText(message)
        }
    }
}

PRESENTER
class Presenter(val tiView: TIView) {
    val tmodel=TModel()
    fun giveResult(){
        Thread{
            tiView.showResult(tmodel.getTitle())
        }.start()
    }
}

MODEL
class TModel {
    fun getTitle():String{
        val document= Jsoup.connect("example").get()
        return document.title()
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmtm, 2020-09-03
@foonfyrick

what happens to the presenter when the screen is rotated?
what happens to a thread that wants to access a destroyed View?
there are two options - to make singleton presenters in the Application, then they will survive the turns
, or, more correctly, to consider long operations as part of the model and then let the presenter be created every time and simply subscribe to changes in the model and run asynchronous methods somewhere, not at itself, the presenter is just logic taken out of the activity

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question