I am using viewModelScope in the ViewModel which calls a suspend function in the repository as shown below:
ViewModel
class DeepFilterViewModel(val repo: DeepFilterRepository) : ViewModel() {
var deepFilterLiveData: LiveData<Result>? = null
fun onImageCompressed(compressedImage: File): LiveData<Result>? {
if (deepFilterLiveData == null) {
viewModelScope.launch {
deepFilterLiveData = repo.applyFilter(compressedImage)
}
}
return deepFilterLiveData
}
}
Repository
class DeepFilterRepository {
suspend fun applyFilter(compressedImage: File): LiveData<Result> {
val mutableLiveData = MutableLiveData<Result>()
mutableLiveData.value = Result.Loading
withContext(Dispatchers.IO) {
mutableLiveData.value = Result.Success("Done")
}
return mutableLiveData
}
}
I am observing the LiveData from the Fragment as shown below:
viewModel.onImageCompressed(compressedImage)?.observe(this, Observer { result ->
when (result) {
is Result.Loading -> {
loader.makeVisible()
}
is Result.Success<*> -> {
// Process result
}
}
})
The problem is I am getting no value from the LiveData. If I don't use viewModelScope.launch {} as shown below, then everything works fine.
class DeepFilterViewModel(val repo: DeepFilterRepository) : ViewModel() {
var deepFilterLiveData: LiveData<Result>? = null
fun onImageCompressed(compressedImage: File): LiveData<Result>? {
if (deepFilterLiveData == null) {
deepFilterLiveData = repo.applyFilter(compressedImage)
}
return deepFilterLiveData
}
}
I don't know what I am missing. Any help will be appreciated.
This code:
viewModelScope.launch {
deepFilterLiveData = repo.applyFilter(compressedImage)
}
returns immediately so when you first invoke the onImageCompressed() method you return null as deepFilterLiveData. Because in your UI you use ?. on the null return value of onImageCompressed() the when clause will not be reached. The code without the coroutine works because in that case you have sequential code, your ViewModel awaits for the repository call.
To solve this you could keep the LiveData for the ViewModel-UI interaction and return the values directly from the repository method:
class DeepFilterRepository {
suspend fun applyFilter(compressedImage: File) = withContext(Dispatchers.IO) {
Result.Success("Done")
}
}
And the ViewModel:
class DeepFilterViewModel(val repo: DeepFilterRepository) : ViewModel() {
private val _backingLiveData = MutableLiveData<Result>()
val deepFilterLiveData: LiveData<Result>
get() = _backingLiveData
fun onImageCompressed(compressedImage: File) {
// you could also set Loading as the initial state for _backingLiveData.value
_backingLiveData.value = Result.Loading
viewModelScope.launch {
_backingLiveData.value = repo.applyFilter(compressedImage)
}
}
}
Related
I want to write a simple test for my viewModel to check if it gets data from repository. The app itself working without problem but in test, i have the following test failed.
It looks like the viewModel init block not running, because it suppose to call getUpcomingMovies() method in init blocks and post value to upcomingMovies live data object. When i test it gets null value.
Looks like i am missing a minor thing, need help to solve this.
Here is the test:
#ExperimentalCoroutinesApi
class MoviesViewModelShould: BaseUnitTest() {
private val repository: MoviesRepository = mock()
private val upcomingMovies = mock<Response<UpcomingResponse>>()
private val upcomingMoviesExpected = Result.success(upcomingMovies)
#Test
fun emitsUpcomingMoviesFromRepository() = runBlocking {
val viewModel = mockSuccessfulCaseUpcomingMovies()
assertEquals(upcomingMoviesExpected, viewModel.upcomingMovies.getValueForTest())
}
private fun mockSuccessfulCaseUpcomingMovies(): MoviesViewModel {
runBlocking {
whenever(repository.getUpcomingMovies(1)).thenReturn(
flow {
emit(upcomingMoviesExpected)
}
)
}
return MoviesViewModel(repository)
}
}
And viewModel:
class MoviesViewModel(
private val repository: MoviesRepository
): ViewModel() {
val upcomingMovies: MutableLiveData<UpcomingResponse> = MutableLiveData()
var upcomingMoviesPage = 0
private var upcomingMoviesResponse: UpcomingResponse? = null
init {
getUpcomingMovies()
}
fun getUpcomingMovies() = viewModelScope.launch {
upcomingMoviesPage++
repository.getUpcomingMovies(upcomingMoviesPage).collect { result ->
if (result.isSuccess) {
result.getOrNull()!!.body()?.let {
if (upcomingMoviesResponse == null) {
upcomingMoviesResponse = it
} else {
val oldMovies = upcomingMoviesResponse?.results
val newMovies = it.results
oldMovies?.addAll(newMovies)
}
upcomingMovies.postValue(upcomingMoviesResponse ?: it)
}
}
}
}
}
And the result is:
expected:<Success(Mock for Response, hashCode: 1625939772)> but was:<null>
Expected :Success(Mock for Response, hashCode: 1625939772)
Actual :null
There is a convenient wrapper that allows you to reduce the boilerplate when you work with LiveData - observeEvents.
open class Event<T>(value: T? = null) {
val liveData = MutableLiveData(value)
protected var hasBeenHandled = false
fun getContentIfNotHandled(): T? {
return if (hasBeenHandled || liveData.value == null) {
null
} else {
hasBeenHandled = true
liveData.value
}
}
companion object {
fun <T> LifecycleOwner.observeEvents(event: Event<T>, body: (T?) -> Unit) {
event.liveData.observe(this) { body(event.getContentIfNotHandled()) }
}
}
}
class MutableEvent<T>(value: T? = null) : Event<T>(value) {
#MainThread
fun fireEvent(event: T) {
hasBeenHandled = false
liveData.value = event
}
#WorkerThread
fun postEvent(event: T) {
hasBeenHandled = false
liveData.postValue(event)
}
}
Next, we can see how to use it.
There is the following sealed class for specific events:
sealed class ProductEvent {
data class AddProduct(val data: SomeProduct) : ProductEvent()
data class RemoveProduct(val productId: String) : ProductEvent()
}
ViewModel code:
private val _productEvents = MutableEvent<ProductEvent>()
val productEvents = _productEvents
private fun addProduct() {
val product: SomeProduct = repository.getProduct()
_productEvents.fireEvent(ProductEvent.AddProduct(product)
}
Activity/Fragment code:
observeEvents(viewModel.productEvents) { event ->
event?.let {
when(event) {
is ProductEvent.AddProduct -> // add product
is ProductEvent.RemoveProduct-> // remove product
}
}
}
Everything works fine, but there is one thing.
For example, when we use registerForActivityResult:
private val result = registerForActivityResult(ActivityResultContracts.StartActivityForResult())
{ result: ActivityResult ->
if (result.resultCode == Activity.RESULT_OK) {
result.data?.getIntExtra(SomeActivity.SOME_RESULT, 0)?.let {
// do work that will call ProductEvent
// viewModel.addProduct() - for example
}
}
}
When SomeActivity finishes and we return here, this code will run before LifecycleOwner will be active and because of that a subscriber will not be called.
There is a solution (lifecycleScope.launchWhenResumed), but the fact is that if we define our LiveData as usual:
// viewModel
private val _product = MutableLiveData<SomeProduct>()
val product = _product
// Activity/Fragment
viewModel.product.observe(lifecycleOwner) {}
then the subscriber will work as expected.
I would like to know what the difference. observeEvents is merely a wrapper that does the same thing, but for some reason works a little differently.
so i'm building an export function to export data from Room Database to external file (*.txt file), i'm trying to achieve this by using suspend function from Dao to ViewModel to get all the data, I think i don't really need LiveData because i dont observe it and just calling it one time. Here are the codes
ItemDao
#Dao
interface ItemDao {
#Query("SELECT * FROM item_table")
suspend fun readItemWithUnits_(): List<ItemModel>
}
ViewModel
#HiltViewModel
class HomeViewModel #Inject constructor (private val itemDao: ItemDao): ViewModel() {
fun readItemWithUnits_(): Deferred<List<ItemModel>> {
return viewModelScope.async(Dispatchers.IO) {
itemDao.readItemWithUnits_()
}
}
}
And calling the Deferred from a Fragment inside withContext(Dispatchers.Main)
Fragment
val viewModel: HomeViewModel by viewModels()
// Codes before //
private val writeExample = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
if (it.resultCode == Activity.RESULT_OK) {
val userChosenUri = it.data?.data
val outStream = requireContext().contentResolver.openOutputStream(userChosenUri!!)
lifecycleScope.launchWhenCreated {
withContext(Dispatchers.Main) {
val listOfItemWithUnit = viewModel.readItemWithUnits_().await()
var exportContent = "#item_table\n"
listOfItemWithUnit.forEach { itemModel ->
exportContent += "${itemModel.itemId};${itemModel.itemName};${itemModel.itemNote}\n"
}
exportContent.byteInputStream().use { input ->
outStream.use { output ->
input.copyTo(output!!)
}
}
}
}
}
}
// Codes After //
For now these codes work just fine, the question is, am i doing it correctly? since i will be dealing with a lot of data or is there a better way?
Edit
I've tried something like this, change from lifecycleScope.launchWhenCreated to CoroutineScope(Dispatchers.IO).launch
private val writeExample = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
if (it.resultCode == Activity.RESULT_OK) {
val userChosenUri = it.data?.data
val outStream = requireContext().contentResolver.openOutputStream(userChosenUri!!)
CoroutineScope(Dispatchers.IO).launch {
val listOfItemWithUnit = viewModel.readItemWithUnits_().await()
var exportContent = "#item_table\n"
listOfItemWithUnit.forEach { itemModel ->
exportContent += "${itemModel.item.itemId};${itemModel.item.itemName};${itemModel.item.itemNote}\n"
}
exportContent.byteInputStream().use { input ->
outStream.use { output ->
input.copyTo(output!!)
}
}
}
}
}
It return an error
java.lang.IllegalStateException: Method addObserver must be called on the main thread
And this, without withContext(Dispatchers.Main)
private val writeExample = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
if (it.resultCode == Activity.RESULT_OK) {
val userChosenUri = it.data?.data
val outStream = requireContext().contentResolver.openOutputStream(userChosenUri!!)
lifecycleScope.launchWhenCreated {
val listOfItemWithUnit = viewModel.readItemWithUnits_().await()
var exportContent = "#item_table\n"
listOfItemWithUnit.forEach { itemModel ->
exportContent += "${itemModel.item.itemId};${itemModel.item.itemName};${itemModel.item.itemNote}\n"
}
exportContent.byteInputStream().use { input ->
outStream.use { output ->
input.copyTo(output!!)
}
}
}
}
}
It return an error
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
Structure your code like this
CoroutineScope(Dispatchers.IO).launch{ // do your background tasks here
withContext(Dispatchers.Main){ //do tasks on the main thread that you want with that data
} }
Since it is a database operation without anything on the main thread the whole code will be in the CoroutineScope(Dispatchers.IO).launch block.
My problem is, that when I try to get a document out of my database, that this document aka the object is always null. I only have this problem when I use Kotlin Coroutines to get the document out of my database. Using the standard approach with listeners do work.
EmailRepository
interface EmailRepository {
suspend fun getCalibratePrice(): Flow<EmailEntity?>
suspend fun getRepairPrice(): Flow<EmailEntity?>
}
EmailRepository Implementation
class EmailRepositoryImpl #Inject constructor(private val db: FirebaseFirestore) : EmailRepository {
fun hasInternet(): Boolean {
return true
}
// This works! When using flow to write a document, the document is written!
override fun sendEmail(email: Email)= flow {
emit(EmailStatus.loading())
if (hasInternet()) {
db.collection("emails").add(email).await()
emit(EmailStatus.success(Unit))
} else {
emit(EmailStatus.failed<Unit>("No Email connection"))
}
}.catch {
emit(EmailStatus.failed(it.message.toString()))
}.flowOn(Dispatchers.Main)
// This does not work! "EmailEntity" is always null. I checked the document path!
override suspend fun getCalibratePrice(): Flow<EmailEntity?> = flow {
val result = db.collection("emailprice").document("Kalibrieren").get().await()
emit(result.toObject<EmailEntity>())
}.catch {
}.flowOn(Dispatchers.Main)
// This does not work! "EmailEntity" is always null. I checked the document path!
override suspend fun getRepairPrice(): Flow<EmailEntity?> = flow {
val result = db.collection("emailprice").document("Reparieren").get().await()
emit(result.toObject<EmailEntity>())
}.catch {
}.flowOn(Dispatchers.Main)
}
Viewmodel where I get the data
init {
viewModelScope.launch {
withContext(Dispatchers.IO) {
if (subject.value != null){
when(subject.value) {
"Test" -> {
emailRepository.getCalibratePrice().collect {
emailEntity.value = it
}
}
"Toast" -> {
emailRepository.getRepairPrice().collect {
emailEntity.value = it
}
}
}
}
}
}
}
private val emailEntity = MutableLiveData<EmailEntity?>()
private val _subject = MutableLiveData<String>()
val subject: LiveData<String> get() = _subject
Fragment
#AndroidEntryPoint
class CalibrateRepairMessageFragment() : EmailFragment<FragmentCalibrateRepairMessageBinding>(
R.layout.fragment_calibrate_repair_message,
) {
// Get current toolbar Title and send it to the next fragment.
private val toolbarText: CharSequence by lazy { toolbar_title.text }
override val viewModel: EmailViewModel by navGraphViewModels(R.id.nav_send_email) { defaultViewModelProviderFactory }
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// Here I set the data from the MutableLiveData "subject". I don't know how to do it better
viewModel.setSubject(toolbarText.toString())
}
}
One would say, that the Firebase rules are the problems here, but that should not be the case here, because the database is open and using the listener approach does work.
I get the subject.value from my CalibrateRepairMessageFragment. When I don't check if(subject.value != null) I get a NullPointerException from my init block.
I will use the emailEntitiy only in my viewModel and not outside it.
I appreciate every help, thank you.
EDIT
This is the new way I get the data. The object is still null! I've also added Timber.d messages in my suspend functions which also never get executed therefore flow never throws an error.. With this new approach I don't get a NullPointerException anymore
private val emailEntity = liveData {
when(subject.value) {
"Test" -> emailRepository.getCalibratePrice().collect {
emit(it)
}
"Toast" -> emailRepository.getRepairPrice().collect {
emit(it)
}
// Else block is never executed, therefore "subject.value" is either Test or toast and the logic works. Still error when using flow!
else -> EmailEntity("ERROR", 0F)
}
}
I check if the emailEntity is null or not with Timber.d("EmailEntity is ${emailEntity.value}") in one of my functions.
I then set the price with val price = MutableLiveData(emailEntity.value?.basePrice ?: 1000F) but because emailentity is null the price is always 1000
EDIT 2
I have now further researched the problem and made a big step forward. When observing the emailEntity from a fragment like CalibrateRepairMessageFragment the value is no longer null.
Furthermore, when observing emailEntity the value is also not null in viewModel, but only when it is observed in one fragment! So how can I observe emailEntity from my viewModel or get the value from my repository and use it in my viewmodel?
Okay, I have solved my problem, this is the final solution:
Status class
sealed class Status<out T> {
data class Success<out T>(val data: T) : Status<T>()
class Loading<T> : Status<T>()
data class Failure<out T>(val message: String?) : Status<T>()
companion object {
fun <T> success(data: T) = Success<T>(data)
fun <T> loading() = Loading<T>()
fun <T> failed(message: String?) = Failure<T>(message)
}
}
EmailRepository
interface EmailRepository {
fun sendEmail(email: Email): Flow<Status<Unit>>
suspend fun getCalibratePrice(): Flow<Status<CalibrateRepairPricing?>>
suspend fun getRepairPrice(): Flow<Status<CalibrateRepairPricing?>>
}
EmailRepositoryImpl
class EmailRepositoryImpl (private val db: FirebaseFirestore) : EmailRepository {
fun hasInternet(): Boolean {
return true
}
override fun sendEmail(email: Email)= flow {
Timber.d("Executed Send Email Repository")
emit(Status.loading())
if (hasInternet()) {
db.collection("emails").add(email).await()
emit(Status.success(Unit))
} else {
emit(Status.failed<Unit>("No Internet connection"))
}
}.catch {
emit(Status.failed(it.message.toString()))
}.flowOn(Dispatchers.Main)
// Sends status and object to viewModel
override suspend fun getCalibratePrice(): Flow<Status<CalibrateRepairPricing?>> = flow {
emit(Status.loading())
val entity = db.collection("emailprice").document("Kalibrieren").get().await().toObject<CalibrateRepairPricing>()
emit(Status.success(entity))
}.catch {
Timber.d("Error on getCalibrate Price")
emit(Status.failed(it.message.toString()))
}
// Sends status and object to viewModel
override suspend fun getRepairPrice(): Flow<Status<CalibrateRepairPricing?>> = flow {
emit(Status.loading())
val entity = db.collection("emailprice").document("Kalibrieren").get().await().toObject<CalibrateRepairPricing>()
emit(Status.success(entity))
}.catch {
Timber.d("Error on getRepairPrice")
emit(Status.failed(it.message.toString()))
}
}
ViewModel
private lateinit var calibrateRepairPrice: CalibrateRepairPricing
private val _calirateRepairPriceErrorState = MutableLiveData<Status<Unit>>()
val calibrateRepairPriceErrorState: LiveData<Status<Unit>> get() = _calirateRepairPriceErrorState
init {
viewModelScope.launch {
when(_subject.value.toString()) {
"Toast" -> emailRepository.getCalibratePrice().collect {
when(it) {
is Status.Success -> {
calibrateRepairPrice = it.data!!
_calirateRepairPriceErrorState.postValue(Status.success(Unit))
}
is Status.Loading -> _calirateRepairPriceErrorState.postValue(Status.loading())
is Status.Failure -> _calirateRepairPriceErrorState.postValue(Status.failed(it.message))
}
}
else -> emailRepository.getRepairPrice().collect {
when(it) {
is Status.Success -> {
calibrateRepairPrice = it.data!!
_calirateRepairPriceErrorState.postValue(Status.success(Unit))
}
is Status.Loading -> _calirateRepairPriceErrorState.postValue(Status.loading())
is Status.Failure -> _calirateRepairPriceErrorState.postValue(Status.failed(it.message))
}
}
}
price.postValue(calibrateRepairPrice.head!!.basePrice)
}
}
You can now observe the status in one of your fragments (but you dont need to!)
Fragment
viewModel.calibrateRepairPriceErrorState.observe(viewLifecycleOwner) { status ->
when(status) {
is Status.Success -> requireContext().toast("Price successfully loaded")
is Status.Loading -> requireContext().toast("Price is loading")
is Status.Failure -> requireContext().toast("Error, Price could not be loaded")
}
}
This is my toast extensions function:
fun Context.toast(text: String, duration: Int = Toast.LENGTH_SHORT) {
Toast.makeText(this, text, duration).show()
}
Im working on an Android App that is using MVVM with Koin and Retrofit.
Suddenly i keep receiving an error saying
Interface can't be instantiated! Interface name:
kotlinx.coroutines.Deferred
however i cannot find where's the problem.
The current structure is this.
ViewModel.
class ExchangeRateBanksViewModel (private val comercialBanksRepository: ComercialBanksRepository): ViewModel() , KoinComponent{
private val _uiState = MutableLiveData<CredentialsDataState>()
val uiState: LiveData<CredentialsDataState> get() = _uiState
init {
viewModelScope.launch {
runCatching {
emitUiState(showProgress = true)
comercialBanksRepository.getExchangeRateByToday()
}.onSuccess {root ->
val nameMap: MutableList<ExchangeRate> = root.data.map { it }.toMutableList()
emitUiState(result = Event(nameMap))
}.onFailure {
println(it.printStackTrace())
}
}
}
private fun emitUiState(showProgress: Boolean = false, result: Event<List<ExchangeRate>>? = null, error: Event<Int>? = null){
val dataState = CredentialsDataState(showProgress, result, error)
_uiState.value = dataState
}
data class CredentialsDataState(val showProgress: Boolean, val result: Event<List<ExchangeRate>>?, val error: Event<Int>?)
}
the interface
interface ComercialBanksInterface {
#GET("api/commercialBankExchangeRates/date/today")
suspend fun requestExchangeRateToday(): Deferred<RootExchangeRate>
}
the repository.
interface ComercialBanksRepository {
suspend fun getExchangeRateByToday(): RootExchangeRate
}
class ComercialBanksRepositoryImplement(val comercialBanksService: ComercialBanksInterface): ComercialBanksRepository{
override suspend fun getExchangeRateByToday(): RootExchangeRate {
return comercialBanksService.requestExchangeRateToday().await()
}
}
and finally the section where i call my viewModel.
exchangeRateBanksViewModel.uiState.observe(this, Observer {
val dataState = it ?: return#Observer
if (!dataState.showProgress) displayHideSkeleton(hide = true) else displayHideSkeleton(hide = false)
if (dataState.result != null && !dataState.result.consumed){
dataState.result.consume()?.let { result ->
println("RESULT: ${result}")
exchangeRateAdapter.submitList(result)
}
}
if (dataState.error != null && !dataState.error.consumed){
dataState.error.consume()?.let { error ->
Toast.makeText(applicationContext, resources.getString(error), Toast.LENGTH_LONG).show()
}
}
})
I can't find which part the interface has the problem...
Retrofit already handles suspend functions so you don't need to return Deferred. Just return RootExchangeRate and that's it.
Also your repository's function should also be suspendable, and the view model should return a LiveData, where the activity (or fragment) will observe it.