mirror of
https://github.com/melihaksoy/Android-Kotlin-Modulerized-CleanArchitecture.git
synced 2026-05-01 05:04:19 +02: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() {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user