I have this rating button on my fragment that observes the post request on my viewmodel. The thing is, I would like to tell the user whether the post request was successful or not through a toast but as I have it now, I can only post the request and see if it was successful by the logs. How can I do that?
This is the button:
private fun ratingButton() {
binding.btnRating.setOnClickListener {
arguments?.getInt("Id")?.let {
arguments?.getString("Session Id").let { it1 ->
if (it1 != null) {
viewModel.postRating(it, mapOf("value" to binding.ratingBar.rating), it1)
}
}
}
}
}
This is the view model:
class RatingViewModel constructor(
private val remoteDataSource: MovieRemoteDataSource
): ViewModel() {
val ratingSuccess = MutableLiveData<Boolean>()
val ratingFailedMessage = MutableLiveData<String?>()
private var _rating = MutableLiveData<Resource<RatingResponse>>()
val rating: LiveData<Resource<RatingResponse>>
get() = _rating
fun postRating(rating:Int, id:Map<String,Float>, session_id:String){
remoteDataSource.postRating(rating, id, session_id, object: MovieRemoteDataSource.RatingCallBack<RatingResponse>{
override fun onSuccess(value: Resource<RatingResponse>){
ratingSuccess.postValue(true)
_rating.value = value
}
override fun onError(message:String?){
ratingSuccess.postValue(false)
ratingFailedMessage.postValue(message)
}
})
}
}
This is the remote data source:
interface RatingCallBack<T> {
fun onSuccess(value: Resource<T>)
fun onError(message: String?)
}
fun postRating(rating: Int, id:Map<String,Float>, session_id:String, ratingCallback: RatingCallBack<RatingResponse>) {
val service = RetrofitService.instance
.create(MovieService::class.java)
val call = service.postRating(rating, session_id, id)
call.enqueue(object : Callback<RatingResponse?> {
override fun onResponse(
call: Call<RatingResponse?>,
response: Response<RatingResponse?>
) {
if (response.isSuccessful) {
Log.d("d", "d")
} else {
Log.d("d", "d")
}
}
override fun onFailure(call: Call<RatingResponse?>, t: Throwable) {
Log.d("d", "d")
}
})
}
I added the resource because I think it might be helpful but it doesn't seem to be working:
data class Resource<out T> (
val status: NetworkStatus,
val data: T? = null,
val message: String? = null
)
enum class NetworkStatus{
LOADING,
SUCCESS,
ERROR
}