Skip to content

Commit

Permalink
Run onSuccess and onError callbacks on the main instead of the defaul…
Browse files Browse the repository at this point in the history
…t thread.
  • Loading branch information
maxme committed May 9, 2024
1 parent d43fab0 commit e0bf99a
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.google.gson.GsonBuilder
import com.gravatar.GravatarApiService
import com.gravatar.GravatarConstants.GRAVATAR_API_BASE_URL
import com.gravatar.GravatarConstants.GRAVATAR_BASE_URL
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import okhttp3.OkHttpClient
import retrofit2.Retrofit
Expand All @@ -20,8 +21,10 @@ internal class GravatarSdkContainer private constructor() {

private fun getRetrofitBaseBuilder() = Retrofit.Builder().baseUrl(GRAVATAR_BASE_URL)

val dispatcherMain: CoroutineDispatcher = Dispatchers.Main
val dispatcherDefault = Dispatchers.Default
val dispatcherIO = Dispatchers.IO

private val gson = GsonBuilder().setLenient().create()

/**
Expand Down
7 changes: 5 additions & 2 deletions gravatar/src/main/java/com/gravatar/services/AvatarService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public class AvatarService(private val okHttpClient: OkHttpClient? = null) {
const val LOG_TAG = "AvatarService"
}

private val coroutineScope = CoroutineScope(GravatarSdkDI.dispatcherDefault)
// Run onResponse and onError callbacks on the main thread
private val coroutineScope = CoroutineScope(GravatarSdkDI.dispatcherMain)

/**
* Uploads a Gravatar image for the given email address.
Expand Down Expand Up @@ -61,7 +62,9 @@ public class AvatarService(private val okHttpClient: OkHttpClient? = null) {
}

override fun onFailure(call: Call<ResponseBody>, t: Throwable) {
handleError(t, gravatarUploadListener, coroutineScope)
coroutineScope.launch {
gravatarUploadListener.onError(t.error())
}
}
},
)
Expand Down
16 changes: 5 additions & 11 deletions gravatar/src/main/java/com/gravatar/services/ErrorUtils.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
package com.gravatar.services

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import java.net.SocketTimeoutException
import java.net.UnknownHostException

internal fun handleError(t: Throwable, listener: GravatarListener<*, ErrorType>, coroutineScope: CoroutineScope) {
val error: ErrorType =
when (t) {
is SocketTimeoutException -> ErrorType.TIMEOUT
is UnknownHostException -> ErrorType.NETWORK
else -> ErrorType.UNKNOWN
}
coroutineScope.launch {
listener.onError(error)
internal fun Throwable.error(): ErrorType {
return when (this) {
is SocketTimeoutException -> ErrorType.TIMEOUT
is UnknownHostException -> ErrorType.NETWORK
else -> ErrorType.UNKNOWN
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public class ProfileService(private val okHttpClient: OkHttpClient? = null) {
const val LOG_TAG = "ProfileService"
}

private val coroutineScope = CoroutineScope(GravatarSdkDI.dispatcherDefault)
// Run onResponse and onError callbacks on the main thread
private val coroutineScope = CoroutineScope(GravatarSdkDI.dispatcherMain)

private fun fetchWithListener(
hashOrUsername: String,
Expand Down Expand Up @@ -52,7 +53,9 @@ public class ProfileService(private val okHttpClient: OkHttpClient? = null) {
}

override fun onFailure(call: Call<UserProfiles>, t: Throwable) {
handleError(t, getProfileListener, coroutineScope)
coroutineScope.launch {
getProfileListener.onError(t.error())
}
}
},
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ class GravatarSdkContainerRule : TestRule {
gravatarSdkContainerMock = mockk<GravatarSdkContainer>()
gravatarApiServiceMock = mockk<GravatarApiService>(relaxed = true)
mockkObject(GravatarSdkContainer)
every { GravatarSdkContainer.instance } returns gravatarSdkContainerMock
every { gravatarSdkContainerMock.dispatcherMain } returns testDispatcher
every { gravatarSdkContainerMock.dispatcherDefault } returns testDispatcher
every { gravatarSdkContainerMock.dispatcherIO } returns testDispatcher
every { GravatarSdkContainer.instance } returns gravatarSdkContainerMock
every { gravatarSdkContainerMock.getGravatarApiService(any()) } returns gravatarApiServiceMock

base.evaluate()
Expand Down

0 comments on commit e0bf99a

Please sign in to comment.