├── .gitignore ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── kotlin │ │ └── net │ │ └── gahfy │ │ └── mvvmposts │ │ └── ExampleInstrumentedTest.kt │ ├── main │ ├── AndroidManifest.xml │ ├── kotlin │ │ └── net │ │ │ └── gahfy │ │ │ └── mvvmposts │ │ │ ├── base │ │ │ └── BaseViewModel.kt │ │ │ ├── injection │ │ │ ├── ViewModelFactory.kt │ │ │ ├── component │ │ │ │ └── ViewModelInjector.kt │ │ │ └── module │ │ │ │ └── NetworkModule.kt │ │ │ ├── model │ │ │ ├── Post.kt │ │ │ ├── PostDao.kt │ │ │ └── database │ │ │ │ └── AppDatabase.kt │ │ │ ├── network │ │ │ └── PostApi.kt │ │ │ ├── ui │ │ │ └── post │ │ │ │ ├── PostListActivity.kt │ │ │ │ ├── PostListAdapter.kt │ │ │ │ ├── PostListViewModel.kt │ │ │ │ └── PostViewModel.kt │ │ │ └── utils │ │ │ ├── BindingAdapters.kt │ │ │ ├── Constants.kt │ │ │ └── extension │ │ │ └── ViewExtension.kt │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ ├── activity_post_list.xml │ │ └── item_post.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.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 │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── kotlin │ └── net │ └── gahfy │ └── mvvmposts │ └── ExampleUnitTest.kt ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | .idea 5 | .DS_Store 6 | /build 7 | /captures 8 | .externalNativeBuild 9 | app/schemas/* 10 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'kotlin-android' 3 | apply plugin: 'kotlin-android-extensions' 4 | apply plugin: 'kotlin-kapt' 5 | 6 | android { 7 | compileSdkVersion 28 8 | 9 | dataBinding { 10 | enabled = true 11 | } 12 | 13 | defaultConfig { 14 | applicationId "net.gahfy.mvvmposts" 15 | minSdkVersion 15 16 | targetSdkVersion 28 17 | versionCode 1 18 | versionName "1.0" 19 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 20 | 21 | kapt { 22 | arguments { 23 | arg("room.schemaLocation", "$projectDir/schemas".toString()) 24 | } 25 | } 26 | 27 | } 28 | buildTypes { 29 | release { 30 | minifyEnabled false 31 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 32 | } 33 | } 34 | sourceSets { 35 | main.java.srcDirs += 'src/main/kotlin' 36 | test.java.srcDirs = ['src/test/kotlin'] 37 | androidTest.java.srcDirs = ['src/androidTest/kotlin'] 38 | } 39 | } 40 | 41 | dependencies { 42 | implementation fileTree(dir: 'libs', include: ['*.jar']) 43 | implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 44 | implementation "com.android.support:appcompat-v7:$android_support_version" 45 | testImplementation 'junit:junit:4.12' 46 | androidTestImplementation 'com.android.support.test:runner:1.0.2' 47 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' 48 | 49 | // Support Design 50 | implementation "com.android.support:design:$android_support_version" 51 | 52 | // RecyclerView 53 | implementation "com.android.support:recyclerview-v7:$android_support_version" 54 | 55 | // Constraint Layout 56 | implementation "com.android.support.constraint:constraint-layout:1.1.3" 57 | 58 | // LiveData & ViewModel 59 | implementation"android.arch.lifecycle:extensions:$lifecycle_version" 60 | 61 | // Room 62 | implementation "android.arch.persistence.room:runtime:$room_version" 63 | kapt "android.arch.persistence.room:compiler:$room_version" 64 | 65 | // Retrofit 66 | implementation "com.squareup.retrofit2:retrofit:$retrofit_version" 67 | implementation "com.squareup.retrofit2:adapter-rxjava2:$retrofit_version" 68 | implementation "com.squareup.retrofit2:converter-moshi:$retrofit_version" 69 | 70 | // Dagger 2 71 | implementation "com.google.dagger:dagger:$dagger2_version" 72 | kapt "com.google.dagger:dagger-compiler:$dagger2_version" 73 | compileOnly "org.glassfish:javax.annotation:3.1.1" 74 | 75 | //Rx 76 | implementation "io.reactivex.rxjava2:rxjava:2.2.0" 77 | implementation "io.reactivex.rxjava2:rxandroid:2.1.0" 78 | } 79 | -------------------------------------------------------------------------------- /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/androidTest/kotlin/net/gahfy/mvvmposts/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package net.gahfy.mvvmposts 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("net.gahfy.mvvmposts", appContext.packageName) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | 13 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /app/src/main/kotlin/net/gahfy/mvvmposts/base/BaseViewModel.kt: -------------------------------------------------------------------------------- 1 | package net.gahfy.mvvmposts.base 2 | 3 | import android.arch.lifecycle.ViewModel 4 | import net.gahfy.mvvmposts.injection.component.DaggerViewModelInjector 5 | import net.gahfy.mvvmposts.injection.component.ViewModelInjector 6 | import net.gahfy.mvvmposts.injection.module.NetworkModule 7 | import net.gahfy.mvvmposts.ui.post.PostListViewModel 8 | import net.gahfy.mvvmposts.ui.post.PostViewModel 9 | 10 | abstract class BaseViewModel:ViewModel(){ 11 | private val injector: ViewModelInjector = DaggerViewModelInjector 12 | .builder() 13 | .networkModule(NetworkModule) 14 | .build() 15 | 16 | init { 17 | inject() 18 | } 19 | 20 | /** 21 | * Injects the required dependencies 22 | */ 23 | private fun inject() { 24 | when (this) { 25 | is PostListViewModel -> injector.inject(this) 26 | is PostViewModel -> injector.inject(this) 27 | } 28 | } 29 | } -------------------------------------------------------------------------------- /app/src/main/kotlin/net/gahfy/mvvmposts/injection/ViewModelFactory.kt: -------------------------------------------------------------------------------- 1 | package net.gahfy.mvvmposts.injection 2 | 3 | import android.arch.lifecycle.ViewModel 4 | import android.arch.lifecycle.ViewModelProvider 5 | import android.arch.persistence.room.Room 6 | import android.support.v7.app.AppCompatActivity 7 | import net.gahfy.mvvmposts.model.database.AppDatabase 8 | import net.gahfy.mvvmposts.ui.post.PostListViewModel 9 | 10 | class ViewModelFactory(private val activity: AppCompatActivity): ViewModelProvider.Factory{ 11 | override fun create(modelClass: Class): T { 12 | if (modelClass.isAssignableFrom(PostListViewModel::class.java)) { 13 | val db = Room.databaseBuilder(activity.applicationContext, AppDatabase::class.java, "posts").build() 14 | @Suppress("UNCHECKED_CAST") 15 | return PostListViewModel(db.postDao()) as T 16 | } 17 | throw IllegalArgumentException("Unknown ViewModel class") 18 | 19 | } 20 | } -------------------------------------------------------------------------------- /app/src/main/kotlin/net/gahfy/mvvmposts/injection/component/ViewModelInjector.kt: -------------------------------------------------------------------------------- 1 | package net.gahfy.mvvmposts.injection.component 2 | 3 | import dagger.Component 4 | import net.gahfy.mvvmposts.injection.module.NetworkModule 5 | import net.gahfy.mvvmposts.ui.post.PostListViewModel 6 | import net.gahfy.mvvmposts.ui.post.PostViewModel 7 | import javax.inject.Singleton 8 | 9 | /** 10 | * Component providing inject() methods for presenters. 11 | */ 12 | @Singleton 13 | @Component(modules = [(NetworkModule::class)]) 14 | interface ViewModelInjector { 15 | /** 16 | * Injects required dependencies into the specified PostListViewModel. 17 | * @param postListViewModel PostListViewModel in which to inject the dependencies 18 | */ 19 | fun inject(postListViewModel: PostListViewModel) 20 | /** 21 | * Injects required dependencies into the specified PostViewModel. 22 | * @param postViewModel PostViewModel in which to inject the dependencies 23 | */ 24 | fun inject(postViewModel: PostViewModel) 25 | 26 | @Component.Builder 27 | interface Builder { 28 | fun build(): ViewModelInjector 29 | 30 | fun networkModule(networkModule: NetworkModule): Builder 31 | } 32 | } -------------------------------------------------------------------------------- /app/src/main/kotlin/net/gahfy/mvvmposts/injection/module/NetworkModule.kt: -------------------------------------------------------------------------------- 1 | package net.gahfy.mvvmposts.injection.module 2 | 3 | import dagger.Module 4 | import dagger.Provides 5 | import dagger.Reusable 6 | import io.reactivex.schedulers.Schedulers 7 | import net.gahfy.mvvmposts.network.PostApi 8 | import net.gahfy.mvvmposts.utils.BASE_URL 9 | import retrofit2.Retrofit 10 | import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory 11 | import retrofit2.converter.moshi.MoshiConverterFactory 12 | 13 | /** 14 | * Module which provides all required dependencies about network 15 | */ 16 | @Module 17 | // Safe here as we are dealing with a Dagger 2 module 18 | @Suppress("unused") 19 | object NetworkModule { 20 | /** 21 | * Provides the Post service implementation. 22 | * @param retrofit the Retrofit object used to instantiate the service 23 | * @return the Post service implementation. 24 | */ 25 | @Provides 26 | @Reusable 27 | @JvmStatic 28 | internal fun providePostApi(retrofit: Retrofit): PostApi { 29 | return retrofit.create(PostApi::class.java) 30 | } 31 | 32 | /** 33 | * Provides the Retrofit object. 34 | * @return the Retrofit object 35 | */ 36 | @Provides 37 | @Reusable 38 | @JvmStatic 39 | internal fun provideRetrofitInterface(): Retrofit { 40 | return Retrofit.Builder() 41 | .baseUrl(BASE_URL) 42 | .addConverterFactory(MoshiConverterFactory.create()) 43 | .addCallAdapterFactory(RxJava2CallAdapterFactory.createWithScheduler(Schedulers.io())) 44 | .build() 45 | } 46 | } -------------------------------------------------------------------------------- /app/src/main/kotlin/net/gahfy/mvvmposts/model/Post.kt: -------------------------------------------------------------------------------- 1 | package net.gahfy.mvvmposts.model 2 | 3 | import android.arch.persistence.room.Entity 4 | import android.arch.persistence.room.PrimaryKey 5 | 6 | /** 7 | * Class which provides a model for post 8 | * @constructor Sets all properties of the post 9 | * @property userId the unique identifier of the author of the post 10 | * @property id the unique identifier of the post 11 | * @property title the title of the post 12 | * @property body the content of the post 13 | */ 14 | @Entity 15 | data class Post( 16 | val userId: Int, 17 | @field:PrimaryKey 18 | val id: Int, 19 | val title: String, 20 | val body: String 21 | ) -------------------------------------------------------------------------------- /app/src/main/kotlin/net/gahfy/mvvmposts/model/PostDao.kt: -------------------------------------------------------------------------------- 1 | package net.gahfy.mvvmposts.model 2 | 3 | import android.arch.persistence.room.Dao 4 | import android.arch.persistence.room.Insert 5 | import android.arch.persistence.room.Query 6 | 7 | @Dao 8 | interface PostDao { 9 | @get:Query("SELECT * FROM post") 10 | val all: List 11 | 12 | @Insert 13 | fun insertAll(vararg users: Post) 14 | } -------------------------------------------------------------------------------- /app/src/main/kotlin/net/gahfy/mvvmposts/model/database/AppDatabase.kt: -------------------------------------------------------------------------------- 1 | package net.gahfy.mvvmposts.model.database 2 | 3 | import android.arch.persistence.room.Database 4 | import android.arch.persistence.room.RoomDatabase 5 | import net.gahfy.mvvmposts.model.Post 6 | import net.gahfy.mvvmposts.model.PostDao 7 | 8 | @Database(entities = [Post::class], version = 1) 9 | abstract class AppDatabase : RoomDatabase() { 10 | abstract fun postDao(): PostDao 11 | } -------------------------------------------------------------------------------- /app/src/main/kotlin/net/gahfy/mvvmposts/network/PostApi.kt: -------------------------------------------------------------------------------- 1 | package net.gahfy.mvvmposts.network 2 | 3 | import io.reactivex.Observable 4 | import net.gahfy.mvvmposts.model.Post 5 | import retrofit2.http.GET 6 | 7 | /** 8 | * The interface which provides methods to get result of webservices 9 | */ 10 | interface PostApi { 11 | /** 12 | * Get the list of the pots from the API 13 | */ 14 | @GET("/posts") 15 | fun getPosts(): Observable> 16 | } -------------------------------------------------------------------------------- /app/src/main/kotlin/net/gahfy/mvvmposts/ui/post/PostListActivity.kt: -------------------------------------------------------------------------------- 1 | package net.gahfy.mvvmposts.ui.post 2 | 3 | import android.arch.lifecycle.Observer 4 | import android.arch.lifecycle.ViewModelProviders 5 | import android.databinding.DataBindingUtil 6 | import android.os.Bundle 7 | import android.support.annotation.StringRes 8 | import android.support.design.widget.Snackbar 9 | import android.support.v7.app.AppCompatActivity 10 | import android.support.v7.widget.LinearLayoutManager 11 | import net.gahfy.mvvmposts.R 12 | import net.gahfy.mvvmposts.databinding.ActivityPostListBinding 13 | import net.gahfy.mvvmposts.injection.ViewModelFactory 14 | 15 | class PostListActivity: AppCompatActivity() { 16 | private lateinit var binding: ActivityPostListBinding 17 | private lateinit var viewModel: PostListViewModel 18 | private var errorSnackbar: Snackbar? = null 19 | 20 | override fun onCreate(savedInstanceState: Bundle?){ 21 | super.onCreate(savedInstanceState) 22 | 23 | binding = DataBindingUtil.setContentView(this, R.layout.activity_post_list) 24 | binding.postList.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false) 25 | 26 | viewModel = ViewModelProviders.of(this, ViewModelFactory(this)).get(PostListViewModel::class.java) 27 | viewModel.errorMessage.observe(this, Observer { 28 | errorMessage -> if(errorMessage != null) showError(errorMessage) else hideError() 29 | }) 30 | binding.viewModel = viewModel 31 | } 32 | 33 | private fun showError(@StringRes errorMessage:Int){ 34 | errorSnackbar = Snackbar.make(binding.root, errorMessage, Snackbar.LENGTH_INDEFINITE) 35 | errorSnackbar?.setAction(R.string.retry, viewModel.errorClickListener) 36 | errorSnackbar?.show() 37 | } 38 | 39 | private fun hideError(){ 40 | errorSnackbar?.dismiss() 41 | } 42 | } -------------------------------------------------------------------------------- /app/src/main/kotlin/net/gahfy/mvvmposts/ui/post/PostListAdapter.kt: -------------------------------------------------------------------------------- 1 | package net.gahfy.mvvmposts.ui.post 2 | 3 | import android.arch.lifecycle.ViewModelProviders 4 | import android.databinding.DataBindingUtil 5 | import android.support.v7.widget.RecyclerView 6 | import android.view.LayoutInflater 7 | import android.view.ViewGroup 8 | import net.gahfy.mvvmposts.R 9 | import net.gahfy.mvvmposts.databinding.ItemPostBinding 10 | import net.gahfy.mvvmposts.model.Post 11 | 12 | class PostListAdapter: RecyclerView.Adapter() { 13 | private lateinit var postList:List 14 | 15 | override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PostListAdapter.ViewHolder { 16 | val binding: ItemPostBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.context), R.layout.item_post, parent, false) 17 | return ViewHolder(binding) 18 | } 19 | 20 | override fun onBindViewHolder(holder: PostListAdapter.ViewHolder, position: Int) { 21 | holder.bind(postList[position]) 22 | } 23 | 24 | override fun getItemCount(): Int { 25 | return if(::postList.isInitialized) postList.size else 0 26 | } 27 | 28 | fun updatePostList(postList:List){ 29 | this.postList = postList 30 | notifyDataSetChanged() 31 | } 32 | 33 | class ViewHolder(private val binding: ItemPostBinding):RecyclerView.ViewHolder(binding.root){ 34 | private val viewModel = PostViewModel() 35 | 36 | fun bind(post:Post){ 37 | viewModel.bind(post) 38 | binding.viewModel = viewModel 39 | } 40 | } 41 | } -------------------------------------------------------------------------------- /app/src/main/kotlin/net/gahfy/mvvmposts/ui/post/PostListViewModel.kt: -------------------------------------------------------------------------------- 1 | package net.gahfy.mvvmposts.ui.post 2 | 3 | import android.arch.lifecycle.MutableLiveData 4 | import android.view.View 5 | import io.reactivex.Observable 6 | import io.reactivex.android.schedulers.AndroidSchedulers 7 | import io.reactivex.disposables.Disposable 8 | import io.reactivex.schedulers.Schedulers 9 | import net.gahfy.mvvmposts.R 10 | import net.gahfy.mvvmposts.base.BaseViewModel 11 | import net.gahfy.mvvmposts.model.Post 12 | import net.gahfy.mvvmposts.model.PostDao 13 | import net.gahfy.mvvmposts.network.PostApi 14 | import javax.inject.Inject 15 | 16 | class PostListViewModel(private val postDao: PostDao):BaseViewModel(){ 17 | @Inject 18 | lateinit var postApi: PostApi 19 | val postListAdapter: PostListAdapter = PostListAdapter() 20 | 21 | val loadingVisibility: MutableLiveData = MutableLiveData() 22 | val errorMessage:MutableLiveData = MutableLiveData() 23 | val errorClickListener = View.OnClickListener { loadPosts() } 24 | 25 | private lateinit var subscription: Disposable 26 | 27 | init{ 28 | loadPosts() 29 | } 30 | 31 | override fun onCleared() { 32 | super.onCleared() 33 | subscription.dispose() 34 | } 35 | 36 | private fun loadPosts(){ 37 | subscription = Observable.fromCallable { postDao.all } 38 | .concatMap { 39 | dbPostList -> 40 | if(dbPostList.isEmpty()) 41 | postApi.getPosts().concatMap { 42 | apiPostList -> postDao.insertAll(*apiPostList.toTypedArray()) 43 | Observable.just(apiPostList) 44 | } 45 | else 46 | Observable.just(dbPostList) 47 | } 48 | .subscribeOn(Schedulers.io()) 49 | .observeOn(AndroidSchedulers.mainThread()) 50 | .doOnSubscribe { onRetrievePostListStart() } 51 | .doOnTerminate { onRetrievePostListFinish() } 52 | .subscribe( 53 | { result -> onRetrievePostListSuccess(result) }, 54 | { onRetrievePostListError() } 55 | ) 56 | } 57 | 58 | private fun onRetrievePostListStart(){ 59 | loadingVisibility.value = View.VISIBLE 60 | errorMessage.value = null 61 | } 62 | 63 | private fun onRetrievePostListFinish(){ 64 | loadingVisibility.value = View.GONE 65 | } 66 | 67 | private fun onRetrievePostListSuccess(postList:List){ 68 | postListAdapter.updatePostList(postList) 69 | } 70 | 71 | private fun onRetrievePostListError(){ 72 | errorMessage.value = R.string.post_error 73 | } 74 | } -------------------------------------------------------------------------------- /app/src/main/kotlin/net/gahfy/mvvmposts/ui/post/PostViewModel.kt: -------------------------------------------------------------------------------- 1 | package net.gahfy.mvvmposts.ui.post 2 | 3 | import android.arch.lifecycle.MutableLiveData 4 | import net.gahfy.mvvmposts.base.BaseViewModel 5 | import net.gahfy.mvvmposts.model.Post 6 | 7 | class PostViewModel:BaseViewModel() { 8 | private val postTitle = MutableLiveData() 9 | private val postBody = MutableLiveData() 10 | 11 | fun bind(post: Post){ 12 | postTitle.value = post.title 13 | postBody.value = post.body 14 | } 15 | 16 | fun getPostTitle():MutableLiveData{ 17 | return postTitle 18 | } 19 | 20 | fun getPostBody():MutableLiveData{ 21 | return postBody 22 | } 23 | } -------------------------------------------------------------------------------- /app/src/main/kotlin/net/gahfy/mvvmposts/utils/BindingAdapters.kt: -------------------------------------------------------------------------------- 1 | package net.gahfy.mvvmposts.utils 2 | 3 | import android.arch.lifecycle.MutableLiveData 4 | import android.arch.lifecycle.Observer 5 | import android.databinding.BindingAdapter 6 | import android.support.v7.widget.RecyclerView 7 | import android.support.v7.app.AppCompatActivity 8 | import android.view.View 9 | import android.widget.TextView 10 | import net.gahfy.mvvmposts.utils.extension.getParentActivity 11 | 12 | @BindingAdapter("adapter") 13 | fun setAdapter(view: RecyclerView, adapter: RecyclerView.Adapter<*>) { 14 | view.adapter = adapter 15 | } 16 | 17 | @BindingAdapter("mutableVisibility") 18 | fun setMutableVisibility(view: View, visibility: MutableLiveData?) { 19 | val parentActivity:AppCompatActivity? = view.getParentActivity() 20 | if(parentActivity != null && visibility != null) { 21 | visibility.observe(parentActivity, Observer { value -> view.visibility = value?:View.VISIBLE}) 22 | } 23 | } 24 | 25 | @BindingAdapter("mutableText") 26 | fun setMutableText(view: TextView, text: MutableLiveData?) { 27 | val parentActivity:AppCompatActivity? = view.getParentActivity() 28 | if(parentActivity != null && text != null) { 29 | text.observe(parentActivity, Observer { value -> view.text = value?:""}) 30 | } 31 | } -------------------------------------------------------------------------------- /app/src/main/kotlin/net/gahfy/mvvmposts/utils/Constants.kt: -------------------------------------------------------------------------------- 1 | package net.gahfy.mvvmposts.utils 2 | 3 | /** The base URL of the API */ 4 | const val BASE_URL: String = "https://jsonplaceholder.typicode.com" -------------------------------------------------------------------------------- /app/src/main/kotlin/net/gahfy/mvvmposts/utils/extension/ViewExtension.kt: -------------------------------------------------------------------------------- 1 | package net.gahfy.mvvmposts.utils.extension 2 | 3 | import android.content.ContextWrapper 4 | import android.support.v7.app.AppCompatActivity 5 | import android.view.View 6 | 7 | fun View.getParentActivity(): AppCompatActivity?{ 8 | var context = this.context 9 | while (context is ContextWrapper) { 10 | if (context is AppCompatActivity) { 11 | return context 12 | } 13 | context = context.baseContext 14 | } 15 | return null 16 | } -------------------------------------------------------------------------------- /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/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_post_list.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 8 | 9 | 12 | 13 | 21 | 22 | 31 | 32 | -------------------------------------------------------------------------------- /app/src/main/res/layout/item_post.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 9 | 10 | 11 | 16 | 17 | 26 | 27 | 36 | 37 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gahfy/MVVMPosts/f54cafffd92fa24c2c1194b732f964b1b49cd505/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gahfy/MVVMPosts/f54cafffd92fa24c2c1194b732f964b1b49cd505/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gahfy/MVVMPosts/f54cafffd92fa24c2c1194b732f964b1b49cd505/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gahfy/MVVMPosts/f54cafffd92fa24c2c1194b732f964b1b49cd505/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gahfy/MVVMPosts/f54cafffd92fa24c2c1194b732f964b1b49cd505/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gahfy/MVVMPosts/f54cafffd92fa24c2c1194b732f964b1b49cd505/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gahfy/MVVMPosts/f54cafffd92fa24c2c1194b732f964b1b49cd505/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gahfy/MVVMPosts/f54cafffd92fa24c2c1194b732f964b1b49cd505/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gahfy/MVVMPosts/f54cafffd92fa24c2c1194b732f964b1b49cd505/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gahfy/MVVMPosts/f54cafffd92fa24c2c1194b732f964b1b49cd505/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | MVVMPosts 3 | 4 | Retry 5 | An error occurred while loading the posts 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/test/kotlin/net/gahfy/mvvmposts/ExampleUnitTest.kt: -------------------------------------------------------------------------------- 1 | package net.gahfy.mvvmposts 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 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | ext.kotlin_version = '1.2.71' 5 | ext.lifecycle_version = '1.1.1' 6 | ext.retrofit_version = '2.4.0' 7 | ext.dagger2_version = '2.16' 8 | ext.android_support_version = '28.0.0' 9 | ext.room_version = '1.1.1' 10 | ext.gradle_version = '3.1.0' 11 | 12 | repositories { 13 | google() 14 | jcenter() 15 | } 16 | dependencies { 17 | classpath "com.android.tools.build:gradle:3.2.0" 18 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 19 | 20 | // NOTE: Do not place your application dependencies here; they belong 21 | // in the individual module build.gradle files 22 | } 23 | } 24 | 25 | allprojects { 26 | repositories { 27 | google() 28 | jcenter() 29 | } 30 | } 31 | 32 | task clean(type: Delete) { 33 | delete rootProject.buildDir 34 | } 35 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | # IDE (e.g. Android Studio) users: 3 | # Gradle settings configured through the IDE *will override* 4 | # any settings specified in this file. 5 | # For more details on how to configure your build environment visit 6 | # http://www.gradle.org/docs/current/userguide/build_environment.html 7 | # Specifies the JVM arguments used for the daemon process. 8 | # The setting is particularly useful for tweaking memory settings. 9 | org.gradle.jvmargs=-Xmx1536m 10 | # When configured, Gradle will run in incubating parallel mode. 11 | # This option should only be used with decoupled projects. More details, visit 12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 13 | # org.gradle.parallel=true 14 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gahfy/MVVMPosts/f54cafffd92fa24c2c1194b732f964b1b49cd505/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Fri Oct 05 13:10:54 IST 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.6-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /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 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 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 Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | --------------------------------------------------------------------------------