From 9bd7a5cfea06175c193bb314ac9e55e40879afd3 Mon Sep 17 00:00:00 2001 From: YANBIN Date: Sun, 30 Aug 2020 21:54:05 +0800 Subject: [PATCH 1/4] add dependencies --- app/build.gradle | 12 ++++++++++++ .../andyang/kotlinandroidworkshop/TodoViewModel.kt | 4 ++++ 2 files changed, 16 insertions(+) create mode 100644 app/src/main/java/tw/andyang/kotlinandroidworkshop/TodoViewModel.kt diff --git a/app/build.gradle b/app/build.gradle index d3827dd..07590c3 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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 { @@ -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' diff --git a/app/src/main/java/tw/andyang/kotlinandroidworkshop/TodoViewModel.kt b/app/src/main/java/tw/andyang/kotlinandroidworkshop/TodoViewModel.kt new file mode 100644 index 0000000..08bab87 --- /dev/null +++ b/app/src/main/java/tw/andyang/kotlinandroidworkshop/TodoViewModel.kt @@ -0,0 +1,4 @@ +package tw.andyang.kotlinandroidworkshop + +class TodoViewModel { +} \ No newline at end of file From 5d41ea56669c94c026c77241f9cb90a1e8fa8914 Mon Sep 17 00:00:00 2001 From: YANBIN Date: Sun, 30 Aug 2020 22:28:36 +0800 Subject: [PATCH 2/4] Add TodoViewModel --- .../kotlinandroidworkshop/MainActivity.kt | 16 ++++++---------- .../kotlinandroidworkshop/TodoViewModel.kt | 17 ++++++++++++++++- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/tw/andyang/kotlinandroidworkshop/MainActivity.kt b/app/src/main/java/tw/andyang/kotlinandroidworkshop/MainActivity.kt index 42fe1e7..2deb6a7 100644 --- a/app/src/main/java/tw/andyang/kotlinandroidworkshop/MainActivity.kt +++ b/app/src/main/java/tw/andyang/kotlinandroidworkshop/MainActivity.kt @@ -2,14 +2,14 @@ package tw.andyang.kotlinandroidworkshop import android.os.Bundle import androidx.appcompat.app.AppCompatActivity +import androidx.lifecycle.ViewModelProvider +import androidx.lifecycle.get import androidx.recyclerview.widget.DividerItemDecoration import androidx.recyclerview.widget.LinearLayoutManager import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() { - private var todos = listOf() - override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) @@ -19,17 +19,13 @@ 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() - adapter.submitList(todos) + adapter.submitList(todoViewModel.todos) buttonAdd.setOnClickListener { - todos = todos.toMutableList().apply { - add(Todo.Item("world", false)) - } - adapter.submitList(todos) + todoViewModel.addNewTodo() + adapter.submitList(todoViewModel.todos) } } } \ No newline at end of file diff --git a/app/src/main/java/tw/andyang/kotlinandroidworkshop/TodoViewModel.kt b/app/src/main/java/tw/andyang/kotlinandroidworkshop/TodoViewModel.kt index 08bab87..ed9dd23 100644 --- a/app/src/main/java/tw/andyang/kotlinandroidworkshop/TodoViewModel.kt +++ b/app/src/main/java/tw/andyang/kotlinandroidworkshop/TodoViewModel.kt @@ -1,4 +1,19 @@ package tw.andyang.kotlinandroidworkshop -class TodoViewModel { +import androidx.lifecycle.ViewModel + +class TodoViewModel: ViewModel() { + + var todos = listOf(Todo.Title("This is a title")) + private set + + private var count = 0 + + fun addNewTodo() { + val todo = Todo.Item("note $count", false) + todos = todos.toMutableList().apply { + add(todo) + } + count ++ + } } \ No newline at end of file From 76374b1be32df24de23c8a1644c73d295cf4c39c Mon Sep 17 00:00:00 2001 From: YANBIN Date: Sun, 30 Aug 2020 23:01:41 +0800 Subject: [PATCH 3/4] Add LiveData --- .../tw/andyang/kotlinandroidworkshop/MainActivity.kt | 6 ++++-- .../tw/andyang/kotlinandroidworkshop/TodoViewModel.kt | 10 +++++----- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/tw/andyang/kotlinandroidworkshop/MainActivity.kt b/app/src/main/java/tw/andyang/kotlinandroidworkshop/MainActivity.kt index 2deb6a7..bb14713 100644 --- a/app/src/main/java/tw/andyang/kotlinandroidworkshop/MainActivity.kt +++ b/app/src/main/java/tw/andyang/kotlinandroidworkshop/MainActivity.kt @@ -2,6 +2,7 @@ package tw.andyang.kotlinandroidworkshop import android.os.Bundle import androidx.appcompat.app.AppCompatActivity +import androidx.lifecycle.Observer import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.get import androidx.recyclerview.widget.DividerItemDecoration @@ -21,11 +22,12 @@ class MainActivity : AppCompatActivity() { val todoViewModel = ViewModelProvider(this).get() - adapter.submitList(todoViewModel.todos) + todoViewModel.todoLiveData.observe(this, Observer { todos: List -> + adapter.submitList(todos) + }) buttonAdd.setOnClickListener { todoViewModel.addNewTodo() - adapter.submitList(todoViewModel.todos) } } } \ No newline at end of file diff --git a/app/src/main/java/tw/andyang/kotlinandroidworkshop/TodoViewModel.kt b/app/src/main/java/tw/andyang/kotlinandroidworkshop/TodoViewModel.kt index ed9dd23..ab056be 100644 --- a/app/src/main/java/tw/andyang/kotlinandroidworkshop/TodoViewModel.kt +++ b/app/src/main/java/tw/andyang/kotlinandroidworkshop/TodoViewModel.kt @@ -1,19 +1,19 @@ package tw.andyang.kotlinandroidworkshop +import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel class TodoViewModel: ViewModel() { - var todos = listOf(Todo.Title("This is a title")) - private set + val todoLiveData = MutableLiveData>( + mutableListOf(Todo.Title("This is a title")) + ) private var count = 0 fun addNewTodo() { val todo = Todo.Item("note $count", false) - todos = todos.toMutableList().apply { - add(todo) - } + todoLiveData.value = todoLiveData.value!! + listOf(todo) count ++ } } \ No newline at end of file From 434f01532dcdccbe0fc3d5a1fe99165e08c48a83 Mon Sep 17 00:00:00 2001 From: YANBIN Date: Wed, 2 Sep 2020 09:30:16 +0800 Subject: [PATCH 4/4] LiveData as Intent --- .../kotlinandroidworkshop/MainActivity.kt | 6 ++--- .../kotlinandroidworkshop/TodoViewModel.kt | 23 +++++++++++-------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/tw/andyang/kotlinandroidworkshop/MainActivity.kt b/app/src/main/java/tw/andyang/kotlinandroidworkshop/MainActivity.kt index bb14713..8c4f46f 100644 --- a/app/src/main/java/tw/andyang/kotlinandroidworkshop/MainActivity.kt +++ b/app/src/main/java/tw/andyang/kotlinandroidworkshop/MainActivity.kt @@ -2,9 +2,7 @@ package tw.andyang.kotlinandroidworkshop import android.os.Bundle import androidx.appcompat.app.AppCompatActivity -import androidx.lifecycle.Observer -import androidx.lifecycle.ViewModelProvider -import androidx.lifecycle.get +import androidx.lifecycle.* import androidx.recyclerview.widget.DividerItemDecoration import androidx.recyclerview.widget.LinearLayoutManager import kotlinx.android.synthetic.main.activity_main.* @@ -27,7 +25,7 @@ class MainActivity : AppCompatActivity() { }) buttonAdd.setOnClickListener { - todoViewModel.addNewTodo() + todoViewModel.onNewTodo.postValue(Unit) } } } \ No newline at end of file diff --git a/app/src/main/java/tw/andyang/kotlinandroidworkshop/TodoViewModel.kt b/app/src/main/java/tw/andyang/kotlinandroidworkshop/TodoViewModel.kt index ab056be..fdfab8b 100644 --- a/app/src/main/java/tw/andyang/kotlinandroidworkshop/TodoViewModel.kt +++ b/app/src/main/java/tw/andyang/kotlinandroidworkshop/TodoViewModel.kt @@ -1,19 +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() { +class TodoViewModel : ViewModel() { - val todoLiveData = MutableLiveData>( - mutableListOf(Todo.Title("This is a title")) - ) + val onNewTodo = MutableLiveData() - private var count = 0 - - fun addNewTodo() { - val todo = Todo.Item("note $count", false) - todoLiveData.value = todoLiveData.value!! + listOf(todo) - count ++ + val todoLiveData: LiveData> = MediatorLiveData>().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 } \ No newline at end of file