1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-12-22 21:16:57 +00:00

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

This commit is contained in:
Ammar Githam 2021-05-03 17:10:20 +09:00
parent ac3f8b9bfd
commit 109d08c59f
3 changed files with 23 additions and 33 deletions

View File

@ -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))
@ -429,12 +435,12 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage
private List<Tab> 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,

View File

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

View File

@ -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;
}
}