Search code examples
androiddesign-patternsmvp

Android Internet Check Layer


I am creating an Android application which basically calls an web service and shows back the response; and I am following the MVP pattern to accomplish it. So the biggest & basic challenge is to check the internet status before every call. I was thinking to create an Abstract layer between the Presenter & the API. So the API request will pass first from the Abstract Internet Layer and if it passes successfully then only the API will get called.
Creating a separate layer looks a better solution in my opinion rather than calling the Internet check logic from Presenter, every time. Please help me with the design if you've any better idea. Thanks


Solution

  • You can create a BasePresenter, that presenter will be a super class of all your presenters. Now in BasePresenter you can write your common APIs (protected/public) stuff like making http request, showing loader, check for internet connectivity etc.

    Each time when you make http call via BasePresenter, your BasePresenter will first check for the requirements and then makes an http call to the server. This is because of, it will be always easy to change anything related to http logic or internet check logic or anything. Because everything will be at one place only (BasePresenter) and easily accessible to the sub presenters.

    So, by this way you can achieve the feature implementation.