Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Week5 view model live data #5

Open
wants to merge 4 commits into
base: week4-recycler-view
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ android {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

kotlinOptions {
jvmTarget = JavaVersion.VERSION_1_8.toString()
}
}

dependencies {
Expand All @@ -30,6 +39,9 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation "androidx.recyclerview:recyclerview:1.1.0"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.2.0"

testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
Expand Down
16 changes: 6 additions & 10 deletions app/src/main/java/tw/andyang/kotlinandroidworkshop/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ package tw.andyang.kotlinandroidworkshop

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.*
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

private var todos = listOf<Todo>()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Expand All @@ -19,17 +18,14 @@ class MainActivity : AppCompatActivity() {
recyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
recyclerView.addItemDecoration(DividerItemDecoration(this, LinearLayoutManager.VERTICAL))

todos = todos.toMutableList().apply {
add(Todo.Title(getString(R.string.todo_list_title)))
}
val todoViewModel = ViewModelProvider(this).get<TodoViewModel>()

adapter.submitList(todos)
todoViewModel.todoLiveData.observe(this, Observer { todos: List<Todo> ->
adapter.submitList(todos)
})

buttonAdd.setOnClickListener {
todos = todos.toMutableList().apply {
add(Todo.Item("world", false))
}
adapter.submitList(todos)
todoViewModel.onNewTodo.postValue(Unit)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package tw.andyang.kotlinandroidworkshop

import androidx.lifecycle.LiveData
import androidx.lifecycle.MediatorLiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel

class TodoViewModel : ViewModel() {

val onNewTodo = MutableLiveData<Unit>()

val todoLiveData: LiveData<List<Todo>> = MediatorLiveData<List<Todo>>().apply {
addSource(onNewTodo) {
val todo = Todo.Item("note $count", false)
this.value = this.value!! + listOf(todo)
count++
}
value = mutableListOf(Todo.Title("This is a title"))
}

private var count = 0
}