MVP+Dagger2+Retrofit2.0+Rxjava看这一个例子就够了

最近有小伙伴问我有没有一个MVP+Dagger2+Retrofit2.0+Rxjava的案例,本身还没彻底集合实现。今天就介绍一个开源项目:owspace。基于MVP+Dagger2+Retrofit2.0+Rxjava的一款APP供你们去学习和参考。java

owspace是一款高仿单读的APP,API接口经过非法手段获取。react

你能学到

  • MVP模式android

  • Retrofit2.0 + rxjavagit

  • Dagger2
  • jsoup解析DOM
  • sqldelight SqlBrite
  • 音视频播放等
  • some fucking source code

owspace
owspace

采用了mvp的模式和dagger2注解。

关于mvp的优点:

  • 模型与视图彻底分离,咱们能够修改视图而不影响模型github

  • 能够更高效地使用模型,由于全部的交互都发生在一个地方——Presenter内部sql

  • 咱们能够将一个Presenter用于多个视图,而不须要改变Presenter的逻辑。这个特性很是的有用,由于视图的变化老是比模型的变化频繁。c#

  • 若是咱们把逻辑放在Presenter中,那么咱们就能够脱离用户接口来测试这些逻辑(单元测试)bash

下面是mvp的架构图:微信

关于mvp还能够参考:session

一个关于MVP+Retrofit+Rxjava应用的实战

Dagger2

看看Dagger2 的流程:

image
image

Dagger2的优势

  • 全局对象实例的简单访问方式

    和ButterKnife 库定义了view,事件处理以及资源的引用同样,Dagger2 提供全局对象引用的简易访问方式。声明了单例的实例均可以使用@inject进行访问。好比下面的MyTwitterApiClient 和SharedPreferences 的实例:

    public class MainActivity extends Activity {
       @Inject 
       MyTwitterApiClient mTwitterApiClient; 
       @Inject
       SharedPreferences sharedPreferences;
    
       public void onCreate(Bundle savedInstance) {     // assign singleton instances to fields
           InjectorClass.inject(this);
       }复制代码
  • 复杂的依赖关系只须要简单的配置

    Dagger2 会经过依赖关系而且生成易懂易分析的代码。之前经过手写的大量模板代码中的对象引用将会由它给你建立并传递到相应对象中。所以你能够更多的关注模块中构建的内容而不是模块中的对象实例的建立顺序。

  • 让单元测试和集成测试更加方便

    由于依赖关系已经为咱们独立出来,因此咱们能够轻松的抽取出不一样的模块进行测试。依赖的注入和配置独立于组件以外。由于对象是在一个独立、不耦合的地方初始化,因此当注入抽象方法的时候,咱们只须要修改对象的实现方法,而不用大改代码库。依赖能够注入到一个组件中:咱们能够注入这些依赖的模拟实现,这样使得测试更加简单。

  • 做用域实例(Scoped instances)

    咱们不只能够轻松的管理全局实例对象,也能够使用Dagger2中的scope定义不一样的做用域。(好比根据user session,activity的生命周期)

效果图:

声明

单读这个文艺的APP本人比较喜欢,一时冲动就抓取了数据,反编译了APP。so,API接口是经过非法手段获取,严禁商用,违者后果自负。

compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile(name: 'SildeMenulibrary-release', ext: 'aar')
    compile(name: 'ObservableScrollView-release', ext: 'aar')
    compile 'com.android.support:appcompat-v7:24.2.1'
    compile 'com.jakewharton:butterknife:7.0.1'
    compile 'com.google.code.gson:gson:2.6.2'
    compile 'net.danlew:android.joda:2.9.3'
    compile 'com.squareup.okhttp3:okhttp:3.4.1'
    compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
    compile 'com.squareup.retrofit2:retrofit:2.1.0'
    compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
    compile 'com.squareup.retrofit2:converter-gson:2.1.0'
    compile 'com.squareup.sqlbrite:sqlbrite:0.7.0'
    compile 'io.reactivex:rxandroid:1.2.1'
    compile 'io.reactivex:rxjava:1.1.6'
    compile 'com.github.bumptech.glide:glide:3.7.0'
    compile 'com.github.bumptech.glide:glide:3.7.0'
    compile 'com.orhanobut:logger:1.15'
    compile 'com.elyeproj.libraries:loaderviewlibrary:1.2.1'
    compile 'in.srain.cube:ultra-ptr:1.0.11'
    compile 'com.android.support:recyclerview-v7:24.2.1'
    compile 'com.android.support:design:24.2.1'
    compile 'org.jsoup:jsoup:1.7.3'
    compile 'pub.devrel:easypermissions:0.2.0'
    compile 'com.wang.avi:library:2.1.3'
    compile 'com.google.dagger:dagger:2.7'
    apt 'com.google.dagger:dagger-compiler:2.7'
    compile 'org.glassfish:javax.annotation:10.0-b28'
    compile 'uk.co.chrisjenx:calligraphy:2.2.0'复制代码

项目地址:

github.com/babylikebir…

相信本身,没有作不到的,只有想不到的

若是你以为此文对您有所帮助,欢迎入群 QQ交流群 :644196190
微信公众号:终端研发部

技术+职场
技术+职场
相关文章
相关标签/搜索