From a51ba4f2a8d1c4339c0fe339ad2000922786693b Mon Sep 17 00:00:00 2001 From: M M Arif Date: Thu, 31 Dec 2020 15:38:18 +0100 Subject: [PATCH] Fix offline mode (#794) Closes #790 update libs Co-authored-by: M M Arif Reviewed-on: https://codeberg.org/gitnex/GitNex/pulls/794 Reviewed-by: 6543 <6543@noreply.codeberg.org> Co-Authored-By: M M Arif Co-Committed-By: M M Arif --- app/build.gradle | 10 ++--- .../mian/gitnex/clients/RetrofitClient.java | 41 +++++++++---------- 2 files changed, 25 insertions(+), 26 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index b04beb2d..ff5bb97c 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -54,8 +54,8 @@ configurations { } dependencies { - def lifecycle_version = '2.3.0-beta01' - def markwon_version = '4.6.0' + def lifecycle_version = '2.3.0-rc01' + def markwon_version = '4.6.1' def work_version = "2.4.0" def acra = "5.7.0" @@ -76,7 +76,7 @@ dependencies { implementation 'com.squareup.retrofit2:converter-gson:2.9.0' implementation 'com.squareup.retrofit2:converter-scalars:2.9.0' implementation 'com.squareup.okhttp3:logging-interceptor:4.9.0' - implementation 'org.ocpsoft.prettytime:prettytime:4.0.6.Final' + implementation 'org.ocpsoft.prettytime:prettytime:5.0.0.Final' implementation "com.pes.materialcolorpicker:library:1.2.5" implementation "io.noties.markwon:core:$markwon_version" implementation "io.noties.markwon:ext-latex:$markwon_version" @@ -104,8 +104,8 @@ dependencies { implementation "ch.acra:acra-mail:$acra" implementation "ch.acra:acra-limiter:$acra" implementation "ch.acra:acra-notification:$acra" - implementation "androidx.room:room-runtime:2.2.5" - annotationProcessor "androidx.room:room-compiler:2.2.5" + implementation 'androidx.room:room-runtime:2.2.6' + annotationProcessor 'androidx.room:room-compiler:2.2.6' implementation "androidx.work:work-runtime:$work_version" implementation "com.eightbitlab:blurview:1.6.4" implementation "io.mikael:urlbuilder:2.0.9" diff --git a/app/src/main/java/org/mian/gitnex/clients/RetrofitClient.java b/app/src/main/java/org/mian/gitnex/clients/RetrofitClient.java index 9fabe73e..5418ce71 100644 --- a/app/src/main/java/org/mian/gitnex/clients/RetrofitClient.java +++ b/app/src/main/java/org/mian/gitnex/clients/RetrofitClient.java @@ -2,6 +2,7 @@ package org.mian.gitnex.clients; import android.content.Context; import android.util.Log; +import org.mian.gitnex.helpers.AppUtil; import org.mian.gitnex.helpers.FilesData; import org.mian.gitnex.helpers.TinyDB; import org.mian.gitnex.helpers.ssl.MemorizingTrustManager; @@ -35,6 +36,8 @@ public class RetrofitClient { TinyDB tinyDB = TinyDB.getInstance(context); + final boolean connToInternet = AppUtil.hasNetworkConnection(context); + int cacheSize = FilesData.returnOnlyNumber(tinyDB.getString("cacheSizeStr")) * 1024 * 1024; Cache cache = new Cache(new File(context.getCacheDir(), "responses"), cacheSize); @@ -54,13 +57,16 @@ public class RetrofitClient { .hostnameVerifier(memorizingTrustManager.wrapHostnameVerifier(HttpsURLConnection.getDefaultHostnameVerifier())) .addInterceptor(chain -> { - Request request = chain.request() - .newBuilder() - .header("Cache-Control", "public, max-age=" + 60) - .build(); + Request request = chain.request(); + if(connToInternet) { + request = request.newBuilder().header("Cache-Control", "public, max-age=" + 60).build(); + } + else { + + request = request.newBuilder().header("Cache-Control", "public, only-if-cached, max-stale=" + 60 * 60 * 24 * 30).build(); + } return chain.proceed(request); - }); return new Retrofit.Builder() @@ -70,9 +76,10 @@ public class RetrofitClient { .addConverterFactory(GsonConverterFactory.create()) .build(); - } catch(Exception e) { + } + catch(Exception e) { - Log.e("onFailure", e.toString()); + Log.e("onFailureRetrofit", e.toString()); } return null; @@ -93,29 +100,21 @@ public class RetrofitClient { } public static synchronized ApiInterface getApiInterface(Context context, String url) { - if(!apiInterfaces.containsKey(url)) { - ApiInterface apiInterface = createRetrofit(context, url) - .create(ApiInterface.class); + ApiInterface apiInterface = createRetrofit(context, url) + .create(ApiInterface.class); - apiInterfaces.put(url, apiInterface); - return apiInterface; - - } + apiInterfaces.put(url, apiInterface); return apiInterfaces.get(url); } public static synchronized WebInterface getWebInterface(Context context, String url) { - if(!webInterfaces.containsKey(url)) { - WebInterface webInterface = createRetrofit(context, url) - .create(WebInterface.class); + WebInterface webInterface = createRetrofit(context, url) + .create(WebInterface.class); - webInterfaces.put(url, webInterface); - return webInterface; - - } + webInterfaces.put(url, webInterface); return webInterfaces.get(url); }