Skip to content

Commit d0fcd76

Browse files
committed
Do not use viewmodel from simple ui trigger event
1 parent d851cdf commit d0fcd76

File tree

6 files changed

+56
-84
lines changed

6 files changed

+56
-84
lines changed

app/src/main/java/com/example/wordbook/main/MainFragment.kt

+22-26
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,12 @@ import android.view.ViewGroup
1010
import android.widget.Toast
1111
import androidx.databinding.DataBindingUtil
1212
import androidx.fragment.app.FragmentManager
13-
import androidx.lifecycle.lifecycleScope
1413
import com.example.wordbook.BaseActivity
1514
import com.example.wordbook.R
1615
import com.example.wordbook.databinding.FragmentMainBinding
1716
import com.example.wordbook.study.StudyFragment
1817
import com.example.wordbook.test.TestFragment
1918
import com.example.wordbook.vocalist.VocaListBaseFragment
20-
import com.example.wordbook.vocalist.VocaListFragment
21-
import kotlinx.coroutines.launch
2219

2320
class MainFragment : Fragment() {
2421

@@ -40,37 +37,36 @@ class MainFragment : Fragment() {
4037

4138
binding.viewModel = viewModel
4239

43-
viewModel.mMoveToStudy.observe(viewLifecycleOwner) {
44-
if (it) {
45-
lifecycleScope.launch {
46-
if (viewModel.moveToStudyEnabled()) {
47-
addStudyFragment()
48-
viewModel.moveToStudyDone()
49-
} else {
50-
Toast.makeText(context, R.string.error_limit_1_word, Toast.LENGTH_SHORT).show()
51-
}
40+
viewModel.mStudyMovingState.observe(viewLifecycleOwner) {
41+
when (it) {
42+
MovingState.IDLE -> {}
43+
MovingState.MOVE -> {
44+
addStudyFragment()
45+
viewModel.setStudyMovingStateIdle()
46+
}
47+
MovingState.FAIL -> {
48+
Toast.makeText(context, R.string.error_limit_1_word, Toast.LENGTH_SHORT).show()
49+
viewModel.setStudyMovingStateIdle()
5250
}
5351
}
5452
}
5553

56-
viewModel.mMoveToTest.observe(viewLifecycleOwner) {
57-
if (it) {
58-
lifecycleScope.launch {
59-
if (viewModel.moveToTestEnabled()) {
60-
addTestFragment()
61-
viewModel.moveToTestDone()
62-
} else {
63-
Toast.makeText(context, R.string.error_limit_4_word, Toast.LENGTH_SHORT).show()
64-
}
54+
viewModel.mTestMovingState.observe(viewLifecycleOwner) {
55+
when (it) {
56+
MovingState.IDLE -> {}
57+
MovingState.MOVE -> {
58+
addTestFragment()
59+
viewModel.setTestMovingStateIdle()
60+
}
61+
MovingState.FAIL -> {
62+
Toast.makeText(context, R.string.error_limit_4_word, Toast.LENGTH_SHORT).show()
63+
viewModel.setTestMovingStateIdle()
6564
}
6665
}
6766
}
6867

69-
viewModel.mMoveToVocaList.observe(viewLifecycleOwner) {
70-
if (it) {
71-
addVocaListFragment()
72-
viewModel.moveToVocaListDone()
73-
}
68+
binding.register.setOnClickListener {
69+
addVocaListFragment()
7470
}
7571

7672
return binding.root
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
package com.example.wordbook.main
22

33
import android.app.Application
4-
import android.util.Log
54
import androidx.lifecycle.*
65
import com.example.wordbook.database.getDatabase
76
import com.example.wordbook.repository.WordRepository
87
import kotlinx.coroutines.async
8+
import kotlinx.coroutines.launch
9+
10+
11+
enum class MovingState {
12+
IDLE, MOVE, FAIL
13+
}
914

1015
class MainViewModel(application: Application): AndroidViewModel(application) {
1116
companion object {
@@ -14,57 +19,51 @@ class MainViewModel(application: Application): AndroidViewModel(application) {
1419
}
1520
private val repository = WordRepository(getDatabase(application))
1621

17-
private val _mMoveToStudy = MutableLiveData<Boolean>(false)
18-
val mMoveToStudy: LiveData<Boolean>
19-
get() = _mMoveToStudy
20-
21-
private val _mMoveToTest = MutableLiveData<Boolean>(false)
22-
val mMoveToTest: LiveData<Boolean>
23-
get() = _mMoveToTest
22+
private val _mStudyMovingState = MutableLiveData<MovingState>(MovingState.IDLE)
23+
val mStudyMovingState: LiveData<MovingState>
24+
get() = _mStudyMovingState
2425

25-
private val _mMoveToVocaList= MutableLiveData<Boolean>(false)
26-
val mMoveToVocaList: LiveData<Boolean>
27-
get() = _mMoveToVocaList
26+
private val _mTestMovingState = MutableLiveData<MovingState>(MovingState.IDLE)
27+
val mTestMovingState: LiveData<MovingState>
28+
get() = _mTestMovingState
2829

2930
fun moveToStudy() {
30-
_mMoveToStudy.value = true
31+
viewModelScope.launch {
32+
if (moveToStudyEnabled()) {
33+
_mStudyMovingState.value = MovingState.MOVE
34+
} else {
35+
_mStudyMovingState.value = MovingState.FAIL
36+
}
37+
}
3138
}
3239

33-
fun moveToStudyDone() {
34-
_mMoveToStudy.value = false
40+
fun setStudyMovingStateIdle() {
41+
_mStudyMovingState.value = MovingState.IDLE
3542
}
3643

3744
fun moveToTest() {
38-
_mMoveToTest.value = true
39-
}
40-
41-
fun moveToTestDone() {
42-
_mMoveToTest.value = false
45+
viewModelScope.launch {
46+
if (moveToTestEnabled()) {
47+
_mTestMovingState.value = MovingState.MOVE
48+
} else {
49+
_mTestMovingState.value = MovingState.FAIL
50+
}
51+
}
4352
}
4453

45-
fun moveToVocaList() {
46-
_mMoveToVocaList.value = true
54+
fun setTestMovingStateIdle() {
55+
_mTestMovingState.value = MovingState.IDLE
4756
}
4857

49-
fun moveToVocaListDone() {
50-
_mMoveToVocaList.value = false
51-
}
52-
53-
suspend fun moveToStudyEnabled(): Boolean {
58+
private suspend fun moveToStudyEnabled(): Boolean {
5459
return viewModelScope.async {
5560
repository.getCounts() >= LIMIT_TO_MOVE_STUDY
5661
}.await()
5762
}
5863

59-
suspend fun moveToTestEnabled(): Boolean {
64+
private suspend fun moveToTestEnabled(): Boolean {
6065
return viewModelScope.async {
6166
repository.getCounts() >= LIMIT_TO_MOVE_TEST
6267
}.await()
6368
}
64-
65-
override fun onCleared() {
66-
super.onCleared()
67-
68-
Log.d("Yebon", "onClear main")
69-
}
7069
}

app/src/main/java/com/example/wordbook/vocalist/VocaListFragment.kt

+2-5
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,8 @@ class VocaListFragment : Fragment() {
3838
adapter = VocaListAdapter(::moveToEditVoca)
3939
binding.list.adapter = adapter
4040

41-
viewModel.moveToRegisterVoca.observe(viewLifecycleOwner) {
42-
if (it) {
43-
moveToRegisterVoca()
44-
viewModel.moveToRegisterVocaDone()
45-
}
41+
binding.add.setOnClickListener {
42+
moveToRegisterVoca()
4643
}
4744

4845
viewModel.vocas.observe(viewLifecycleOwner) {

app/src/main/java/com/example/wordbook/vocalist/VocaListViewModel.kt

-18
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,5 @@ import com.example.wordbook.repository.WordRepository
1212
class VocaListViewModel(application: Application): AndroidViewModel(application) {
1313
private val repository = WordRepository(getDatabase(application))
1414

15-
private val _moveToRegisterVoca = MutableLiveData<Boolean>()
16-
val moveToRegisterVoca: LiveData<Boolean>
17-
get() = _moveToRegisterVoca
18-
1915
val vocas = repository.getWordListByLiveData()
20-
21-
fun moveToRegisterVoca() {
22-
_moveToRegisterVoca.value = true
23-
}
24-
25-
fun moveToRegisterVocaDone() {
26-
_moveToRegisterVoca.value = false
27-
}
28-
29-
override fun onCleared() {
30-
super.onCleared()
31-
32-
Log.d("Yebon", "cleared");
33-
}
3416
}

app/src/main/res/layout/fragment_main.xml

-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@
5959
android:layout_height="wrap_content"
6060
android:layout_marginTop="20dp"
6161
android:background="@color/black"
62-
android:onClick="@{() -> viewModel.moveToVocaList()}"
6362
android:text="@string/action_register"
6463
android:textColor="@color/white"
6564
app:layout_constraintLeft_toLeftOf="parent"

app/src/main/res/layout/fragment_voca_list.xml

-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
android:layout_height="wrap_content"
3030
android:layout_marginRight="20dp"
3131
android:layout_marginBottom="20dp"
32-
android:onClick="@{() -> viewModel.moveToRegisterVoca()}"
3332
android:scaleType="centerCrop"
3433
android:src="@android:drawable/ic_menu_add"
3534
app:borderWidth="0dp"

0 commit comments

Comments
 (0)