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. 20
      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

20
app/src/main/java/awais/instagrabber/activities/MainActivity.java

@ -99,6 +99,8 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage
R.id.locationFragment R.id.locationFragment
); );
private static MainActivity instance;
private ActivityMainBinding binding; private ActivityMainBinding binding;
private LiveData<NavController> currentNavControllerLiveData; private LiveData<NavController> currentNavControllerLiveData;
private MenuItem searchMenuItem; private MenuItem searchMenuItem;
@ -125,10 +127,14 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage
} }
}; };
public static MainActivity getInstance() {
return instance;
}
@Override @Override
protected void onCreate(@Nullable final Bundle savedInstanceState) { protected void onCreate(@Nullable final Bundle savedInstanceState) {
RetrofitFactory.setup(this);
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
instance = this;
binding = ActivityMainBinding.inflate(getLayoutInflater()); binding = ActivityMainBinding.inflate(getLayoutInflater());
setupCookie(); setupCookie();
if (settingsHelper.getBoolean(Constants.FLAG_SECURE)) if (settingsHelper.getBoolean(Constants.FLAG_SECURE))
@ -429,12 +435,12 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage
private List<Tab> setupAnonBottomNav() { private List<Tab> setupAnonBottomNav() {
final int selectedItemId = binding.bottomNavView.getSelectedItemId(); final int selectedItemId = binding.bottomNavView.getSelectedItemId();
final Tab favoriteTab = new Tab(R.drawable.ic_star_24, 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, final Tab profileTab = new Tab(R.drawable.ic_person_24,
getString(R.string.profile), getString(R.string.profile),
false, false,

21
app/src/main/java/awais/instagrabber/webservices/RetrofitFactory.java

@ -1,7 +1,5 @@
package awais.instagrabber.webservices; package awais.instagrabber.webservices;
import androidx.annotation.NonNull;
import com.google.gson.FieldNamingPolicy; import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
@ -9,7 +7,6 @@ import com.google.gson.GsonBuilder;
import java.io.File; import java.io.File;
import awais.instagrabber.BuildConfig; import awais.instagrabber.BuildConfig;
import awais.instagrabber.activities.MainActivity;
import awais.instagrabber.repositories.responses.Caption; import awais.instagrabber.repositories.responses.Caption;
import awais.instagrabber.utils.Utils; import awais.instagrabber.utils.Utils;
import awais.instagrabber.webservices.interceptors.AddCookiesInterceptor; 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 final Cache cache = new Cache(new File(Utils.cacheDir), cacheSize);
private IgErrorsInterceptor igErrorsInterceptor; private IgErrorsInterceptor igErrorsInterceptor;
private MainActivity mainActivity;
private Retrofit.Builder builder; private Retrofit.Builder builder;
private Retrofit retrofit; private Retrofit retrofit;
private Retrofit retrofitWeb; private Retrofit retrofitWeb;
public static void setup(@NonNull final MainActivity mainActivity) {
public static RetrofitFactory getInstance() {
if (instance == null) { if (instance == null) {
synchronized (LOCK) { synchronized (LOCK) {
if (instance == null) { 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; return instance;
} }
private RetrofitFactory(@NonNull final MainActivity mainActivity) {
this.mainActivity = mainActivity;
}
private Retrofit.Builder getRetrofitBuilder() { private Retrofit.Builder getRetrofitBuilder() {
if (builder == null) { if (builder == null) {
igErrorsInterceptor = new IgErrorsInterceptor(mainActivity);
igErrorsInterceptor = new IgErrorsInterceptor();
final OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder() final OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder()
.followRedirects(false) .followRedirects(false)
.followSslRedirects(false) .followSslRedirects(false)
@ -103,7 +89,6 @@ public final class RetrofitFactory {
igErrorsInterceptor.destroy(); igErrorsInterceptor.destroy();
} }
igErrorsInterceptor = null; igErrorsInterceptor = null;
mainActivity = null;
retrofit = null; retrofit = null;
retrofitWeb = null; retrofitWeb = null;
builder = 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 { public class IgErrorsInterceptor implements Interceptor {
private static final String TAG = IgErrorsInterceptor.class.getSimpleName(); private static final String TAG = IgErrorsInterceptor.class.getSimpleName();
private MainActivity mainActivity;
public IgErrorsInterceptor(@NonNull final MainActivity mainActivity) {
this.mainActivity = mainActivity;
}
public IgErrorsInterceptor() { }
@NonNull @NonNull
@Override @Override
@ -107,6 +103,8 @@ public class IgErrorsInterceptor implements Interceptor {
} }
private void showSnackbar(final String message) { private void showSnackbar(final String message) {
final MainActivity mainActivity = MainActivity.getInstance();
if (mainActivity == null) return;
final View view = mainActivity.getRootView(); final View view = mainActivity.getRootView();
if (view == null) return; if (view == null) return;
Snackbar.make(view, message, Snackbar.LENGTH_LONG).show(); Snackbar.make(view, message, Snackbar.LENGTH_LONG).show();
@ -118,7 +116,10 @@ public class IgErrorsInterceptor implements Interceptor {
} }
private void showErrorDialog(@StringRes final int messageResId) { private void showErrorDialog(@StringRes final int messageResId) {
final MainActivity mainActivity = MainActivity.getInstance();
if (mainActivity == null) return; if (mainActivity == null) return;
final FragmentManager fragmentManager = mainActivity.getSupportFragmentManager();
if (fragmentManager.isStateSaved()) return;
if (messageResId == 0) return; if (messageResId == 0) return;
final ConfirmDialogFragment dialogFragment = ConfirmDialogFragment.newInstance( final ConfirmDialogFragment dialogFragment = ConfirmDialogFragment.newInstance(
Constants.GLOBAL_NETWORK_ERROR_DIALOG_REQUEST_CODE, Constants.GLOBAL_NETWORK_ERROR_DIALOG_REQUEST_CODE,
@ -128,12 +129,10 @@ public class IgErrorsInterceptor implements Interceptor {
0, 0,
0 0
); );
final FragmentManager fragmentManager = mainActivity.getSupportFragmentManager();
if (fragmentManager.isStateSaved()) return;
dialogFragment.show(fragmentManager, "network_error_dialog"); dialogFragment.show(fragmentManager, "network_error_dialog");
} }
public void destroy() { public void destroy() {
mainActivity = null;
// mainActivity = null;
} }
} }
Loading…
Cancel
Save