From 109d08c59f49bf22812b0b89a40c4d5cde8bc444 Mon Sep 17 00:00:00 2001 From: Ammar Githam Date: Mon, 3 May 2021 17:10:20 +0900 Subject: [PATCH] Always return new RetrofitFactory instance. Fixes austinhuang0131/barinsta#1099. --- .../instagrabber/activities/MainActivity.java | 20 +++++++++++------- .../webservices/RetrofitFactory.java | 21 +++---------------- .../interceptors/IgErrorsInterceptor.java | 15 +++++++------ 3 files changed, 23 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/activities/MainActivity.java b/app/src/main/java/awais/instagrabber/activities/MainActivity.java index ad153f80..deeeb232 100644 --- a/app/src/main/java/awais/instagrabber/activities/MainActivity.java +++ b/app/src/main/java/awais/instagrabber/activities/MainActivity.java @@ -99,6 +99,8 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage R.id.locationFragment ); + private static MainActivity instance; + private ActivityMainBinding binding; private LiveData currentNavControllerLiveData; private MenuItem searchMenuItem; @@ -125,10 +127,14 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage } }; + public static MainActivity getInstance() { + return instance; + } + @Override protected void onCreate(@Nullable final Bundle savedInstanceState) { - RetrofitFactory.setup(this); super.onCreate(savedInstanceState); + instance = this; binding = ActivityMainBinding.inflate(getLayoutInflater()); setupCookie(); if (settingsHelper.getBoolean(Constants.FLAG_SECURE)) @@ -429,12 +435,12 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage private List setupAnonBottomNav() { final int selectedItemId = binding.bottomNavView.getSelectedItemId(); final Tab favoriteTab = new Tab(R.drawable.ic_star_24, - getString(R.string.title_favorites), - false, - "favorites_nav_graph", - R.navigation.favorites_nav_graph, - R.id.favorites_nav_graph, - R.id.favoritesFragment); + getString(R.string.title_favorites), + false, + "favorites_nav_graph", + R.navigation.favorites_nav_graph, + R.id.favorites_nav_graph, + R.id.favoritesFragment); final Tab profileTab = new Tab(R.drawable.ic_person_24, getString(R.string.profile), false, diff --git a/app/src/main/java/awais/instagrabber/webservices/RetrofitFactory.java b/app/src/main/java/awais/instagrabber/webservices/RetrofitFactory.java index 5062e5c7..981f205e 100644 --- a/app/src/main/java/awais/instagrabber/webservices/RetrofitFactory.java +++ b/app/src/main/java/awais/instagrabber/webservices/RetrofitFactory.java @@ -1,7 +1,5 @@ package awais.instagrabber.webservices; -import androidx.annotation.NonNull; - import com.google.gson.FieldNamingPolicy; import com.google.gson.Gson; import com.google.gson.GsonBuilder; @@ -9,7 +7,6 @@ import com.google.gson.GsonBuilder; import java.io.File; import awais.instagrabber.BuildConfig; -import awais.instagrabber.activities.MainActivity; import awais.instagrabber.repositories.responses.Caption; import awais.instagrabber.utils.Utils; import awais.instagrabber.webservices.interceptors.AddCookiesInterceptor; @@ -29,35 +26,24 @@ public final class RetrofitFactory { private final Cache cache = new Cache(new File(Utils.cacheDir), cacheSize); private IgErrorsInterceptor igErrorsInterceptor; - private MainActivity mainActivity; private Retrofit.Builder builder; private Retrofit retrofit; private Retrofit retrofitWeb; - public static void setup(@NonNull final MainActivity mainActivity) { + public static RetrofitFactory getInstance() { if (instance == null) { synchronized (LOCK) { if (instance == null) { - instance = new RetrofitFactory(mainActivity); + instance = new RetrofitFactory(); } } } - } - - public static RetrofitFactory getInstance() { - if (instance == null) { - throw new RuntimeException("Setup not done!"); - } return instance; } - private RetrofitFactory(@NonNull final MainActivity mainActivity) { - this.mainActivity = mainActivity; - } - private Retrofit.Builder getRetrofitBuilder() { if (builder == null) { - igErrorsInterceptor = new IgErrorsInterceptor(mainActivity); + igErrorsInterceptor = new IgErrorsInterceptor(); final OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder() .followRedirects(false) .followSslRedirects(false) @@ -103,7 +89,6 @@ public final class RetrofitFactory { igErrorsInterceptor.destroy(); } igErrorsInterceptor = null; - mainActivity = null; retrofit = null; retrofitWeb = null; builder = null; diff --git a/app/src/main/java/awais/instagrabber/webservices/interceptors/IgErrorsInterceptor.java b/app/src/main/java/awais/instagrabber/webservices/interceptors/IgErrorsInterceptor.java index 01cec822..99659b46 100644 --- a/app/src/main/java/awais/instagrabber/webservices/interceptors/IgErrorsInterceptor.java +++ b/app/src/main/java/awais/instagrabber/webservices/interceptors/IgErrorsInterceptor.java @@ -26,11 +26,7 @@ import okhttp3.ResponseBody; public class IgErrorsInterceptor implements Interceptor { private static final String TAG = IgErrorsInterceptor.class.getSimpleName(); - private MainActivity mainActivity; - - public IgErrorsInterceptor(@NonNull final MainActivity mainActivity) { - this.mainActivity = mainActivity; - } + public IgErrorsInterceptor() { } @NonNull @Override @@ -107,6 +103,8 @@ public class IgErrorsInterceptor implements Interceptor { } private void showSnackbar(final String message) { + final MainActivity mainActivity = MainActivity.getInstance(); + if (mainActivity == null) return; final View view = mainActivity.getRootView(); if (view == null) return; Snackbar.make(view, message, Snackbar.LENGTH_LONG).show(); @@ -118,7 +116,10 @@ public class IgErrorsInterceptor implements Interceptor { } private void showErrorDialog(@StringRes final int messageResId) { + final MainActivity mainActivity = MainActivity.getInstance(); if (mainActivity == null) return; + final FragmentManager fragmentManager = mainActivity.getSupportFragmentManager(); + if (fragmentManager.isStateSaved()) return; if (messageResId == 0) return; final ConfirmDialogFragment dialogFragment = ConfirmDialogFragment.newInstance( Constants.GLOBAL_NETWORK_ERROR_DIALOG_REQUEST_CODE, @@ -128,12 +129,10 @@ public class IgErrorsInterceptor implements Interceptor { 0, 0 ); - final FragmentManager fragmentManager = mainActivity.getSupportFragmentManager(); - if (fragmentManager.isStateSaved()) return; dialogFragment.show(fragmentManager, "network_error_dialog"); } public void destroy() { - mainActivity = null; + // mainActivity = null; } } \ No newline at end of file