├── app ├── .gitignore ├── src │ ├── main │ │ ├── res │ │ │ ├── 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 │ │ │ ├── mipmap-anydpi-v26 │ │ │ │ ├── ic_launcher.xml │ │ │ │ └── ic_launcher_round.xml │ │ │ ├── layout │ │ │ │ ├── activity_list_repo.xml │ │ │ │ └── item_repo.xml │ │ │ ├── drawable-v24 │ │ │ │ └── ic_launcher_foreground.xml │ │ │ └── drawable │ │ │ │ └── ic_launcher_background.xml │ │ ├── java │ │ │ └── com │ │ │ │ └── github │ │ │ │ └── pramodgarg │ │ │ │ └── samplemvvm │ │ │ │ ├── data │ │ │ │ ├── model │ │ │ │ │ ├── Owner.kt │ │ │ │ │ └── Repo.kt │ │ │ │ └── remote │ │ │ │ │ ├── AppApiHelper.kt │ │ │ │ │ ├── ApiHelper.kt │ │ │ │ │ ├── ApiInterface.kt │ │ │ │ │ └── RetrofitClient.kt │ │ │ │ ├── utils │ │ │ │ └── rx │ │ │ │ │ ├── AppSchedulerProvider.kt │ │ │ │ │ ├── SchedulerProvider.kt │ │ │ │ │ └── RxBinding.java │ │ │ │ └── ui │ │ │ │ ├── viewmodel │ │ │ │ ├── ListRepoModelFactory.java │ │ │ │ └── ListRepoViewModel.kt │ │ │ │ ├── TrackRecyclerAdapter.kt │ │ │ │ └── ListRepoActivity.kt │ │ └── AndroidManifest.xml │ ├── test │ │ └── java │ │ │ └── com │ │ │ └── github │ │ │ └── pramodgarg │ │ │ └── samplemvvm │ │ │ └── ExampleUnitTest.kt │ └── androidTest │ │ └── java │ │ └── com │ │ └── github │ │ └── pramodgarg │ │ └── samplemvvm │ │ └── ExampleInstrumentedTest.kt ├── proguard-rules.pro └── build.gradle ├── settings.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradle.properties ├── README.md ├── .gitignore ├── gradlew.bat └── gradlew /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PramodGarg/SampleMVVM/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PramodGarg/SampleMVVM/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PramodGarg/SampleMVVM/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PramodGarg/SampleMVVM/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PramodGarg/SampleMVVM/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PramodGarg/SampleMVVM/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PramodGarg/SampleMVVM/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/PramodGarg/SampleMVVM/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/PramodGarg/SampleMVVM/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/PramodGarg/SampleMVVM/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/PramodGarg/SampleMVVM/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/java/com/github/pramodgarg/samplemvvm/data/model/Owner.kt: -------------------------------------------------------------------------------- 1 | package com.github.pramodgarg.samplemvvm.data.model 2 | 3 | data class Owner(val avatar_url: String?) -------------------------------------------------------------------------------- /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 | SampleMVVM 3 | 4 | %d Stars 5 | %d Open Issues 6 | | 7 | 8 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Tue Mar 20 18:51:02 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.1-all.zip 7 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/pramodgarg/samplemvvm/data/remote/AppApiHelper.kt: -------------------------------------------------------------------------------- 1 | package com.github.pramodgarg.samplemvvm.data.remote 2 | 3 | /** 4 | * Created by pramod on 21/03/18. 5 | */ 6 | class AppApiHelper : ApiHelper { 7 | override fun fetchUserRepos(name: String) = RetrofitClient.getApiInterface().getRepos(name) 8 | } -------------------------------------------------------------------------------- /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/java/com/github/pramodgarg/samplemvvm/data/remote/ApiHelper.kt: -------------------------------------------------------------------------------- 1 | package com.github.pramodgarg.samplemvvm.data.remote 2 | 3 | import com.github.pramodgarg.samplemvvm.data.model.Repo 4 | import io.reactivex.Single 5 | 6 | interface ApiHelper { 7 | /** 8 | * fetch github repositories for username provided 9 | */ 10 | fun fetchUserRepos(name: String): Single> 11 | } -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/pramodgarg/samplemvvm/data/remote/ApiInterface.kt: -------------------------------------------------------------------------------- 1 | package com.github.pramodgarg.samplemvvm.data.remote 2 | 3 | import com.github.pramodgarg.samplemvvm.data.model.Repo 4 | import io.reactivex.Single 5 | import retrofit2.Call 6 | import retrofit2.http.GET 7 | import retrofit2.http.Path 8 | 9 | interface ApiInterface { 10 | 11 | @GET("/users/{userId}/repos") 12 | fun getRepos(@Path("userId") userName: String): Single> 13 | } -------------------------------------------------------------------------------- /app/src/main/java/com/github/pramodgarg/samplemvvm/utils/rx/AppSchedulerProvider.kt: -------------------------------------------------------------------------------- 1 | package com.github.pramodgarg.samplemvvm.utils.rx 2 | 3 | import io.reactivex.android.schedulers.AndroidSchedulers 4 | import io.reactivex.schedulers.Schedulers 5 | 6 | /** 7 | * Created by pramod on 21/03/18. 8 | */ 9 | class AppSchedulerProvider : SchedulerProvider { 10 | 11 | override fun io() = Schedulers.io() 12 | 13 | override fun main() = AndroidSchedulers.mainThread() 14 | } -------------------------------------------------------------------------------- /app/src/main/java/com/github/pramodgarg/samplemvvm/utils/rx/SchedulerProvider.kt: -------------------------------------------------------------------------------- 1 | package com.github.pramodgarg.samplemvvm.utils.rx 2 | 3 | import io.reactivex.Scheduler 4 | 5 | /** 6 | * Created by pramod on 21/03/18. 7 | */ 8 | interface SchedulerProvider { 9 | /** 10 | * get background scheduler for making io calls 11 | */ 12 | fun io(): Scheduler 13 | 14 | /** 15 | * get main thread scheduler for making ui changes 16 | */ 17 | fun main(): Scheduler 18 | } -------------------------------------------------------------------------------- /app/src/test/java/com/github/pramodgarg/samplemvvm/ExampleUnitTest.kt: -------------------------------------------------------------------------------- 1 | package com.github.pramodgarg.samplemvvm 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/com/github/pramodgarg/samplemvvm/data/model/Repo.kt: -------------------------------------------------------------------------------- 1 | package com.github.pramodgarg.samplemvvm.data.model 2 | 3 | import android.databinding.BindingAdapter 4 | import android.widget.ImageView 5 | import com.bumptech.glide.Glide 6 | 7 | @BindingAdapter("imageUrl") 8 | fun loadImage(view: ImageView, url: String) = Glide.with(view.context).load(url).into(view) 9 | 10 | public data class Repo(val name: String?, val open_issues_count: Int?, val stargazers_count: Int?, 11 | val description: String?, val html_url: String?, 12 | val owner: Owner) -------------------------------------------------------------------------------- /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/src/androidTest/java/com/github/pramodgarg/samplemvvm/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package com.github.pramodgarg.samplemvvm 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("com.github.pramodgarg.samplemvvm", appContext.packageName) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /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/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/pramodgarg/samplemvvm/data/remote/RetrofitClient.kt: -------------------------------------------------------------------------------- 1 | package com.github.pramodgarg.samplemvvm.data.remote 2 | 3 | import okhttp3.OkHttpClient 4 | import retrofit2.Retrofit 5 | import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory 6 | import retrofit2.converter.gson.GsonConverterFactory 7 | 8 | class RetrofitClient { 9 | companion object { 10 | private val BASE_URL = "https://api.github.com/" 11 | private var retrofit: Retrofit? = null 12 | 13 | @JvmStatic 14 | fun getApiInterface(): ApiInterface { 15 | if (retrofit == null) { 16 | retrofit = Retrofit.Builder() 17 | .baseUrl(BASE_URL) 18 | .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) 19 | .addConverterFactory(GsonConverterFactory.create()) 20 | .client(OkHttpClient.Builder().build()) 21 | .build() 22 | } 23 | return retrofit!!.create(ApiInterface::class.java) 24 | } 25 | } 26 | } -------------------------------------------------------------------------------- /app/src/main/java/com/github/pramodgarg/samplemvvm/ui/viewmodel/ListRepoModelFactory.java: -------------------------------------------------------------------------------- 1 | package com.github.pramodgarg.samplemvvm.ui.viewmodel; 2 | 3 | import android.arch.lifecycle.ViewModel; 4 | import android.arch.lifecycle.ViewModelProvider; 5 | import android.support.annotation.NonNull; 6 | 7 | import com.github.pramodgarg.samplemvvm.data.remote.ApiHelper; 8 | import com.github.pramodgarg.samplemvvm.utils.rx.SchedulerProvider; 9 | 10 | /** 11 | * Created by pramod on 21/03/18. 12 | */ 13 | 14 | public class ListRepoModelFactory implements ViewModelProvider.Factory { 15 | 16 | private SchedulerProvider mSchedulerProvider; 17 | private ApiHelper mApiHelper; 18 | 19 | public ListRepoModelFactory(final SchedulerProvider schedulerProvider, final ApiHelper apiHelper) { 20 | mSchedulerProvider = schedulerProvider; 21 | mApiHelper = apiHelper; 22 | } 23 | 24 | @NonNull 25 | @Override 26 | public T create(@NonNull final Class modelClass) { 27 | ListRepoViewModel model = new ListRepoViewModel(mSchedulerProvider, mApiHelper); 28 | return (T) model; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/pramodgarg/samplemvvm/ui/TrackRecyclerAdapter.kt: -------------------------------------------------------------------------------- 1 | package com.github.pramodgarg.samplemvvm.ui 2 | 3 | import android.databinding.DataBindingUtil 4 | import android.support.v7.widget.RecyclerView 5 | import android.view.LayoutInflater 6 | import android.view.ViewGroup 7 | import com.github.pramodgarg.samplemvvm.R 8 | import com.github.pramodgarg.samplemvvm.data.model.Repo 9 | import com.github.pramodgarg.samplemvvm.databinding.ItemRepoBinding 10 | 11 | class TrackRecyclerAdapter(private val mTrackList: List) : RecyclerView.Adapter() { 12 | 13 | override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TrackVH { 14 | val binding: ItemRepoBinding = 15 | DataBindingUtil.inflate(LayoutInflater.from(parent.context), R.layout.item_repo, parent, false) 16 | return TrackVH(binding) 17 | } 18 | 19 | override fun getItemCount() = mTrackList.size 20 | 21 | override fun onBindViewHolder(holder: TrackVH, position: Int) { 22 | holder.bind(mTrackList.get(holder.adapterPosition)) 23 | } 24 | 25 | class TrackVH(private val binding: ItemRepoBinding) : RecyclerView.ViewHolder(binding.root) { 26 | fun bind(track: Repo) { 27 | binding.repo = track 28 | binding.executePendingBindings() 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /app/src/main/java/com/github/pramodgarg/samplemvvm/ui/ListRepoActivity.kt: -------------------------------------------------------------------------------- 1 | package com.github.pramodgarg.samplemvvm.ui 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.v7.app.AppCompatActivity 8 | import android.support.v7.widget.LinearLayoutManager 9 | import com.github.pramodgarg.samplemvvm.R 10 | import com.github.pramodgarg.samplemvvm.data.model.Repo 11 | import com.github.pramodgarg.samplemvvm.data.remote.AppApiHelper 12 | import com.github.pramodgarg.samplemvvm.databinding.ActivityListRepoBinding 13 | import com.github.pramodgarg.samplemvvm.ui.viewmodel.ListRepoModelFactory 14 | import com.github.pramodgarg.samplemvvm.ui.viewmodel.ListRepoViewModel 15 | import com.github.pramodgarg.samplemvvm.utils.rx.AppSchedulerProvider 16 | 17 | class ListRepoActivity : AppCompatActivity() { 18 | private val binding 19 | by lazy { DataBindingUtil.setContentView(this, R.layout.activity_list_repo) as ActivityListRepoBinding } 20 | 21 | override fun onCreate(savedInstanceState: Bundle?) { 22 | super.onCreate(savedInstanceState) 23 | 24 | val viewModel = ViewModelProviders.of(this, ListRepoModelFactory(AppSchedulerProvider(), AppApiHelper())) 25 | .get(ListRepoViewModel::class.java) 26 | binding.viewModel = viewModel 27 | binding.rvItems.layoutManager = LinearLayoutManager(this) 28 | viewModel.repo.observe(this, Observer> { updateAdapterList(it) }) 29 | } 30 | 31 | private fun updateAdapterList(list: List?) { 32 | if (list == null) { 33 | binding.rvItems.adapter = TrackRecyclerAdapter(emptyList()) 34 | } else { 35 | binding.rvItems.adapter = TrackRecyclerAdapter(list) 36 | } 37 | } 38 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SampleMVVM 2 | Sample project that implements the MVVM architecture using Kotlin, Data Binding, RxJava2 and Android Architecture Components. 3 | The application retrieves the list of github repositories of the username entered. 4 | 5 | 6 | ### Library reference resources: 7 | 1. Architecture Components: https://developer.android.com/topic/libraries/architecture/adding-components.html 8 | 2. RxJava2: https://github.com/ReactiveX/RxJava 9 | 3. Dagger2: https://github.com/google/dagger 10 | 4. Retrofit: https://github.com/square/retrofit 11 | 5. Glide: https://github.com/bumptech/glide 12 | 13 | ### Branch 14 | | Sample | Description | 15 | | ------------- | ------------- | 16 | | [master](https://github.com/PramodGarg/SampleMVVM/tree/master) | Basic implementation of MVVM using Kotlin, Architecture Components, RxJava2 and Data Binding | 17 | | [dagger](https://github.com/PramodGarg/SampleMVVM/tree/dagger/) | Basic implementation of MVVM using Kotlin, Architecture Components, RxJava2, Data Binding and Dagger2 (Dependency Injection) | 18 | 19 | ### For Sample project on Data Binding - [Check here](https://github.com/PramodGarg/SampleDataBinding) 20 | ### For Sample project on MVP Architecture - [Check here](https://github.com/PramodGarg/SampleMVP) 21 | 22 | ### License 23 | ``` 24 | Copyright (C) 2018 Pramod Garg 25 | 26 | Licensed under the Apache License, Version 2.0 (the "License"); 27 | You may not use this file except in compliance with the License. 28 | You may obtain a copy of the License at 29 | 30 | http://www.apache.org/licenses/LICENSE-2.0 31 | 32 | Unless required by applicable law or agreed to in writing, 33 | software distributed under the License is distributed on an "AS IS" BASIS, 34 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 35 | 36 | See the License for the specific language governing permissions and limitations under the License. 37 | ``` 38 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_list_repo.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 13 | 14 | 15 | 18 | 19 | 26 | 27 | 35 | 36 | 37 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /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/com/github/pramodgarg/samplemvvm/ui/viewmodel/ListRepoViewModel.kt: -------------------------------------------------------------------------------- 1 | package com.github.pramodgarg.samplemvvm.ui.viewmodel 2 | 3 | import android.arch.lifecycle.MutableLiveData 4 | import android.arch.lifecycle.ViewModel 5 | import android.databinding.ObservableBoolean 6 | import android.databinding.ObservableField 7 | import android.util.Log 8 | import com.github.pramodgarg.samplemvvm.data.model.Repo 9 | import com.github.pramodgarg.samplemvvm.data.remote.ApiHelper 10 | import com.github.pramodgarg.samplemvvm.utils.rx.RxBinding 11 | import com.github.pramodgarg.samplemvvm.utils.rx.SchedulerProvider 12 | import io.reactivex.disposables.CompositeDisposable 13 | import io.reactivex.schedulers.Schedulers 14 | import java.util.concurrent.TimeUnit 15 | 16 | class ListRepoViewModel(private val schedulerProvider: SchedulerProvider, private val apiHelper: ApiHelper) : ViewModel() { 17 | 18 | private val compositeDisposable by lazy { CompositeDisposable() } 19 | 20 | val email = ObservableField() 21 | val isLoading = ObservableBoolean(false) 22 | var repo = MutableLiveData>() 23 | 24 | init { 25 | compositeDisposable.add(RxBinding.toObservable(email) 26 | .subscribeOn(schedulerProvider.io()) 27 | // wait 1 second after all typing events are fired, then make api request 28 | .debounce(1000, TimeUnit.MILLISECONDS) 29 | .observeOn(schedulerProvider.main()) 30 | // hit api only if the name entered is different from the previous entry 31 | .distinctUntilChanged() 32 | .subscribe { if (it.isNotEmpty()) getRepos(it) }) 33 | } 34 | 35 | /** 36 | * fetch github repositories for the username entered 37 | */ 38 | private fun getRepos(userName: String) { 39 | isLoading.set(true) 40 | compositeDisposable.add(apiHelper.fetchUserRepos(userName).subscribeOn(Schedulers.io()) 41 | .subscribeOn(schedulerProvider.io()) 42 | .observeOn(schedulerProvider.main()) 43 | .subscribe({ list -> 44 | repo.value = list 45 | isLoading.set(false) 46 | }, { error -> 47 | repo.value = emptyList() 48 | isLoading.set(false) 49 | Log.d("error", "" + error.message) 50 | })) 51 | } 52 | 53 | override fun onCleared() { 54 | // dispose all observable when viewmodel is to be destroyed to prevent memory leaks 55 | compositeDisposable.dispose() 56 | super.onCleared() 57 | } 58 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by https://www.gitignore.io/api/androidstudio 2 | 3 | ### AndroidStudio ### 4 | # Covers files to be ignored for android development using Android Studio. 5 | 6 | # Built application files 7 | *.apk 8 | *.ap_ 9 | 10 | # Files for the ART/Dalvik VM 11 | *.dex 12 | 13 | # Java class files 14 | *.class 15 | 16 | # Generated files 17 | bin/ 18 | gen/ 19 | out/ 20 | 21 | # Gradle files 22 | .gradle 23 | .gradle/ 24 | build/ 25 | 26 | # Signing files 27 | .signing/ 28 | 29 | # Local configuration file (sdk path, etc) 30 | local.properties 31 | 32 | # Proguard folder generated by Eclipse 33 | proguard/ 34 | 35 | # Log Files 36 | *.log 37 | 38 | # Android Studio 39 | /*/build/ 40 | /*/local.properties 41 | /*/out 42 | /*/*/build 43 | /*/*/production 44 | captures/ 45 | .navigation/ 46 | *.ipr 47 | *~ 48 | *.swp 49 | 50 | # Android Patch 51 | gen-external-apklibs 52 | 53 | # External native build folder generated in Android Studio 2.2 and later 54 | .externalNativeBuild 55 | 56 | # NDK 57 | obj/ 58 | 59 | # IntelliJ IDEA 60 | *.iml 61 | *.iws 62 | /out/ 63 | 64 | # User-specific configurations 65 | .idea/gradle.xml 66 | .idea/runConfigurations.xml 67 | 68 | .idea/libraries/ 69 | .idea/workspace.xml 70 | .idea/tasks.xml 71 | .idea/.name 72 | .idea/compiler.xml 73 | .idea/copyright/profiles_settings.xml 74 | .idea/encodings.xml 75 | .idea/misc.xml 76 | .idea/modules.xml 77 | .idea/scopes/scope_settings.xml 78 | .idea/dictionaries 79 | .idea/vcs.xml 80 | .idea/jsLibraryMappings.xml 81 | .idea/datasources.xml 82 | .idea/dataSources.ids 83 | .idea/sqlDataSources.xml 84 | .idea/dynamic.xml 85 | .idea/uiDesigner.xml 86 | 87 | # OS-specific files 88 | .DS_Store 89 | .DS_Store? 90 | ._* 91 | .Spotlight-V100 92 | .Trashes 93 | ehthumbs.db 94 | Thumbs.db 95 | 96 | # Legacy Eclipse project files 97 | .classpath 98 | .project 99 | 100 | # Mobile Tools for Java (J2ME) 101 | .mtj.tmp/ 102 | 103 | # Package Files # 104 | *.war 105 | *.ear 106 | 107 | # virtual machine crash logs (Reference: http://www.java.com/en/download/help/error_hotspot.xml) 108 | hs_err_pid* 109 | 110 | ## Plugin-specific files: 111 | 112 | # mpeltonen/sbt-idea plugin 113 | .idea_modules/ 114 | 115 | # JIRA plugin 116 | atlassian-ide-plugin.xml 117 | 118 | # Mongo Explorer plugin 119 | .idea/mongoSettings.xml 120 | 121 | # Crashlytics plugin (for Android Studio and IntelliJ) 122 | com_crashlytics_export_strings.xml 123 | crashlytics.properties 124 | crashlytics-build.properties 125 | fabric.properties 126 | 127 | ### AndroidStudio Patch ### 128 | 129 | !/gradle/wrapper/gradle-wrapper.jar 130 | 131 | # End of https://www.gitignore.io/api/androidstudio -------------------------------------------------------------------------------- /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/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | apply plugin: 'kotlin-android' 4 | 5 | apply plugin: 'kotlin-kapt' 6 | 7 | android { 8 | compileSdkVersion 26 9 | defaultConfig { 10 | applicationId "com.github.pramodgarg.samplemvvm" 11 | minSdkVersion 19 12 | targetSdkVersion 26 13 | versionCode 1 14 | versionName "1.0" 15 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 16 | 17 | compileOptions { 18 | sourceCompatibility JavaVersion.VERSION_1_8 19 | targetCompatibility JavaVersion.VERSION_1_8 20 | } 21 | } 22 | buildTypes { 23 | release { 24 | minifyEnabled false 25 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 26 | } 27 | } 28 | dataBinding { 29 | enabled true 30 | } 31 | } 32 | 33 | project.ext { 34 | constraintLayoutVersion = "1.1.0-beta5" 35 | supportLibraryVersion = "26.1.0" 36 | rxJavaVersion = "2.1.0" 37 | rxAndroidVersion = "2.0.2" 38 | lifecycleVersion = "1.1.0" 39 | retrofitVersion = "2.3.0" 40 | okhttpVersion = "3.9.0" 41 | glideVersion = "4.6.1" 42 | } 43 | 44 | dependencies { 45 | implementation fileTree(dir: 'libs', include: ['*.jar']) 46 | implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" 47 | implementation "com.android.support:appcompat-v7:$project.supportLibraryVersion" 48 | implementation "com.android.support:design:$project.supportLibraryVersion" 49 | implementation "com.android.support.constraint:constraint-layout:$project.constraintLayoutVersion" 50 | 51 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 52 | kapt "com.android.databinding:compiler:$buildToolsVersion" 53 | 54 | // ReactiveX 55 | implementation "io.reactivex.rxjava2:rxjava:$project.rxJavaVersion" 56 | implementation "io.reactivex.rxjava2:rxandroid:$project.rxAndroidVersion" 57 | 58 | // Lifecycle 59 | implementation "android.arch.lifecycle:runtime:$project.lifecycleVersion" 60 | implementation "android.arch.lifecycle:extensions:$project.lifecycleVersion" 61 | annotationProcessor "android.arch.lifecycle:compiler:$project.lifecycleVersion" 62 | 63 | // retrofit 64 | implementation "com.squareup.retrofit2:retrofit:$project.retrofitVersion" 65 | implementation "com.squareup.okhttp3:logging-interceptor:$project.okhttpVersion" 66 | implementation "com.squareup.retrofit2:converter-gson:$project.retrofitVersion" 67 | implementation "com.squareup.retrofit2:adapter-rxjava2:$project.retrofitVersion" 68 | 69 | //glide 70 | implementation("com.github.bumptech.glide:glide:$project.glideVersion") { 71 | exclude group: "com.android.support" 72 | } 73 | 74 | testImplementation 'junit:junit:4.12' 75 | androidTestImplementation 'com.android.support.test:runner:1.0.1' 76 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' 77 | } 78 | -------------------------------------------------------------------------------- /app/src/main/res/layout/item_repo.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 8 | 9 | 10 | 13 | 14 | 15 | 21 | 22 | 32 | 33 | 45 | 46 | 58 | 59 | 68 | 69 | 77 | 78 | 88 | 89 | 90 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/pramodgarg/samplemvvm/utils/rx/RxBinding.java: -------------------------------------------------------------------------------- 1 | package com.github.pramodgarg.samplemvvm.utils.rx; 2 | 3 | import android.databinding.Observable.OnPropertyChangedCallback; 4 | import android.databinding.ObservableBoolean; 5 | import android.databinding.ObservableField; 6 | import android.databinding.ObservableList; 7 | import android.support.annotation.NonNull; 8 | 9 | import java.util.List; 10 | 11 | import io.reactivex.Observable; 12 | import io.reactivex.android.schedulers.AndroidSchedulers; 13 | 14 | public class RxBinding { 15 | 16 | private RxBinding() { 17 | } 18 | 19 | public static Observable toObservableOnUIReset(@NonNull final ObservableField observableField) { 20 | return toObservableReset(observableField).observeOn(AndroidSchedulers.mainThread()); 21 | } 22 | 23 | public static Observable toObservableOnUI(@NonNull final ObservableField observableField) { 24 | return toObservable(observableField).observeOn(AndroidSchedulers.mainThread()); 25 | } 26 | 27 | public static Observable toObservableReset(@NonNull final ObservableField observableField) { 28 | return toObs(observableField) 29 | .filter(observableField1 -> observableField1.get() != null) 30 | .doAfterNext(observableField1 -> observableField1.set(null)) 31 | .map(ObservableField::get); 32 | } 33 | 34 | public static Observable toObservable(@NonNull final ObservableField observableField) { 35 | return toObs(observableField) 36 | .filter(observableField1 -> observableField1.get() != null) 37 | .map(ObservableField::get); 38 | } 39 | 40 | private static Observable> toObs(@NonNull final ObservableField observableField) { 41 | return Observable.create(emitter -> { 42 | final OnPropertyChangedCallback callback = new OnPropertyChangedCallback() { 43 | @Override 44 | public void onPropertyChanged(android.databinding.Observable dataBindingObservable, int propertyId) { 45 | if (dataBindingObservable == observableField) { 46 | emitter.onNext(observableField); 47 | } 48 | } 49 | }; 50 | observableField.addOnPropertyChangedCallback(callback); 51 | emitter.setCancellable(() -> observableField.removeOnPropertyChangedCallback(callback)); 52 | }); 53 | } 54 | 55 | public static Observable toObservableOnUIReset(@NonNull final ObservableBoolean observableBoolean) { 56 | return toObservableReset(observableBoolean).observeOn(AndroidSchedulers.mainThread()); 57 | } 58 | 59 | public static Observable toObservableOnUI(@NonNull final ObservableBoolean observableBoolean) { 60 | return toObservable(observableBoolean).observeOn(AndroidSchedulers.mainThread()); 61 | } 62 | 63 | public static Observable toObservableReset(@NonNull final ObservableBoolean observableBoolean) { 64 | return toObs(observableBoolean) 65 | .filter(ObservableBoolean::get) 66 | .doAfterNext(observableBoolean1 -> observableBoolean1.set(false)) 67 | .map(ObservableBoolean::get); 68 | } 69 | 70 | public static Observable toObservable(@NonNull final ObservableBoolean observableBoolean) { 71 | return toObs(observableBoolean).map(ObservableBoolean::get); 72 | } 73 | 74 | private static Observable toObs(@NonNull final ObservableBoolean observableBoolean) { 75 | return Observable.create(emitter -> { 76 | final OnPropertyChangedCallback callback = new OnPropertyChangedCallback() { 77 | @Override 78 | public void onPropertyChanged(android.databinding.Observable dataBindingObservable, int propertyId) { 79 | if (dataBindingObservable == observableBoolean) { 80 | emitter.onNext(observableBoolean); 81 | } 82 | } 83 | }; 84 | observableBoolean.addOnPropertyChangedCallback(callback); 85 | emitter.setCancellable(() -> observableBoolean.removeOnPropertyChangedCallback(callback)); 86 | }); 87 | } 88 | 89 | public static Observable> toObservableOnUI(@NonNull final ObservableList observableList) { 90 | return toObservable(observableList).observeOn(AndroidSchedulers.mainThread()); 91 | } 92 | 93 | public static Observable> toObservable(@NonNull final ObservableList observableList) { 94 | return Observable.create(emitter -> { 95 | final ObservableList.OnListChangedCallback> callback = 96 | new ObservableList.OnListChangedCallback>() { 97 | @Override 98 | public void onChanged(ObservableList dataBindingObservableList) { 99 | } 100 | 101 | @Override 102 | public void onItemRangeChanged(ObservableList observableList, int i, int i1) { 103 | } 104 | 105 | @Override 106 | public void onItemRangeInserted(ObservableList dataBindingObservableList, int i, int i1) { 107 | if (dataBindingObservableList == observableList) { 108 | emitter.onNext(observableList); 109 | } 110 | } 111 | 112 | @Override 113 | public void onItemRangeMoved(ObservableList observableList, int i, int i1, int i2) { 114 | } 115 | 116 | @Override 117 | public void onItemRangeRemoved(ObservableList observableList, int i, int i1) { 118 | } 119 | }; 120 | observableList.addOnListChangedCallback(callback); 121 | emitter.setCancellable(() -> observableList.removeOnListChangedCallback(callback)); 122 | }); 123 | } 124 | 125 | } -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 11 | 16 | 21 | 26 | 31 | 36 | 41 | 46 | 51 | 56 | 61 | 66 | 71 | 76 | 81 | 86 | 91 | 96 | 101 | 106 | 111 | 116 | 121 | 126 | 131 | 136 | 141 | 146 | 151 | 156 | 161 | 166 | 171 | 172 | --------------------------------------------------------------------------------