Skip to content

Commit

Permalink
Use getSharedViewModel() for sharing an instance of the MainViewModel
Browse files Browse the repository at this point in the history
  • Loading branch information
skydoves committed Aug 29, 2020
1 parent 5265aab commit fbf60b1
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import com.skydoves.disneymotions.databinding.FragmentHomeBinding
import com.skydoves.disneymotions.extensions.gone
import com.skydoves.disneymotions.extensions.visible
import com.skydoves.disneymotions.view.adapter.PosterAdapter
import org.koin.android.viewmodel.ext.android.getViewModel
import org.koin.android.viewmodel.ext.android.getSharedViewModel

class HomeFragment : DatabindingFragment() {

Expand All @@ -42,7 +42,7 @@ class HomeFragment : DatabindingFragment() {
savedInstanceState: Bundle?
): View? {
binding = binding<FragmentHomeBinding>(inflater, R.layout.fragment_home, container).apply {
viewModel = getViewModel<MainViewModel>().apply { fetchDisneyPosterList() }
viewModel = getSharedViewModel<MainViewModel>().apply { fetchDisneyPosterList() }
lifecycleOwner = this@HomeFragment
adapter = PosterAdapter()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import com.skydoves.disneymotions.R
import com.skydoves.disneymotions.base.DatabindingFragment
import com.skydoves.disneymotions.databinding.FragmentLibraryBinding
import com.skydoves.disneymotions.view.adapter.PosterLineAdapter
import org.koin.android.viewmodel.ext.android.getViewModel
import org.koin.android.viewmodel.ext.android.getSharedViewModel

class LibraryFragment : DatabindingFragment() {

Expand All @@ -34,7 +34,7 @@ class LibraryFragment : DatabindingFragment() {
savedInstanceState: Bundle?
): View? {
return binding<FragmentLibraryBinding>(inflater, R.layout.fragment_library, container).apply {
viewModel = getViewModel<MainViewModel>().apply { fetchDisneyPosterList() }
viewModel = getSharedViewModel<MainViewModel>().apply { fetchDisneyPosterList() }
lifecycleOwner = this@LibraryFragment
adapter = PosterLineAdapter()
}.root
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import com.skydoves.disneymotions.R
import com.skydoves.disneymotions.base.DatabindingFragment
import com.skydoves.disneymotions.databinding.FragmentRadioBinding
import com.skydoves.disneymotions.view.adapter.PosterCircleAdapter
import org.koin.android.viewmodel.ext.android.getViewModel
import org.koin.android.viewmodel.ext.android.getSharedViewModel

class RadioFragment : DatabindingFragment() {

Expand All @@ -34,7 +34,7 @@ class RadioFragment : DatabindingFragment() {
savedInstanceState: Bundle?
): View? {
return binding<FragmentRadioBinding>(inflater, R.layout.fragment_radio, container).apply {
viewModel = getViewModel<MainViewModel>().apply { fetchDisneyPosterList() }
viewModel = getSharedViewModel<MainViewModel>().apply { fetchDisneyPosterList() }
lifecycleOwner = this@RadioFragment
adapter = PosterCircleAdapter()
}.root
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,10 @@
app:layout_constraintStart_toStartOf="parent" />

<ProgressBar
android:id="@+id/progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:gone="@{!vm.isLoading()}"
app:gone="@{!vm.loading}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ class MainRepositoryTest {
ApiResponse.of { Response.success(mockData) })

repository.loadDisneyPosters(
error = {}
onSuccess = {},
onError = {}
).collect {
assertThat(it[0].id, `is`(0L))
assertThat(it[0].name, `is`("Frozen II"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ class MainViewModelTest {
val mockData = MockTestUtil.mockPosterList()
whenever(posterDao.getPosterList()).thenReturn(mockData)

val fetchedData = mainRepository.loadDisneyPosters { }.asLiveData()
val fetchedData = mainRepository.loadDisneyPosters(
onSuccess = {},
onError = {}
).asLiveData()

val observer: Observer<List<Poster>> = mock()
fetchedData.observeForever(observer)

Expand Down

0 comments on commit fbf60b1

Please sign in to comment.