Skip to content

Commit 3f97116

Browse files
committed
LiveData configuration change 시 데이터 한번 더 발행 되는 행위 Event 객체로 처리하게 수정
1 parent d4cf4f4 commit 3f97116

File tree

5 files changed

+46
-13
lines changed

5 files changed

+46
-13
lines changed

app/src/main/java/com/study/myapplication/base/BaseViewModel.kt

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,17 @@ package com.study.myapplication.base
33
import androidx.lifecycle.LiveData
44
import androidx.lifecycle.MutableLiveData
55
import androidx.lifecycle.ViewModel
6+
import com.study.myapplication.ui.Event
67
import io.reactivex.disposables.CompositeDisposable
78
import io.reactivex.disposables.Disposable
89

910
abstract class BaseViewModel : ViewModel() {
1011

11-
protected val _isDataLoading = MutableLiveData(true)
12-
val isDataLoading: LiveData<Boolean> get() = _isDataLoading
12+
protected val _isDataLoading = MutableLiveData(Event(true))
13+
val isDataLoading: LiveData<Event<Boolean>> get() = _isDataLoading
1314

14-
protected val _isDataLoadingError = MutableLiveData(false)
15-
val isDataLoadingError: LiveData<Boolean> get() = _isDataLoadingError
16-
17-
protected val _toastMessage = MutableLiveData<String>()
18-
val toastMessage: LiveData<String> get() = _toastMessage
15+
protected val _isDataLoadingError = MutableLiveData(Event(false))
16+
val isDataLoadingError: LiveData<Event<Boolean>> get() = _isDataLoadingError
1917

2018
private val compositeDisposable = CompositeDisposable()
2119

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.study.myapplication.ui
2+
3+
open class Event<out T>(private val content: T) {
4+
5+
private var hasBeenHandled = false
6+
private set
7+
8+
/**
9+
* Returns the content and prevents its use again.
10+
*/
11+
fun getContentIfNotHandled(): T? {
12+
return if (hasBeenHandled) {
13+
null
14+
} else {
15+
hasBeenHandled = true
16+
content
17+
}
18+
}
19+
20+
/**
21+
* Returns the content, even if it's already been handled.
22+
*/
23+
fun peekContent(): T = content
24+
}

app/src/main/java/com/study/myapplication/ui/MainActivity.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import android.annotation.SuppressLint
44
import android.content.Intent
55
import android.net.Uri
66
import android.os.Bundle
7+
import androidx.lifecycle.Observer
78
import com.study.myapplication.BR
89
import com.study.myapplication.R
910
import com.study.myapplication.api.model.MovieResponse
@@ -38,12 +39,21 @@ class MainActivity : BaseActivity<ActivityMainBinding, MainViewModel>(R.layout.a
3839
)
3940
}
4041
) {}
42+
// 검색 버튼 처리
4143
mainBtnSearch.setOnClickListener {
4244
viewModel.searchKeyword.value?.let {
4345
publishSubject.onNext(it)
4446
}
4547
}
4648
}
49+
// 데이터 로딩 에러 처리
50+
viewModel.isDataLoadingError.observe(this, Observer {
51+
it.getContentIfNotHandled()?.let {isError->
52+
if(isError){
53+
toastM(getString(R.string.main_toast_error_network))
54+
}
55+
}
56+
})
4757
}
4858

4959
@SuppressLint("CheckResult")

app/src/main/java/com/study/myapplication/ui/MainViewModel.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import com.study.myapplication.base.BaseViewModel
77
import com.study.myapplication.source.NaverRepository
88
import io.reactivex.android.schedulers.AndroidSchedulers
99
import io.reactivex.schedulers.Schedulers
10-
import io.reactivex.subjects.BehaviorSubject
11-
import javax.security.auth.Subject
1210

1311
class MainViewModel(private val naverRepository: NaverRepository) : BaseViewModel() {
1412

@@ -20,17 +18,19 @@ class MainViewModel(private val naverRepository: NaverRepository) : BaseViewMode
2018
searchKeyword.value = ""
2119
}
2220

23-
fun getMovieList(query : String) {
21+
fun getMovieList(query: String) {
2422
naverRepository.getMovieList(query)?.let { single ->
2523
addDisposable(
2624
single
2725
.subscribeOn(Schedulers.io())
2826
.observeOn(AndroidSchedulers.mainThread())
2927
.subscribe({
30-
_movieList.postValue(it.items)
31-
clearKeyword()
28+
_isDataLoadingError.value = Event(true)
29+
// _isDataLoadingError.value = Event(false)
30+
// _movieList.postValue(it.items)
31+
// clearKeyword()
3232
}, {
33-
33+
_isDataLoadingError.value = Event(true)
3434
})
3535
)
3636
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<resources>
22
<string name="app_name">My Application</string>
33
<string name="main_search">검색</string>
4+
<string name="main_toast_error_network">데이터를 가져올 수 없습니다</string>
45
</resources>

0 commit comments

Comments
 (0)