Browse Source

More null checks. Probably resolves austinhuang0131/barinsta#992

renovate/org.robolectric-robolectric-4.x
Ammar Githam 4 years ago
parent
commit
9dae40fae0
  1. 55
      app/src/main/java/awais/instagrabber/fragments/settings/MorePreferencesFragment.java

55
app/src/main/java/awais/instagrabber/fragments/settings/MorePreferencesFragment.java

@ -60,7 +60,6 @@ public class MorePreferencesFragment extends BasePreferencesFragment {
final MainActivity activity = (MainActivity) getActivity(); final MainActivity activity = (MainActivity) getActivity();
// screen.addPreference(new MoreHeaderPreference(getContext())); // screen.addPreference(new MoreHeaderPreference(getContext()));
final Context context = getContext(); final Context context = getContext();
final Resources resources = context.getResources();
if (context == null) return; if (context == null) return;
accountRepository = AccountRepository.getInstance(AccountDataSource.getInstance(context)); accountRepository = AccountRepository.getInstance(AccountDataSource.getInstance(context));
final PreferenceCategory accountCategory = new PreferenceCategory(context); final PreferenceCategory accountCategory = new PreferenceCategory(context);
@ -77,9 +76,7 @@ public class MorePreferencesFragment extends BasePreferencesFragment {
// shouldRecreate(); // shouldRecreate();
Toast.makeText(context1, R.string.logout_success, Toast.LENGTH_SHORT).show(); Toast.makeText(context1, R.string.logout_success, Toast.LENGTH_SHORT).show();
settingsHelper.putString(Constants.COOKIE, ""); settingsHelper.putString(Constants.COOKIE, "");
AppExecutors.getInstance().mainThread().execute(() -> {
ProcessPhoenix.triggerRebirth(context1);
}, 200);
AppExecutors.getInstance().mainThread().execute(() -> ProcessPhoenix.triggerRebirth(context1), 200);
return true; return true;
})); }));
} }
@ -88,27 +85,35 @@ public class MorePreferencesFragment extends BasePreferencesFragment {
public void onSuccess(@NonNull final List<Account> accounts) { public void onSuccess(@NonNull final List<Account> accounts) {
if (!isLoggedIn) { if (!isLoggedIn) {
if (accounts.size() > 0) { if (accounts.size() > 0) {
final AccountSwitcherPreference preference = getAccountSwitcherPreference(null, context);
final Context context1 = getContext();
final AccountSwitcherPreference preference = getAccountSwitcherPreference(null, context1);
if (preference == null) return; if (preference == null) return;
accountCategory.addPreference(preference); accountCategory.addPreference(preference);
} }
// Need to show something to trigger login activity // Need to show something to trigger login activity
final Preference preference1 = getPreference(R.string.add_account, R.drawable.ic_add, preference -> { final Preference preference1 = getPreference(R.string.add_account, R.drawable.ic_add, preference -> {
startActivityForResult(new Intent(getContext(), Login.class), Constants.LOGIN_RESULT_CODE);
final Context context1 = getContext();
if (context1 == null) return false;
startActivityForResult(new Intent(context1, Login.class), Constants.LOGIN_RESULT_CODE);
return true; return true;
}); });
if (preference1 == null) return; if (preference1 == null) return;
accountCategory.addPreference(preference1); accountCategory.addPreference(preference1);
} }
if (accounts.size() > 0) { if (accounts.size() > 0) {
accountCategory
.addPreference(getPreference(R.string.remove_all_acc, null, R.drawable.ic_account_multiple_remove_24, preference -> {
final Preference preference1 = getPreference(
R.string.remove_all_acc,
null,
R.drawable.ic_account_multiple_remove_24,
preference -> {
if (getContext() == null) return false; if (getContext() == null) return false;
new AlertDialog.Builder(getContext()) new AlertDialog.Builder(getContext())
.setTitle(R.string.logout) .setTitle(R.string.logout)
.setMessage(R.string.remove_all_acc_warning) .setMessage(R.string.remove_all_acc_warning)
.setPositiveButton(R.string.yes, (dialog, which) -> { .setPositiveButton(R.string.yes, (dialog, which) -> {
CookieUtils.removeAllAccounts(context, new RepositoryCallback<Void>() {
final Context context1 = getContext();
if (context1 == null) return;
CookieUtils.removeAllAccounts(context1, new RepositoryCallback<Void>() {
@Override @Override
public void onSuccess(final Void result) { public void onSuccess(final Void result) {
// shouldRecreate(); // shouldRecreate();
@ -116,9 +121,8 @@ public class MorePreferencesFragment extends BasePreferencesFragment {
if (context1 == null) return; if (context1 == null) return;
Toast.makeText(context1, R.string.logout_success, Toast.LENGTH_SHORT).show(); Toast.makeText(context1, R.string.logout_success, Toast.LENGTH_SHORT).show();
settingsHelper.putString(Constants.COOKIE, ""); settingsHelper.putString(Constants.COOKIE, "");
AppExecutors.getInstance().mainThread().execute(() -> {
ProcessPhoenix.triggerRebirth(context1);
}, 200);
AppExecutors.getInstance().mainThread()
.execute(() -> ProcessPhoenix.triggerRebirth(context1), 200);
} }
@Override @Override
@ -128,7 +132,9 @@ public class MorePreferencesFragment extends BasePreferencesFragment {
.setNegativeButton(R.string.cancel, null) .setNegativeButton(R.string.cancel, null)
.show(); .show();
return true; return true;
}));
});
if (preference1 == null) return;
accountCategory.addPreference(preference1);
} }
} }
@ -236,12 +242,14 @@ public class MorePreferencesFragment extends BasePreferencesFragment {
-1, -1,
preference -> { preference -> {
if (BuildConfig.isPre) return true; if (BuildConfig.isPre) return true;
if (activity == null) return false;
FlavorTown.updateCheck(activity, true); FlavorTown.updateCheck(activity, true);
return true; return true;
})); }));
screen.addPreference(getDivider(context)); screen.addPreference(getDivider(context));
final Preference reminderPreference = getPreference(R.string.reminder, R.string.reminder_summary, R.drawable.ic_warning, null); final Preference reminderPreference = getPreference(R.string.reminder, R.string.reminder_summary, R.drawable.ic_warning, null);
if (reminderPreference == null) return;
reminderPreference.setSelectable(false); reminderPreference.setSelectable(false);
screen.addPreference(reminderPreference); screen.addPreference(reminderPreference);
} }
@ -304,6 +312,7 @@ public class MorePreferencesFragment extends BasePreferencesFragment {
} }
} }
@Nullable
private AccountSwitcherPreference getAccountSwitcherPreference(final String cookie, final Context context) { private AccountSwitcherPreference getAccountSwitcherPreference(final String cookie, final Context context) {
if (context == null) return null; if (context == null) return null;
return new AccountSwitcherPreference(context, cookie, accountRepository, v -> showAccountSwitcherDialog()); return new AccountSwitcherPreference(context, cookie, accountRepository, v -> showAccountSwitcherDialog());
@ -318,12 +327,14 @@ public class MorePreferencesFragment extends BasePreferencesFragment {
dialogFragment.show(fragmentManager, "accountSwitcher"); dialogFragment.show(fragmentManager, "accountSwitcher");
} }
@Nullable
private Preference getPreference(final int title, private Preference getPreference(final int title,
final int icon, final int icon,
final Preference.OnPreferenceClickListener clickListener) { final Preference.OnPreferenceClickListener clickListener) {
return getPreference(title, -1, icon, clickListener); return getPreference(title, -1, icon, clickListener);
} }
@Nullable
private Preference getPreference(final int title, private Preference getPreference(final int title,
final int summary, final int summary,
final int icon, final int icon,
@ -339,6 +350,7 @@ public class MorePreferencesFragment extends BasePreferencesFragment {
return getPreference(title, string, icon, clickListener); return getPreference(title, string, icon, clickListener);
} }
@Nullable
private Preference getPreference(final int title, private Preference getPreference(final int title,
final String summary, final String summary,
final int icon, final int icon,
@ -356,15 +368,14 @@ public class MorePreferencesFragment extends BasePreferencesFragment {
return preference; return preference;
} }
public static class MoreHeaderPreference extends Preference {
public MoreHeaderPreference(final Context context) {
super(context);
setLayoutResource(R.layout.pref_more_header);
setSelectable(false);
}
}
// public static class MoreHeaderPreference extends Preference {
//
// public MoreHeaderPreference(final Context context) {
// super(context);
// setLayoutResource(R.layout.pref_more_header);
// setSelectable(false);
// }
// }
public static class AccountSwitcherPreference extends Preference { public static class AccountSwitcherPreference extends Preference {

Loading…
Cancel
Save