mirror of
https://github.com/melihaksoy/Android-Kotlin-Modulerized-CleanArchitecture.git
synced 2026-01-16 14:06:55 +01:00
Fixing crash due to initialization order in detail inj
This commit is contained in:
@@ -3,10 +3,8 @@ package com.melih.core.base.viewmodel
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.melih.abstractions.deliverable.Reason
|
||||
import com.melih.abstractions.deliverable.State
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
/**
|
||||
* Base [ViewModel] for view models that will process data.
|
||||
@@ -15,17 +13,6 @@ import kotlinx.coroutines.launch
|
||||
*/
|
||||
abstract class BaseViewModel<T> : ViewModel() {
|
||||
|
||||
//region Abstractions
|
||||
|
||||
abstract suspend fun loadData()
|
||||
//endregion
|
||||
|
||||
init {
|
||||
viewModelScope.launch {
|
||||
loadData()
|
||||
}
|
||||
}
|
||||
|
||||
//region Properties
|
||||
|
||||
private val _successData = MutableLiveData<T>()
|
||||
@@ -79,23 +66,5 @@ abstract class BaseViewModel<T> : ViewModel() {
|
||||
protected fun handleFailure(reason: 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
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ class BaseViewModelTest : BaseTestWithMainThread() {
|
||||
@Test
|
||||
fun `refresh should invoke loadData`() {
|
||||
val baseVm = spyk(TestViewModel())
|
||||
baseVm.refresh()
|
||||
baseVm.loadData()
|
||||
|
||||
coVerify(exactly = 1) { baseVm.loadData() }
|
||||
}
|
||||
@@ -19,14 +19,14 @@ class BaseViewModelTest : BaseTestWithMainThread() {
|
||||
@Test
|
||||
fun `retry should invoke loadData`() {
|
||||
val baseVm = spyk(TestViewModel())
|
||||
baseVm.retry()
|
||||
baseVm.loadData()
|
||||
|
||||
coVerify(exactly = 1) { baseVm.loadData() }
|
||||
}
|
||||
}
|
||||
|
||||
class TestViewModel : BaseViewModel<Unit>() {
|
||||
override suspend fun loadData() {
|
||||
// no - op
|
||||
fun loadData() {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ class DetailFragment : BaseDaggerFragment<DetailBinding>() {
|
||||
// Observing error to show toast with retry action
|
||||
observe(viewModel.errorData) {
|
||||
showSnackbarWithAction(it) {
|
||||
viewModel.retry()
|
||||
viewModel.loadData()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
package com.melih.detail.ui
|
||||
|
||||
import androidx.lifecycle.Transformations.map
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.melih.abstractions.deliverable.handle
|
||||
import com.melih.core.base.viewmodel.BaseViewModel
|
||||
import com.melih.interactors.GetLaunchDetails
|
||||
import com.melih.launches.data.LaunchDetailItem
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.collect
|
||||
import kotlinx.coroutines.launch
|
||||
import javax.inject.Inject
|
||||
|
||||
class DetailViewModel @Inject constructor(
|
||||
@@ -29,14 +30,20 @@ class DetailViewModel @Inject constructor(
|
||||
}
|
||||
//endregion
|
||||
|
||||
init {
|
||||
loadData()
|
||||
}
|
||||
|
||||
//region Functions
|
||||
|
||||
/**
|
||||
* Triggering interactor in view model scope
|
||||
*/
|
||||
override suspend fun loadData() {
|
||||
getLaunchDetails(getLaunchDetailsParams).collect {
|
||||
it.handle(::handleState, ::handleFailure, ::handleSuccess)
|
||||
fun loadData() {
|
||||
viewModelScope.launch {
|
||||
getLaunchDetails(getLaunchDetailsParams).collect {
|
||||
it.handle(::handleState, ::handleFailure, ::handleSuccess)
|
||||
}
|
||||
}
|
||||
}
|
||||
//endregion
|
||||
|
||||
Reference in New Issue
Block a user