Browse Source

Always return new RetrofitFactory instance. Fixes austinhuang0131/barinsta#1099.

renovate/org.robolectric-robolectric-4.x
Ammar Githam 4 years ago
parent
commit
109d08c59f
  1. 8
      app/src/main/java/awais/instagrabber/activities/MainActivity.java
  2. 21
      app/src/main/java/awais/instagrabber/webservices/RetrofitFactory.java
  3. 15
      app/src/main/java/awais/instagrabber/webservices/interceptors/IgErrorsInterceptor.java

8
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<NavController> 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))

21
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;

15
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;
}
}
Loading…
Cancel
Save