Skip to content

Commit e0bf99a

Browse files
committed
Run onSuccess and onError callbacks on the main instead of the default thread.
1 parent d43fab0 commit e0bf99a

5 files changed

Lines changed: 21 additions & 16 deletions

File tree

gravatar/src/main/java/com/gravatar/di/container/GravatarSdkContainer.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import com.google.gson.GsonBuilder
44
import com.gravatar.GravatarApiService
55
import com.gravatar.GravatarConstants.GRAVATAR_API_BASE_URL
66
import com.gravatar.GravatarConstants.GRAVATAR_BASE_URL
7+
import kotlinx.coroutines.CoroutineDispatcher
78
import kotlinx.coroutines.Dispatchers
89
import okhttp3.OkHttpClient
910
import retrofit2.Retrofit
@@ -20,8 +21,10 @@ internal class GravatarSdkContainer private constructor() {
2021

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

24+
val dispatcherMain: CoroutineDispatcher = Dispatchers.Main
2325
val dispatcherDefault = Dispatchers.Default
2426
val dispatcherIO = Dispatchers.IO
27+
2528
private val gson = GsonBuilder().setLenient().create()
2629

2730
/**

gravatar/src/main/java/com/gravatar/services/AvatarService.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ public class AvatarService(private val okHttpClient: OkHttpClient? = null) {
2222
const val LOG_TAG = "AvatarService"
2323
}
2424

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

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

6364
override fun onFailure(call: Call<ResponseBody>, t: Throwable) {
64-
handleError(t, gravatarUploadListener, coroutineScope)
65+
coroutineScope.launch {
66+
gravatarUploadListener.onError(t.error())
67+
}
6568
}
6669
},
6770
)
Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
package com.gravatar.services
22

3-
import kotlinx.coroutines.CoroutineScope
4-
import kotlinx.coroutines.launch
53
import java.net.SocketTimeoutException
64
import java.net.UnknownHostException
75

8-
internal fun handleError(t: Throwable, listener: GravatarListener<*, ErrorType>, coroutineScope: CoroutineScope) {
9-
val error: ErrorType =
10-
when (t) {
11-
is SocketTimeoutException -> ErrorType.TIMEOUT
12-
is UnknownHostException -> ErrorType.NETWORK
13-
else -> ErrorType.UNKNOWN
14-
}
15-
coroutineScope.launch {
16-
listener.onError(error)
6+
internal fun Throwable.error(): ErrorType {
7+
return when (this) {
8+
is SocketTimeoutException -> ErrorType.TIMEOUT
9+
is UnknownHostException -> ErrorType.NETWORK
10+
else -> ErrorType.UNKNOWN
1711
}
1812
}

gravatar/src/main/java/com/gravatar/services/ProfileService.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ public class ProfileService(private val okHttpClient: OkHttpClient? = null) {
2222
const val LOG_TAG = "ProfileService"
2323
}
2424

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

2728
private fun fetchWithListener(
2829
hashOrUsername: String,
@@ -52,7 +53,9 @@ public class ProfileService(private val okHttpClient: OkHttpClient? = null) {
5253
}
5354

5455
override fun onFailure(call: Call<UserProfiles>, t: Throwable) {
55-
handleError(t, getProfileListener, coroutineScope)
56+
coroutineScope.launch {
57+
getProfileListener.onError(t.error())
58+
}
5659
}
5760
},
5861
)

gravatar/src/test/java/com/gravatar/GravatarSdkContainerRule.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ class GravatarSdkContainerRule : TestRule {
2323
gravatarSdkContainerMock = mockk<GravatarSdkContainer>()
2424
gravatarApiServiceMock = mockk<GravatarApiService>(relaxed = true)
2525
mockkObject(GravatarSdkContainer)
26-
every { GravatarSdkContainer.instance } returns gravatarSdkContainerMock
26+
every { gravatarSdkContainerMock.dispatcherMain } returns testDispatcher
2727
every { gravatarSdkContainerMock.dispatcherDefault } returns testDispatcher
28+
every { gravatarSdkContainerMock.dispatcherIO } returns testDispatcher
29+
every { GravatarSdkContainer.instance } returns gravatarSdkContainerMock
2830
every { gravatarSdkContainerMock.getGravatarApiService(any()) } returns gravatarApiServiceMock
2931

3032
base.evaluate()

0 commit comments

Comments
 (0)