Build MVC project from scratch

Keywords: OkHttp Retrofit network Google

Preface

This article mainly records how to build an MVP architecture. At the same time, it realizes the network request by combining with the mainstream frameworks such as Retrofit,RxJava,Okhttp, which is convenient for later review and rapid development.

Project subcontracting

Base: put some base classes, such as Activity,Adapter,Present, etc
 common: store constant, Application
 Model: model layer, which stores entity classes and network requests
 presenter:P layer, receive the command of View layer, call M layer to get data, and notify View layer to update
 util: store some tool classes
 view:View layer, including Activity,Fragment,adapter, custom view, etc

Framework dependency used

//RxJava
    compile 'io.reactivex:rxjava:1.1.6'
    compile 'com.trello:rxlifecycle:0.6.1'
    compile 'com.trello:rxlifecycle-components:0.6.1'
    //Picasso
    compile 'com.squareup.picasso:picasso:2.5.2'
    //Retrofit Library
    compile 'com.squareup.retrofit2:retrofit:2.0.2'
    compile 'com.squareup.retrofit2:converter-gson:2.0.2'
    compile 'com.squareup.retrofit2:adapter-rxjava:2.0.2'
    //Switch Android main thread
    compile 'io.reactivex:rxandroid:1.0.1'
    // Okhttp Library
    compile 'com.squareup.okhttp3:okhttp:3.1.2'
    //Dagger2 (not used temporarily)
    compile 'com.google.dagger:dagger:2.4'
    apt 'com.google.dagger:dagger-compiler:2.4'
    //java annotation (not used temporarily)
    compile 'org.glassfish:javax.annotation:10.0-b28'
    //Journal
    compile 'com.orhanobut:logger:1.15'
    //okhttp universal parameter interceptor (not used temporarily)
    compile 'com.github.jkyeo:okhttp-basicparamsinterceptor:v0.9'

View layer implementation

First, we define a MvpView

/**
 * Base class for all views
 */

public interface MvpView {
}

Our own View needs to implement this interface

public interface IMovieView extends MvpView {
    void showLoading();
    void hideLoading();
    void showError(Throwable e);
    void setMovieInfo(MovieInfo movieInfo);
}

Presenter layer

/**
 *  P Layer interface base class, need to associate View
 */
interface MvpPresenter<V extends MvpView>  {
    //Associated View
    void attachView(V view);
    //Disassociate
    void  detachView();
}
/**
 *  Implementing the concrete logic of Presenter layer
 */

public class BasePresenter<V extends MvpView> implements MvpPresenter<V>{
    private V mMvpView;
    //Manage RxJava subscriptions
    protected CompositeSubscription mCompositeSubscription;
    //Associated View
    @Override
    public void attachView(V view) {
        mMvpView = view;
        mCompositeSubscription = new CompositeSubscription();
    }

    @Override
    public void detachView() {
        mMvpView = null;
        mCompositeSubscription.clear();
        mCompositeSubscription = null;
    }

    protected V getMvpView() {
        return mMvpView;
    }
}

Processing in Activity

public abstract class BaseActivity extends AppCompatActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base);
        initPresenter();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        detachView();
    }
    //Need subclass to initialize Presenter
    public abstract void initPresenter();
    //Remove View and RxJava to prevent memory leakage
    public abstract void detachView();
}

Above are some code instances of the project. The complete code can be downloaded and viewed by Demo
Click here to download the Demo source code

Then post the final effect

Posted by VapiD on Sun, 03 May 2020 11:56:40 -0700