Fixing crash due to initialization order in detail inj

This commit is contained in:
Melih Aksoy
2019-11-18 14:19:15 +01:00
parent 88022629e1
commit a6fec53b83
4 changed files with 16 additions and 40 deletions

View File

@@ -3,10 +3,8 @@ package com.melih.core.base.viewmodel
import androidx.lifecycle.LiveData import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.melih.abstractions.deliverable.Reason import com.melih.abstractions.deliverable.Reason
import com.melih.abstractions.deliverable.State import com.melih.abstractions.deliverable.State
import kotlinx.coroutines.launch
/** /**
* Base [ViewModel] for view models that will process data. * Base [ViewModel] for view models that will process data.
@@ -15,17 +13,6 @@ import kotlinx.coroutines.launch
*/ */
abstract class BaseViewModel<T> : ViewModel() { abstract class BaseViewModel<T> : ViewModel() {
//region Abstractions
abstract suspend fun loadData()
//endregion
init {
viewModelScope.launch {
loadData()
}
}
//region Properties //region Properties
private val _successData = MutableLiveData<T>() private val _successData = MutableLiveData<T>()
@@ -79,23 +66,5 @@ abstract class BaseViewModel<T> : ViewModel() {
protected fun handleFailure(reason: Reason) { protected fun handleFailure(reason: Reason) {
_errorData.value = reason _errorData.value = reason
} }
/**
* Reload data
*/
fun refresh() {
viewModelScope.launch {
loadData()
}
}
/**
* Retry loading data, incase there's difference between refresh and retry, should go here
*/
fun retry() {
viewModelScope.launch {
loadData()
}
}
//endregion //endregion
} }

View File

@@ -11,7 +11,7 @@ class BaseViewModelTest : BaseTestWithMainThread() {
@Test @Test
fun `refresh should invoke loadData`() { fun `refresh should invoke loadData`() {
val baseVm = spyk(TestViewModel()) val baseVm = spyk(TestViewModel())
baseVm.refresh() baseVm.loadData()
coVerify(exactly = 1) { baseVm.loadData() } coVerify(exactly = 1) { baseVm.loadData() }
} }
@@ -19,14 +19,14 @@ class BaseViewModelTest : BaseTestWithMainThread() {
@Test @Test
fun `retry should invoke loadData`() { fun `retry should invoke loadData`() {
val baseVm = spyk(TestViewModel()) val baseVm = spyk(TestViewModel())
baseVm.retry() baseVm.loadData()
coVerify(exactly = 1) { baseVm.loadData() } coVerify(exactly = 1) { baseVm.loadData() }
} }
} }
class TestViewModel : BaseViewModel<Unit>() { class TestViewModel : BaseViewModel<Unit>() {
override suspend fun loadData() { fun loadData() {
// no - op
} }
} }

View File

@@ -27,7 +27,7 @@ class DetailFragment : BaseDaggerFragment<DetailBinding>() {
// Observing error to show toast with retry action // Observing error to show toast with retry action
observe(viewModel.errorData) { observe(viewModel.errorData) {
showSnackbarWithAction(it) { showSnackbarWithAction(it) {
viewModel.retry() viewModel.loadData()
} }
} }
} }

View File

@@ -1,12 +1,13 @@
package com.melih.detail.ui package com.melih.detail.ui
import androidx.lifecycle.Transformations.map import androidx.lifecycle.Transformations.map
import androidx.lifecycle.viewModelScope
import com.melih.abstractions.deliverable.handle import com.melih.abstractions.deliverable.handle
import com.melih.core.base.viewmodel.BaseViewModel import com.melih.core.base.viewmodel.BaseViewModel
import com.melih.interactors.GetLaunchDetails import com.melih.interactors.GetLaunchDetails
import com.melih.launches.data.LaunchDetailItem import com.melih.launches.data.LaunchDetailItem
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.collect import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import javax.inject.Inject import javax.inject.Inject
class DetailViewModel @Inject constructor( class DetailViewModel @Inject constructor(
@@ -29,14 +30,20 @@ class DetailViewModel @Inject constructor(
} }
//endregion //endregion
init {
loadData()
}
//region Functions //region Functions
/** /**
* Triggering interactor in view model scope * Triggering interactor in view model scope
*/ */
override suspend fun loadData() { fun loadData() {
getLaunchDetails(getLaunchDetailsParams).collect { viewModelScope.launch {
it.handle(::handleState, ::handleFailure, ::handleSuccess) getLaunchDetails(getLaunchDetailsParams).collect {
it.handle(::handleState, ::handleFailure, ::handleSuccess)
}
} }
} }
//endregion //endregion