MVP+Dagger2+Retrofit2.0+Rxjava

来源:互联网 发布:阿里云客服网址 编辑:程序博客网 时间:2024/05/19 18:10

今天就介绍一个开源项目:owspace。基于MVP+Dagger2+Retrofit2.0+Rxjava的一款APP供大家去学习和参考。

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

你能学到:

  • MVP模式

  • Retrofit2.0 + rxjava

  • Dagger2

  • jsoup解析DOM

  • sqldelight SqlBrite

  • 音视频播放等

  • some fucking source code


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

关于mvp的优势:

  1. 模型与视图完全分离,我们可以修改视图而不影响模型

  2. 可以更高效地使用模型,因为所有的交互都发生在一个地方——Presenter内部

  3. 我们可以将一个Presenter用于多个视图,而不需要改变Presenter的逻辑。这个特性非常的有用,因为视图的变化总是比模型的变化频繁。

  4. 如果我们把逻辑放在Presenter中,那么我们就可以脱离用户接口来测试这些逻辑(单元测试)



下面是mvp的架构图:


关于mvp还可以参考:

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


Dagger2

看看Dagger2 的流程:



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的生命周期)


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'

项目地址:

https://github.com/babylikebird/owspace

终端研发部提倡 没有做不到的,只有想不到的

原创粉丝点击