├── app ├── .gitignore ├── src │ ├── main │ │ ├── res │ │ │ ├── values │ │ │ │ ├── strings.xml │ │ │ │ ├── colors.xml │ │ │ │ └── styles.xml │ │ │ ├── mipmap-hdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-mdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xxhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xxxhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-anydpi-v26 │ │ │ │ ├── ic_launcher.xml │ │ │ │ └── ic_launcher_round.xml │ │ │ ├── drawable-v24 │ │ │ │ └── ic_launcher_foreground.xml │ │ │ ├── layout │ │ │ │ └── activity_main.xml │ │ │ └── drawable │ │ │ │ └── ic_launcher_background.xml │ │ ├── java │ │ │ └── yang │ │ │ │ └── cehome │ │ │ │ └── com │ │ │ │ └── mvvmdemo │ │ │ │ ├── model │ │ │ │ ├── data │ │ │ │ │ └── Onclick.kt │ │ │ │ ├── local │ │ │ │ │ ├── dao │ │ │ │ │ │ ├── PostEntity.kt │ │ │ │ │ │ └── PostDao.kt │ │ │ │ │ └── AppDatabase.kt │ │ │ │ ├── repository │ │ │ │ │ └── PostRepo.kt │ │ │ │ └── remote │ │ │ │ │ └── PostService.kt │ │ │ │ ├── MyApplication.kt │ │ │ │ ├── viewmodel │ │ │ │ ├── OnclikViewModel.kt │ │ │ │ ├── WeatherViewModel.kt │ │ │ │ └── PostViewModel.kt │ │ │ │ ├── network │ │ │ │ ├── ApiClient.kt │ │ │ │ └── InternalOkHttpClient.kt │ │ │ │ └── view │ │ │ │ └── MainActivity.kt │ │ └── AndroidManifest.xml │ ├── test │ │ └── java │ │ │ └── yang │ │ │ └── cehome │ │ │ └── com │ │ │ └── mvvmdemo │ │ │ └── ExampleUnitTest.kt │ └── androidTest │ │ └── java │ │ └── yang │ │ └── cehome │ │ └── com │ │ └── mvvmdemo │ │ └── ExampleInstrumentedTest.kt ├── proguard-rules.pro └── build.gradle ├── settings.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .idea ├── caches │ └── build_file_checksums.ser ├── encodings.xml ├── vcs.xml ├── kotlinc.xml ├── modules.xml ├── compiler.xml ├── runConfigurations.xml ├── inspectionProfiles │ └── Project_Default.xml ├── codeStyles │ └── Project.xml └── misc.xml ├── local.properties ├── README.md ├── gradle.properties ├── MVVM.iml ├── .gitignore ├── gradlew.bat ├── config.gradle └── gradlew /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | MVVMDemo 3 | 4 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yang0range/MVVM/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /.idea/caches/build_file_checksums.ser: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yang0range/MVVM/HEAD/.idea/caches/build_file_checksums.ser -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yang0range/MVVM/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yang0range/MVVM/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yang0range/MVVM/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yang0range/MVVM/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yang0range/MVVM/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yang0range/MVVM/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yang0range/MVVM/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yang0range/MVVM/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yang0range/MVVM/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yang0range/MVVM/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | ;#Sat Apr 14 19:00:57 CST 2018 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.7-all.zip 7 | -------------------------------------------------------------------------------- /app/src/main/java/yang/cehome/com/mvvmdemo/model/data/Onclick.kt: -------------------------------------------------------------------------------- 1 | package yang.cehome.com.mvvmdemo.model.data 2 | 3 | /** 4 | * @author yangzc 5 | * @data 2018/9/6 13:58 6 | * @desc 数据源Model(MVVM 中的V),负责提供ViewModel中需要处理的数据 7 | * 8 | */ 9 | class Onclick(val who: String, var count: Int) 10 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /.idea/kotlinc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | 8 | 10 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/test/java/yang/cehome/com/mvvmdemo/ExampleUnitTest.kt: -------------------------------------------------------------------------------- 1 | package yang.cehome.com.mvvmdemo 2 | 3 | import org.junit.Test 4 | 5 | import org.junit.Assert.* 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * See [testing documentation](http://d.android.com/tools/testing). 11 | */ 12 | class ExampleUnitTest { 13 | @Test 14 | fun addition_isCorrect() { 15 | assertEquals(4, 2 + 2) 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /app/src/main/java/yang/cehome/com/mvvmdemo/MyApplication.kt: -------------------------------------------------------------------------------- 1 | package yang.cehome.com.mvvmdemo 2 | 3 | import android.app.Application 4 | import com.facebook.stetho.Stetho 5 | 6 | /** 7 | * @author yangzc 8 | * @data 2018/11/2 11:59 9 | * @desc 10 | * 11 | */ 12 | class MyApplication : Application() { 13 | override fun onCreate() { 14 | super.onCreate() 15 | if (BuildConfig.DEBUG) { 16 | Stetho.initializeWithDefaults(this); 17 | } 18 | 19 | } 20 | 21 | } -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /local.properties: -------------------------------------------------------------------------------- 1 | ## This file is automatically generated by Android Studio. 2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED! 3 | # 4 | # This file must *NOT* be checked into Version Control Systems, 5 | # as it contains information specific to your local configuration. 6 | # 7 | # Location of the SDK. This is only used by Gradle. 8 | # For customization when using a Version Control System, please read the 9 | # header note. 10 | #Tue Sep 04 15:37:03 CST 2018 11 | ndk.dir=F\:\\studiosdk\\ndk-bundle 12 | sdk.dir=F\:\\studiosdk 13 | -------------------------------------------------------------------------------- /app/src/main/java/yang/cehome/com/mvvmdemo/model/local/dao/PostEntity.kt: -------------------------------------------------------------------------------- 1 | package yang.cehome.com.mvvmdemo.model.local.dao 2 | 3 | import android.arch.persistence.room.Entity 4 | import android.arch.persistence.room.PrimaryKey 5 | 6 | /** 7 | * @author yangzc 8 | * @data 2018/11/7 10:23 9 | * @desc 创建Post的Entity 10 | * 11 | */ 12 | @Entity 13 | data class PostEntity( 14 | val message: String, 15 | @PrimaryKey 16 | val nu: String, 17 | val ischeck: String, 18 | val condition: String, 19 | val com: String, 20 | val status: String, 21 | val state: String 22 | ) -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/main/java/yang/cehome/com/mvvmdemo/viewmodel/OnclikViewModel.kt: -------------------------------------------------------------------------------- 1 | package yang.cehome.com.mvvmdemo.viewmodel 2 | 3 | import android.databinding.ObservableField 4 | import yang.cehome.com.mvvmdemo.model.data.Onclick 5 | 6 | /** 7 | * @author yangzc 8 | * @data 2018/9/6 16:59 9 | * @desc 处理数据V(MVVM 中的V),负责提供View中需要处理的数据 10 | * 11 | */ 12 | class OnclikViewModel(val onlick: Onclick) { 13 | /******data******/ 14 | val info = ObservableField("\"${onlick.who}点击了${onlick.count}次\"") 15 | 16 | /******binding******/ 17 | fun click() { 18 | onlick.count++ 19 | info.set("\"${onlick.who}点击了${onlick.count}次\"") 20 | } 21 | 22 | } -------------------------------------------------------------------------------- /app/src/main/java/yang/cehome/com/mvvmdemo/model/repository/PostRepo.kt: -------------------------------------------------------------------------------- 1 | package yang.cehome.com.mvvmdemo.model.repository 2 | 3 | import yang.cehome.com.mvvmdemo.model.local.dao.PostDao 4 | import yang.cehome.com.mvvmdemo.model.remote.PostService 5 | 6 | /** 7 | * @author yangzc 8 | * @data 2018/11/6 11:55 9 | * @desc PostRepo 10 | * 11 | */ 12 | class PostRepo constructor(private val remote: PostService, private val local: PostDao){ 13 | //首先查看本地数据库是否有数据 14 | fun getPostInfo() = local.getPostInfo() 15 | .onErrorResumeNext { 16 | //本地数据库不存在,会抛出会抛出EmptyResultSetException 17 | //转而进行获取网络数据,成功后保存在数据库 18 | remote.getPostInfo().doOnSuccess { local.inserttPost(it) } 19 | } 20 | } -------------------------------------------------------------------------------- /app/src/main/java/yang/cehome/com/mvvmdemo/model/remote/PostService.kt: -------------------------------------------------------------------------------- 1 | package yang.cehome.com.mvvmdemo.model.remote 2 | 3 | import io.reactivex.Single 4 | import retrofit2.http.GET 5 | import yang.cehome.com.mvvmdemo.model.local.dao.PostEntity 6 | 7 | /** 8 | * @author yangzc 9 | * @data 2018/9/11 18:13 10 | * @desc PostService 11 | * 12 | */ 13 | interface PostService { 14 | //获取天气 15 | // @GET("/data/sk/101190408.html") 16 | // fun getWeatherInfo(): Single> 17 | 18 | 19 | //获取天气 20 | // @GET("/data/sk/101190408.html") 21 | // fun getWeatherInfos(): Single 22 | 23 | 24 | //获取快递 25 | @GET("/query?type=yuantong&postid=112323") 26 | fun getPostInfo(): Single 27 | 28 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MVVM 2 | 3 | #### 项目介绍 4 | mvvm架构的学习 5 | 6 | #### 博客地址 7 | [MVVM框架的搭建(一)——背景](https://www.jianshu.com/p/60dc39a8553a) 8 | 9 | [MVVM框架的搭建(二)——项目搭建](https://www.jianshu.com/p/6a5a9803d03b) 10 | 11 | [MVVM框架的搭建(三)——网络请求](https://www.jianshu.com/p/df2bde94c0bd) 12 | 13 | [MVVM的数据持久化(一)——ROOM的集成](https://www.jianshu.com/p/0159a5f1c557) 14 | 15 | [MVVM的数据持久化(二)——ROOM的使用](https://www.jianshu.com/p/9e0129c4fbd4) 16 | 17 | ### 欢迎关注公共号 18 | #### 关注公共号会有更多收获! 19 | ![](https://upload-images.jianshu.io/upload_images/3258163-635809c97c6586e8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 20 | 21 | #### 扫一扫,据说年轻、优秀、颜值高的互联网人都在这个群里 22 | ![](https://upload-images.jianshu.io/upload_images/3258163-a7132ff7effe4397.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 23 | -------------------------------------------------------------------------------- /app/src/main/java/yang/cehome/com/mvvmdemo/model/local/dao/PostDao.kt: -------------------------------------------------------------------------------- 1 | package yang.cehome.com.mvvmdemo.model.local.dao 2 | 3 | import android.arch.persistence.room.Dao 4 | import android.arch.persistence.room.Insert 5 | import android.arch.persistence.room.OnConflictStrategy 6 | import android.arch.persistence.room.Query 7 | import io.reactivex.Single 8 | 9 | /** 10 | * @author yangzc 11 | * @data 2018/11/5 17:40 12 | * @desc PostDao 13 | * 14 | */ 15 | @Dao 16 | interface PostDao { 17 | @Insert(onConflict = OnConflictStrategy.REPLACE) 18 | fun insetAll(postinfo: List) 19 | 20 | 21 | @Insert(onConflict = OnConflictStrategy.REPLACE) 22 | fun inserttPost(postinfo: PostEntity) 23 | 24 | @Query("SELECT * FROM postentity") 25 | fun getPostInfo(): Single 26 | 27 | } -------------------------------------------------------------------------------- /app/src/androidTest/java/yang/cehome/com/mvvmdemo/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package yang.cehome.com.mvvmdemo 2 | 3 | import android.support.test.InstrumentationRegistry 4 | import android.support.test.runner.AndroidJUnit4 5 | 6 | import org.junit.Test 7 | import org.junit.runner.RunWith 8 | 9 | import org.junit.Assert.* 10 | 11 | /** 12 | * Instrumented test, which will execute on an Android device. 13 | * 14 | * See [testing documentation](http://d.android.com/tools/testing). 15 | */ 16 | @RunWith(AndroidJUnit4::class) 17 | class ExampleInstrumentedTest { 18 | @Test 19 | fun useAppContext() { 20 | // Context of the app under test. 21 | val appContext = InstrumentationRegistry.getTargetContext() 22 | assertEquals("yang.cehome.com.mvvmdemo", appContext.packageName) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | org.gradle.jvmargs=-Xmx1536m 13 | 14 | # When configured, Gradle will run in incubating parallel mode. 15 | # This option should only be used with decoupled projects. More details, visit 16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 17 | # org.gradle.parallel=true 18 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/main/java/yang/cehome/com/mvvmdemo/viewmodel/WeatherViewModel.kt: -------------------------------------------------------------------------------- 1 | package yang.cehome.com.mvvmdemo.viewmodel 2 | 3 | /** 4 | * @author yangzc 5 | * @data 2018/9/12 14:20 6 | * @desc WeatherViewModel 7 | * 8 | */ 9 | //@Deprecated 10 | //class WeatherViewModel(private val repo: PostRepo) { 11 | // /******data******/ 12 | // val weatherinfo = ObservableField() 13 | // 14 | // /******binding******/ 15 | // fun loadWeather() { 16 | // repo.getWeatherInfo() 17 | // .subscribeOn(Schedulers.io()) 18 | // .observeOn(AndroidSchedulers.mainThread()) 19 | // .subscribe({ t: WeatherInfo? -> 20 | // weatherinfo.set(t?.let { it.toString() }) 21 | // }, { t: Throwable? -> 22 | // weatherinfo.set(t?.message ?: "error") 23 | // }) 24 | // } 25 | //} -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /app/src/main/java/yang/cehome/com/mvvmdemo/network/ApiClient.kt: -------------------------------------------------------------------------------- 1 | package yang.cehome.com.mvvmdemo.network 2 | 3 | import retrofit2.Retrofit 4 | import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory 5 | import retrofit2.converter.gson.GsonConverterFactory 6 | 7 | /** 8 | * @author yangzc 9 | * @data 2018/11/9 14:32 10 | * @desc ApiClient 这样就保证了全局只有一个retrofit对象 11 | * 12 | */ 13 | class ApiClient { 14 | //这里我们可以控制URL 改变URL为测试/线上环境URL 15 | private val BASE_URL = "https://api.douban.com/v2/movie/" 16 | private var retrofit: Retrofit? = null 17 | 18 | init { 19 | retrofit = Retrofit.Builder() 20 | .addConverterFactory(GsonConverterFactory.create()) 21 | .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) 22 | .baseUrl(BASE_URL) 23 | .client(InternalOkHttpClient.getOkhttpClient()) 24 | .build() 25 | } 26 | 27 | 28 | } -------------------------------------------------------------------------------- /MVVM.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /app/src/main/java/yang/cehome/com/mvvmdemo/viewmodel/PostViewModel.kt: -------------------------------------------------------------------------------- 1 | package yang.cehome.com.mvvmdemo.viewmodel 2 | 3 | import android.databinding.ObservableField 4 | import io.reactivex.android.schedulers.AndroidSchedulers 5 | import io.reactivex.schedulers.Schedulers 6 | import yang.cehome.com.mvvmdemo.model.local.dao.PostEntity 7 | import yang.cehome.com.mvvmdemo.model.repository.PostRepo 8 | 9 | /** 10 | * @author yangzc 11 | * @data 2018/11/7 10:26 12 | * @desc PostViewModel 13 | * 14 | */ 15 | class PostViewModel(private val repo: PostRepo) { 16 | /******data******/ 17 | val postinfo = ObservableField() 18 | 19 | /******binding******/ 20 | fun loadpost() { 21 | repo.getPostInfo() 22 | .subscribeOn(Schedulers.io()) 23 | .observeOn(AndroidSchedulers.mainThread()) 24 | .subscribe({ t: PostEntity? -> 25 | postinfo.set(t?.let { it.toString() }) 26 | }, { t: Throwable? -> 27 | postinfo.set(t?.message ?: "error") 28 | }) 29 | } 30 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # Files for the ART/Dalvik VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # Generated files 12 | bin/ 13 | gen/ 14 | out/ 15 | 16 | # Gradle files 17 | .gradle/ 18 | build/ 19 | 20 | # Local configuration file (sdk path, etc) 21 | local.properties 22 | 23 | # Proguard folder generated by Eclipse 24 | proguard/ 25 | 26 | # Log Files 27 | *.log 28 | 29 | # Android Studio Navigation editor temp files 30 | .navigation/ 31 | 32 | # Android Studio captures folder 33 | captures/ 34 | 35 | # IntelliJ 36 | *.iml 37 | .idea/workspace.xml 38 | .idea/tasks.xml 39 | .idea/gradle.xml 40 | .idea/dictionaries 41 | .idea/libraries 42 | 43 | # Keystore files 44 | # Uncomment the following line if you do not want to check your keystore files in. 45 | #*.jks 46 | 47 | # External native build folder generated in Android Studio 2.2 and later 48 | .externalNativeBuild 49 | 50 | # Google Services (e.g. APIs or Firebase) 51 | google-services.json 52 | 53 | # Freeline 54 | freeline.py 55 | freeline/ 56 | freeline_project_description.json 57 | -------------------------------------------------------------------------------- /app/src/main/java/yang/cehome/com/mvvmdemo/model/local/AppDatabase.kt: -------------------------------------------------------------------------------- 1 | package yang.cehome.com.mvvmdemo.model.local 2 | 3 | import android.arch.persistence.room.Database 4 | import android.arch.persistence.room.Room 5 | import android.arch.persistence.room.RoomDatabase 6 | import android.content.Context 7 | import yang.cehome.com.mvvmdemo.model.local.dao.PostDao 8 | import yang.cehome.com.mvvmdemo.model.local.dao.PostEntity 9 | 10 | /** 11 | * @author yangzc 12 | * @data 2018/11/5 18:21 13 | * @desc 包含所有Entity以及操作它们的 DAO 14 | * 15 | */ 16 | @Database(entities = arrayOf(PostEntity::class), version = 1) 17 | abstract class AppDatabase : RoomDatabase() { 18 | abstract fun PostDao(): PostDao 19 | 20 | companion object { 21 | @Volatile 22 | private var INSTANCE: AppDatabase? = null 23 | 24 | fun getInstance(context: Context): AppDatabase = 25 | INSTANCE ?: synchronized(this) { 26 | INSTANCE ?: buildDatabase(context).also { INSTANCE = it } 27 | } 28 | 29 | private fun buildDatabase(context: Context) = 30 | Room.databaseBuilder(context.applicationContext, 31 | AppDatabase::class.java, "app.db") 32 | .build() 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /app/src/main/java/yang/cehome/com/mvvmdemo/network/InternalOkHttpClient.kt: -------------------------------------------------------------------------------- 1 | package yang.cehome.com.mvvmdemo.network 2 | 3 | import com.facebook.stetho.okhttp3.StethoInterceptor 4 | import okhttp3.OkHttpClient 5 | import okhttp3.logging.HttpLoggingInterceptor 6 | import yang.cehome.com.mvvmdemo.BuildConfig 7 | import java.util.concurrent.TimeUnit 8 | 9 | class InternalOkHttpClient { 10 | companion object { 11 | 12 | fun getOkhttpClient(): OkHttpClient { 13 | var okHttpClient: OkHttpClient? = null 14 | 15 | if (okHttpClient == null) { 16 | okHttpClient = OkHttpClient.Builder() 17 | .retryOnConnectionFailure(true) 18 | .connectTimeout(60, TimeUnit.SECONDS) 19 | .readTimeout(60, TimeUnit.SECONDS) 20 | .writeTimeout(60, TimeUnit.SECONDS) 21 | .build() 22 | 23 | if (BuildConfig.DEBUG) {//printf logs while debug 24 | okHttpClient = okHttpClient?.newBuilder()?.addNetworkInterceptor(StethoInterceptor())?.addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BASIC))?.build() 25 | } 26 | } 27 | return okHttpClient!! 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /.idea/inspectionProfiles/Project_Default.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 36 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'kotlin-android' 3 | apply plugin: 'kotlin-kapt' 4 | apply plugin: 'kotlin-android-extensions' 5 | apply from: "${rootProject.rootDir}/config.gradle" 6 | 7 | android { 8 | compileSdkVersion 27 9 | buildToolsVersion '27.0.3' 10 | 11 | defaultConfig { 12 | minSdkVersion project.ext.minSdkVersion 13 | targetSdkVersion project.ext.targetSdkVersion 14 | versionCode project.ext.module_appVersionCode 15 | versionName project.ext.module_appVersionName 16 | applicationId project.ext.module_appApplicationId 17 | multiDexEnabled true 18 | 19 | manifestPlaceholders = [ 20 | app_name: project.ext.module_appName 21 | 22 | ] 23 | } 24 | buildTypes { 25 | release { 26 | minifyEnabled false 27 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 28 | } 29 | } 30 | 31 | dataBinding { 32 | enabled = true 33 | } 34 | 35 | kapt { 36 | generateStubs = true 37 | } 38 | 39 | android { 40 | compileOptions { 41 | sourceCompatibility 1.8 42 | targetCompatibility 1.8 43 | } 44 | } 45 | 46 | } 47 | 48 | dependencies { 49 | implementation fileTree(include: ['*.jar'], dir: 'libs') 50 | implementation project.ext.kotlinDeps 51 | implementation project.ext.supportDeps 52 | implementation project.ext.networkDeps 53 | implementation project.ext.rxDeps 54 | implementation project.ext.stethoDeps 55 | implementation project.ext.roomDeps 56 | implementation project.ext.okhttpDep 57 | kapt project.ext.roomDep 58 | kapt project.ext.databindingDeps 59 | testImplementation 'junit:junit:4.12' 60 | } 61 | -------------------------------------------------------------------------------- /.idea/codeStyles/Project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 15 | 16 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/java/yang/cehome/com/mvvmdemo/view/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package yang.cehome.com.mvvmdemo.view 2 | 3 | import android.databinding.DataBindingUtil 4 | import android.os.Bundle 5 | import android.support.v7.app.AppCompatActivity 6 | import com.facebook.stetho.okhttp3.StethoInterceptor 7 | import okhttp3.OkHttpClient 8 | import retrofit2.Retrofit 9 | import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory 10 | import retrofit2.converter.gson.GsonConverterFactory 11 | import yang.cehome.com.mvvmdemo.R 12 | import yang.cehome.com.mvvmdemo.databinding.ActivityMainBinding 13 | import yang.cehome.com.mvvmdemo.model.data.Onclick 14 | import yang.cehome.com.mvvmdemo.model.local.AppDatabase 15 | import yang.cehome.com.mvvmdemo.model.remote.PostService 16 | import yang.cehome.com.mvvmdemo.model.repository.PostRepo 17 | import yang.cehome.com.mvvmdemo.viewmodel.OnclikViewModel 18 | import yang.cehome.com.mvvmdemo.viewmodel.PostViewModel 19 | 20 | 21 | 22 | 23 | /** 24 | * MVVM 当中的一个V层 将三者联系起来 25 | */ 26 | class MainActivity : AppCompatActivity() { 27 | private lateinit var mBinding: ActivityMainBinding 28 | private lateinit var mViewMode: OnclikViewModel 29 | private lateinit var mViewMode2: PostViewModel 30 | 31 | 32 | override fun onCreate(savedInstanceState: Bundle?) { 33 | super.onCreate(savedInstanceState) 34 | 35 | mBinding = DataBindingUtil.setContentView(this, R.layout.activity_main) 36 | /////model 37 | val onclick = Onclick("me", 0) 38 | ///ViewModel 39 | mViewMode = OnclikViewModel(onclick) 40 | ///binding 41 | 42 | val client = OkHttpClient.Builder() 43 | .addNetworkInterceptor(StethoInterceptor()) 44 | .build() 45 | 46 | val remote = Retrofit.Builder() 47 | .baseUrl("http://www.kuaidi100.com") 48 | .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) 49 | .addConverterFactory(GsonConverterFactory.create()) 50 | .client(client) 51 | .build().create(PostService::class.java) 52 | val local= AppDatabase.getInstance(applicationContext).PostDao() 53 | val repo = PostRepo(remote, local) 54 | 55 | ////ViewModel2 56 | mViewMode2 = PostViewModel(repo) 57 | mBinding.vm = mViewMode 58 | ////binding 59 | mBinding.post = mViewMode2 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 11 | 12 | 15 | 16 | 17 | 18 | 19 | 24 | 25 | 29 | 30 |