1
votes

Is it okay a presenter having Android Handler?
I know presenters should not have any Android related objects, but I really don't have any clear answer.

Here's the thing, this presenter runs a disk IO task on another thread, meanwhile, the activity has to change its view.

These jobs are supposed to be done concurrently. So I decided to pass the activity's handler as an argument and let presenter send message to activity like this:

class FooPresenter: FooContract.Presenter {
    …
    private fun doDiskIOTask(handler: Handler) {
            handler.sendEmptyMessage(0)
        do_something_on_new_thread_and_join()
        handler.sendEmptyMessage(1)
    }
    …
}

The activity has to know when the task is started and finished both and change view. Could you tell me if I'm doing it any wrong or the better way?

1

1 Answers

1
votes

Passing a Handler to the presenter doesn't sound like a good idea, you wouldn't be able to unit test it. I think a better approach would be to just call a method on the view and then run on UI thread from there. If your view interface is an Activity you can use the very convenient runOnUiThread method, e.g.

class MyActivity : AppCompatActivity, FooContract.View {

    override fun emptyMessage0Method(){
        runOnUiThread { 
            // manipulate views here
        }
    }

}