Android MVP where check your internet connection

I am using the MVP pattern in an Andorid app, and I have doubts about where is the best place to test your Internet connection. I usually check if there is an Internet connection before making any kind of network call.

So where should I check this in Activity or in Presenter ? I think the Presenter will be a good place, so he decides what to do, however, I'm not 100% sure. If I have to put it in action and not make a call to the Leader.

+8
android mvp
source share
2 answers

I don’t think the Presenter is a good place. The host must set new data from the model, for example getData (). The presenter does not need to know if he is from the local database or from the server. So checking your internet connection with Presenter would not be a good idea.

If you are using a repository template, Presenter will ask the model / repository for data. First, the model will send local data to the master. In parallel, it will send a server request (if there is a network connection) to download new data and send the new data to the presenter.

So, I think the network check should be in the repository / model. You may have a Util class that implements a valid network validation code. And call this method from the repository, for example AppUtil.isNetworkConnectionAvailable();

For more information see https://github.com/googlesamples/android-architecture/tree/todo-mvp/

+4
source share

Decision: -

You should check the availability of the Internet connection in the BaseActivity class, and then expand this activity, this is the best practice that I think of.

In my project, I do the following: -

 public boolean isInternetAvailable() { return internet.isAvailable(); } 
0
source share

All Articles