[Sample App] Additional threading fix for network callbacks
@@ -7,6 +7,8 @@ import android.arch.lifecycle.MutableLiveData
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.net.Uri
+import android.os.Handler
+import android.os.Looper
import com.airbnb.lottie.LottieComposition
import com.airbnb.lottie.samples.model.CompositionArgs
import io.reactivex.Observable
@@ -28,6 +30,8 @@ class CompositionData(var composition: LottieComposition? = null) {
class PlayerViewModel(application: Application) : AndroidViewModel(application) {
+ private val handler = Handler(Looper.getMainLooper())
+
val composition = MutableLiveData<CompositionData>()
val error = MutableLiveData<Throwable>()
@@ -56,25 +60,28 @@ class PlayerViewModel(application: Application) : AndroidViewModel(application)
getApplication<LottieApplication>().okHttpClient
.newCall(request)
?.enqueue(OkHttpCallback(
- onFailure = { _, e -> error.value = e },
+ onFailure = { _, e -> onFailure(e) },
onResponse = { _, response ->
if (!response.isSuccessful) {
- error.value = IllegalStateException("Response was unsuccessful.")
+ onFailure(IllegalStateException("Response was unsuccessful."))
} else {
if (response.body()?.contentType() == MediaType.parse("application/zip")) {
handleZipResponse(response.body()!!)
} else {
val string = response.body()?.string()
if (string == null) {
- error.value = IllegalStateException("Response body was null")
+ onFailure(IllegalStateException("Response body was null"))
return@OkHttpCallback
}
handleJsonResponse(string)
}
-
-
}
- }))
+ }
+ ))
+ }
+
+ private fun onFailure(e: Exception) {
+ handler.post { error.value = e }
}
private fun handleJsonResponse(jsonString: String) {
