Somesh
10/31/2019, 6:15 AMRepository
and `ViewModel`'s livedata in-case of @GET
request and observe them in fragment.
I don't have this problem when the request type is @POST
because I can use Transformation.switchMap
on body and whenever the body changes repository's function gets invoked and emit value to the response live data something like this
val matchSetsDetail: LiveData<Resource<MatchDetailBean>> = Transformations.switchMap(matchIdLiveData) { matchId ->
val body = MatchSetRequest(matchId)
repository.getMatchSet(body)
}
but in case of @GET
request I have several query parameter the my View supplies
I have this retrofit API call in repository class and the code looks like this
fun checkInCheckOutUser(apiKey: String, userId: Int, status: String, latitude: Double, longitude: Double, checkedOn: Long): LiveData<Resource<BaseResponse>> = liveData {
emit(Resource.Loading())
try {
val response: Response<BaseResponse> = ApiClient.coachApi.checkInCheckOutUser(apiKey, userId, status, latitude, longitude, checkedOn)
if (response.isSuccessful && response.body() != null) {
if (response.body()!!.isValidKey && response.body()!!.success) {
emit(Resource.Success(response.body()!!))
} else {
emit(Resource.Failure(response.body()!!.message))
}
} else {
emit(Resource.Failure())
}
} catch (e: Exception) {
emit(Resource.Failure())
}
}
and ViewModel
class CheckInMapViewModel : ViewModel() {
val checkInResponse: LiveData<Resource<BaseResponse>> = MutableLiveData()
fun checkInCheckOut(apiKey: String, userId: Int, status: String, latitude: Double, longitude: Double, checkedOn: Long): LiveData<Resource<BaseResponse>> {
return repository.checkInCheckOutUser(apiKey,userId,status,latitude,longitude,checkedOn)
}
}
Main problem is I want to observe checkInResponse
but don't know how to pass repository's livedata same as I did with my post request above using Transformations.switchMap
. Can anyone help me with this case?pavi2410
10/31/2019, 7:54 AMclass CheckInMapViewModel : ViewModel() {
val _checkInResponse = MutableLiveData()
val checkInResponse: LiveData<Resource<BaseResponse>
get() = _checkInResponse
fun checkInCheckOut(apiKey: String, userId: Int, status: String, latitude: Double, longitude: Double, checkedOn: Long) {
_checkInResponse.value = repository.checkInCheckOutUser(apiKey,userId,status,latitude,longitude,checkedOn)
}
}
and in Activity
checkInMapViewModel.checkInResponse(this) { /* observe */}
checkInMapViewModel.checkInCheckOut(...) // Refreshes livedata
Somesh
10/31/2019, 8:45 AMResource<BaseResponse>
. I simply can not do _checkInResponse.value = repository.checkInCheckOutUser(apiKey,userId,status,latitude,longitude,checkedOn)
because of this.kristianconk
10/31/2019, 4:49 PMpavi2410
10/31/2019, 4:51 PM_checkInResponse.value = repository.checkInCheckOutUser(...).value
or
_checkInResponse = repository.checkInCheckOutUser(...)
livedata.value = anotherLivedata.value
or
livedata = anotherLivedata
Bacho Kurtanidze
11/01/2019, 7:00 AM