Skip to content

Commit

Permalink
Merge pull request #49 from develop-playground/feature/use-constructo…
Browse files Browse the repository at this point in the history
…r-parameter

ViewModel의 livedata로 값을 전달하지 않고 생성자 주입을 통해 id 전달
  • Loading branch information
junhyung0927 authored Jun 8, 2022
2 parents fdc3d40 + f3f758e commit 901ff2f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ import org.koin.dsl.module

val viewModelModule = module {
viewModel { RestaurantListViewModel(getRestaurantListUseCase = get())}
viewModel { RestaurantDetailViewModel(get()) }
viewModel { (id: Long) -> RestaurantDetailViewModel(id, get()) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,23 @@ import github.dev_playground.jeju_road.presentation.model.RestaurantDetailInform
import github.dev_playground.jeju_road.presentation.model.RestaurantIntroductionModel
import github.dev_playground.jeju_road.presentation.ui.base.BaseActivity
import github.dev_playground.jeju_road.presentation.ui.list.RestaurantListItemDecoration
import github.dev_playground.jeju_road.presentation.util.*
import github.dev_playground.jeju_road.presentation.util.addEnterMaterialSharedElementCallback
import github.dev_playground.jeju_road.presentation.util.addMaterialSharedElementEnterTransition
import github.dev_playground.jeju_road.presentation.util.addMaterialSharedElementReturnTransition
import github.dev_playground.jeju_road.presentation.util.onSuccess
import org.koin.androidx.viewmodel.ext.android.viewModel
import org.koin.core.parameter.parametersOf

class RestaurantDetailActivity : BaseActivity<ActivityRestaurantDetailBinding>(
R.layout.activity_restaurant_detail
) {

private val viewModel by viewModel<RestaurantDetailViewModel>()
private val viewModel by viewModel<RestaurantDetailViewModel> {
parametersOf(intent.getLongExtra(KEY_RESTAURANT_ID, 0))
}
private val adapter: RestaurantDetailListAdapter by lazy { RestaurantDetailListAdapter() }

private val transitionName: String? by lazy { intent.getStringExtra(KEY_TRANSITION_NAME) }
private val id: Long by lazy { intent.getLongExtra(KEY_RESTAURANT_ID, 0) }

override fun onCreate(savedInstanceState: Bundle?) {
makeTransition()
Expand All @@ -42,18 +47,14 @@ class RestaurantDetailActivity : BaseActivity<ActivityRestaurantDetailBinding>(
)
}

with(viewModel) {
id.value = this@RestaurantDetailActivity.id

detailInformationState.observe { state ->
state.onSuccess {
adapter.submitList(
listOf(
RestaurantIntroductionModel.toPresentation(it),
RestaurantDetailInformationModel.toPresentation(it)
)
viewModel.detailInformationState.observe { state ->
state.onSuccess {
adapter.submitList(
listOf(
RestaurantIntroductionModel.toPresentation(it),
RestaurantDetailInformationModel.toPresentation(it)
)
}
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
package github.dev_playground.jeju_road.presentation.ui.page

import androidx.lifecycle.*
import androidx.lifecycle.LiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.liveData
import github.dev_playground.jeju_road.domain.model.DetailInformation
import github.dev_playground.jeju_road.domain.usecase.GetRestaurantDetailUseCase
import github.dev_playground.jeju_road.presentation.util.UiState
import github.dev_playground.jeju_road.presentation.util.toUiState
import kotlinx.coroutines.*

class RestaurantDetailViewModel(
id: Long,
private val getRestaurantPageUseCase: GetRestaurantDetailUseCase
) : ViewModel() {

val id: MutableLiveData<Long> = MutableLiveData<Long>()

val detailInformationState: LiveData<UiState<DetailInformation>> = id.switchMap {
val detailInformationState: LiveData<UiState<DetailInformation>> =
liveData {
emit(UiState.loading())
emit(getRestaurantPageUseCase.invoke(it).toUiState())
emit(getRestaurantPageUseCase.invoke(id).toUiState())
}
}
}

0 comments on commit 901ff2f

Please sign in to comment.