Skip to content

Commit

Permalink
refactor: MediatorLiveData 확장 함수 Util 파일 생성
Browse files Browse the repository at this point in the history
  • Loading branch information
inseonyun committed Aug 9, 2023
1 parent 27df92e commit b2f62e8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import com.created.team201.presentation.createStudy.model.CreateStudyUiModel
import com.created.team201.presentation.createStudy.model.PeriodUiModel
import com.created.team201.util.NonNullLiveData
import com.created.team201.util.NonNullMutableLiveData
import com.created.team201.util.addSourceList
import kotlinx.coroutines.launch

class CreateStudyViewModel(
Expand Down Expand Up @@ -136,17 +137,6 @@ class CreateStudyViewModel(

private fun Int.isNotZero(): Boolean = this != 0

private fun <T> MediatorLiveData<T>.addSourceList(
vararg liveDataArgument: LiveData<*>,
onChanged: () -> T,
) {
liveDataArgument.forEach {
this.addSource(it) {
value = onChanged()
}
}
}

companion object {
val Factory: ViewModelProvider.Factory = viewModelFactory {
initializer {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.created.team201.util

import androidx.lifecycle.LiveData
import androidx.lifecycle.MediatorLiveData

fun <T> MediatorLiveData<T>.addSourceList(
vararg liveDataArgument: LiveData<*>,
onChanged: () -> T,
) {
liveDataArgument.forEach {
this.addSource(it) {
value = onChanged()
}
}
}

0 comments on commit b2f62e8

Please sign in to comment.