skydoves / DisneyMotions

🦁 A Disney app using transformation motions based on MVVM (ViewModel, Coroutines, Flow, Room, Repository, Koin) architecture.
Apache License 2.0
1.51k stars 189 forks source link

Type mismatch required LiveData<TypeVariable(T)> found Unit #4

Closed abdulmalekDery closed 4 years ago

abdulmalekDery commented 4 years ago

I'm trying to understanding this architecture by coding example and I followed the project set up till I stuck with that compile time error

Type mismatch required LiveData<TypeVariable(T)> found Unit

in the viewModel I have code like that for register request

 fun postRegisterRequest(request: RegisterRequest) {
    this.registerLiveData =  launchOnViewModelScope {
            this.authRepository.postRegister ({ s -> this.toastLiveData.postValue(s) },
                request
            )
        }
}

in the repository I have the following code

suspend fun postRegister(error: (String) -> Unit, request: RegisterRequest) = withContext(Dispatchers.IO) {
    val liveData = MutableLiveData<ResponseWrapper<RegisterResponse>>()
    isLoading = true
    networkClient.postRegister( { response ->
        isLoading = false
        when (response) {
            is ApiResponse.Success -> {
                response.data.whatIfNotNull {
                    liveData.postValue(it)
                }
            }
            is ApiResponse.Failure.Error -> error(response.message())
            is ApiResponse.Failure.Exception -> error(response.message())
        }
    },request)
}

the network client request look like that

 fun postRegister(
    onResult: (response: ApiResponse<ResponseWrapper<RegisterResponse>>) -> Unit,
    request: RegisterRequest
) {
    this.authService.register(request).transform(onResult)
}

the authService code

  @POST(Urls.REGISTER)
fun register(@Body request: RegisterRequest): Call<ResponseWrapper<RegisterResponse>>

what I did wrong and how to make postRegister in the repository return LiveData<TypeVariable(T)> instead of Unit

skydoves commented 4 years ago

@abdulmalekDery Hi, you can return the liveData in the coroutines context.

suspend fun postRegister(error: (String) -> Unit, request: RegisterRequest) = withContext(Dispatchers.IO) {
    val liveData = MutableLiveData<ResponseWrapper<RegisterResponse>>()
    // skip stubs
    liveData // return liveData
}
abdulmalekDery commented 4 years ago

thanks I will try that