Skip to content

Commit

Permalink
chore(fix): update network client retry strategies and content wrappe…
Browse files Browse the repository at this point in the history
…r defaults
  • Loading branch information
wax911 committed May 5, 2024
1 parent c97b018 commit ae2a320
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ private fun ContentText(
textAlign = TextAlign.Center,
style = MaterialTheme.typography.bodyLarge,
overflow = TextOverflow.Ellipsis,
color = MaterialTheme.colorScheme.onSurface,
maxLines = 3,
modifier = modifier,
)
Expand All @@ -82,13 +83,14 @@ private fun LoadingContent(
ContentImage(drawableResource = config.loadingDrawable)
Spacer(modifier = Modifier.height(8.dp))
Row(
horizontalArrangement = Arrangement.spacedBy(16.dp),
horizontalArrangement = Arrangement.spacedBy(8.dp),
verticalAlignment = Alignment.CenterVertically,
) {
CircularProgressIndicator(
modifier = Modifier.size(24.dp).align(alignment = Alignment.CenterVertically),
modifier = Modifier.size(16.dp)
.align(alignment = Alignment.CenterVertically),
color = MaterialTheme.colorScheme.surfaceVariant,
strokeWidth = 4.dp,
strokeWidth = 2.dp,
trackColor = MaterialTheme.colorScheme.secondary,
)
config.loadingMessage?.also {
Expand Down Expand Up @@ -174,7 +176,8 @@ fun <P: IParam> ContentWrapper(
}


@AniTrendPreview.Mobile
@AniTrendPreview.Light
@AniTrendPreview.Dark
@Composable
private fun ContentWrapperPreview(
@PreviewParameter(provider = ContentWrapperPreviewParameter::class) loadState: LoadState
Expand Down
15 changes: 12 additions & 3 deletions app-core/src/main/kotlin/co/anitrend/core/koin/Modules.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ import co.anitrend.core.coil.client.CoilRequestClient
import co.anitrend.core.coil.fetch.RequestImageFetcher
import co.anitrend.core.coil.mapper.RequestImageMapper
import co.anitrend.data.android.koin.dataModules
import co.anitrend.data.android.network.cache.CacheHelper
import co.anitrend.data.android.network.model.NetworkMessage
import co.anitrend.data.core.extensions.defaultBuilder
import coil.ImageLoader
import coil.decode.GifDecoder
import coil.decode.ImageDecoderDecoder
Expand All @@ -45,6 +47,7 @@ import coil.disk.DiskCache
import coil.memory.MemoryCache
import io.wax911.emojify.EmojiManager
import io.wax911.emojify.serializer.kotlinx.KotlinxDeserializer
import okhttp3.CookieJar
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import org.koin.android.ext.koin.androidContext
Expand Down Expand Up @@ -113,9 +116,15 @@ private val configurationModule = module {
val memoryLimit = if (isLowRamDevice) 0.15 else 0.35
val storageController = get<IStorageController>()

val client = get<OkHttpClient.Builder> {
parametersOf(HttpLoggingInterceptor.Level.BASIC)
}.build()
val client = defaultBuilder()
.cookieJar(get<CookieJar>())
.cache(
CacheHelper.createCache(
androidContext(),
"coil-okhttp"
)
)
.build()

val imageCache = storageController.getImageCache(
context = androidContext()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import co.anitrend.arch.request.callback.RequestCallback
import co.anitrend.data.android.network.model.NetworkMessage
import retrofit2.HttpException
import java.net.SocketTimeoutException
import java.net.UnknownHostException

/**
* Contract for controller strategy
Expand Down Expand Up @@ -49,6 +50,13 @@ abstract class ControllerStrategy<D> {
cause
)
}
is UnknownHostException -> {
RequestError(
networkMessage.connectivityErrorTittle,
networkMessage.connectivityErrorMessage,
cause
)
}
else -> {
if (cause == null)
RequestError(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class OfflineStrategy<D> private constructor(
): D? {
runCatching {
block()
}.onSuccess {result ->
}.onSuccess { result ->
callback.recordSuccess()
return result
}.onFailure { exception ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ abstract class DeferrableNetworkClient<T> : AbstractNetworkClient<Deferred<Respo
*/
override fun defaultShouldRetry(exception: Throwable) = when (exception) {
is HttpException -> exception.code() == 429
is SocketTimeoutException,
is IOException -> true
is SocketTimeoutException -> true
else -> false
}

Expand Down Expand Up @@ -120,4 +119,4 @@ abstract class DeferrableNetworkClient<T> : AbstractNetworkClient<Deferred<Respo
maxAttempts,
shouldRetry
).bodyOrThrow()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ abstract class OkHttpCallNetworkClient : AbstractNetworkClient<Call, Response>()
*/
override fun defaultShouldRetry(exception: Throwable) = when (exception) {
is OkHttpException -> exception.code == 429
is SocketTimeoutException,
is IOException -> true
is SocketTimeoutException -> true
else -> false
}

Expand Down Expand Up @@ -118,4 +117,4 @@ abstract class OkHttpCallNetworkClient : AbstractNetworkClient<Call, Response>()
maxAttempts,
shouldRetry
).bodyOrThrow()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ internal abstract class RetrofitCallNetworkClient<T> : AbstractNetworkClient<Cal
*/
override fun defaultShouldRetry(exception: Throwable) = when (exception) {
is HttpException -> exception.code() == 429
is SocketTimeoutException,
is IOException -> true
is SocketTimeoutException -> true
else -> false
}

Expand Down Expand Up @@ -112,4 +111,4 @@ internal abstract class RetrofitCallNetworkClient<T> : AbstractNetworkClient<Cal
maxAttempts,
shouldRetry
).bodyOrThrow()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ internal abstract class AuthLocalSource : AbstractLocalSource<AuthEntity>() {
delete from auth
where user_id = :userId
""")
abstract fun clearByUserId(userId: Long)
abstract suspend fun clearByUserId(userId: Long)

@Query("""
select * from auth
Expand All @@ -51,4 +51,4 @@ internal abstract class AuthLocalSource : AbstractLocalSource<AuthEntity>() {
select user_id from auth
""")
abstract suspend fun userIds(): List<Long>
}
}

0 comments on commit ae2a320

Please sign in to comment.