-
-
Save walee-balogun/9c2f699c934703696601ad7ec6f433e9 to your computer and use it in GitHub Desktop.
MVVM LiveData ViewModel Gradle Data Binding
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private lateinit var binding: ActivityMainBinding | |
binding = DataBindingUtil.setContentView(this, R.layout.activity_main) | |
// Set view model | |
binding.viewModel = viewModel | |
// Execute pending binding data | |
binding.executePendingBindings() | |
// View model | |
val viewModel = ViewModelProviders.of(this, viewModelFactory).get(MainViewModel::class.java) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Setup gradle | |
// at the top of file | |
apply plugin: 'kotlin-kapt' | |
android { | |
//other things that we already used | |
dataBinding.enabled = true | |
} | |
dependencies { | |
//other dependencies that we used | |
kapt "com.android.databinding:compiler:3.0.0-beta1" | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Setup gradle dependencies | |
// A ViewModel clas is reponsible for preparing and managing data for Activity or a Fragment | |
// https://developer.android.com/reference/android/arch/lifecycle/ViewModel.html | |
OurViewModel extends ViewModel(android.arch.lifecycle.ViewModel) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Add the Google maven repository | |
allprojects { | |
repositories { | |
jcenter() | |
google() | |
} | |
} | |
// https://developer.android.com/topic/libraries/architecture/adding-components.html | |
// ViewModel and LiveData | |
implementation "android.arch.lifecycle:extensions:1.1.0" | |
// alternatively, just ViewModel | |
implementation "android.arch.lifecycle:viewmodel:1.1.0" | |
// alternatively, just LiveData | |
implementation "android.arch.lifecycle:livedata:1.1.0" | |
annotationProcessor "android.arch.lifecycle:compiler:1.1.0" | |
// Room (use 1.1.0-alpha3 for latest alpha) | |
implementation "android.arch.persistence.room:runtime:1.0.0" | |
annotationProcessor "android.arch.persistence.room:compiler:1.0.0" | |
// Paging | |
implementation "android.arch.paging:runtime:1.0.0-alpha6" | |
// Test helpers for LiveData | |
testImplementation "android.arch.core:core-testing:1.1.0" | |
// Test helpers for Room | |
testImplementation "android.arch.persistence.room:testing:1.0.0" | |
// Adds optional support for Room RxJava and LiveData ReactiveStreams. | |
dependencies { | |
// RxJava support for Room (use 1.1.0-alpha3 for latest alpha) | |
implementation "android.arch.persistence.room:rxjava2:1.0.0" | |
// ReactiveStreams support for LiveData | |
implementation "android.arch.lifecycle:reactivestreams:1.1.0" | |
} | |
// Adds support for Guava's Optional and ListenableFuture types in Room @Dao queries. | |
dependencies { | |
// Guava support for Room | |
implementation "android.arch.persistence.room:guava:1.1.0-alpha3" | |
} | |
// Alternative import for lightweight Lifecycles only | |
dependencies { | |
// Lifecycles only (no ViewModel or LiveData) | |
implementation "android.arch.lifecycle:runtime:1.1.0" | |
annotationProcessor "android.arch.lifecycle:compiler:1.1.0" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment