preface

  • This year’s MVP in the circle of Android technology, Dagger2 Rxjava, Retrofit of these words is very fire, literally open a technical BBS are filled with a lot of articles about the technology, making also filled with all sorts of based on the MVP + Retrofit + Rxjava + Dagger2 + M AterialDesign developed an open source project or Demo under the title XXXX.

  • But you so enthusiastic open source projects such as these, professor always repeat doing the same thing you ever thought of methods and techniques used by relying on a third-party library that can quickly build such framework?

What is MVPArms?

  • MVPArms is an integration of a number of major Android frameworks, all managed using Dagger2, and provides apis to connect all libraries for easy use, as well as detailed Wiki documentation.

  • Behind it can make the development of all projects don’t have to repeat the copy and paste (used friends should know that such a framework, these libraries are dependent on other libraries, even if a build gradle will waste a lot of time), a dependence on save a lot of troubles, and framework is difficult for beginners that not only is the use of API, the more difficult is how to put them Together, it should be used in various scenarios.

  • For a new Android project, especially if you are familiar with Dagger2 and Rxjava, you just need to Clone the project, the Demo just implements a page, remove the page and add the Retrofit you need API, your framework is set up, you can use Demo directly for subsequent development, package structure is suitable for subsequent extensions.

Feature

  • Support the development of large projects for easy expansion,Demo package structure can be directly used
  • All useDagger2management
  • A lot of useRxjava
  • provideMvpBase class, fast access
  • allUIThe adaptive
  • The image-loading class ImageLoader uses Policy mode and Builder mode to easily switch between image-loading frameworks and extensions
  • The Model layer provides the Retrofit API and the cache, which is optional
  • globalhttp Request(Params.headers) Response(Params.Time consuming) information monitoring, can parse JSON according to the status code to do the corresponding operation
  • globalRxjavaError handling, automatic retry after error

Where?

MVPArms welcomeStarandFork

Structure

How?

Wiki

For details, please refer to the Wiki. Here is a brief introduction to the MVP

Contract

According to Google’s official MVP project, MVP interface can be defined in Contract for easy management. This framework uses Dagger to inject Presenter without defining Presenter interface, so Contract only defines Model and View interface

Public Interface UserContract {// The BaseView extends BaseView {void for commonly used UI methods, such as displaying hidden progress bars and displaying text messages setAdapter(DefaultAdapter adapter); void startLoadMore(); void endLoadMore(); } // Interface Model {Observable> getUsers(int lastIdQueried, boolean update); }}Copy the code

View

The View interface for an Activity or Fragment is defined in a Contract. The BaseActivity is injected with Presenter by default. If you want to use Presenter, you must specify the Presenter template, and Implement setupActivityComponent to provide the components and modules required by Presenter

public class UserActivity extends WEActivity implements UserContract.View {

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerUserComponent
                .builder()
                .appComponent(appComponent)
                .userModule(new UserModule(this))
                .build()
                .inject(this);

    }

    @Override
    protected View initView() {
        return LayoutInflater.from(this).inflate(R.layout.activity_user, null, false);
    }

    @Override
    protected void initData() {
       }
}
Copy the code

Model

Model implements the Model interface of the Contract, inherits BaseModel, specifies the ServiceManager and CacheManager templates, and then obtains the required Service and Cache from the two managers to provide the data required by the Presenter ( Caching is optional,Presenter does not need to care about details.)

public class UserModel extends BaseModel implements UserContract.Model{
    private CommonService mCommonService;
    private CommonCache mCommonCache;

    public UserModel(ServiceManager serviceManager, CacheManager cacheManager) {
        super(serviceManager, cacheManager);
        this.mCommonService = mServiceManager.getCommonService();
        this.mCommonCache = mCacheManager.getCommonCache();
    }
    
    @Override
    public Observable> getUsers(int lastIdQueried, boolean update) {
    
    }
  
}

Copy the code

Presenter

Most of the role of Presenter in MVP is to get data from the Model layer interface and display data by calling the View layer interface. First implement BasePresenter, specify the Model and View template. Note that you must specify the interface defined in Contract, which is required by Presenter Dagger2 injection is used for both Model and View.

@ActivityScope public class UserPresenter extends BasePresenter { @Inject public UserPresenter(UserContract.Model model,  UserContract.View rootView) { super(model, rootView); Public void requestUsers(final Boolean pullToRefresh) {}}Copy the code

Acknowledgement

Thanks to all the authors of the tripartite libraries used in this framework, as well as all the Developers and Organizations that contributed selflessly to Open Sourece so that we can work and learn better, I will also give back my spare time to the Open source community