概述
前面介紹了架構組件中Lifcycle、LiveData、ViewModel以及Room的相關知識,在看了谷歌的例子後用kotlin簡單寫一下實際應用,直接上代碼
環境依賴
project的build.gradle文件:
buildscript {
ext.kotlin_version = '1.1.51'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
module的build.gradle文件:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 26
defaultConfig {
applicationId "example.rxx.kotlin"
minSdkVersion 21
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
// 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"
implementation "android.arch.lifecycle:common-java8:1.1.0"
// 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"
}
創建數據源
新建一個user表:
@Entity(tableName = "user")
data class User(@PrimaryKey
@ColumnInfo(name = "userId")
val id: String = UUID.randomUUID().toString(),
@ColumnInfo(name = "userName")
val userName: String)
新建一個UserDao用來操作user表:
@Dao
interface UserDao {
// 配合LiveData使用
@Query("SELECT * FROM user WHERE userId = :id")
fun getUserById(id: String): MutableLiveData<User>
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertUser(user: User)
@Delete
fun deleteAllUsers()
}
新建一個數據庫:
@Database(entities = arrayOf(User::class), version = 1)
abstract class UsersDatabase : RoomDatabase() {
abstract fun userDao(): UserDao
companion object {
@Volatile private var INSTANCE: UsersDatabase? = null
// 單例
fun getInstance(context: Context) =
INSTANCE ?: synchronized(this) {
INSTANCE ?: buildDatabase(context).also { INSTANCE = it }
}
// 新建數據庫
private fun buildDatabase(context: Context) =
Room.databaseBuilder(context.applicationContext,
UsersDatabase::class.java, "Sample.db")
.build()
}
}
創建ViewModel
創建一個ViewModel保存UI數據:
class UserViewModel(private val dataSource: UserDao) : ViewModel() {
companion object {
const val USER_ID = "1"
}
private lateinit var user: MutableLiveData<User>
fun getUser(): MutableLiveData<User> {
return dataSource.getUserById(USER_ID).also { user = it }
}
fun get(): MutableLiveData<User> {
if (user == null) {
user = MutableLiveData()
}
return user
}
fun updateUserName(userName: String): Completable {
return Completable.fromAction {
val newUser = User(USER_ID, userName)
dataSource.insertUser(newUser)
user.value = newUser
}
}
}
自定義一個ViewModelFactory:
class UserViewModelFactory(private val dataSource: UserDao) : ViewModelProvider.Factory {
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
if (modelClass.isAssignableFrom(UserViewModel::class.java)) {
return UserViewModel(dataSource) as T
}
throw IllegalArgumentException("Unknown ViewModel Class")
}
}
提供創建factory的初始化方法:
object Injection {
fun provideUserDataSource(context: Context): UserDao {
val database = UsersDatabase.getInstance(context)
return database.userDao()
}
fun provideViewModelFactory(context: Context): UserViewModelFactory {
val dataSource = provideUserDataSource(context)
return UserViewModelFactory(dataSource)
}
}
在UI層使用(即activity等):
class MainActivity : AppCompatActivity() {
private lateinit var viewModelFactory: UserViewModelFactory
private lateinit var viewModel: UserViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
viewModelFactory = Injection.provideViewModelFactory(this)
viewModel = ViewModelProviders.of(this, viewModelFactory).get(UserViewModel::class.java)
viewModel.getUser().observe(this, Observer { tv_test.text = it?.userName })
tv_test.setOnClickListener { updateUserName() }
}
private fun updateUserName() {
val userName = "change"
tv_test.isEnabled = false
viewModel.updateUserName(userName)
}
companion object {
private val TAG = MainActivity::class.java.simpleName
}
}
注意: ViewModel中LiveData保持的數據只要有變化,activity中該LiveData的observe就會回調
總結
這就是簡單的使用流程,後續會使用MvpClean架構來優化
我們藉助Goole組件開發的同時,也可以藉助其他三方庫來輔助我們開發出更好的應用, 開發者技術前線建議:
LiveData在某些情況下可使用RxJava2代替。
Lifecyle等生命週期管理我們可以藉助RxLifeCyle。
數據層官方推薦使用Room或者Realm,等其他ORM皆可。
網絡請求庫推薦使用Retrofit+Okhttp
多層之間解耦合,推薦使用服務發現(Service Locator) 或者依賴注入(DI),推薦Dagger2。
Modle和View綁定我們可以使用DataBinding進行快速實現
在使用組件架構時候,推薦使用MvpClean,切記不要Mvp,Mvp,Mvp!