From ae54b83ec7f330596b7eb0d7077838508d6053a8 Mon Sep 17 00:00:00 2001 From: Matthieu <24-artectrex@users.noreply.shinice.net> Date: Mon, 5 Jun 2023 21:38:20 +0200 Subject: [PATCH] Small improvement to error showing --- .../pixeldroid/app/posts/feeds/CommonFeedFragmentUtils.kt | 6 ++++++ .../feeds/cachedFeeds/postFeeds/HomeFeedRemoteMediator.kt | 2 +- .../feeds/cachedFeeds/postFeeds/PublicFeedRemoteMediator.kt | 2 +- .../main/java/org/pixeldroid/app/utils/api/PixelfedAPI.kt | 6 +++++- 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/org/pixeldroid/app/posts/feeds/CommonFeedFragmentUtils.kt b/app/src/main/java/org/pixeldroid/app/posts/feeds/CommonFeedFragmentUtils.kt index b7b0d436..f6158085 100644 --- a/app/src/main/java/org/pixeldroid/app/posts/feeds/CommonFeedFragmentUtils.kt +++ b/app/src/main/java/org/pixeldroid/app/posts/feeds/CommonFeedFragmentUtils.kt @@ -13,6 +13,7 @@ import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.RecyclerView import androidx.swiperefreshlayout.widget.SwipeRefreshLayout import com.google.gson.Gson +import kotlinx.coroutines.CancellationException import kotlinx.coroutines.Job import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.launch @@ -80,6 +81,11 @@ internal fun initAdapter( ?: loadState.append as? LoadState.Error ?: loadState.prepend as? LoadState.Error ?: loadState.refresh as? LoadState.Error + + if(errorState?.error is CancellationException){ + return@addLoadStateListener + } + errorState?.let { val error: String = (it.error as? HttpException)?.response()?.errorBody()?.string()?.ifEmpty { null }?.let { s -> try { diff --git a/app/src/main/java/org/pixeldroid/app/posts/feeds/cachedFeeds/postFeeds/HomeFeedRemoteMediator.kt b/app/src/main/java/org/pixeldroid/app/posts/feeds/cachedFeeds/postFeeds/HomeFeedRemoteMediator.kt index 41e2f29f..c0b974ba 100644 --- a/app/src/main/java/org/pixeldroid/app/posts/feeds/cachedFeeds/postFeeds/HomeFeedRemoteMediator.kt +++ b/app/src/main/java/org/pixeldroid/app/posts/feeds/cachedFeeds/postFeeds/HomeFeedRemoteMediator.kt @@ -47,7 +47,7 @@ class HomeFeedRemoteMediator @Inject constructor( HomeStatusDatabaseEntity(user.user_id, user.instance_uri, it) } - val endOfPaginationReached = apiResponse.isEmpty() + val endOfPaginationReached = apiResponse.isEmpty() || maxId == apiResponse.sortedBy { it.created_at }.last().id db.withTransaction { // Clear table in the database diff --git a/app/src/main/java/org/pixeldroid/app/posts/feeds/cachedFeeds/postFeeds/PublicFeedRemoteMediator.kt b/app/src/main/java/org/pixeldroid/app/posts/feeds/cachedFeeds/postFeeds/PublicFeedRemoteMediator.kt index fd5f19e9..9651ff0e 100644 --- a/app/src/main/java/org/pixeldroid/app/posts/feeds/cachedFeeds/postFeeds/PublicFeedRemoteMediator.kt +++ b/app/src/main/java/org/pixeldroid/app/posts/feeds/cachedFeeds/postFeeds/PublicFeedRemoteMediator.kt @@ -62,7 +62,7 @@ class PublicFeedRemoteMediator @Inject constructor( val dbObjects = apiResponse.map{ PublicFeedStatusDatabaseEntity(user.user_id, user.instance_uri, it) } - val endOfPaginationReached = apiResponse.isEmpty() + val endOfPaginationReached = apiResponse.isEmpty() || maxId == apiResponse.sortedBy { it.created_at }.last().id db.withTransaction { // Clear table in the database diff --git a/app/src/main/java/org/pixeldroid/app/utils/api/PixelfedAPI.kt b/app/src/main/java/org/pixeldroid/app/utils/api/PixelfedAPI.kt index 6750dfef..7598f0d3 100644 --- a/app/src/main/java/org/pixeldroid/app/utils/api/PixelfedAPI.kt +++ b/app/src/main/java/org/pixeldroid/app/utils/api/PixelfedAPI.kt @@ -23,6 +23,7 @@ import retrofit2.converter.gson.GsonConverterFactory import retrofit2.http.* import retrofit2.http.Field import java.time.Instant +import java.util.concurrent.TimeUnit /* @@ -51,7 +52,9 @@ interface PixelfedAPI { .client( OkHttpClient().newBuilder().addNetworkInterceptor(headerInterceptor) // Only do secure-ish TLS connections (no HTTP or very old SSL/TLS) - .connectionSpecs(listOf(ConnectionSpec.MODERN_TLS)).build() + .connectionSpecs(listOf(ConnectionSpec.MODERN_TLS)) + .readTimeout(20, TimeUnit.SECONDS) + .build() ) .build().create(PixelfedAPI::class.java) } @@ -74,6 +77,7 @@ interface PixelfedAPI { OkHttpClient().newBuilder().addNetworkInterceptor(headerInterceptor) // Only do secure-ish TLS connections (no HTTP or very old SSL/TLS) .connectionSpecs(listOf(ConnectionSpec.MODERN_TLS)) + .readTimeout(20, TimeUnit.SECONDS) .authenticator(TokenAuthenticator(user, db, pixelfedAPIHolder)) .addInterceptor { it.request().newBuilder().run {