diff options
Diffstat (limited to 'app/src')
37 files changed, 501 insertions, 1305 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/AccountPostsActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/AccountPostsActivity.java index 2f82cf36..ac328bba 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/AccountPostsActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/AccountPostsActivity.java @@ -3,7 +3,6 @@ package ml.docilealligator.infinityforreddit.activities; import android.content.SharedPreferences; import android.os.Build; import android.os.Bundle; -import android.os.Handler; import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; @@ -37,7 +36,6 @@ import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.SortType; import ml.docilealligator.infinityforreddit.SortTypeSelectionCallback; -import ml.docilealligator.infinityforreddit.asynctasks.GetCurrentAccount; import ml.docilealligator.infinityforreddit.bottomsheetfragments.PostLayoutBottomSheetFragment; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.events.ChangeNSFWEvent; @@ -52,9 +50,6 @@ public class AccountPostsActivity extends BaseActivity implements SortTypeSelect static final String EXTRA_USER_WHERE = "EUW"; private static final String IS_IN_LAZY_MODE_STATE = "IILMS"; - private static final String NULL_ACCESS_TOKEN_STATE = "NATS"; - private static final String ACCESS_TOKEN_STATE = "ATS"; - private static final String ACCOUNT_NAME_STATE = "ANS"; private static final String FRAGMENT_OUT_STATE = "FOS"; @BindView(R.id.coordinator_layout_account_posts_activity) @@ -74,11 +69,13 @@ public class AccountPostsActivity extends BaseActivity implements SortTypeSelect @Named("post_layout") SharedPreferences mPostLayoutSharedPreferences; @Inject + @Named("current_account") + SharedPreferences mCurrentAccountSharedPreferences; + @Inject CustomThemeWrapper mCustomThemeWrapper; @Inject Executor mExecutor; private boolean isInLazyMode = false; - private boolean mNullAccessToken = false; private String mAccessToken; private String mAccountName; private String mUserWhere; @@ -143,20 +140,16 @@ public class AccountPostsActivity extends BaseActivity implements SortTypeSelect postLayoutBottomSheetFragment = new PostLayoutBottomSheetFragment(); + mAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null); + mAccountName = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_NAME, null); + if (savedInstanceState != null) { - mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE); - mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE); - mAccountName = savedInstanceState.getString(ACCOUNT_NAME_STATE); isInLazyMode = savedInstanceState.getBoolean(IS_IN_LAZY_MODE_STATE); - if (!mNullAccessToken && mAccessToken == null) { - getCurrentAccountAndInitializeFragment(); - } else { - mFragment = getSupportFragmentManager().getFragment(savedInstanceState, FRAGMENT_OUT_STATE); - getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_account_posts_activity, mFragment).commit(); - } + mFragment = getSupportFragmentManager().getFragment(savedInstanceState, FRAGMENT_OUT_STATE); + getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_account_posts_activity, mFragment).commit(); } else { - getCurrentAccountAndInitializeFragment(); + initializeFragment(); } } @@ -185,18 +178,6 @@ public class AccountPostsActivity extends BaseActivity implements SortTypeSelect applyAppBarLayoutAndToolbarTheme(appBarLayout, toolbar); } - private void getCurrentAccountAndInitializeFragment() { - GetCurrentAccount.getCurrentAccount(mExecutor, new Handler(), mRedditDataRoomDatabase, account -> { - if (account == null) { - mNullAccessToken = true; - } else { - mAccessToken = account.getAccessToken(); - mAccountName = account.getAccountName(); - } - initializeFragment(); - }); - } - private void initializeFragment() { mFragment = new PostFragment(); Bundle bundle = new Bundle(); @@ -271,9 +252,6 @@ public class AccountPostsActivity extends BaseActivity implements SortTypeSelect super.onSaveInstanceState(outState); getSupportFragmentManager().putFragment(outState, FRAGMENT_OUT_STATE, mFragment); outState.putBoolean(IS_IN_LAZY_MODE_STATE, isInLazyMode); - outState.putString(ACCESS_TOKEN_STATE, mAccessToken); - outState.putString(ACCOUNT_NAME_STATE, mAccountName); - outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken); } @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/AccountSavedThingActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/AccountSavedThingActivity.java index d59ecc65..02fe124f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/AccountSavedThingActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/AccountSavedThingActivity.java @@ -3,7 +3,6 @@ package ml.docilealligator.infinityforreddit.activities; import android.content.SharedPreferences; import android.os.Build; import android.os.Bundle; -import android.os.Handler; import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; @@ -44,7 +43,6 @@ import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.MarkPostAsReadInterface; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; -import ml.docilealligator.infinityforreddit.asynctasks.GetCurrentAccount; import ml.docilealligator.infinityforreddit.bottomsheetfragments.PostLayoutBottomSheetFragment; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.events.ChangeNSFWEvent; @@ -60,9 +58,6 @@ import retrofit2.Retrofit; public class AccountSavedThingActivity extends BaseActivity implements ActivityToolbarInterface, PostLayoutBottomSheetFragment.PostLayoutSelectionCallback, MarkPostAsReadInterface { - private static final String NULL_ACCESS_TOKEN_STATE = "NATS"; - private static final String ACCESS_TOKEN_STATE = "ATS"; - private static final String ACCOUNT_NAME_STATE = "ANS"; private static final String IS_IN_LAZY_MODE_STATE = "IILMS"; @BindView(R.id.coordinator_layout_account_saved_thing_activity) @@ -89,6 +84,9 @@ public class AccountSavedThingActivity extends BaseActivity implements ActivityT @Named("post_layout") SharedPreferences mPostLayoutSharedPreferences; @Inject + @Named("current_account") + SharedPreferences mCurrentAccountSharedPreferences; + @Inject CustomThemeWrapper mCustomThemeWrapper; @Inject Executor mExecutor; @@ -97,7 +95,6 @@ public class AccountSavedThingActivity extends BaseActivity implements ActivityT private SlidrInterface mSlidrInterface; private Menu mMenu; private AppBarLayout.LayoutParams params; - private boolean mNullAccessToken = false; private String mAccessToken; private String mAccountName; private boolean isInLazyMode = false; @@ -150,19 +147,13 @@ public class AccountSavedThingActivity extends BaseActivity implements ActivityT fragmentManager = getSupportFragmentManager(); + mAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null); + mAccountName = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_NAME, null); + if (savedInstanceState != null) { - mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE); - mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE); - mAccountName = savedInstanceState.getString(ACCOUNT_NAME_STATE); isInLazyMode = savedInstanceState.getBoolean(IS_IN_LAZY_MODE_STATE); - if (!mNullAccessToken && mAccessToken == null) { - getCurrentAccountAndInitializeViewPager(); - } else { - initializeViewPager(); - } - } else { - getCurrentAccountAndInitializeViewPager(); } + initializeViewPager(); } @Override @@ -191,18 +182,6 @@ public class AccountSavedThingActivity extends BaseActivity implements ActivityT applyTabLayoutTheme(tabLayout); } - private void getCurrentAccountAndInitializeViewPager() { - GetCurrentAccount.getCurrentAccount(mExecutor, new Handler(), mRedditDataRoomDatabase, account -> { - if (account == null) { - mNullAccessToken = true; - } else { - mAccessToken = account.getAccessToken(); - mAccountName = account.getAccountName(); - } - initializeViewPager(); - }); - } - private void initializeViewPager() { sectionsPagerAdapter = new SectionsPagerAdapter(this); viewPager2.setAdapter(sectionsPagerAdapter); @@ -295,9 +274,6 @@ public class AccountSavedThingActivity extends BaseActivity implements ActivityT protected void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); outState.putBoolean(IS_IN_LAZY_MODE_STATE, isInLazyMode); - outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken); - outState.putString(ACCESS_TOKEN_STATE, mAccessToken); - outState.putString(ACCOUNT_NAME_STATE, mAccountName); } @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CommentActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CommentActivity.java index e33ca99c..f8f4c4e0 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CommentActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CommentActivity.java @@ -6,7 +6,6 @@ import android.content.SharedPreferences; import android.net.Uri; import android.os.Build; import android.os.Bundle; -import android.os.Handler; import android.text.Spanned; import android.text.style.SuperscriptSpan; import android.text.util.Linkify; @@ -50,16 +49,15 @@ import io.noties.markwon.recycler.MarkwonAdapter; import io.noties.markwon.recycler.table.TableEntry; import io.noties.markwon.recycler.table.TableEntryPlugin; import io.noties.markwon.simple.ext.SimpleExtPlugin; +import ml.docilealligator.infinityforreddit.Infinity; +import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.adapters.MarkdownBottomBarRecyclerViewAdapter; -import ml.docilealligator.infinityforreddit.asynctasks.GetCurrentAccount; import ml.docilealligator.infinityforreddit.bottomsheetfragments.CopyTextBottomSheetFragment; import ml.docilealligator.infinityforreddit.comment.Comment; import ml.docilealligator.infinityforreddit.comment.SendComment; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.events.SwitchAccountEvent; -import ml.docilealligator.infinityforreddit.Infinity; -import ml.docilealligator.infinityforreddit.R; -import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; +import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; import ml.docilealligator.infinityforreddit.utils.Utils; import retrofit2.Retrofit; @@ -76,9 +74,6 @@ public class CommentActivity extends BaseActivity { public static final String RETURN_EXTRA_COMMENT_DATA_KEY = "RECDK"; public static final int WRITE_COMMENT_REQUEST_CODE = 1; - private static final String NULL_ACCESS_TOKEN_STATE = "NATS"; - private static final String ACCESS_TOKEN_STATE = "ATS"; - @BindView(R.id.coordinator_layout_comment_activity) CoordinatorLayout coordinatorLayout; @BindView(R.id.appbar_layout_comment_activity) @@ -99,15 +94,15 @@ public class CommentActivity extends BaseActivity { @Named("oauth") Retrofit mOauthRetrofit; @Inject - RedditDataRoomDatabase mRedditDataRoomDatabase; - @Inject @Named("default") SharedPreferences mSharedPreferences; @Inject + @Named("current_account") + SharedPreferences mCurrentAccountSharedPreferences; + @Inject CustomThemeWrapper mCustomThemeWrapper; @Inject Executor mExecutor; - private boolean mNullAccessToken = false; private String mAccessToken; private String parentFullname; private int parentDepth; @@ -136,15 +131,7 @@ public class CommentActivity extends BaseActivity { addOnOffsetChangedListener(appBarLayout); } - if (savedInstanceState == null) { - getCurrentAccount(); - } else { - mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE); - mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE); - if (!mNullAccessToken && mAccessToken == null) { - getCurrentAccount(); - } - } + mAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null); Intent intent = getIntent(); String parentTextMarkdown = intent.getStringExtra(EXTRA_COMMENT_PARENT_TEXT_MARKDOWN_KEY); @@ -296,16 +283,6 @@ public class CommentActivity extends BaseActivity { markdownColor = secondaryTextColor; } - private void getCurrentAccount() { - GetCurrentAccount.getCurrentAccount(mExecutor, new Handler(), mRedditDataRoomDatabase, account -> { - if (account == null) { - mNullAccessToken = true; - } else { - mAccessToken = account.getAccessToken(); - } - }); - } - @Override protected void onPause() { super.onPause(); @@ -398,13 +375,6 @@ public class CommentActivity extends BaseActivity { } @Override - protected void onSaveInstanceState(@NonNull Bundle outState) { - super.onSaveInstanceState(outState); - outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken); - outState.putString(ACCESS_TOKEN_STATE, mAccessToken); - } - - @Override public void onBackPressed() { if (isSubmitting) { promptAlertDialog(R.string.exit_when_submit, R.string.exit_when_edit_comment_detail); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CreateMultiRedditActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CreateMultiRedditActivity.java index 39a56416..8898c55b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CreateMultiRedditActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CreateMultiRedditActivity.java @@ -4,7 +4,6 @@ import android.content.Intent; import android.content.SharedPreferences; import android.os.Build; import android.os.Bundle; -import android.os.Handler; import android.view.Menu; import android.view.MenuItem; import android.view.View; @@ -34,7 +33,6 @@ import butterknife.ButterKnife; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; -import ml.docilealligator.infinityforreddit.asynctasks.GetCurrentAccount; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.multireddit.CreateMultiReddit; import ml.docilealligator.infinityforreddit.multireddit.MultiRedditJSONModel; @@ -44,9 +42,6 @@ import retrofit2.Retrofit; public class CreateMultiRedditActivity extends BaseActivity { private static final int SUBREDDIT_SELECTION_REQUEST_CODE = 1; - private static final String NULL_ACCESS_TOKEN_STATE = "NATS"; - private static final String ACCESS_TOKEN_STATE = "ATS"; - private static final String ACCOUNT_NAME_STATE = "ANS"; private static final String SELECTED_SUBREDDITS_STATE = "SSS"; @BindView(R.id.coordinator_layout_create_multi_reddit_activity) CoordinatorLayout coordinatorLayout; @@ -79,10 +74,12 @@ public class CreateMultiRedditActivity extends BaseActivity { @Named("default") SharedPreferences mSharedPreferences; @Inject + @Named("current_account") + SharedPreferences mCurrentAccountSharedPreferences; + @Inject CustomThemeWrapper mCustomThemeWrapper; @Inject Executor mExecutor; - private boolean mNullAccessToken = false; private String mAccessToken; private String mAccountName; private ArrayList<String> mSubreddits; @@ -111,35 +108,21 @@ public class CreateMultiRedditActivity extends BaseActivity { setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); + mAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null); + mAccountName = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_NAME, null); + + if (mAccessToken == null) { + Toast.makeText(this, R.string.logged_out, Toast.LENGTH_SHORT).show(); + finish(); + return; + } + if (savedInstanceState != null) { - mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE); - mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE); - mAccountName = savedInstanceState.getString(ACCOUNT_NAME_STATE); mSubreddits = savedInstanceState.getStringArrayList(SELECTED_SUBREDDITS_STATE); - - if (!mNullAccessToken && mAccountName == null) { - getCurrentAccountAndBindView(); - } else { - bindView(); - } } else { mSubreddits = new ArrayList<>(); - getCurrentAccountAndBindView(); } - } - - private void getCurrentAccountAndBindView() { - GetCurrentAccount.getCurrentAccount(mExecutor, new Handler(), mRedditDataRoomDatabase, account -> { - if (account == null) { - mNullAccessToken = true; - Toast.makeText(this, R.string.logged_out, Toast.LENGTH_SHORT).show(); - finish(); - } else { - mAccessToken = account.getAccessToken(); - mAccountName = account.getAccountName(); - bindView(); - } - }); + bindView(); } private void bindView() { @@ -210,9 +193,6 @@ public class CreateMultiRedditActivity extends BaseActivity { @Override protected void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); - outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken); - outState.putString(ACCESS_TOKEN_STATE, mAccessToken); - outState.putString(ACCOUNT_NAME_STATE, mAccountName); outState.putStringArrayList(SELECTED_SUBREDDITS_STATE, mSubreddits); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/EditMultiRedditActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/EditMultiRedditActivity.java index e7bb81ee..2ffcbd20 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/EditMultiRedditActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/EditMultiRedditActivity.java @@ -5,7 +5,6 @@ import android.content.SharedPreferences; import android.content.res.ColorStateList; import android.os.Build; import android.os.Bundle; -import android.os.Handler; import android.view.Menu; import android.view.MenuItem; import android.view.View; @@ -35,7 +34,6 @@ import butterknife.ButterKnife; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; -import ml.docilealligator.infinityforreddit.asynctasks.GetCurrentAccount; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.multireddit.EditMultiReddit; import ml.docilealligator.infinityforreddit.multireddit.FetchMultiRedditInfo; @@ -47,9 +45,6 @@ import retrofit2.Retrofit; public class EditMultiRedditActivity extends BaseActivity { public static final String EXTRA_MULTI_PATH = "EMP"; private static final int SUBREDDIT_SELECTION_REQUEST_CODE = 1; - private static final String NULL_ACCESS_TOKEN_STATE = "NATS"; - private static final String ACCESS_TOKEN_STATE = "ATS"; - private static final String ACCOUNT_NAME_STATE = "ANS"; private static final String MULTI_REDDIT_STATE = "MRS"; private static final String MULTI_PATH_STATE = "MPS"; @BindView(R.id.coordinator_layout_edit_multi_reddit_activity) @@ -87,10 +82,12 @@ public class EditMultiRedditActivity extends BaseActivity { @Named("default") SharedPreferences mSharedPreferences; @Inject + @Named("current_account") + SharedPreferences mCurrentAccountSharedPreferences; + @Inject CustomThemeWrapper mCustomThemeWrapper; @Inject Executor mExecutor; - private boolean mNullAccessToken = false; private String mAccessToken; private String mAccountName; private MultiReddit multiReddit; @@ -120,36 +117,22 @@ public class EditMultiRedditActivity extends BaseActivity { setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); + mAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null); + mAccountName = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_NAME, null); + + if (mAccessToken == null) { + Toast.makeText(this, R.string.logged_out, Toast.LENGTH_SHORT).show(); + finish(); + } + if (savedInstanceState != null) { - mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE); - mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE); - mAccountName = savedInstanceState.getString(ACCOUNT_NAME_STATE); multiReddit = savedInstanceState.getParcelable(MULTI_REDDIT_STATE); multipath = savedInstanceState.getString(MULTI_PATH_STATE); - - if (!mNullAccessToken && mAccountName == null) { - getCurrentAccountAndBindView(); - } else { - bindView(); - } } else { multipath = getIntent().getStringExtra(EXTRA_MULTI_PATH); - getCurrentAccountAndBindView(); } - } - private void getCurrentAccountAndBindView() { - GetCurrentAccount.getCurrentAccount(mExecutor, new Handler(), mRedditDataRoomDatabase, account -> { - if (account == null) { - mNullAccessToken = true; - Toast.makeText(this, R.string.logged_out, Toast.LENGTH_SHORT).show(); - finish(); - } else { - mAccessToken = account.getAccessToken(); - mAccountName = account.getAccountName(); - bindView(); - } - }); + bindView(); } private void bindView() { @@ -242,9 +225,6 @@ public class EditMultiRedditActivity extends BaseActivity { @Override protected void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); - outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken); - outState.putString(ACCESS_TOKEN_STATE, mAccessToken); - outState.putString(ACCOUNT_NAME_STATE, mAccountName); outState.putParcelable(MULTI_REDDIT_STATE, multiReddit); outState.putString(MULTI_PATH_STATE, multipath); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/FilteredPostsActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/FilteredPostsActivity.java index 9441dd35..73217aad 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/FilteredPostsActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/FilteredPostsActivity.java @@ -4,7 +4,6 @@ import android.content.Intent; import android.content.SharedPreferences; import android.os.Build; import android.os.Bundle; -import android.os.Handler; import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; @@ -36,12 +35,11 @@ import ml.docilealligator.infinityforreddit.ActivityToolbarInterface; import ml.docilealligator.infinityforreddit.FragmentCommunicator; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.MarkPostAsReadInterface; -import ml.docilealligator.infinityforreddit.RecyclerViewContentScrollingInterface; import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.RecyclerViewContentScrollingInterface; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.SortType; import ml.docilealligator.infinityforreddit.SortTypeSelectionCallback; -import ml.docilealligator.infinityforreddit.asynctasks.GetCurrentAccount; import ml.docilealligator.infinityforreddit.bottomsheetfragments.FilteredThingFABMoreOptionsBottomSheetFragment; import ml.docilealligator.infinityforreddit.bottomsheetfragments.PostLayoutBottomSheetFragment; import ml.docilealligator.infinityforreddit.bottomsheetfragments.SearchPostSortTypeBottomSheetFragment; @@ -69,9 +67,6 @@ public class FilteredPostsActivity extends BaseActivity implements SortTypeSelec public static final String EXTRA_USER_WHERE = "EUW"; private static final String IS_IN_LAZY_MODE_STATE = "IILMS"; - private static final String NULL_ACCESS_TOKEN_STATE = "NATS"; - private static final String ACCESS_TOKEN_STATE = "ATS"; - private static final String ACCOUNT_NAME_STATE = "ANS"; private static final String FRAGMENT_OUT_STATE = "FOS"; private static final int CUSTOMIZE_POST_FILTER_ACTIVITY_REQUEST_CODE = 1000; @@ -94,11 +89,13 @@ public class FilteredPostsActivity extends BaseActivity implements SortTypeSelec @Named("post_layout") SharedPreferences mPostLayoutSharedPreferences; @Inject + @Named("current_account") + SharedPreferences mCurrentAccountSharedPreferences; + @Inject CustomThemeWrapper mCustomThemeWrapper; @Inject Executor mExecutor; private boolean isInLazyMode = false; - private boolean mNullAccessToken = false; private String mAccessToken; private String mAccountName; private String name; @@ -223,21 +220,16 @@ public class FilteredPostsActivity extends BaseActivity implements SortTypeSelec } } + mAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null); + mAccountName = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_NAME, null); + if (savedInstanceState != null) { isInLazyMode = savedInstanceState.getBoolean(IS_IN_LAZY_MODE_STATE); - mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE); - mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE); - mAccountName = savedInstanceState.getString(ACCOUNT_NAME_STATE); - - if (!mNullAccessToken && mAccessToken == null) { - getCurrentAccountAndBindView(postFilter); - } else { - mFragment = (PostFragment) getSupportFragmentManager().getFragment(savedInstanceState, FRAGMENT_OUT_STATE); - getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_filtered_posts_activity, mFragment).commit(); - bindView(postFilter, false); - } + mFragment = (PostFragment) getSupportFragmentManager().getFragment(savedInstanceState, FRAGMENT_OUT_STATE); + getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_filtered_posts_activity, mFragment).commit(); + bindView(postFilter, false); } else { - getCurrentAccountAndBindView(postFilter); + bindView(postFilter, true); } } @@ -267,18 +259,6 @@ public class FilteredPostsActivity extends BaseActivity implements SortTypeSelec applyFABTheme(fab); } - private void getCurrentAccountAndBindView(PostFilter postFilter) { - GetCurrentAccount.getCurrentAccount(mExecutor, new Handler(), mRedditDataRoomDatabase, account -> { - if (account == null) { - mNullAccessToken = true; - } else { - mAccessToken = account.getAccessToken(); - mAccountName = account.getAccountName(); - } - bindView(postFilter, true); - }); - } - private void bindView(PostFilter postFilter, boolean initializeFragment) { switch (postType) { case PostDataSource.TYPE_FRONT_PAGE: @@ -467,9 +447,6 @@ public class FilteredPostsActivity extends BaseActivity implements SortTypeSelec super.onSaveInstanceState(outState); getSupportFragmentManager().putFragment(outState, FRAGMENT_OUT_STATE, mFragment); outState.putBoolean(IS_IN_LAZY_MODE_STATE, isInLazyMode); - outState.putString(ACCESS_TOKEN_STATE, mAccessToken); - outState.putString(ACCOUNT_NAME_STATE, mAccountName); - outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken); } @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/GiveAwardActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/GiveAwardActivity.java index 12f9c174..a5b27c2d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/GiveAwardActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/GiveAwardActivity.java @@ -7,7 +7,6 @@ import android.content.Intent; import android.content.SharedPreferences; import android.os.Build; import android.os.Bundle; -import android.os.Handler; import android.view.LayoutInflater; import android.view.MenuItem; import android.view.View; @@ -37,7 +36,6 @@ import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.adapters.AwardRecyclerViewAdapter; -import ml.docilealligator.infinityforreddit.asynctasks.GetCurrentAccount; import ml.docilealligator.infinityforreddit.award.GiveAward; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; @@ -50,8 +48,6 @@ public class GiveAwardActivity extends BaseActivity { public static final String EXTRA_RETURN_ITEM_POSITION = "ERIP"; public static final String EXTRA_RETURN_NEW_AWARDS = "ERNA"; public static final String EXTRA_RETURN_NEW_AWARDS_COUNT = "ERNAC"; - private static final String NULL_ACCESS_TOKEN_STATE = "NATS"; - private static final String ACCESS_TOKEN_STATE = "ATS"; @BindView(R.id.coordinator_layout_give_award_activity) CoordinatorLayout coordinatorLayout; @@ -70,12 +66,14 @@ public class GiveAwardActivity extends BaseActivity { @Named("default") SharedPreferences mSharedPreferences; @Inject + @Named("current_account") + SharedPreferences mCurrentAccountSharedPreferences; + @Inject CustomThemeWrapper mCustomThemeWrapper; @Inject Executor mExecutor; private String thingFullname; private int itemPosition; - private boolean mNullAccessToken = false; private String mAccessToken; private AwardRecyclerViewAdapter adapter; @@ -108,29 +106,9 @@ public class GiveAwardActivity extends BaseActivity { thingFullname = getIntent().getStringExtra(EXTRA_THING_FULLNAME); itemPosition = getIntent().getIntExtra(EXTRA_ITEM_POSITION, 0); - if (savedInstanceState != null) { - mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE); - mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE); + mAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null); - if (!mNullAccessToken && mAccessToken == null) { - getCurrentAccountAndBindView(); - } else { - bindView(); - } - } else { - getCurrentAccountAndBindView(); - } - } - - private void getCurrentAccountAndBindView() { - GetCurrentAccount.getCurrentAccount(mExecutor, new Handler(), mRedditDataRoomDatabase, account -> { - if (account == null) { - mNullAccessToken = true; - } else { - mAccessToken = account.getAccessToken(); - } - bindView(); - }); + bindView(); } private void bindView() { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/InboxActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/InboxActivity.java index 951e92bc..78fdc00f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/InboxActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/InboxActivity.java @@ -49,7 +49,6 @@ import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RecyclerViewContentScrollingInterface; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.apis.RedditAPI; -import ml.docilealligator.infinityforreddit.asynctasks.GetCurrentAccount; import ml.docilealligator.infinityforreddit.asynctasks.SwitchAccount; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.events.SwitchAccountEvent; @@ -67,8 +66,6 @@ public class InboxActivity extends BaseActivity implements ActivityToolbarInterf public static final String EXTRA_NEW_ACCOUNT_NAME = "ENAN"; public static final String EXTRA_VIEW_MESSAGE = "EVM"; - private static final String NULL_ACCESS_TOKEN_STATE = "NATS"; - private static final String ACCESS_TOKEN_STATE = "ATS"; private static final String NEW_ACCOUNT_NAME_STATE = "NANS"; private static final int SEARCH_USER_REQUEST_CODE = 1; @@ -103,8 +100,8 @@ public class InboxActivity extends BaseActivity implements ActivityToolbarInterf Executor mExecutor; private SlidrInterface mSlidrInterface; private SectionsPagerAdapter sectionsPagerAdapter; - private boolean mNullAccessToken = false; private String mAccessToken; + private String mAccountName; private String mNewAccountName; @Override @@ -155,20 +152,15 @@ public class InboxActivity extends BaseActivity implements ActivityToolbarInterf setSupportActionBar(mToolbar); setToolbarGoToTop(mToolbar); + mAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null); + mAccountName = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_NAME, null); + if (savedInstanceState != null) { - mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE); - mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE); mNewAccountName = savedInstanceState.getString(NEW_ACCOUNT_NAME_STATE); - - if (!mNullAccessToken && mAccessToken == null) { - getCurrentAccountAndFetchMessage(savedInstanceState); - } else { - bindView(savedInstanceState); - } } else { mNewAccountName = getIntent().getStringExtra(EXTRA_NEW_ACCOUNT_NAME); - getCurrentAccountAndFetchMessage(savedInstanceState); } + getCurrentAccountAndFetchMessage(savedInstanceState); viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){ @Override @@ -235,37 +227,26 @@ public class InboxActivity extends BaseActivity implements ActivityToolbarInterf } private void getCurrentAccountAndFetchMessage(Bundle savedInstanceState) { - GetCurrentAccount.getCurrentAccount(mExecutor, new Handler(), mRedditDataRoomDatabase, account -> { - if (mNewAccountName != null) { - if (account == null || !account.getAccountName().equals(mNewAccountName)) { - SwitchAccount.switchAccount(mRedditDataRoomDatabase, mCurrentAccountSharedPreferences, - mExecutor, new Handler(), mNewAccountName, newAccount -> { - EventBus.getDefault().post(new SwitchAccountEvent(getClass().getName())); - Toast.makeText(this, R.string.account_switched, Toast.LENGTH_SHORT).show(); - - mNewAccountName = null; - if (newAccount == null) { - mNullAccessToken = true; - } else { - mAccessToken = newAccount.getAccessToken(); - } + if (mNewAccountName != null) { + if (mAccountName == null || !mAccountName.equals(mNewAccountName)) { + SwitchAccount.switchAccount(mRedditDataRoomDatabase, mCurrentAccountSharedPreferences, + mExecutor, new Handler(), mNewAccountName, newAccount -> { + EventBus.getDefault().post(new SwitchAccountEvent(getClass().getName())); + Toast.makeText(this, R.string.account_switched, Toast.LENGTH_SHORT).show(); + + mNewAccountName = null; + if (newAccount != null) { + mAccessToken = newAccount.getAccessToken(); + } - bindView(savedInstanceState); - }); - } else { - mAccessToken = account.getAccessToken(); - bindView(savedInstanceState); - } + bindView(savedInstanceState); + }); } else { - if (account == null) { - mNullAccessToken = true; - } else { - mAccessToken = account.getAccessToken(); - } - bindView(savedInstanceState); } - }); + } else { + bindView(savedInstanceState); + } } private void bindView(Bundle savedInstanceState) { @@ -350,8 +331,6 @@ public class InboxActivity extends BaseActivity implements ActivityToolbarInterf @Override protected void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); - outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken); - outState.putString(ACCESS_TOKEN_STATE, mAccessToken); outState.putString(NEW_ACCOUNT_NAME_STATE, mNewAccountName); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java index 2040a566..a2d8ac7f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java @@ -80,7 +80,6 @@ import ml.docilealligator.infinityforreddit.SortType; import ml.docilealligator.infinityforreddit.SortTypeSelectionCallback; import ml.docilealligator.infinityforreddit.account.AccountViewModel; import ml.docilealligator.infinityforreddit.adapters.NavigationDrawerRecyclerViewAdapter; -import ml.docilealligator.infinityforreddit.asynctasks.GetCurrentAccount; import ml.docilealligator.infinityforreddit.asynctasks.InsertSubscribedThingsAsyncTask; import ml.docilealligator.infinityforreddit.asynctasks.SwitchAccount; import ml.docilealligator.infinityforreddit.asynctasks.SwitchToAnonymousMode; @@ -127,12 +126,8 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb private static final String FETCH_SUBSCRIPTIONS_STATE = "FSS"; private static final String DRAWER_ON_ACCOUNT_SWITCH_STATE = "DOASS"; private static final String IS_IN_LAZY_MODE_STATE = "IILMS"; - private static final String NULL_ACCESS_TOKEN_STATE = "NATS"; private static final String ACCESS_TOKEN_STATE = "ATS"; private static final String ACCOUNT_NAME_STATE = "ANS"; - private static final String ACCOUNT_PROFILE_IMAGE_URL_STATE = "APIUS"; - private static final String ACCOUNT_BANNER_IMAGE_URL_STATE = "ABIUS"; - private static final String ACCOUNT_KARMA_STATE = "AKS"; private static final String MESSAGE_FULLNAME_STATE = "MFS"; private static final String NEW_ACCOUNT_NAME_STATE = "NANS"; @@ -197,10 +192,10 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb SharedPreferences mNsfwAndSpoilerSharedPreferences; @Inject @Named("bottom_app_bar") - SharedPreferences bottomAppBarSharedPreference; + SharedPreferences mBottomAppBarSharedPreference; @Inject @Named("current_account") - SharedPreferences currentAccountSharedPreferences; + SharedPreferences mCurrentAccountSharedPreferences; @Inject CustomThemeWrapper mCustomThemeWrapper; @Inject @@ -209,12 +204,8 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb private SectionsPagerAdapter sectionsPagerAdapter; private AppBarLayout.LayoutParams params; private NavigationDrawerRecyclerViewAdapter adapter; - private boolean mNullAccessToken = false; private String mAccessToken; private String mAccountName; - private String mProfileImageUrl; - private String mBannerImageUrl; - private int mKarma; private boolean mFetchUserInfoSuccess = false; private boolean mFetchSubscriptionsSuccess = false; private boolean mDrawerOnAccountSwitch = false; @@ -309,29 +300,21 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb fragmentManager = getSupportFragmentManager(); + mAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null); + mAccountName = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_NAME, null); + if (savedInstanceState != null) { mFetchUserInfoSuccess = savedInstanceState.getBoolean(FETCH_USER_INFO_STATE); mFetchSubscriptionsSuccess = savedInstanceState.getBoolean(FETCH_SUBSCRIPTIONS_STATE); mDrawerOnAccountSwitch = savedInstanceState.getBoolean(DRAWER_ON_ACCOUNT_SWITCH_STATE); isInLazyMode = savedInstanceState.getBoolean(IS_IN_LAZY_MODE_STATE); - mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE); - mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE); - mAccountName = savedInstanceState.getString(ACCOUNT_NAME_STATE); - mProfileImageUrl = savedInstanceState.getString(ACCOUNT_PROFILE_IMAGE_URL_STATE); - mBannerImageUrl = savedInstanceState.getString(ACCOUNT_BANNER_IMAGE_URL_STATE); - mKarma = savedInstanceState.getInt(ACCOUNT_KARMA_STATE); mMessageFullname = savedInstanceState.getString(MESSAGE_FULLNAME_STATE); mNewAccountName = savedInstanceState.getString(NEW_ACCOUNT_NAME_STATE); - - if (!mNullAccessToken && mAccessToken == null) { - getCurrentAccountAndBindView(); - } else { - bindView(); - } + initializeNotificationAndBindView(true); } else { mMessageFullname = getIntent().getStringExtra(EXTRA_MESSSAGE_FULLNAME); mNewAccountName = getIntent().getStringExtra(EXTRA_NEW_ACCOUNT_NAME); - getCurrentAccountAndBindView(); + initializeNotificationAndBindView(false); } } @@ -361,108 +344,62 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb applyFABTheme(fab); } - private void getCurrentAccountAndBindView() { - GetCurrentAccount.getCurrentAccount(mExecutor, new Handler(), mRedditDataRoomDatabase, account -> { - boolean enableNotification = mSharedPreferences.getBoolean(SharedPreferencesUtils.ENABLE_NOTIFICATION_KEY, true); - long notificationInterval = Long.parseLong(mSharedPreferences.getString(SharedPreferencesUtils.NOTIFICATION_INTERVAL_KEY, "1")); - TimeUnit timeUnit = (notificationInterval == 15 || notificationInterval == 30) ? TimeUnit.MINUTES : TimeUnit.HOURS; - - WorkManager workManager = WorkManager.getInstance(this); - - if (mNewAccountName != null) { - if (account == null || !account.getAccountName().equals(mNewAccountName)) { - SwitchAccount.switchAccount(mRedditDataRoomDatabase, currentAccountSharedPreferences, - mExecutor, new Handler(), mNewAccountName, newAccount -> { - EventBus.getDefault().post(new SwitchAccountEvent(getClass().getName())); - Toast.makeText(this, R.string.account_switched, Toast.LENGTH_SHORT).show(); - - mNewAccountName = null; - if (newAccount == null) { - mNullAccessToken = true; - } else { - mAccessToken = newAccount.getAccessToken(); - mAccountName = newAccount.getAccountName(); - mProfileImageUrl = newAccount.getProfileImageUrl(); - mBannerImageUrl = newAccount.getBannerImageUrl(); - mKarma = newAccount.getKarma(); - } - - if (enableNotification) { - Constraints constraints = new Constraints.Builder() - .setRequiredNetworkType(NetworkType.CONNECTED) - .build(); - - PeriodicWorkRequest pullNotificationRequest = - new PeriodicWorkRequest.Builder(PullNotificationWorker.class, - notificationInterval, timeUnit) - .setConstraints(constraints) - .build(); - - workManager.enqueueUniquePeriodicWork(PullNotificationWorker.UNIQUE_WORKER_NAME, - ExistingPeriodicWorkPolicy.KEEP, pullNotificationRequest); - } else { - workManager.cancelUniqueWork(PullNotificationWorker.UNIQUE_WORKER_NAME); - } - - bindView(); - }); - } else { - mAccessToken = account.getAccessToken(); - mAccountName = account.getAccountName(); - mProfileImageUrl = account.getProfileImageUrl(); - mBannerImageUrl = account.getBannerImageUrl(); - mKarma = account.getKarma(); - - if (enableNotification) { - Constraints constraints = new Constraints.Builder() - .setRequiredNetworkType(NetworkType.CONNECTED) - .build(); - - PeriodicWorkRequest pullNotificationRequest = - new PeriodicWorkRequest.Builder(PullNotificationWorker.class, - notificationInterval, timeUnit) - .setConstraints(constraints) - .build(); - - workManager.enqueueUniquePeriodicWork(PullNotificationWorker.UNIQUE_WORKER_NAME, - ExistingPeriodicWorkPolicy.KEEP, pullNotificationRequest); - } else { - workManager.cancelUniqueWork(PullNotificationWorker.UNIQUE_WORKER_NAME); - } + private void initializeNotificationAndBindView(boolean doNotInitializeNotificationIfNoNewAccount) { + boolean enableNotification = mSharedPreferences.getBoolean(SharedPreferencesUtils.ENABLE_NOTIFICATION_KEY, true); + long notificationInterval = Long.parseLong(mSharedPreferences.getString(SharedPreferencesUtils.NOTIFICATION_INTERVAL_KEY, "1")); + TimeUnit timeUnit = (notificationInterval == 15 || notificationInterval == 30) ? TimeUnit.MINUTES : TimeUnit.HOURS; - bindView(); - } - } else { - if (account == null) { - mNullAccessToken = true; - } else { - mAccessToken = account.getAccessToken(); - mAccountName = account.getAccountName(); - mProfileImageUrl = account.getProfileImageUrl(); - mBannerImageUrl = account.getBannerImageUrl(); - mKarma = account.getKarma(); - } + WorkManager workManager = WorkManager.getInstance(this); - if (enableNotification) { - Constraints constraints = new Constraints.Builder() - .setRequiredNetworkType(NetworkType.CONNECTED) - .build(); + if (mNewAccountName != null) { + if (mAccountName == null || !mAccountName.equals(mNewAccountName)) { + SwitchAccount.switchAccount(mRedditDataRoomDatabase, mCurrentAccountSharedPreferences, + mExecutor, new Handler(), mNewAccountName, newAccount -> { + EventBus.getDefault().post(new SwitchAccountEvent(getClass().getName())); + Toast.makeText(this, R.string.account_switched, Toast.LENGTH_SHORT).show(); - PeriodicWorkRequest pullNotificationRequest = - new PeriodicWorkRequest.Builder(PullNotificationWorker.class, - notificationInterval, timeUnit) - .setConstraints(constraints) - .build(); + mNewAccountName = null; + if (newAccount != null) { + mAccessToken = newAccount.getAccessToken(); + mAccountName = newAccount.getAccountName(); + } - workManager.enqueueUniquePeriodicWork(PullNotificationWorker.UNIQUE_WORKER_NAME, - ExistingPeriodicWorkPolicy.KEEP, pullNotificationRequest); - } else { - workManager.cancelUniqueWork(PullNotificationWorker.UNIQUE_WORKER_NAME); - } + setNotification(workManager, notificationInterval, timeUnit, enableNotification); + + bindView(); + }); + } else { + setNotification(workManager, notificationInterval, timeUnit, enableNotification); bindView(); } - }); + } else { + if (doNotInitializeNotificationIfNoNewAccount) { + setNotification(workManager, notificationInterval, timeUnit, enableNotification); + } + + bindView(); + } + } + + private void setNotification(WorkManager workManager, long notificationInterval, TimeUnit timeUnit, + boolean enableNotification) { + if (enableNotification) { + Constraints constraints = new Constraints.Builder() + .setRequiredNetworkType(NetworkType.CONNECTED) + .build(); + + PeriodicWorkRequest pullNotificationRequest = + new PeriodicWorkRequest.Builder(PullNotificationWorker.class, + notificationInterval, timeUnit) + .setConstraints(constraints) + .build(); + + workManager.enqueueUniquePeriodicWork(PullNotificationWorker.UNIQUE_WORKER_NAME, + ExistingPeriodicWorkPolicy.KEEP, pullNotificationRequest); + } else { + workManager.cancelUniqueWork(PullNotificationWorker.UNIQUE_WORKER_NAME); + } } private void bottomAppBarOptionAction(int option) { @@ -582,9 +519,9 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb bottomAppBar.setVisibility(View.GONE); } else { if (showBottomAppBar) { - int optionCount = bottomAppBarSharedPreference.getInt(SharedPreferencesUtils.MAIN_ACTIVITY_BOTTOM_APP_BAR_OPTION_COUNT, 4); - int option1 = bottomAppBarSharedPreference.getInt(SharedPreferencesUtils.MAIN_ACTIVITY_BOTTOM_APP_BAR_OPTION_1, SharedPreferencesUtils.MAIN_ACTIVITY_BOTTOM_APP_BAR_OPTION_SUBSCRIPTIONS); - int option2 = bottomAppBarSharedPreference.getInt(SharedPreferencesUtils.MAIN_ACTIVITY_BOTTOM_APP_BAR_OPTION_2, SharedPreferencesUtils.MAIN_ACTIVITY_BOTTOM_APP_BAR_OPTION_MULTIREDDITS); + int optionCount = mBottomAppBarSharedPreference.getInt(SharedPreferencesUtils.MAIN_ACTIVITY_BOTTOM_APP_BAR_OPTION_COUNT, 4); + int option1 = mBottomAppBarSharedPreference.getInt(SharedPreferencesUtils.MAIN_ACTIVITY_BOTTOM_APP_BAR_OPTION_1, SharedPreferencesUtils.MAIN_ACTIVITY_BOTTOM_APP_BAR_OPTION_SUBSCRIPTIONS); + int option2 = mBottomAppBarSharedPreference.getInt(SharedPreferencesUtils.MAIN_ACTIVITY_BOTTOM_APP_BAR_OPTION_2, SharedPreferencesUtils.MAIN_ACTIVITY_BOTTOM_APP_BAR_OPTION_MULTIREDDITS); bottomAppBar.setVisibility(View.VISIBLE); @@ -604,8 +541,8 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb bottomAppBarOptionAction(option2); }); } else { - int option3 = bottomAppBarSharedPreference.getInt(SharedPreferencesUtils.MAIN_ACTIVITY_BOTTOM_APP_BAR_OPTION_3, SharedPreferencesUtils.MAIN_ACTIVITY_BOTTOM_APP_BAR_OPTION_INBOX); - int option4 = bottomAppBarSharedPreference.getInt(SharedPreferencesUtils.MAIN_ACTIVITY_BOTTOM_APP_BAR_OPTION_4, SharedPreferencesUtils.MAIN_ACTIVITY_BOTTOM_APP_BAR_OPTION_PROFILE); + int option3 = mBottomAppBarSharedPreference.getInt(SharedPreferencesUtils.MAIN_ACTIVITY_BOTTOM_APP_BAR_OPTION_3, SharedPreferencesUtils.MAIN_ACTIVITY_BOTTOM_APP_BAR_OPTION_INBOX); + int option4 = mBottomAppBarSharedPreference.getInt(SharedPreferencesUtils.MAIN_ACTIVITY_BOTTOM_APP_BAR_OPTION_4, SharedPreferencesUtils.MAIN_ACTIVITY_BOTTOM_APP_BAR_OPTION_PROFILE); option1BottomAppBar.setImageResource(getBottomAppBarOptionDrawableResource(option1)); option2BottomAppBar.setImageResource(getBottomAppBarOptionDrawableResource(option2)); @@ -636,7 +573,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb } } - fabOption = bottomAppBarSharedPreference.getInt(SharedPreferencesUtils.MAIN_ACTIVITY_BOTTOM_APP_BAR_FAB, + fabOption = mBottomAppBarSharedPreference.getInt(SharedPreferencesUtils.MAIN_ACTIVITY_BOTTOM_APP_BAR_FAB, SharedPreferencesUtils.MAIN_ACTIVITY_BOTTOM_APP_BAR_FAB_SUBMIT_POSTS); switch (fabOption) { case SharedPreferencesUtils.MAIN_ACTIVITY_BOTTOM_APP_BAR_FAB_REFRESH: @@ -739,7 +676,6 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb adapter = new NavigationDrawerRecyclerViewAdapter(this, mSharedPreferences, mNsfwAndSpoilerSharedPreferences, mCustomThemeWrapper, mAccountName, - mProfileImageUrl, mBannerImageUrl, mKarma, new NavigationDrawerRecyclerViewAdapter.ItemClickListener() { @Override public void onMenuClick(int stringId) { @@ -796,14 +732,14 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb Intent addAccountIntent = new Intent(MainActivity.this, LoginActivity.class); startActivityForResult(addAccountIntent, LOGIN_ACTIVITY_REQUEST_CODE); } else if (stringId == R.string.anonymous_account) { - SwitchToAnonymousMode.switchToAnonymousMode(mRedditDataRoomDatabase, currentAccountSharedPreferences, + SwitchToAnonymousMode.switchToAnonymousMode(mRedditDataRoomDatabase, mCurrentAccountSharedPreferences, mExecutor, new Handler(), false, () -> { Intent anonymousIntent = new Intent(MainActivity.this, MainActivity.class); startActivity(anonymousIntent); finish(); }); } else if (stringId == R.string.log_out) { - SwitchToAnonymousMode.switchToAnonymousMode(mRedditDataRoomDatabase, currentAccountSharedPreferences, + SwitchToAnonymousMode.switchToAnonymousMode(mRedditDataRoomDatabase, mCurrentAccountSharedPreferences, mExecutor, new Handler(), true, () -> { Intent logOutIntent = new Intent(MainActivity.this, MainActivity.class); @@ -826,7 +762,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb @Override public void onAccountClick(String accountName) { - SwitchAccount.switchAccount(mRedditDataRoomDatabase, currentAccountSharedPreferences, + SwitchAccount.switchAccount(mRedditDataRoomDatabase, mCurrentAccountSharedPreferences, mExecutor, new Handler(), accountName, newAccount -> { Intent intent = new Intent(MainActivity.this, MainActivity.class); startActivity(intent); @@ -927,7 +863,8 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb accountViewModel.getAccountsExceptCurrentAccountLiveData().observe(this, adapter::changeAccountsDataset); accountViewModel.getCurrentAccountLiveData().observe(this, account -> { if (account != null) { - adapter.updateKarma(account.getKarma()); + adapter.updateAccountInfo(account.getProfileImageUrl(), account.getBannerImageUrl(), + account.getKarma()); } }); @@ -984,9 +921,6 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb @Override public void onFetchMyInfoSuccess(String name, String profileImageUrl, String bannerImageUrl, int karma) { mAccountName = name; - mProfileImageUrl = profileImageUrl; - mBannerImageUrl = bannerImageUrl; - mKarma = karma; mFetchUserInfoSuccess = true; } @@ -1119,12 +1053,8 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb outState.putBoolean(FETCH_SUBSCRIPTIONS_STATE, mFetchSubscriptionsSuccess); outState.putBoolean(DRAWER_ON_ACCOUNT_SWITCH_STATE, mDrawerOnAccountSwitch); outState.putBoolean(IS_IN_LAZY_MODE_STATE, isInLazyMode); - outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken); outState.putString(ACCESS_TOKEN_STATE, mAccessToken); outState.putString(ACCOUNT_NAME_STATE, mAccountName); - outState.putString(ACCOUNT_PROFILE_IMAGE_URL_STATE, mProfileImageUrl); - outState.putString(ACCOUNT_BANNER_IMAGE_URL_STATE, mBannerImageUrl); - outState.putInt(ACCOUNT_KARMA_STATE, mKarma); outState.putString(MESSAGE_FULLNAME_STATE, mMessageFullname); outState.putString(NEW_ACCOUNT_NAME_STATE, mNewAccountName); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MultiredditSelectionActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MultiredditSelectionActivity.java index 36b7b25d..e84534b1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MultiredditSelectionActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MultiredditSelectionActivity.java @@ -5,7 +5,6 @@ import android.content.Intent; import android.content.SharedPreferences; import android.os.Build; import android.os.Bundle; -import android.os.Handler; import android.view.MenuItem; import android.view.View; import android.view.Window; @@ -35,7 +34,6 @@ import ml.docilealligator.infinityforreddit.ActivityToolbarInterface; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; -import ml.docilealligator.infinityforreddit.asynctasks.GetCurrentAccount; import ml.docilealligator.infinityforreddit.asynctasks.InsertMultiRedditAsyncTask; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.events.SwitchAccountEvent; @@ -50,9 +48,6 @@ public class MultiredditSelectionActivity extends BaseActivity implements Activi static final String EXTRA_RETURN_MULTIREDDIT = "ERM"; private static final String INSERT_SUBSCRIBED_MULTIREDDIT_STATE = "ISSS"; - private static final String NULL_ACCESS_TOKEN_STATE = "NATS"; - private static final String ACCESS_TOKEN_STATE = "ATS"; - private static final String ACCOUNT_NAME_STATE = "ANS"; private static final String FRAGMENT_OUT_STATE = "FOS"; @BindView(R.id.coordinator_layout_multireddit_selection_activity) @@ -70,10 +65,12 @@ public class MultiredditSelectionActivity extends BaseActivity implements Activi @Named("default") SharedPreferences mSharedPreferences; @Inject + @Named("current_account") + SharedPreferences mCurrentAccountSharedPreferences; + @Inject CustomThemeWrapper mCustomThemeWrapper; @Inject Executor mExecutor; - private boolean mNullAccessToken = false; private String mAccessToken; private String mAccountName; private boolean mInsertSuccess = false; @@ -118,21 +115,16 @@ public class MultiredditSelectionActivity extends BaseActivity implements Activi setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); + mAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null); + mAccountName = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_NAME, null); + if (savedInstanceState == null) { - getCurrentAccountAndBindView(); + bindView(true); } else { mInsertSuccess = savedInstanceState.getBoolean(INSERT_SUBSCRIBED_MULTIREDDIT_STATE); - mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE); - mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE); - mAccountName = savedInstanceState.getString(ACCOUNT_NAME_STATE); - - if (!mNullAccessToken && mAccountName == null) { - getCurrentAccountAndBindView(); - } else { - mFragment = getSupportFragmentManager().getFragment(savedInstanceState, FRAGMENT_OUT_STATE); - getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_multireddit_selection_activity, mFragment).commit(); - bindView(false); - } + mFragment = getSupportFragmentManager().getFragment(savedInstanceState, FRAGMENT_OUT_STATE); + getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_multireddit_selection_activity, mFragment).commit(); + bindView(false); } } @@ -152,18 +144,6 @@ public class MultiredditSelectionActivity extends BaseActivity implements Activi applyAppBarLayoutAndToolbarTheme(appBarLayout, toolbar); } - private void getCurrentAccountAndBindView() { - GetCurrentAccount.getCurrentAccount(mExecutor, new Handler(), mRedditDataRoomDatabase, account -> { - if (account == null) { - mNullAccessToken = true; - } else { - mAccessToken = account.getAccessToken(); - mAccountName = account.getAccountName(); - } - bindView(true); - }); - } - private void bindView(boolean initializeFragment) { if (isFinishing() || isDestroyed()) { return; @@ -225,9 +205,6 @@ public class MultiredditSelectionActivity extends BaseActivity implements Activi super.onSaveInstanceState(outState); getSupportFragmentManager().putFragment(outState, FRAGMENT_OUT_STATE, mFragment); outState.putBoolean(INSERT_SUBSCRIBED_MULTIREDDIT_STATE, mInsertSuccess); - outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken); - outState.putString(ACCESS_TOKEN_STATE, mAccessToken); - outState.putString(ACCOUNT_NAME_STATE, mAccountName); } @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostImageActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostImageActivity.java index b26685c4..5e0d1d6e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostImageActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostImageActivity.java @@ -8,7 +8,6 @@ import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.os.Environment; -import android.os.Handler; import android.provider.MediaStore; import android.view.Menu; import android.view.MenuItem; @@ -52,7 +51,6 @@ import ml.docilealligator.infinityforreddit.Flair; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; -import ml.docilealligator.infinityforreddit.asynctasks.GetCurrentAccount; import ml.docilealligator.infinityforreddit.asynctasks.LoadSubredditIconAsyncTask; import ml.docilealligator.infinityforreddit.bottomsheetfragments.FlairBottomSheetFragment; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; @@ -60,6 +58,7 @@ import ml.docilealligator.infinityforreddit.events.SubmitImagePostEvent; import ml.docilealligator.infinityforreddit.events.SubmitVideoOrGifPostEvent; import ml.docilealligator.infinityforreddit.events.SwitchAccountEvent; import ml.docilealligator.infinityforreddit.services.SubmitPostService; +import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; import pl.droidsonroids.gif.GifImageView; import retrofit2.Retrofit; @@ -77,9 +76,6 @@ public class PostImageActivity extends BaseActivity implements FlairBottomSheetF private static final String FLAIR_STATE = "FS"; private static final String IS_SPOILER_STATE = "ISS"; private static final String IS_NSFW_STATE = "INS"; - private static final String NULL_ACCESS_TOKEN_STATE = "NATS"; - private static final String ACCESS_TOKEN_STATE = "ATS"; - private static final String ACCOUNT_NAME_STATE = "ANS"; private static final int SUBREDDIT_SELECTION_REQUEST_CODE = 0; private static final int PICK_IMAGE_REQUEST_CODE = 1; @@ -136,10 +132,12 @@ public class PostImageActivity extends BaseActivity implements FlairBottomSheetF @Named("default") SharedPreferences mSharedPreferences; @Inject + @Named("current_account") + SharedPreferences mCurrentAccountSharedPreferences; + @Inject CustomThemeWrapper mCustomThemeWrapper; @Inject Executor mExecutor; - private boolean mNullAccessToken = false; private String mAccessToken; private String mAccountName; private String iconUrl; @@ -194,15 +192,10 @@ public class PostImageActivity extends BaseActivity implements FlairBottomSheetF resources = getResources(); - if (savedInstanceState != null) { - mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE); - mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE); - mAccountName = savedInstanceState.getString(ACCOUNT_NAME_STATE); - - if (!mNullAccessToken && mAccessToken == null) { - getCurrentAccount(); - } + mAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null); + mAccountName = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_NAME, null); + if (savedInstanceState != null) { subredditName = savedInstanceState.getString(SUBREDDIT_NAME_STATE); iconUrl = savedInstanceState.getString(SUBREDDIT_ICON_STATE); subredditSelected = savedInstanceState.getBoolean(SUBREDDIT_SELECTED_STATE); @@ -249,8 +242,6 @@ public class PostImageActivity extends BaseActivity implements FlairBottomSheetF nsfwTextView.setTextColor(nsfwTextColor); } } else { - getCurrentAccount(); - isPosting = false; if (getIntent().hasExtra(EXTRA_SUBREDDIT_NAME)) { @@ -410,17 +401,6 @@ public class PostImageActivity extends BaseActivity implements FlairBottomSheetF selectAgainTextView.setTextColor(mCustomThemeWrapper.getColorAccent()); } - private void getCurrentAccount() { - GetCurrentAccount.getCurrentAccount(mExecutor, new Handler(), mRedditDataRoomDatabase, account -> { - if (account == null) { - mNullAccessToken = true; - } else { - mAccessToken = account.getAccessToken(); - mAccountName = account.getAccountName(); - } - }); - } - private void loadImage() { constraintLayout.setVisibility(View.GONE); imageView.setVisibility(View.VISIBLE); @@ -569,9 +549,6 @@ public class PostImageActivity extends BaseActivity implements FlairBottomSheetF outState.putParcelable(FLAIR_STATE, flair); outState.putBoolean(IS_SPOILER_STATE, isSpoiler); outState.putBoolean(IS_NSFW_STATE, isNSFW); - outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken); - outState.putString(ACCESS_TOKEN_STATE, mAccessToken); - outState.putString(ACCOUNT_NAME_STATE, mAccountName); } @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostLinkActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostLinkActivity.java index efd93c31..51369282 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostLinkActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostLinkActivity.java @@ -6,7 +6,6 @@ import android.content.res.ColorStateList; import android.content.res.Resources; import android.os.Build; import android.os.Bundle; -import android.os.Handler; import android.view.Menu; import android.view.MenuItem; import android.view.View; @@ -43,7 +42,6 @@ import ml.docilealligator.infinityforreddit.Flair; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; -import ml.docilealligator.infinityforreddit.asynctasks.GetCurrentAccount; import ml.docilealligator.infinityforreddit.asynctasks.LoadSubredditIconAsyncTask; import ml.docilealligator.infinityforreddit.bottomsheetfragments.FlairBottomSheetFragment; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; @@ -51,6 +49,7 @@ import ml.docilealligator.infinityforreddit.events.SubmitTextOrLinkPostEvent; import ml.docilealligator.infinityforreddit.events.SwitchAccountEvent; import ml.docilealligator.infinityforreddit.services.SubmitPostService; import ml.docilealligator.infinityforreddit.utils.APIUtils; +import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; import pl.droidsonroids.gif.GifImageView; import retrofit2.Retrofit; @@ -68,8 +67,6 @@ public class PostLinkActivity extends BaseActivity implements FlairBottomSheetFr private static final String FLAIR_STATE = "FS"; private static final String IS_SPOILER_STATE = "ISS"; private static final String IS_NSFW_STATE = "INS"; - private static final String NULL_ACCESS_TOKEN_STATE = "NATS"; - private static final String ACCESS_TOKEN_STATE = "ATS"; private static final int SUBREDDIT_SELECTION_REQUEST_CODE = 0; @@ -113,10 +110,12 @@ public class PostLinkActivity extends BaseActivity implements FlairBottomSheetFr @Named("default") SharedPreferences mSharedPreferences; @Inject + @Named("current_account") + SharedPreferences mCurrentAccountSharedPreferences; + @Inject CustomThemeWrapper mCustomThemeWrapper; @Inject Executor mExecutor; - private boolean mNullAccessToken = false; private String mAccessToken; private String iconUrl; private String subredditName; @@ -169,14 +168,9 @@ public class PostLinkActivity extends BaseActivity implements FlairBottomSheetFr resources = getResources(); - if (savedInstanceState != null) { - mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE); - mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE); - - if (!mNullAccessToken && mAccessToken == null) { - getCurrentAccount(); - } + mAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null); + if (savedInstanceState != null) { subredditName = savedInstanceState.getString(SUBREDDIT_NAME_STATE); iconUrl = savedInstanceState.getString(SUBREDDIT_ICON_STATE); subredditSelected = savedInstanceState.getBoolean(SUBREDDIT_SELECTED_STATE); @@ -218,8 +212,6 @@ public class PostLinkActivity extends BaseActivity implements FlairBottomSheetFr nsfwTextView.setTextColor(nsfwTextColor); } } else { - getCurrentAccount(); - isPosting = false; if (getIntent().hasExtra(EXTRA_SUBREDDIT_NAME)) { @@ -347,16 +339,6 @@ public class PostLinkActivity extends BaseActivity implements FlairBottomSheetFr linkEditText.setHintTextColor(secondaryTextColor); } - private void getCurrentAccount() { - GetCurrentAccount.getCurrentAccount(mExecutor, new Handler(), mRedditDataRoomDatabase, account -> { - if (account == null) { - mNullAccessToken = true; - } else { - mAccessToken = account.getAccessToken(); - } - }); - } - private void displaySubredditIcon() { if (iconUrl != null && !iconUrl.equals("")) { mGlide.load(iconUrl) @@ -488,8 +470,6 @@ public class PostLinkActivity extends BaseActivity implements FlairBottomSheetFr outState.putParcelable(FLAIR_STATE, flair); outState.putBoolean(IS_SPOILER_STATE, isSpoiler); outState.putBoolean(IS_NSFW_STATE, isNSFW); - outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken); - outState.putString(ACCESS_TOKEN_STATE, mAccessToken); } @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostTextActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostTextActivity.java index 592b2817..131baa39 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostTextActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostTextActivity.java @@ -6,7 +6,6 @@ import android.content.res.ColorStateList; import android.content.res.Resources; import android.os.Build; import android.os.Bundle; -import android.os.Handler; import android.view.Menu; import android.view.MenuItem; import android.view.View; @@ -46,7 +45,6 @@ import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.adapters.MarkdownBottomBarRecyclerViewAdapter; -import ml.docilealligator.infinityforreddit.asynctasks.GetCurrentAccount; import ml.docilealligator.infinityforreddit.asynctasks.LoadSubredditIconAsyncTask; import ml.docilealligator.infinityforreddit.bottomsheetfragments.FlairBottomSheetFragment; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; @@ -54,6 +52,7 @@ import ml.docilealligator.infinityforreddit.events.SubmitTextOrLinkPostEvent; import ml.docilealligator.infinityforreddit.events.SwitchAccountEvent; import ml.docilealligator.infinityforreddit.services.SubmitPostService; import ml.docilealligator.infinityforreddit.utils.APIUtils; +import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; import pl.droidsonroids.gif.GifImageView; import retrofit2.Retrofit; @@ -71,8 +70,6 @@ public class PostTextActivity extends BaseActivity implements FlairBottomSheetFr private static final String FLAIR_STATE = "FS"; private static final String IS_SPOILER_STATE = "ISS"; private static final String IS_NSFW_STATE = "INS"; - private static final String NULL_ACCESS_TOKEN_STATE = "NATS"; - private static final String ACCESS_TOKEN_STATE = "ATS"; private static final int SUBREDDIT_SELECTION_REQUEST_CODE = 0; @@ -118,10 +115,12 @@ public class PostTextActivity extends BaseActivity implements FlairBottomSheetFr @Named("default") SharedPreferences mSharedPreferences; @Inject + @Named("current_account") + SharedPreferences mCurrentAccountSharedPreferences; + @Inject CustomThemeWrapper mCustomThemeWrapper; @Inject Executor mExecutor; - private boolean mNullAccessToken = false; private String mAccessToken; private String iconUrl; private String subredditName; @@ -174,14 +173,9 @@ public class PostTextActivity extends BaseActivity implements FlairBottomSheetFr resources = getResources(); - if (savedInstanceState != null) { - mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE); - mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE); - - if (!mNullAccessToken && mAccessToken == null) { - getCurrentAccount(); - } + mAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null); + if (savedInstanceState != null) { subredditName = savedInstanceState.getString(SUBREDDIT_NAME_STATE); iconUrl = savedInstanceState.getString(SUBREDDIT_ICON_STATE); subredditSelected = savedInstanceState.getBoolean(SUBREDDIT_SELECTED_STATE); @@ -223,8 +217,6 @@ public class PostTextActivity extends BaseActivity implements FlairBottomSheetFr nsfwTextView.setTextColor(nsfwTextColor); } } else { - getCurrentAccount(); - isPosting = false; if (getIntent().hasExtra(EXTRA_SUBREDDIT_NAME)) { @@ -364,16 +356,6 @@ public class PostTextActivity extends BaseActivity implements FlairBottomSheetFr contentEditText.setHintTextColor(secondaryTextColor); } - private void getCurrentAccount() { - GetCurrentAccount.getCurrentAccount(mExecutor, new Handler(), mRedditDataRoomDatabase, account -> { - if (account == null) { - mNullAccessToken = true; - } else { - mAccessToken = account.getAccessToken(); - } - }); - } - private void displaySubredditIcon() { if (iconUrl != null && !iconUrl.equals("")) { mGlide.load(iconUrl) @@ -505,8 +487,6 @@ public class PostTextActivity extends BaseActivity implements FlairBottomSheetFr outState.putParcelable(FLAIR_STATE, flair); outState.putBoolean(IS_SPOILER_STATE, isSpoiler); outState.putBoolean(IS_NSFW_STATE, isNSFW); - outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken); - outState.putString(ACCESS_TOKEN_STATE, mAccessToken); } @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostVideoActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostVideoActivity.java index 88fe606c..cfb747a4 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostVideoActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostVideoActivity.java @@ -7,7 +7,6 @@ import android.content.res.Resources; import android.net.Uri; import android.os.Build; import android.os.Bundle; -import android.os.Handler; import android.provider.MediaStore; import android.view.Menu; import android.view.MenuItem; @@ -55,13 +54,13 @@ import ml.docilealligator.infinityforreddit.Flair; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; -import ml.docilealligator.infinityforreddit.asynctasks.GetCurrentAccount; import ml.docilealligator.infinityforreddit.asynctasks.LoadSubredditIconAsyncTask; import ml.docilealligator.infinityforreddit.bottomsheetfragments.FlairBottomSheetFragment; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.events.SubmitVideoOrGifPostEvent; import ml.docilealligator.infinityforreddit.events.SwitchAccountEvent; import ml.docilealligator.infinityforreddit.services.SubmitPostService; +import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; import pl.droidsonroids.gif.GifImageView; import retrofit2.Retrofit; @@ -79,9 +78,6 @@ public class PostVideoActivity extends BaseActivity implements FlairBottomSheetF private static final String FLAIR_STATE = "FS"; private static final String IS_SPOILER_STATE = "ISS"; private static final String IS_NSFW_STATE = "INS"; - private static final String NULL_ACCESS_TOKEN_STATE = "NATS"; - private static final String ACCESS_TOKEN_STATE = "ATS"; - private static final String ACCOUNT_NAME_STATE = "ANS"; private static final int SUBREDDIT_SELECTION_REQUEST_CODE = 0; private static final int PICK_VIDEO_REQUEST_CODE = 1; @@ -141,10 +137,12 @@ public class PostVideoActivity extends BaseActivity implements FlairBottomSheetF @Named("default") SharedPreferences mSharedPreferences; @Inject + @Named("current_account") + SharedPreferences mCurrentAccountSharedPreferences; + @Inject CustomThemeWrapper mCustomThemeWrapper; @Inject Executor mExecutor; - private boolean mNullAccessToken = false; private String mAccessToken; private String mAccountName; private String iconUrl; @@ -208,6 +206,9 @@ public class PostVideoActivity extends BaseActivity implements FlairBottomSheetF resources = getResources(); + mAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null); + mAccountName = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_NAME, null); + if (savedInstanceState != null) { subredditName = savedInstanceState.getString(SUBREDDIT_NAME_STATE); iconUrl = savedInstanceState.getString(SUBREDDIT_ICON_STATE); @@ -218,13 +219,6 @@ public class PostVideoActivity extends BaseActivity implements FlairBottomSheetF flair = savedInstanceState.getParcelable(FLAIR_STATE); isSpoiler = savedInstanceState.getBoolean(IS_SPOILER_STATE); isNSFW = savedInstanceState.getBoolean(IS_NSFW_STATE); - mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE); - mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE); - mAccountName = savedInstanceState.getString(ACCOUNT_NAME_STATE); - - if (!mNullAccessToken && mAccessToken == null) { - getCurrentAccount(); - } if (savedInstanceState.getString(VIDEO_URI_STATE) != null) { videoUri = Uri.parse(savedInstanceState.getString(VIDEO_URI_STATE)); @@ -262,8 +256,6 @@ public class PostVideoActivity extends BaseActivity implements FlairBottomSheetF nsfwTextView.setTextColor(nsfwTextColor); } } else { - getCurrentAccount(); - isPosting = false; if (getIntent().hasExtra(EXTRA_SUBREDDIT_NAME)) { @@ -415,17 +407,6 @@ public class PostVideoActivity extends BaseActivity implements FlairBottomSheetF selectAgainTextView.setTextColor(mCustomThemeWrapper.getColorAccent()); } - private void getCurrentAccount() { - GetCurrentAccount.getCurrentAccount(mExecutor, new Handler(), mRedditDataRoomDatabase, account -> { - if (account == null) { - mNullAccessToken = true; - } else { - mAccessToken = account.getAccessToken(); - mAccountName = account.getAccountName(); - } - }); - } - private void loadVideo() { constraintLayout.setVisibility(View.GONE); selectAgainTextView.setVisibility(View.VISIBLE); @@ -584,9 +565,6 @@ public class PostVideoActivity extends BaseActivity implements FlairBottomSheetF outState.putParcelable(FLAIR_STATE, flair); outState.putBoolean(IS_SPOILER_STATE, isSpoiler); outState.putBoolean(IS_NSFW_STATE, isNSFW); - outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken); - outState.putString(ACCESS_TOKEN_STATE, mAccessToken); - outState.putString(ACCOUNT_NAME_STATE, mAccountName); } @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ReportActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ReportActivity.java index 996e86e6..7e273b09 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ReportActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ReportActivity.java @@ -3,7 +3,6 @@ package ml.docilealligator.infinityforreddit.activities; import android.content.SharedPreferences; import android.os.Build; import android.os.Bundle; -import android.os.Handler; import android.view.Menu; import android.view.MenuItem; import android.widget.Toast; @@ -34,7 +33,6 @@ import ml.docilealligator.infinityforreddit.ReportReason; import ml.docilealligator.infinityforreddit.ReportThing; import ml.docilealligator.infinityforreddit.Rule; import ml.docilealligator.infinityforreddit.adapters.ReportReasonRecyclerViewAdapter; -import ml.docilealligator.infinityforreddit.asynctasks.GetCurrentAccount; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; import retrofit2.Retrofit; @@ -43,8 +41,6 @@ public class ReportActivity extends BaseActivity { public static final String EXTRA_SUBREDDIT_NAME = "ESN"; public static final String EXTRA_THING_FULLNAME = "ETF"; - private static final String NULL_ACCESS_TOKEN_STATE = "NATS"; - private static final String ACCESS_TOKEN_STATE = "ATS"; private static final String GENERAL_REASONS_STATE = "GRS"; private static final String RULES_REASON_STATE = "RRS"; @@ -66,12 +62,14 @@ public class ReportActivity extends BaseActivity { @Named("default") SharedPreferences mSharedPreferences; @Inject + @Named("current_account") + SharedPreferences mCurrentAccountSharedPreferences; + @Inject RedditDataRoomDatabase mRedditDataRoomDatabase; @Inject CustomThemeWrapper mCustomThemeWrapper; @Inject Executor mExecutor; - private boolean mNullAccessToken = false; private String mAccessToken; private String mFullname; private String mSubredditName; @@ -106,18 +104,11 @@ public class ReportActivity extends BaseActivity { mFullname = getIntent().getStringExtra(EXTRA_THING_FULLNAME); mSubredditName = getIntent().getStringExtra(EXTRA_SUBREDDIT_NAME); - if (savedInstanceState != null) { - mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE); - mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE); - - if (!mNullAccessToken && mAccessToken == null) { - getCurrentAccount(); - } + mAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null); + if (savedInstanceState != null) { generalReasons = savedInstanceState.getParcelableArrayList(GENERAL_REASONS_STATE); rulesReasons = savedInstanceState.getParcelableArrayList(RULES_REASON_STATE); - } else { - getCurrentAccount(); } if (generalReasons != null) { @@ -145,16 +136,6 @@ public class ReportActivity extends BaseActivity { } } - private void getCurrentAccount() { - GetCurrentAccount.getCurrentAccount(mExecutor, new Handler(), mRedditDataRoomDatabase, account -> { - if (account == null) { - mNullAccessToken = true; - } else { - mAccessToken = account.getAccessToken(); - } - }); - } - @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.report_activity, menu); @@ -164,31 +145,31 @@ public class ReportActivity extends BaseActivity { @Override public boolean onOptionsItemSelected(@NonNull MenuItem item) { - switch (item.getItemId()) { - case android.R.id.home: - finish(); - return true; - case R.id.action_send_report_activity: - ReportReason reportReason = mAdapter.getSelectedReason(); - if (reportReason != null) { - Toast.makeText(ReportActivity.this, R.string.reporting, Toast.LENGTH_SHORT).show(); - ReportThing.reportThing(mOauthRetrofit, mAccessToken, mFullname, mSubredditName, - reportReason.getReasonType(), reportReason.getReportReason(), new ReportThing.ReportThingListener() { - @Override - public void success() { - Toast.makeText(ReportActivity.this, R.string.report_successful, Toast.LENGTH_SHORT).show(); - finish(); - } - - @Override - public void failed() { - Toast.makeText(ReportActivity.this, R.string.report_failed, Toast.LENGTH_SHORT).show(); - } - }); - } else { - Toast.makeText(ReportActivity.this, R.string.report_reason_not_selected, Toast.LENGTH_SHORT).show(); - } - return true; + int itemId = item.getItemId(); + if (itemId == android.R.id.home) { + finish(); + return true; + } else if (itemId == R.id.action_send_report_activity) { + ReportReason reportReason = mAdapter.getSelectedReason(); + if (reportReason != null) { + Toast.makeText(ReportActivity.this, R.string.reporting, Toast.LENGTH_SHORT).show(); + ReportThing.reportThing(mOauthRetrofit, mAccessToken, mFullname, mSubredditName, + reportReason.getReasonType(), reportReason.getReportReason(), new ReportThing.ReportThingListener() { + @Override + public void success() { + Toast.makeText(ReportActivity.this, R.string.report_successful, Toast.LENGTH_SHORT).show(); + finish(); + } + + @Override + public void failed() { + Toast.makeText(ReportActivity.this, R.string.report_failed, Toast.LENGTH_SHORT).show(); + } + }); + } else { + Toast.makeText(ReportActivity.this, R.string.report_reason_not_selected, Toast.LENGTH_SHORT).show(); + } + return true; } return false; @@ -197,8 +178,6 @@ public class ReportActivity extends BaseActivity { @Override protected void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); - outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken); - outState.putString(ACCESS_TOKEN_STATE, mAccessToken); if (mAdapter != null) { outState.putParcelableArrayList(GENERAL_REASONS_STATE, mAdapter.getGeneralReasons()); outState.putParcelableArrayList(RULES_REASON_STATE, mAdapter.getRules()); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchActivity.java index 19bba9cd..9dd2a891 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchActivity.java @@ -6,7 +6,6 @@ import android.content.Intent; import android.content.SharedPreferences; import android.graphics.drawable.ColorDrawable; import android.os.Bundle; -import android.os.Handler; import android.view.Menu; import android.view.MenuItem; import android.view.View; @@ -29,8 +28,6 @@ import com.r0adkll.slidr.Slidr; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; -import java.util.concurrent.Executor; - import javax.inject.Inject; import javax.inject.Named; @@ -40,7 +37,6 @@ import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.adapters.SearchActivityRecyclerViewAdapter; -import ml.docilealligator.infinityforreddit.asynctasks.GetCurrentAccount; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.events.SwitchAccountEvent; import ml.docilealligator.infinityforreddit.recentsearchquery.DeleteRecentSearchQuery; @@ -60,8 +56,6 @@ public class SearchActivity extends BaseActivity { static final String EXTRA_RETURN_USER_NAME = "ERUN"; static final String EXTRA_RETURN_USER_ICON_URL = "ERUIU"; - private static final String NULL_ACCOUNT_NAME_STATE = "NANS"; - private static final String ACCOUNT_NAME_STATE = "ANS"; private static final String SUBREDDIT_NAME_STATE = "SNS"; private static final String SUBREDDIT_IS_USER_STATE = "SIUS"; @@ -95,10 +89,10 @@ public class SearchActivity extends BaseActivity { @Named("default") SharedPreferences mSharedPreferences; @Inject - CustomThemeWrapper mCustomThemeWrapper; + @Named("current_account") + SharedPreferences mCurrentAccountSharedPreferences; @Inject - Executor mExecutor; - private boolean mNullAccountName = false; + CustomThemeWrapper mCustomThemeWrapper; private String mAccountName; private String query; private String subredditName; @@ -179,9 +173,9 @@ public class SearchActivity extends BaseActivity { } }); + mAccountName = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_NAME, null); + if (savedInstanceState != null) { - mNullAccountName = savedInstanceState.getBoolean(NULL_ACCOUNT_NAME_STATE); - mAccountName = savedInstanceState.getString(ACCOUNT_NAME_STATE); subredditName = savedInstanceState.getString(SUBREDDIT_NAME_STATE); subredditIsUser = savedInstanceState.getBoolean(SUBREDDIT_IS_USER_STATE); @@ -190,15 +184,10 @@ public class SearchActivity extends BaseActivity { } else { subredditNameTextView.setText(subredditName); } - - if (!mNullAccountName && mAccountName == null) { - getCurrentAccountAndInitializeViewPager(); - } else { - bindView(); - } + bindView(); } else { query = getIntent().getStringExtra(EXTRA_QUERY); - getCurrentAccountAndInitializeViewPager(); + bindView(); } if (searchOnlySubreddits || searchOnlyUsers) { @@ -219,17 +208,6 @@ public class SearchActivity extends BaseActivity { } } - private void getCurrentAccountAndInitializeViewPager() { - GetCurrentAccount.getCurrentAccount(mExecutor, new Handler(), mRedditDataRoomDatabase, account -> { - if (account == null) { - mNullAccountName = true; - } else { - mAccountName = account.getAccountName(); - } - bindView(); - }); - } - private void bindView() { if (mAccountName != null) { adapter = new SearchActivityRecyclerViewAdapter(this, mCustomThemeWrapper, new SearchActivityRecyclerViewAdapter.ItemOnClickListener() { @@ -403,8 +381,6 @@ public class SearchActivity extends BaseActivity { @Override public void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); - outState.putBoolean(NULL_ACCOUNT_NAME_STATE, mNullAccountName); - outState.putString(ACCOUNT_NAME_STATE, mAccountName); outState.putString(SUBREDDIT_NAME_STATE, subredditName); outState.putBoolean(SUBREDDIT_IS_USER_STATE, subredditIsUser); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchResultActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchResultActivity.java index 7d052796..44cae730 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchResultActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchResultActivity.java @@ -5,7 +5,6 @@ import android.content.Intent; import android.content.SharedPreferences; import android.os.Build; import android.os.Bundle; -import android.os.Handler; import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; @@ -46,12 +45,11 @@ import butterknife.ButterKnife; import ml.docilealligator.infinityforreddit.ActivityToolbarInterface; import ml.docilealligator.infinityforreddit.FragmentCommunicator; import ml.docilealligator.infinityforreddit.Infinity; -import ml.docilealligator.infinityforreddit.RecyclerViewContentScrollingInterface; import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.RecyclerViewContentScrollingInterface; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.SortType; import ml.docilealligator.infinityforreddit.SortTypeSelectionCallback; -import ml.docilealligator.infinityforreddit.asynctasks.GetCurrentAccount; import ml.docilealligator.infinityforreddit.bottomsheetfragments.FABMoreOptionsBottomSheetFragment; import ml.docilealligator.infinityforreddit.bottomsheetfragments.PostLayoutBottomSheetFragment; import ml.docilealligator.infinityforreddit.bottomsheetfragments.PostTypeBottomSheetFragment; @@ -77,9 +75,6 @@ public class SearchResultActivity extends BaseActivity implements SortTypeSelect static final String EXTRA_QUERY = "QK"; static final String EXTRA_SUBREDDIT_NAME = "ESN"; - private static final String NULL_ACCESS_TOKEN_STATE = "NATS"; - private static final String ACCESS_TOKEN_STATE = "ATS"; - private static final String ACCOUNT_NAME_STATE = "ANS"; private static final String INSERT_SEARCH_QUERY_SUCCESS_STATE = "ISQSS"; @BindView(R.id.coordinator_layout_search_result_activity) CoordinatorLayout coordinatorLayout; @@ -111,10 +106,12 @@ public class SearchResultActivity extends BaseActivity implements SortTypeSelect @Named("nsfw_and_spoiler") SharedPreferences mNsfwAndSpoilerSharedPreferences; @Inject + @Named("current_account") + SharedPreferences mCurrentAccountSharedPreferences; + @Inject CustomThemeWrapper mCustomThemeWrapper; @Inject Executor mExecutor; - private boolean mNullAccessToken = false; private String mAccessToken; private String mAccountName; private String mQuery; @@ -186,19 +183,13 @@ public class SearchResultActivity extends BaseActivity implements SortTypeSelect fragmentManager = getSupportFragmentManager(); - if (savedInstanceState == null) { - getCurrentAccountAndInitializeViewPager(); - } else { - mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE); - mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE); - mAccountName = savedInstanceState.getString(ACCOUNT_NAME_STATE); + mAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null); + mAccountName = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_NAME, null); + + if (savedInstanceState != null) { mInsertSearchQuerySuccess = savedInstanceState.getBoolean(INSERT_SEARCH_QUERY_SUCCESS_STATE); - if (!mNullAccessToken && mAccessToken == null) { - getCurrentAccountAndInitializeViewPager(); - } else { - bindView(); - } } + bindView(); } @Override @@ -227,18 +218,6 @@ public class SearchResultActivity extends BaseActivity implements SortTypeSelect applyFABTheme(fab); } - private void getCurrentAccountAndInitializeViewPager() { - GetCurrentAccount.getCurrentAccount(mExecutor, new Handler(), mRedditDataRoomDatabase, account -> { - if (account == null) { - mNullAccessToken = true; - } else { - mAccessToken = account.getAccessToken(); - mAccountName = account.getAccountName(); - } - bindView(); - }); - } - private void bindView() { sectionsPagerAdapter = new SectionsPagerAdapter(this); viewPager2.setAdapter(sectionsPagerAdapter); @@ -440,9 +419,6 @@ public class SearchResultActivity extends BaseActivity implements SortTypeSelect @Override protected void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); - outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken); - outState.putString(ACCESS_TOKEN_STATE, mAccessToken); - outState.putString(ACCOUNT_NAME_STATE, mAccountName); outState.putBoolean(INSERT_SEARCH_QUERY_SUCCESS_STATE, mInsertSearchQuerySuccess); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchSubredditsResultActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchSubredditsResultActivity.java index 55de7e87..4f417ca0 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchSubredditsResultActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchSubredditsResultActivity.java @@ -5,7 +5,6 @@ import android.content.Intent; import android.content.SharedPreferences; import android.os.Build; import android.os.Bundle; -import android.os.Handler; import android.view.MenuItem; import android.view.View; import android.view.Window; @@ -33,7 +32,6 @@ import ml.docilealligator.infinityforreddit.ActivityToolbarInterface; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; -import ml.docilealligator.infinityforreddit.asynctasks.GetCurrentAccount; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.events.SwitchAccountEvent; import ml.docilealligator.infinityforreddit.fragments.SubredditListingFragment; @@ -45,9 +43,6 @@ public class SearchSubredditsResultActivity extends BaseActivity implements Acti static final String EXTRA_RETURN_SUBREDDIT_NAME = "ERSN"; static final String EXTRA_RETURN_SUBREDDIT_ICON_URL = "ERSIU"; - private static final String NULL_ACCESS_TOKEN_STATE = "NATS"; - private static final String ACCESS_TOKEN_STATE = "ATS"; - private static final String ACCOUNT_NAME_STATE = "ANS"; private static final String FRAGMENT_OUT_STATE = "FOS"; @BindView(R.id.coordinator_layout_search_subreddits_result_activity) @@ -63,10 +58,12 @@ public class SearchSubredditsResultActivity extends BaseActivity implements Acti @Named("default") SharedPreferences mSharedPreferences; @Inject + @Named("current_account") + SharedPreferences mCurrentAccountSharedPreferences; + @Inject CustomThemeWrapper mCustomThemeWrapper; @Inject Executor mExecutor; - private boolean mNullAccessToken = false; private String mAccessToken; private String mAccountName; @@ -113,19 +110,21 @@ public class SearchSubredditsResultActivity extends BaseActivity implements Acti String query = getIntent().getExtras().getString(EXTRA_QUERY); + mAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null); + mAccountName = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_NAME, null); + if (savedInstanceState == null) { - getCurrentAccountAndInitializeFragment(query); + mFragment = new SubredditListingFragment(); + Bundle bundle = new Bundle(); + bundle.putString(SubredditListingFragment.EXTRA_QUERY, query); + bundle.putBoolean(SubredditListingFragment.EXTRA_IS_GETTING_SUBREDDIT_INFO, true); + bundle.putString(SubredditListingFragment.EXTRA_ACCESS_TOKEN, mAccessToken); + bundle.putString(SubredditListingFragment.EXTRA_ACCOUNT_NAME, mAccountName); + mFragment.setArguments(bundle); + getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_search_subreddits_result_activity, mFragment).commit(); } else { - mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE); - mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE); - mAccountName = savedInstanceState.getString(ACCOUNT_NAME_STATE); - - if (!mNullAccessToken && mAccessToken == null) { - getCurrentAccountAndInitializeFragment(query); - } else { - mFragment = getSupportFragmentManager().getFragment(savedInstanceState, FRAGMENT_OUT_STATE); - getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_search_subreddits_result_activity, mFragment).commit(); - } + mFragment = getSupportFragmentManager().getFragment(savedInstanceState, FRAGMENT_OUT_STATE); + getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_search_subreddits_result_activity, mFragment).commit(); } } @@ -145,26 +144,6 @@ public class SearchSubredditsResultActivity extends BaseActivity implements Acti applyAppBarLayoutAndToolbarTheme(appBarLayout, toolbar); } - private void getCurrentAccountAndInitializeFragment(String query) { - GetCurrentAccount.getCurrentAccount(mExecutor, new Handler(), mRedditDataRoomDatabase, account -> { - if (account == null) { - mNullAccessToken = true; - } else { - mAccessToken = account.getAccessToken(); - mAccountName = account.getAccountName(); - } - - mFragment = new SubredditListingFragment(); - Bundle bundle = new Bundle(); - bundle.putString(SubredditListingFragment.EXTRA_QUERY, query); - bundle.putBoolean(SubredditListingFragment.EXTRA_IS_GETTING_SUBREDDIT_INFO, true); - bundle.putString(SubredditListingFragment.EXTRA_ACCESS_TOKEN, mAccessToken); - bundle.putString(SubredditListingFragment.EXTRA_ACCOUNT_NAME, mAccountName); - mFragment.setArguments(bundle); - getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_search_subreddits_result_activity, mFragment).commit(); - }); - } - public void getSelectedSubreddit(String name, String iconUrl) { Intent returnIntent = new Intent(); returnIntent.putExtra(EXTRA_RETURN_SUBREDDIT_NAME, name); @@ -186,9 +165,6 @@ public class SearchSubredditsResultActivity extends BaseActivity implements Acti protected void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); getSupportFragmentManager().putFragment(outState, FRAGMENT_OUT_STATE, mFragment); - outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken); - outState.putString(ACCESS_TOKEN_STATE, mAccessToken); - outState.putString(ACCOUNT_NAME_STATE, mAccountName); } @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchUsersResultActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchUsersResultActivity.java index 3fa7ab4f..37c470b3 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchUsersResultActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchUsersResultActivity.java @@ -5,7 +5,6 @@ import android.content.Intent; import android.content.SharedPreferences; import android.os.Build; import android.os.Bundle; -import android.os.Handler; import android.view.View; import android.view.Window; import android.view.WindowManager; @@ -21,8 +20,6 @@ import com.r0adkll.slidr.Slidr; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; -import java.util.concurrent.Executor; - import javax.inject.Inject; import javax.inject.Named; @@ -31,8 +28,6 @@ import butterknife.ButterKnife; import ml.docilealligator.infinityforreddit.ActivityToolbarInterface; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; -import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; -import ml.docilealligator.infinityforreddit.asynctasks.GetCurrentAccount; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.events.SwitchAccountEvent; import ml.docilealligator.infinityforreddit.fragments.UserListingFragment; @@ -44,9 +39,6 @@ public class SearchUsersResultActivity extends BaseActivity implements ActivityT static final String EXTRA_RETURN_USER_NAME = "ERUN"; static final String EXTRA_RETURN_USER_ICON_URL = "ERUIU"; - private static final String NULL_ACCESS_TOKEN_STATE = "NATS"; - private static final String ACCESS_TOKEN_STATE = "ATS"; - private static final String ACCOUNT_NAME_STATE = "ANS"; private static final String FRAGMENT_OUT_STATE = "FOS"; @BindView(R.id.coordinator_layout_search_users_result_activity) @@ -57,15 +49,13 @@ public class SearchUsersResultActivity extends BaseActivity implements ActivityT Toolbar toolbar; Fragment mFragment; @Inject - RedditDataRoomDatabase mRedditDataRoomDatabase; - @Inject @Named("default") SharedPreferences mSharedPreferences; @Inject - CustomThemeWrapper mCustomThemeWrapper; + @Named("current_account") + SharedPreferences mCurrentAccountSharedPreferences; @Inject - Executor mExecutor; - private boolean mNullAccessToken = false; + CustomThemeWrapper mCustomThemeWrapper; private String mAccessToken; private String mAccountName; @@ -111,19 +101,21 @@ public class SearchUsersResultActivity extends BaseActivity implements ActivityT String query = getIntent().getExtras().getString(EXTRA_QUERY); + mAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null); + mAccountName = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_NAME, null); + if (savedInstanceState == null) { - getCurrentAccountAndInitializeFragment(query); + mFragment = new UserListingFragment(); + Bundle bundle = new Bundle(); + bundle.putString(UserListingFragment.EXTRA_QUERY, query); + bundle.putBoolean(UserListingFragment.EXTRA_IS_GETTING_USER_INFO, true); + bundle.putString(UserListingFragment.EXTRA_ACCESS_TOKEN, mAccessToken); + bundle.putString(UserListingFragment.EXTRA_ACCOUNT_NAME, mAccountName); + mFragment.setArguments(bundle); + getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_search_users_result_activity, mFragment).commit(); } else { - mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE); - mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE); - mAccountName = savedInstanceState.getString(ACCOUNT_NAME_STATE); - - if (!mNullAccessToken && mAccessToken == null) { - getCurrentAccountAndInitializeFragment(query); - } else { - mFragment = getSupportFragmentManager().getFragment(savedInstanceState, FRAGMENT_OUT_STATE); - getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_search_users_result_activity, mFragment).commit(); - } + mFragment = getSupportFragmentManager().getFragment(savedInstanceState, FRAGMENT_OUT_STATE); + getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_search_users_result_activity, mFragment).commit(); } } @@ -143,26 +135,6 @@ public class SearchUsersResultActivity extends BaseActivity implements ActivityT applyAppBarLayoutAndToolbarTheme(appBarLayout, toolbar); } - private void getCurrentAccountAndInitializeFragment(String query) { - GetCurrentAccount.getCurrentAccount(mExecutor, new Handler(), mRedditDataRoomDatabase, account -> { - if (account == null) { - mNullAccessToken = true; - } else { - mAccessToken = account.getAccessToken(); - mAccountName = account.getAccountName(); - } - - mFragment = new UserListingFragment(); - Bundle bundle = new Bundle(); - bundle.putString(UserListingFragment.EXTRA_QUERY, query); - bundle.putBoolean(UserListingFragment.EXTRA_IS_GETTING_USER_INFO, true); - bundle.putString(UserListingFragment.EXTRA_ACCESS_TOKEN, mAccessToken); - bundle.putString(UserListingFragment.EXTRA_ACCOUNT_NAME, mAccountName); - mFragment.setArguments(bundle); - getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_search_users_result_activity, mFragment).commit(); - }); - } - public void getSelectedUser(String name, String iconUrl) { Intent returnIntent = new Intent(); returnIntent.putExtra(EXTRA_RETURN_USER_NAME, name); @@ -175,9 +147,6 @@ public class SearchUsersResultActivity extends BaseActivity implements ActivityT protected void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); getSupportFragmentManager().putFragment(outState, FRAGMENT_OUT_STATE, mFragment); - outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken); - outState.putString(ACCESS_TOKEN_STATE, mAccessToken); - outState.putString(ACCOUNT_NAME_STATE, mAccountName); } @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SelectUserFlairActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SelectUserFlairActivity.java index 793029b8..6da07407 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SelectUserFlairActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SelectUserFlairActivity.java @@ -4,7 +4,6 @@ import android.content.Context; import android.content.SharedPreferences; import android.os.Build; import android.os.Bundle; -import android.os.Handler; import android.view.MenuItem; import android.view.View; import android.view.inputmethod.InputMethodManager; @@ -38,7 +37,6 @@ import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.SelectUserFlair; import ml.docilealligator.infinityforreddit.UserFlair; import ml.docilealligator.infinityforreddit.adapters.UserFlairRecyclerViewAdapter; -import ml.docilealligator.infinityforreddit.asynctasks.GetCurrentAccount; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; import retrofit2.Retrofit; @@ -46,9 +44,6 @@ import retrofit2.Retrofit; public class SelectUserFlairActivity extends BaseActivity implements ActivityToolbarInterface { public static final String EXTRA_SUBREDDIT_NAME = "ESN"; - private static final String NULL_ACCESS_TOKEN_STATE = "NATS"; - private static final String ACCESS_TOKEN_STATE = "ATS"; - private static final String ACCOUNT_NAME_STATE = "ANS"; private static final String USER_FLAIRS_STATE = "UFS"; @BindView(R.id.coordinator_layout_select_user_flair_activity) @@ -68,6 +63,9 @@ public class SelectUserFlairActivity extends BaseActivity implements ActivityToo @Named("default") SharedPreferences mSharedPreferences; @Inject + @Named("current_account") + SharedPreferences mCurrentAccountSharedPreferences; + @Inject CustomThemeWrapper mCustomThemeWrapper; @Inject Executor mExecutor; @@ -107,32 +105,13 @@ public class SelectUserFlairActivity extends BaseActivity implements ActivityToo mSubredditName = getIntent().getStringExtra(EXTRA_SUBREDDIT_NAME); setTitle(mSubredditName); + mAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null); + mAccountName = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_NAME, null); + if (savedInstanceState != null) { - mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE); - mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE); - mAccountName = savedInstanceState.getString(ACCOUNT_NAME_STATE); mUserFlairs = savedInstanceState.getParcelableArrayList(USER_FLAIRS_STATE); - - if (!mNullAccessToken && mAccessToken == null) { - getCurrentAccountAndBindView(); - } else { - bindView(); - } - } else { - getCurrentAccountAndBindView(); } - } - - private void getCurrentAccountAndBindView() { - GetCurrentAccount.getCurrentAccount(mExecutor, new Handler(), mRedditDataRoomDatabase, account -> { - if (account == null) { - mNullAccessToken = true; - } else { - mAccessToken = account.getAccessToken(); - mAccountName = account.getAccountName(); - } - bindView(); - }); + bindView(); } private void bindView() { @@ -234,9 +213,6 @@ public class SelectUserFlairActivity extends BaseActivity implements ActivityToo @Override protected void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); - outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken); - outState.putString(ACCESS_TOKEN_STATE, mAccessToken); - outState.putString(ACCOUNT_NAME_STATE, mAccountName); outState.putParcelableArrayList(USER_FLAIRS_STATE, mUserFlairs); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SendPrivateMessageActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SendPrivateMessageActivity.java index 57744487..61d44645 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SendPrivateMessageActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SendPrivateMessageActivity.java @@ -3,7 +3,6 @@ package ml.docilealligator.infinityforreddit.activities; import android.content.SharedPreferences; import android.os.Build; import android.os.Bundle; -import android.os.Handler; import android.view.Menu; import android.view.MenuItem; import android.view.View; @@ -27,15 +26,13 @@ import butterknife.ButterKnife; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; -import ml.docilealligator.infinityforreddit.asynctasks.GetCurrentAccount; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.message.ComposeMessage; +import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; import retrofit2.Retrofit; public class SendPrivateMessageActivity extends BaseActivity { public static final String EXTRA_RECIPIENT_USERNAME = "ERU"; - private static final String NULL_ACCESS_TOKEN_STATE = "NATS"; - private static final String ACCESS_TOKEN_STATE = "ATS"; @BindView(R.id.coordinator_layout_send_private_message_activity) CoordinatorLayout coordinatorLayout; @BindView(R.id.appbar_layout_send_private_message_activity) @@ -61,10 +58,12 @@ public class SendPrivateMessageActivity extends BaseActivity { @Named("default") SharedPreferences mSharedPreferences; @Inject + @Named("current_account") + SharedPreferences mCurrentAccountSharedPreferences; + @Inject CustomThemeWrapper mCustomThemeWrapper; @Inject Executor mExecutor; - private boolean mNullAccessToken = false; private String mAccessToken; private boolean isSubmitting = false; @@ -85,15 +84,7 @@ public class SendPrivateMessageActivity extends BaseActivity { addOnOffsetChangedListener(appBarLayout); } - if (savedInstanceState == null) { - getCurrentAccount(); - } else { - mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE); - mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE); - if (!mNullAccessToken && mAccessToken == null) { - getCurrentAccount(); - } - } + mAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null); setSupportActionBar(toolbar); @@ -103,16 +94,6 @@ public class SendPrivateMessageActivity extends BaseActivity { } } - private void getCurrentAccount() { - GetCurrentAccount.getCurrentAccount(mExecutor, new Handler(), mRedditDataRoomDatabase, account -> { - if (account == null) { - mNullAccessToken = true; - } else { - mAccessToken = account.getAccessToken(); - } - }); - } - @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.send_private_message_activity, menu); @@ -185,8 +166,6 @@ public class SendPrivateMessageActivity extends BaseActivity { @Override protected void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); - outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken); - outState.putString(ACCESS_TOKEN_STATE, mAccessToken); } @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SettingsActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SettingsActivity.java index 3c1d8c3f..0831bf44 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SettingsActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SettingsActivity.java @@ -3,7 +3,6 @@ package ml.docilealligator.infinityforreddit.activities; import android.content.SharedPreferences; import android.os.Build; import android.os.Bundle; -import android.os.Handler; import android.view.MenuItem; import androidx.annotation.NonNull; @@ -28,7 +27,6 @@ import butterknife.ButterKnife; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; -import ml.docilealligator.infinityforreddit.asynctasks.GetCurrentAccount; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.events.RecreateActivityEvent; import ml.docilealligator.infinityforreddit.settings.AboutPreferenceFragment; @@ -40,6 +38,7 @@ import ml.docilealligator.infinityforreddit.settings.InterfacePreferenceFragment import ml.docilealligator.infinityforreddit.settings.MainPreferenceFragment; import ml.docilealligator.infinityforreddit.settings.NsfwAndBlurringFragment; import ml.docilealligator.infinityforreddit.settings.PostHistoryFragment; +import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; public class SettingsActivity extends BaseActivity implements PreferenceFragmentCompat.OnPreferenceStartFragmentCallback { @@ -50,13 +49,15 @@ public class SettingsActivity extends BaseActivity implements AppBarLayout appBarLayout; @BindView(R.id.toolbar_settings_activity) Toolbar toolbar; - private boolean mNullAccountName; private String mAccountName; @Inject @Named("default") SharedPreferences mSharedPreferences; @Inject + @Named("current_account") + SharedPreferences mCurrentAccountSharedPreferences; + @Inject RedditDataRoomDatabase mRedditDataRoomDatabase; @Inject CustomThemeWrapper mCustomThemeWrapper; @@ -85,7 +86,16 @@ public class SettingsActivity extends BaseActivity implements setSupportActionBar(toolbar); - getCurrentAccountAndBindView(savedInstanceState); + mAccountName = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_NAME, null); + + if (savedInstanceState == null) { + getSupportFragmentManager() + .beginTransaction() + .replace(R.id.frame_layout_settings_activity, new MainPreferenceFragment()) + .commit(); + } else { + setTitle(savedInstanceState.getCharSequence(TITLE_STATE)); + } getSupportFragmentManager().addOnBackStackChangedListener(() -> { if (getSupportFragmentManager().getBackStackEntryCount() == 0) { @@ -105,28 +115,6 @@ public class SettingsActivity extends BaseActivity implements }); } - private void getCurrentAccountAndBindView(Bundle savedInstanceState) { - GetCurrentAccount.getCurrentAccount(mExecutor, new Handler(), mRedditDataRoomDatabase, account -> { - if (getSupportFragmentManager().isDestroyed()) { - return; - } - if (account == null) { - mNullAccountName = true; - } else { - mAccountName = account.getAccountName(); - } - - if (savedInstanceState == null) { - getSupportFragmentManager() - .beginTransaction() - .replace(R.id.frame_layout_settings_activity, new MainPreferenceFragment()) - .commit(); - } else { - setTitle(savedInstanceState.getCharSequence(TITLE_STATE)); - } - }); - } - @Override public SharedPreferences getDefaultSharedPreferences() { return mSharedPreferences; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubmitCrosspostActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubmitCrosspostActivity.java index 937a1b54..366edda0 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubmitCrosspostActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubmitCrosspostActivity.java @@ -8,7 +8,6 @@ import android.graphics.Bitmap; import android.graphics.drawable.Drawable; import android.os.Build; import android.os.Bundle; -import android.os.Handler; import android.view.Menu; import android.view.MenuItem; import android.view.View; @@ -52,7 +51,6 @@ import ml.docilealligator.infinityforreddit.Flair; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; -import ml.docilealligator.infinityforreddit.asynctasks.GetCurrentAccount; import ml.docilealligator.infinityforreddit.asynctasks.LoadSubredditIconAsyncTask; import ml.docilealligator.infinityforreddit.bottomsheetfragments.FlairBottomSheetFragment; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; @@ -61,6 +59,7 @@ import ml.docilealligator.infinityforreddit.events.SwitchAccountEvent; import ml.docilealligator.infinityforreddit.post.Post; import ml.docilealligator.infinityforreddit.services.SubmitPostService; import ml.docilealligator.infinityforreddit.utils.APIUtils; +import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; import pl.droidsonroids.gif.GifImageView; import retrofit2.Retrofit; @@ -77,8 +76,6 @@ public class SubmitCrosspostActivity extends BaseActivity implements FlairBottom private static final String FLAIR_STATE = "FS"; private static final String IS_SPOILER_STATE = "ISS"; private static final String IS_NSFW_STATE = "INS"; - private static final String NULL_ACCESS_TOKEN_STATE = "NATS"; - private static final String ACCESS_TOKEN_STATE = "ATS"; private static final int SUBREDDIT_SELECTION_REQUEST_CODE = 0; @@ -128,10 +125,12 @@ public class SubmitCrosspostActivity extends BaseActivity implements FlairBottom @Named("default") SharedPreferences mSharedPreferences; @Inject + @Named("current_account") + SharedPreferences mCurrentAccountSharedPreferences; + @Inject CustomThemeWrapper mCustomThemeWrapper; @Inject Executor mExecutor; - private boolean mNullAccessToken = false; private String mAccessToken; private Post post; private String iconUrl; @@ -186,14 +185,9 @@ public class SubmitCrosspostActivity extends BaseActivity implements FlairBottom post = getIntent().getParcelableExtra(EXTRA_POST); - if (savedInstanceState != null) { - mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE); - mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE); - - if (!mNullAccessToken && mAccessToken == null) { - getCurrentAccount(); - } + mAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null); + if (savedInstanceState != null) { subredditName = savedInstanceState.getString(SUBREDDIT_NAME_STATE); iconUrl = savedInstanceState.getString(SUBREDDIT_ICON_STATE); subredditSelected = savedInstanceState.getBoolean(SUBREDDIT_SELECTED_STATE); @@ -235,8 +229,6 @@ public class SubmitCrosspostActivity extends BaseActivity implements FlairBottom nsfwTextView.setTextColor(nsfwTextColor); } } else { - getCurrentAccount(); - isPosting = false; mGlide.load(R.drawable.subreddit_default_icon) @@ -417,16 +409,6 @@ public class SubmitCrosspostActivity extends BaseActivity implements FlairBottom contentTextView.setHintTextColor(secondaryTextColor); } - private void getCurrentAccount() { - GetCurrentAccount.getCurrentAccount(mExecutor, new Handler(), mRedditDataRoomDatabase, account -> { - if (account == null) { - mNullAccessToken = true; - } else { - mAccessToken = account.getAccessToken(); - } - }); - } - private void displaySubredditIcon() { if (iconUrl != null && !iconUrl.equals("")) { mGlide.load(iconUrl) @@ -558,8 +540,6 @@ public class SubmitCrosspostActivity extends BaseActivity implements FlairBottom outState.putParcelable(FLAIR_STATE, flair); outState.putBoolean(IS_SPOILER_STATE, isSpoiler); outState.putBoolean(IS_NSFW_STATE, isNSFW); - outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken); - outState.putString(ACCESS_TOKEN_STATE, mAccessToken); } @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubredditMultiselectionActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubredditMultiselectionActivity.java index 24b2b47e..32f6f4a0 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubredditMultiselectionActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubredditMultiselectionActivity.java @@ -4,7 +4,6 @@ import android.content.Intent; import android.content.SharedPreferences; import android.os.Build; import android.os.Bundle; -import android.os.Handler; import android.view.Menu; import android.view.MenuItem; import android.view.View; @@ -42,7 +41,6 @@ import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.adapters.SubredditMultiselectionRecyclerViewAdapter; -import ml.docilealligator.infinityforreddit.asynctasks.GetCurrentAccount; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.subscribedsubreddit.SubscribedSubredditViewModel; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; @@ -53,9 +51,6 @@ public class SubredditMultiselectionActivity extends BaseActivity implements Act static final String EXTRA_RETURN_SELECTED_SUBREDDITS = "ERSS"; private static final int SUBREDDIT_SEARCH_REQUEST_CODE = 1; - private static final String NULL_ACCESS_TOKEN_STATE = "NATS"; - private static final String ACCESS_TOKEN_STATE = "ATS"; - private static final String ACCOUNT_NAME_STATE = "ANS"; @BindView(R.id.coordinator_layout_subreddits_multiselection_activity) CoordinatorLayout mCoordinatorLayout; @@ -82,11 +77,13 @@ public class SubredditMultiselectionActivity extends BaseActivity implements Act @Named("default") SharedPreferences mSharedPreferences; @Inject + @Named("current_account") + SharedPreferences mCurrentAccountSharedPreferences; + @Inject CustomThemeWrapper mCustomThemeWrapper; @Inject Executor mExecutor; public SubscribedSubredditViewModel mSubscribedSubredditViewModel; - private boolean mNullAccessToken = false; private String mAccessToken; private String mAccountName; private LinearLayoutManager mLinearLayoutManager; @@ -138,33 +135,13 @@ public class SubredditMultiselectionActivity extends BaseActivity implements Act mSwipeRefreshLayout.setEnabled(false); - if (savedInstanceState != null) { - mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE); - mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE); - mAccountName = savedInstanceState.getString(ACCOUNT_NAME_STATE); - - if (!mNullAccessToken && mAccountName == null) { - getCurrentAccountAndBindView(); - } else { - bindView(); - } - } else { - getCurrentAccountAndBindView(); + mAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null); + mAccountName = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_NAME, null); + if (mAccessToken == null) { + Toast.makeText(this, R.string.logged_out, Toast.LENGTH_SHORT).show(); + finish(); } - } - - private void getCurrentAccountAndBindView() { - GetCurrentAccount.getCurrentAccount(mExecutor, new Handler(), mRedditDataRoomDatabase, account -> { - if (account == null) { - mNullAccessToken = true; - Toast.makeText(this, R.string.logged_out, Toast.LENGTH_SHORT).show(); - finish(); - } else { - mAccessToken = account.getAccessToken(); - mAccountName = account.getAccountName(); - bindView(); - } - }); + bindView(); } private void bindView() { @@ -239,9 +216,6 @@ public class SubredditMultiselectionActivity extends BaseActivity implements Act @Override protected void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); - outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken); - outState.putString(ACCESS_TOKEN_STATE, mAccessToken); - outState.putString(ACCOUNT_NAME_STATE, mAccountName); } @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubredditSelectionActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubredditSelectionActivity.java index 3aa99783..a28b8903 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubredditSelectionActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubredditSelectionActivity.java @@ -5,7 +5,6 @@ import android.content.Intent; import android.content.SharedPreferences; import android.os.Build; import android.os.Bundle; -import android.os.Handler; import android.view.Menu; import android.view.MenuItem; import android.view.View; @@ -37,7 +36,6 @@ import ml.docilealligator.infinityforreddit.FetchSubscribedThing; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; -import ml.docilealligator.infinityforreddit.asynctasks.GetCurrentAccount; import ml.docilealligator.infinityforreddit.asynctasks.InsertSubscribedThingsAsyncTask; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.events.SwitchAccountEvent; @@ -57,10 +55,6 @@ public class SubredditSelectionActivity extends BaseActivity implements Activity private static final int SUBREDDIT_SEARCH_REQUEST_CODE = 0; private static final String INSERT_SUBSCRIBED_SUBREDDIT_STATE = "ISSS"; - private static final String NULL_ACCESS_TOKEN_STATE = "NATS"; - private static final String ACCESS_TOKEN_STATE = "ATS"; - private static final String ACCOUNT_NAME_STATE = "ANS"; - private static final String ACCOUNT_PROFILE_IMAGE_URL = "APIU"; private static final String FRAGMENT_OUT_STATE = "FOS"; @BindView(R.id.coordinator_layout_subreddit_selection_activity) @@ -78,10 +72,12 @@ public class SubredditSelectionActivity extends BaseActivity implements Activity @Named("default") SharedPreferences mSharedPreferences; @Inject + @Named("current_account") + SharedPreferences mCurrentAccountSharedPreferences; + @Inject CustomThemeWrapper mCustomThemeWrapper; @Inject Executor mExecutor; - private boolean mNullAccessToken = false; private String mAccessToken; private String mAccountName; private String mAccountProfileImageUrl; @@ -128,22 +124,18 @@ public class SubredditSelectionActivity extends BaseActivity implements Activity setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); + mAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null); + mAccountName = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_NAME, null); + mAccountProfileImageUrl = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_IMAGE_URL, null); + if (savedInstanceState == null) { - getCurrentAccountAndBindView(); + bindView(true); } else { mInsertSuccess = savedInstanceState.getBoolean(INSERT_SUBSCRIBED_SUBREDDIT_STATE); - mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE); - mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE); - mAccountName = savedInstanceState.getString(ACCOUNT_NAME_STATE); - mAccountProfileImageUrl = savedInstanceState.getString(ACCOUNT_PROFILE_IMAGE_URL); - - if (!mNullAccessToken && mAccountName == null) { - getCurrentAccountAndBindView(); - } else { - mFragment = getSupportFragmentManager().getFragment(savedInstanceState, FRAGMENT_OUT_STATE); - getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_subreddit_selection_activity, mFragment).commit(); - bindView(false); - } + + mFragment = getSupportFragmentManager().getFragment(savedInstanceState, FRAGMENT_OUT_STATE); + getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_subreddit_selection_activity, mFragment).commit(); + bindView(false); } } @@ -163,19 +155,6 @@ public class SubredditSelectionActivity extends BaseActivity implements Activity applyAppBarLayoutAndToolbarTheme(appBarLayout, toolbar); } - private void getCurrentAccountAndBindView() { - GetCurrentAccount.getCurrentAccount(mExecutor, new Handler(), mRedditDataRoomDatabase, account -> { - if (account == null) { - mNullAccessToken = true; - } else { - mAccessToken = account.getAccessToken(); - mAccountName = account.getAccountName(); - mAccountProfileImageUrl = account.getProfileImageUrl(); - } - bindView(true); - }); - } - private void bindView(boolean initializeFragment) { if (isFinishing() || isDestroyed()) { return; @@ -279,10 +258,6 @@ public class SubredditSelectionActivity extends BaseActivity implements Activity super.onSaveInstanceState(outState); getSupportFragmentManager().putFragment(outState, FRAGMENT_OUT_STATE, mFragment); outState.putBoolean(INSERT_SUBSCRIBED_SUBREDDIT_STATE, mInsertSuccess); - outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken); - outState.putString(ACCESS_TOKEN_STATE, mAccessToken); - outState.putString(ACCOUNT_NAME_STATE, mAccountName); - outState.putString(ACCOUNT_PROFILE_IMAGE_URL, mAccountProfileImageUrl); } @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubscribedThingListingActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubscribedThingListingActivity.java index 18f4c590..775e434c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubscribedThingListingActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubscribedThingListingActivity.java @@ -4,7 +4,6 @@ import android.content.Intent; import android.content.SharedPreferences; import android.os.Build; import android.os.Bundle; -import android.os.Handler; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; @@ -44,7 +43,6 @@ import ml.docilealligator.infinityforreddit.FragmentCommunicator; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; -import ml.docilealligator.infinityforreddit.asynctasks.GetCurrentAccount; import ml.docilealligator.infinityforreddit.asynctasks.InsertMultiRedditAsyncTask; import ml.docilealligator.infinityforreddit.asynctasks.InsertSubscribedThingsAsyncTask; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; @@ -68,9 +66,6 @@ public class SubscribedThingListingActivity extends BaseActivity implements Acti public static final String EXTRA_SHOW_MULTIREDDITS = "ESM"; private static final String INSERT_SUBSCRIBED_SUBREDDIT_STATE = "ISSS"; private static final String INSERT_MULTIREDDIT_STATE = "IMS"; - private static final String NULL_ACCESS_TOKEN_STATE = "NATS"; - private static final String ACCESS_TOKEN_STATE = "ATS"; - private static final String ACCOUNT_NAME_STATE = "ANS"; @BindView(R.id.coordinator_layout_subscribed_thing_listing_activity) CoordinatorLayout coordinatorLayout; @@ -93,11 +88,13 @@ public class SubscribedThingListingActivity extends BaseActivity implements Acti @Named("default") SharedPreferences mSharedPreferences; @Inject + @Named("current_account") + SharedPreferences mCurrentAccountSharedPreferences; + @Inject CustomThemeWrapper mCustomThemeWrapper; @Inject Executor mExecutor; private SlidrInterface mSlidrInterface; - private boolean mNullAccessToken = false; private String mAccessToken; private String mAccountName; private boolean mInsertSuccess = false; @@ -153,21 +150,16 @@ public class SubscribedThingListingActivity extends BaseActivity implements Acti getSupportActionBar().setDisplayHomeAsUpEnabled(true); setToolbarGoToTop(toolbar); + mAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null); + mAccountName = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_NAME, null); + if (savedInstanceState != null) { mInsertSuccess = savedInstanceState.getBoolean(INSERT_SUBSCRIBED_SUBREDDIT_STATE); mInsertMultiredditSuccess = savedInstanceState.getBoolean(INSERT_MULTIREDDIT_STATE); - mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE); - mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE); - mAccountName = savedInstanceState.getString(ACCOUNT_NAME_STATE); - if (!mNullAccessToken && mAccessToken == null) { - getCurrentAccountAndInitializeViewPager(); - } else { - initializeViewPagerAndLoadSubscriptions(); - } } else { showMultiReddits = getIntent().getBooleanExtra(EXTRA_SHOW_MULTIREDDITS, false); - getCurrentAccountAndInitializeViewPager(); } + initializeViewPagerAndLoadSubscriptions(); } @Override @@ -188,18 +180,6 @@ public class SubscribedThingListingActivity extends BaseActivity implements Acti applyFABTheme(fab); } - private void getCurrentAccountAndInitializeViewPager() { - GetCurrentAccount.getCurrentAccount(mExecutor, new Handler(), mRedditDataRoomDatabase, account -> { - if (account == null) { - mNullAccessToken = true; - } else { - mAccessToken = account.getAccessToken(); - mAccountName = account.getAccountName(); - } - initializeViewPagerAndLoadSubscriptions(); - }); - } - private void initializeViewPagerAndLoadSubscriptions() { fab.setOnClickListener(view -> { Intent intent = new Intent(this, CreateMultiRedditActivity.class); @@ -248,9 +228,6 @@ public class SubscribedThingListingActivity extends BaseActivity implements Acti super.onSaveInstanceState(outState); outState.putBoolean(INSERT_SUBSCRIBED_SUBREDDIT_STATE, mInsertSuccess); outState.putBoolean(INSERT_MULTIREDDIT_STATE, mInsertMultiredditSuccess); - outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken); - outState.putString(ACCESS_TOKEN_STATE, mAccessToken); - outState.putString(ACCOUNT_NAME_STATE, mAccountName); } @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewMultiRedditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewMultiRedditDetailActivity.java index 93521a70..e1bdf079 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewMultiRedditDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewMultiRedditDetailActivity.java @@ -4,7 +4,6 @@ import android.content.Intent; import android.content.SharedPreferences; import android.os.Build; import android.os.Bundle; -import android.os.Handler; import android.view.Menu; import android.view.MenuItem; import android.view.View; @@ -39,7 +38,6 @@ import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.SortType; import ml.docilealligator.infinityforreddit.SortTypeSelectionCallback; -import ml.docilealligator.infinityforreddit.asynctasks.GetCurrentAccount; import ml.docilealligator.infinityforreddit.bottomsheetfragments.PostLayoutBottomSheetFragment; import ml.docilealligator.infinityforreddit.bottomsheetfragments.SortTimeBottomSheetFragment; import ml.docilealligator.infinityforreddit.bottomsheetfragments.SortTypeBottomSheetFragment; @@ -63,9 +61,6 @@ public class ViewMultiRedditDetailActivity extends BaseActivity implements SortT private static final String FRAGMENT_OUT_STATE_KEY = "FOSK"; private static final String IS_IN_LAZY_MODE_STATE = "IILMS"; - private static final String NULL_ACCESS_TOKEN_STATE = "NATS"; - private static final String ACCESS_TOKEN_STATE = "ATS"; - private static final String ACCOUNT_NAME_STATE = "ANS"; @BindView(R.id.coordinator_layout_view_multi_reddit_detail_activity) CoordinatorLayout coordinatorLayout; @@ -93,10 +88,12 @@ public class ViewMultiRedditDetailActivity extends BaseActivity implements SortT @Named("post_layout") SharedPreferences mPostLayoutSharedPreferences; @Inject + @Named("current_account") + SharedPreferences mCurrentAccountSharedPreferences; + @Inject CustomThemeWrapper mCustomThemeWrapper; @Inject Executor mExecutor; - private boolean mNullAccessToken = false; private String mAccessToken; private String mAccountName; private String multiPath; @@ -160,20 +157,16 @@ public class ViewMultiRedditDetailActivity extends BaseActivity implements SortT getSupportActionBar().setDisplayHomeAsUpEnabled(true); setToolbarGoToTop(toolbar); + mAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null); + mAccountName = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_NAME, null); + if (savedInstanceState != null) { - mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE); - mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE); - mAccountName = savedInstanceState.getString(ACCOUNT_NAME_STATE); isInLazyMode = savedInstanceState.getBoolean(IS_IN_LAZY_MODE_STATE); - if (!mNullAccessToken && mAccessToken == null) { - getCurrentAccountAndInitializeFragment(); - } else { - mFragment = getSupportFragmentManager().getFragment(savedInstanceState, FRAGMENT_OUT_STATE_KEY); - getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_view_multi_reddit_detail_activity, mFragment).commit(); - } + mFragment = getSupportFragmentManager().getFragment(savedInstanceState, FRAGMENT_OUT_STATE_KEY); + getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_view_multi_reddit_detail_activity, mFragment).commit(); } else { - getCurrentAccountAndInitializeFragment(); + initializeFragment(); } sortTypeBottomSheetFragment = new SortTypeBottomSheetFragment(); @@ -188,18 +181,6 @@ public class ViewMultiRedditDetailActivity extends BaseActivity implements SortT params = (AppBarLayout.LayoutParams) collapsingToolbarLayout.getLayoutParams(); } - private void getCurrentAccountAndInitializeFragment() { - GetCurrentAccount.getCurrentAccount(mExecutor, new Handler(), mRedditDataRoomDatabase, account -> { - if (account == null) { - mNullAccessToken = true; - } else { - mAccessToken = account.getAccessToken(); - mAccountName = account.getAccountName(); - } - initializeFragment(); - }); - } - private void initializeFragment() { mFragment = new PostFragment(); Bundle bundle = new Bundle(); @@ -231,77 +212,77 @@ public class ViewMultiRedditDetailActivity extends BaseActivity implements SortT @Override public boolean onOptionsItemSelected(@NonNull MenuItem item) { - switch (item.getItemId()) { - case android.R.id.home: - finish(); - return true; - case R.id.action_sort_view_multi_reddit_detail_activity: - sortTypeBottomSheetFragment.show(getSupportFragmentManager(), sortTypeBottomSheetFragment.getTag()); - return true; - case R.id.action_search_view_multi_reddit_detail_activity: - Intent intent = new Intent(this, SearchActivity.class); - startActivity(intent); - return true; - case R.id.action_refresh_view_multi_reddit_detail_activity: - if (mMenu != null) { - mMenu.findItem(R.id.action_lazy_mode_view_multi_reddit_detail_activity).setTitle(R.string.action_start_lazy_mode); - } - if (mFragment instanceof FragmentCommunicator) { - ((FragmentCommunicator) mFragment).refresh(); - } - return true; - case R.id.action_lazy_mode_view_multi_reddit_detail_activity: - MenuItem lazyModeItem = mMenu.findItem(R.id.action_lazy_mode_view_multi_reddit_detail_activity); - if (isInLazyMode) { - isInLazyMode = false; - ((FragmentCommunicator) mFragment).stopLazyMode(); - lazyModeItem.setTitle(R.string.action_start_lazy_mode); - params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS); + int itemId = item.getItemId(); + if (itemId == android.R.id.home) { + finish(); + return true; + } else if (itemId == R.id.action_sort_view_multi_reddit_detail_activity) { + sortTypeBottomSheetFragment.show(getSupportFragmentManager(), sortTypeBottomSheetFragment.getTag()); + return true; + } else if (itemId == R.id.action_search_view_multi_reddit_detail_activity) { + Intent intent = new Intent(this, SearchActivity.class); + startActivity(intent); + return true; + } else if (itemId == R.id.action_refresh_view_multi_reddit_detail_activity) { + if (mMenu != null) { + mMenu.findItem(R.id.action_lazy_mode_view_multi_reddit_detail_activity).setTitle(R.string.action_start_lazy_mode); + } + if (mFragment instanceof FragmentCommunicator) { + ((FragmentCommunicator) mFragment).refresh(); + } + return true; + } else if (itemId == R.id.action_lazy_mode_view_multi_reddit_detail_activity) { + MenuItem lazyModeItem = mMenu.findItem(R.id.action_lazy_mode_view_multi_reddit_detail_activity); + if (isInLazyMode) { + isInLazyMode = false; + ((FragmentCommunicator) mFragment).stopLazyMode(); + lazyModeItem.setTitle(R.string.action_start_lazy_mode); + params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS); + collapsingToolbarLayout.setLayoutParams(params); + } else { + isInLazyMode = true; + if (((FragmentCommunicator) mFragment).startLazyMode()) { + lazyModeItem.setTitle(R.string.action_stop_lazy_mode); + appBarLayout.setExpanded(false); + params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_NO_SCROLL); collapsingToolbarLayout.setLayoutParams(params); } else { - isInLazyMode = true; - if (((FragmentCommunicator) mFragment).startLazyMode()) { - lazyModeItem.setTitle(R.string.action_stop_lazy_mode); - appBarLayout.setExpanded(false); - params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_NO_SCROLL); - collapsingToolbarLayout.setLayoutParams(params); - } else { - isInLazyMode = false; - } + isInLazyMode = false; } - return true; - case R.id.action_change_post_layout_view_multi_reddit_detail_activity: - postLayoutBottomSheetFragment.show(getSupportFragmentManager(), postLayoutBottomSheetFragment.getTag()); - return true; - case R.id.action_edit_view_multi_reddit_detail_activity: - Intent editIntent = new Intent(this, EditMultiRedditActivity.class); - editIntent.putExtra(EditMultiRedditActivity.EXTRA_MULTI_PATH, multiPath); - startActivity(editIntent); - return true; - case R.id.action_delete_view_multi_reddit_detail_activity: - new MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialogTheme) - .setTitle(R.string.delete) - .setMessage(R.string.delete_multi_reddit_dialog_message) - .setPositiveButton(R.string.delete, (dialogInterface, i) - -> DeleteMultiReddit.deleteMultiReddit(mOauthRetrofit, mRedditDataRoomDatabase, - mAccessToken, mAccountName, multiPath, new DeleteMultiReddit.DeleteMultiRedditListener() { - @Override - public void success() { - Toast.makeText(ViewMultiRedditDetailActivity.this, - R.string.delete_multi_reddit_success, Toast.LENGTH_SHORT).show(); - EventBus.getDefault().post(new RefreshMultiRedditsEvent()); - finish(); - } - - @Override - public void failed() { - Toast.makeText(ViewMultiRedditDetailActivity.this, - R.string.delete_multi_reddit_failed, Toast.LENGTH_SHORT).show(); - } - })) - .setNegativeButton(R.string.cancel, null) - .show(); - return true; + } + return true; + } else if (itemId == R.id.action_change_post_layout_view_multi_reddit_detail_activity) { + postLayoutBottomSheetFragment.show(getSupportFragmentManager(), postLayoutBottomSheetFragment.getTag()); + return true; + } else if (itemId == R.id.action_edit_view_multi_reddit_detail_activity) { + Intent editIntent = new Intent(this, EditMultiRedditActivity.class); + editIntent.putExtra(EditMultiRedditActivity.EXTRA_MULTI_PATH, multiPath); + startActivity(editIntent); + return true; + } else if (itemId == R.id.action_delete_view_multi_reddit_detail_activity) { + new MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialogTheme) + .setTitle(R.string.delete) + .setMessage(R.string.delete_multi_reddit_dialog_message) + .setPositiveButton(R.string.delete, (dialogInterface, i) + -> DeleteMultiReddit.deleteMultiReddit(mOauthRetrofit, mRedditDataRoomDatabase, + mAccessToken, mAccountName, multiPath, new DeleteMultiReddit.DeleteMultiRedditListener() { + @Override + public void success() { + Toast.makeText(ViewMultiRedditDetailActivity.this, + R.string.delete_multi_reddit_success, Toast.LENGTH_SHORT).show(); + EventBus.getDefault().post(new RefreshMultiRedditsEvent()); + finish(); + } + + @Override + public void failed() { + Toast.makeText(ViewMultiRedditDetailActivity.this, + R.string.delete_multi_reddit_failed, Toast.LENGTH_SHORT).show(); + } + })) + .setNegativeButton(R.string.cancel, null) + .show(); + return true; } return false; } @@ -310,9 +291,6 @@ public class ViewMultiRedditDetailActivity extends BaseActivity implements SortT protected void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); outState.putBoolean(IS_IN_LAZY_MODE_STATE, isInLazyMode); - outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken); - outState.putString(ACCESS_TOKEN_STATE, mAccessToken); - outState.putString(ACCOUNT_NAME_STATE, mAccountName); getSupportFragmentManager().putFragment(outState, FRAGMENT_OUT_STATE_KEY, mFragment); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPostDetailActivity.java index af881091..670a2a1e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPostDetailActivity.java @@ -77,7 +77,6 @@ import ml.docilealligator.infinityforreddit.SortType; import ml.docilealligator.infinityforreddit.SortTypeSelectionCallback; import ml.docilealligator.infinityforreddit.adapters.CommentAndPostRecyclerViewAdapter; import ml.docilealligator.infinityforreddit.apis.RedditAPI; -import ml.docilealligator.infinityforreddit.asynctasks.GetCurrentAccount; import ml.docilealligator.infinityforreddit.asynctasks.SwitchAccount; import ml.docilealligator.infinityforreddit.bottomsheetfragments.FlairBottomSheetFragment; import ml.docilealligator.infinityforreddit.bottomsheetfragments.PostCommentSortTypeBottomSheetFragment; @@ -126,9 +125,6 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS public static final int EDIT_COMMENT_REQUEST_CODE = 3; public static final int GIVE_AWARD_REQUEST_CODE = 100; private static final int EDIT_POST_REQUEST_CODE = 2; - @State - boolean mNullAccessToken = false; - @State String mAccessToken; @State String mAccountName; @@ -528,11 +524,10 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS orientation = resources.getConfiguration().orientation; - if (!mNullAccessToken && mAccessToken == null) { - getCurrentAccountAndBindView(); - } else { - bindView(); - } + mAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null); + mAccountName = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_NAME, null); + + checkNewAccountAndBindView(); if (getIntent().hasExtra(EXTRA_POST_LIST_POSITION)) { postListPosition = getIntent().getIntExtra(EXTRA_POST_LIST_POSITION, -1); @@ -595,41 +590,28 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS applyFABTheme(fab); } - private void getCurrentAccountAndBindView() { - GetCurrentAccount.getCurrentAccount(mExecutor, new Handler(), mRedditDataRoomDatabase, account -> { - if (mNewAccountName != null) { - if (account == null || !account.getAccountName().equals(mNewAccountName)) { - SwitchAccount.switchAccount(mRedditDataRoomDatabase, mCurrentAccountSharedPreferences, - mExecutor, new Handler(), mNewAccountName, newAccount -> { - EventBus.getDefault().post(new SwitchAccountEvent(getClass().getName())); - Toast.makeText(this, R.string.account_switched, Toast.LENGTH_SHORT).show(); - - mNewAccountName = null; - if (newAccount == null) { - mNullAccessToken = true; - } else { - mAccessToken = newAccount.getAccessToken(); - mAccountName = newAccount.getAccountName(); - } + private void checkNewAccountAndBindView() { + if (mNewAccountName != null) { + if (mAccountName == null || !mAccountName.equals(mNewAccountName)) { + SwitchAccount.switchAccount(mRedditDataRoomDatabase, mCurrentAccountSharedPreferences, + mExecutor, new Handler(), mNewAccountName, newAccount -> { + EventBus.getDefault().post(new SwitchAccountEvent(getClass().getName())); + Toast.makeText(this, R.string.account_switched, Toast.LENGTH_SHORT).show(); + + mNewAccountName = null; + if (newAccount != null) { + mAccessToken = newAccount.getAccessToken(); + mAccountName = newAccount.getAccountName(); + } - bindView(); - }); - } else { - mAccessToken = account.getAccessToken(); - mAccountName = account.getAccountName(); - bindView(); - } + bindView(); + }); } else { - if (account == null) { - mNullAccessToken = true; - } else { - mAccessToken = account.getAccessToken(); - mAccountName = account.getAccountName(); - } - bindView(); } - }); + } else { + bindView(); + } } private void bindView() { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPrivateMessagesActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPrivateMessagesActivity.java index 81d97949..6867125b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPrivateMessagesActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPrivateMessagesActivity.java @@ -4,7 +4,6 @@ import android.content.Intent; import android.content.SharedPreferences; import android.os.Build; import android.os.Bundle; -import android.os.Handler; import android.view.MenuItem; import android.view.View; import android.widget.EditText; @@ -37,22 +36,19 @@ import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.adapters.PrivateMessagesDetailRecyclerViewAdapter; -import ml.docilealligator.infinityforreddit.asynctasks.GetCurrentAccount; import ml.docilealligator.infinityforreddit.asynctasks.LoadUserDataAsyncTask; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.events.RepliedToPrivateMessageEvent; import ml.docilealligator.infinityforreddit.message.Message; import ml.docilealligator.infinityforreddit.message.ReadMessage; import ml.docilealligator.infinityforreddit.message.ReplyMessage; +import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; import retrofit2.Retrofit; public class ViewPrivateMessagesActivity extends BaseActivity implements ActivityToolbarInterface { public static final String EXTRA_PRIVATE_MESSAGE = "EPM"; public static final String EXTRA_MESSAGE_POSITION = "EMP"; - private static final String NULL_ACCESS_TOKEN_STATE = "NATS"; - private static final String ACCESS_TOKEN_STATE = "ATS"; - private static final String ACCOUNT_NAME_STATE = "ANS"; private static final String USER_AVATAR_STATE = "UAS"; @BindView(R.id.linear_layout_view_private_messages_activity) LinearLayout mLinearLayout; @@ -84,13 +80,15 @@ public class ViewPrivateMessagesActivity extends BaseActivity implements Activit @Named("default") SharedPreferences mSharedPreferences; @Inject + @Named("current_account") + SharedPreferences mCurrentAccountSharedPreferences; + @Inject CustomThemeWrapper mCustomThemeWrapper; @Inject Executor mExecutor; private LinearLayoutManager mLinearLayoutManager; private PrivateMessagesDetailRecyclerViewAdapter mAdapter; private Message privateMessage; - private boolean mNullAccessToken = false; private String mAccessToken; private String mAccountName; private String mUserAvatar; @@ -126,32 +124,13 @@ public class ViewPrivateMessagesActivity extends BaseActivity implements Activit mProvideUserAvatarCallbacks = new ArrayList<>(); + mAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null); + mAccountName = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_NAME, null); + if (savedInstanceState != null) { - mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE); - mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE); - mAccountName = savedInstanceState.getString(ACCOUNT_NAME_STATE); mUserAvatar = savedInstanceState.getString(USER_AVATAR_STATE); - - if (!mNullAccessToken && mAccessToken == null) { - getCurrentAccountAndBindView(); - } else { - bindView(); - } - } else { - getCurrentAccountAndBindView(); } - } - - private void getCurrentAccountAndBindView() { - GetCurrentAccount.getCurrentAccount(mExecutor, new Handler(), mRedditDataRoomDatabase, account -> { - if (account == null) { - mNullAccessToken = true; - } else { - mAccessToken = account.getAccessToken(); - mAccountName = account.getAccountName(); - } - bindView(); - }); + bindView(); } private void bindView() { @@ -293,9 +272,6 @@ public class ViewPrivateMessagesActivity extends BaseActivity implements Activit @Override protected void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); - outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken); - outState.putString(ACCESS_TOKEN_STATE, mAccessToken); - outState.putString(ACCOUNT_NAME_STATE, mAccountName); outState.putString(USER_AVATAR_STATE, mUserAvatar); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java index 2189c732..3bf7fdf8 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java @@ -65,14 +65,13 @@ import ml.docilealligator.infinityforreddit.AppBarStateChangeListener; import ml.docilealligator.infinityforreddit.FragmentCommunicator; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.MarkPostAsReadInterface; -import ml.docilealligator.infinityforreddit.RecyclerViewContentScrollingInterface; import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.RecyclerViewContentScrollingInterface; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.SortType; import ml.docilealligator.infinityforreddit.SortTypeSelectionCallback; import ml.docilealligator.infinityforreddit.asynctasks.AddSubredditOrUserToMultiReddit; import ml.docilealligator.infinityforreddit.asynctasks.CheckIsSubscribedToSubredditAsyncTask; -import ml.docilealligator.infinityforreddit.asynctasks.GetCurrentAccount; import ml.docilealligator.infinityforreddit.asynctasks.InsertSubredditDataAsyncTask; import ml.docilealligator.infinityforreddit.asynctasks.SwitchAccount; import ml.docilealligator.infinityforreddit.bottomsheetfragments.FABMoreOptionsBottomSheetFragment; @@ -114,9 +113,6 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp private static final String FETCH_SUBREDDIT_INFO_STATE = "FSIS"; private static final String CURRENT_ONLINE_SUBSCRIBERS_STATE = "COSS"; private static final String IS_IN_LAZY_MODE_STATE = "IILMS"; - private static final String NULL_ACCESS_TOKEN_STATE = "NATS"; - private static final String ACCESS_TOKEN_STATE = "ATS"; - private static final String ACCOUNT_NAME_STATE = "ANS"; private static final String MESSAGE_FULLNAME_STATE = "MFS"; private static final String NEW_ACCOUNT_NAME_STATE = "NANS"; private static final int ADD_TO_MULTIREDDIT_REQUEST_CODE = 1; @@ -200,7 +196,6 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp public SubredditViewModel mSubredditViewModel; private FragmentManager fragmentManager; private SectionsPagerAdapter sectionsPagerAdapter; - private boolean mNullAccessToken = false; private String mAccessToken; private String mAccountName; private String subredditName; @@ -329,31 +324,26 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp fragmentManager = getSupportFragmentManager(); + mAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null); + mAccountName = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_NAME, null); + if (savedInstanceState == null) { mMessageFullname = getIntent().getStringExtra(EXTRA_MESSAGE_FULLNAME); mNewAccountName = getIntent().getStringExtra(EXTRA_NEW_ACCOUNT_NAME); - getCurrentAccountAndBindView(); } else { mFetchSubredditInfoSuccess = savedInstanceState.getBoolean(FETCH_SUBREDDIT_INFO_STATE); mNCurrentOnlineSubscribers = savedInstanceState.getInt(CURRENT_ONLINE_SUBSCRIBERS_STATE); - mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE); - mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE); - mAccountName = savedInstanceState.getString(ACCOUNT_NAME_STATE); isInLazyMode = savedInstanceState.getBoolean(IS_IN_LAZY_MODE_STATE); mMessageFullname = savedInstanceState.getString(MESSAGE_FULLNAME_STATE); mNewAccountName = savedInstanceState.getString(NEW_ACCOUNT_NAME_STATE); - if (!mNullAccessToken && mAccessToken == null) { - getCurrentAccountAndBindView(); - } else { - bindView(); - } - if (mFetchSubredditInfoSuccess) { nOnlineSubscribersTextView.setText(getString(R.string.online_subscribers_number_detail, mNCurrentOnlineSubscribers)); } } + checkNewAccountAndBindView(); + fetchSubredditData(); params = (AppBarLayout.LayoutParams) collapsingToolbarLayout.getLayoutParams(); @@ -494,41 +484,28 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp subscribedColor = mCustomThemeWrapper.getSubscribed(); } - private void getCurrentAccountAndBindView() { - GetCurrentAccount.getCurrentAccount(mExecutor, new Handler(), mRedditDataRoomDatabase, account -> { - if (mNewAccountName != null) { - if (account == null || !account.getAccountName().equals(mNewAccountName)) { - SwitchAccount.switchAccount(mRedditDataRoomDatabase, mCurrentAccountSharedPreferences, - mExecutor, new Handler(), mNewAccountName, newAccount -> { - EventBus.getDefault().post(new SwitchAccountEvent(getClass().getName())); - Toast.makeText(this, R.string.account_switched, Toast.LENGTH_SHORT).show(); - - mNewAccountName = null; - if (newAccount == null) { - mNullAccessToken = true; - } else { - mAccessToken = newAccount.getAccessToken(); - mAccountName = newAccount.getAccountName(); - } - - bindView(); - }); - } else { - mAccessToken = account.getAccessToken(); - mAccountName = account.getAccountName(); - bindView(); - } + private void checkNewAccountAndBindView() { + if (mNewAccountName != null) { + if (mAccountName == null || !mAccountName.equals(mNewAccountName)) { + SwitchAccount.switchAccount(mRedditDataRoomDatabase, mCurrentAccountSharedPreferences, + mExecutor, new Handler(), mNewAccountName, newAccount -> { + EventBus.getDefault().post(new SwitchAccountEvent(getClass().getName())); + Toast.makeText(this, R.string.account_switched, Toast.LENGTH_SHORT).show(); + + mNewAccountName = null; + if (newAccount != null) { + mAccessToken = newAccount.getAccessToken(); + mAccountName = newAccount.getAccountName(); + } + + bindView(); + }); } else { - if (account == null) { - mNullAccessToken = true; - } else { - mAccessToken = account.getAccessToken(); - mAccountName = account.getAccountName(); - } - bindView(); } - }); + } else { + bindView(); + } } private void fetchSubredditData() { @@ -1084,9 +1061,6 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp outState.putBoolean(FETCH_SUBREDDIT_INFO_STATE, mFetchSubredditInfoSuccess); outState.putInt(CURRENT_ONLINE_SUBSCRIBERS_STATE, mNCurrentOnlineSubscribers); outState.putBoolean(IS_IN_LAZY_MODE_STATE, isInLazyMode); - outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken); - outState.putString(ACCESS_TOKEN_STATE, mAccessToken); - outState.putString(ACCOUNT_NAME_STATE, mAccountName); outState.putString(MESSAGE_FULLNAME_STATE, mMessageFullname); outState.putString(NEW_ACCOUNT_NAME_STATE, mNewAccountName); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java index 1574b2e2..e3736f1a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java @@ -76,7 +76,6 @@ import ml.docilealligator.infinityforreddit.SortType; import ml.docilealligator.infinityforreddit.SortTypeSelectionCallback; import ml.docilealligator.infinityforreddit.asynctasks.AddSubredditOrUserToMultiReddit; import ml.docilealligator.infinityforreddit.asynctasks.CheckIsFollowingUserAsyncTask; -import ml.docilealligator.infinityforreddit.asynctasks.GetCurrentAccount; import ml.docilealligator.infinityforreddit.asynctasks.SwitchAccount; import ml.docilealligator.infinityforreddit.bottomsheetfragments.FABMoreOptionsBottomSheetFragment; import ml.docilealligator.infinityforreddit.bottomsheetfragments.PostLayoutBottomSheetFragment; @@ -120,9 +119,6 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele public static final int ADD_TO_MULTIREDDIT_REQUEST_CODE = 400; private static final String FETCH_USER_INFO_STATE = "FSIS"; - private static final String NULL_ACCESS_TOKEN_STATE = "NATS"; - private static final String ACCESS_TOKEN_STATE = "ATS"; - private static final String ACCOUNT_NAME_STATE = "ANS"; private static final String IS_IN_LAZY_MODE_STATE = "IILMS"; private static final String MESSAGE_FULLNAME_STATE = "MFS"; private static final String NEW_ACCOUNT_NAME_STATE = "NANS"; @@ -256,26 +252,21 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele fragmentManager = getSupportFragmentManager(); + mAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null); + mAccountName = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_NAME, null); + if (savedInstanceState == null) { mMessageFullname = getIntent().getStringExtra(EXTRA_MESSAGE_FULLNAME); mNewAccountName = getIntent().getStringExtra(EXTRA_NEW_ACCOUNT_NAME); - getCurrentAccountAndInitializeViewPager(); } else { mFetchUserInfoSuccess = savedInstanceState.getBoolean(FETCH_USER_INFO_STATE); - mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE); - mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE); - mAccountName = savedInstanceState.getString(ACCOUNT_NAME_STATE); isInLazyMode = savedInstanceState.getBoolean(IS_IN_LAZY_MODE_STATE); mMessageFullname = savedInstanceState.getString(MESSAGE_FULLNAME_STATE); mNewAccountName = savedInstanceState.getString(NEW_ACCOUNT_NAME_STATE); - - if (!mNullAccessToken && mAccessToken == null) { - getCurrentAccountAndInitializeViewPager(); - } else { - initializeViewPager(); - } } + checkNewAccountAndInitializeViewPager(); + fetchUserInfo(); params = (AppBarLayout.LayoutParams) collapsingToolbarLayout.getLayoutParams(); @@ -563,41 +554,28 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele applyTabLayoutTheme(tabLayout); } - private void getCurrentAccountAndInitializeViewPager() { - GetCurrentAccount.getCurrentAccount(mExecutor, new Handler(), mRedditDataRoomDatabase, account -> { - if (mNewAccountName != null) { - if (account == null || !account.getAccountName().equals(mNewAccountName)) { - SwitchAccount.switchAccount(mRedditDataRoomDatabase, mCurrentAccountSharedPreferences, - mExecutor, new Handler(), mNewAccountName, newAccount -> { - EventBus.getDefault().post(new SwitchAccountEvent(getClass().getName())); - Toast.makeText(this, R.string.account_switched, Toast.LENGTH_SHORT).show(); - - mNewAccountName = null; - if (newAccount == null) { - mNullAccessToken = true; - } else { - mAccessToken = newAccount.getAccessToken(); - mAccountName = newAccount.getAccountName(); - } + private void checkNewAccountAndInitializeViewPager() { + if (mNewAccountName != null) { + if (mAccountName == null || !mAccountName.equals(mNewAccountName)) { + SwitchAccount.switchAccount(mRedditDataRoomDatabase, mCurrentAccountSharedPreferences, + mExecutor, new Handler(), mNewAccountName, newAccount -> { + EventBus.getDefault().post(new SwitchAccountEvent(getClass().getName())); + Toast.makeText(this, R.string.account_switched, Toast.LENGTH_SHORT).show(); + + mNewAccountName = null; + if (newAccount != null) { + mAccessToken = newAccount.getAccessToken(); + mAccountName = newAccount.getAccountName(); + } - initializeViewPager(); - }); - } else { - mAccessToken = account.getAccessToken(); - mAccountName = account.getAccountName(); - initializeViewPager(); - } + initializeViewPager(); + }); } else { - if (account == null) { - mNullAccessToken = true; - } else { - mAccessToken = account.getAccessToken(); - mAccountName = account.getAccountName(); - } - initializeViewPager(); } - }); + } else { + initializeViewPager(); + } } private void initializeViewPager() { @@ -1075,9 +1053,6 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele super.onSaveInstanceState(outState); outState.putBoolean(FETCH_USER_INFO_STATE, mFetchUserInfoSuccess); outState.putBoolean(IS_IN_LAZY_MODE_STATE, isInLazyMode); - outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken); - outState.putString(ACCESS_TOKEN_STATE, mAccessToken); - outState.putString(ACCOUNT_NAME_STATE, mAccountName); outState.putString(MESSAGE_FULLNAME_STATE, mMessageFullname); outState.putString(NEW_ACCOUNT_NAME_STATE, mNewAccountName); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/NavigationDrawerRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/NavigationDrawerRecyclerViewAdapter.java index b7c527be..744e6ad8 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/NavigationDrawerRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/NavigationDrawerRecyclerViewAdapter.java @@ -59,8 +59,8 @@ public class NavigationDrawerRecyclerViewAdapter extends RecyclerView.Adapter<Re private Resources resources; private RequestManager glide; private String accountName; - private String userIconUrl; - private String userBannerUrl; + private String profileImageUrl; + private String bannerImageUrl; private int karma; private boolean isNSFWEnabled; private boolean requireAuthToAccountSection; @@ -78,16 +78,12 @@ public class NavigationDrawerRecyclerViewAdapter extends RecyclerView.Adapter<Re public NavigationDrawerRecyclerViewAdapter(AppCompatActivity appCompatActivity, SharedPreferences sharedPreferences, SharedPreferences nsfwAndSpoilerSharedPreferences, CustomThemeWrapper customThemeWrapper, - String accountName, String userIconUrl, - String userBannerUrl, int karma, + String accountName, ItemClickListener itemClickListener) { this.appCompatActivity = appCompatActivity; resources = appCompatActivity.getResources(); glide = Glide.with(appCompatActivity); this.accountName = accountName; - this.userIconUrl = userIconUrl; - this.userBannerUrl = userBannerUrl; - this.karma = karma; isNSFWEnabled = nsfwAndSpoilerSharedPreferences.getBoolean((accountName == null ? "" : accountName) + SharedPreferencesUtils.NSFW_BASE, false); requireAuthToAccountSection = sharedPreferences.getBoolean(SharedPreferencesUtils.REQUIRE_AUTHENTICATION_TO_GO_TO_ACCOUNT_SECTION_IN_NAVIGATION_DRAWER, false); isLoggedIn = accountName != null; @@ -180,8 +176,8 @@ public class NavigationDrawerRecyclerViewAdapter extends RecyclerView.Adapter<Re if (isLoggedIn) { ((NavHeaderViewHolder) holder).karmaTextView.setText(appCompatActivity.getString(R.string.karma_info, karma)); ((NavHeaderViewHolder) holder).accountNameTextView.setText(accountName); - if (userIconUrl != null && !userIconUrl.equals("")) { - glide.load(userIconUrl) + if (profileImageUrl != null && !profileImageUrl.equals("")) { + glide.load(profileImageUrl) .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(144, 0))) .error(glide.load(R.drawable.subreddit_default_icon) .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(144, 0)))) @@ -192,8 +188,8 @@ public class NavigationDrawerRecyclerViewAdapter extends RecyclerView.Adapter<Re .into(((NavHeaderViewHolder) holder).profileImageView); } - if (userBannerUrl != null && !userBannerUrl.equals("")) { - glide.load(userBannerUrl).into(((NavHeaderViewHolder) holder).bannerImageView); + if (bannerImageUrl != null && !bannerImageUrl.equals("")) { + glide.load(bannerImageUrl).into(((NavHeaderViewHolder) holder).bannerImageView); } } else { ((NavHeaderViewHolder) holder).karmaTextView.setText(R.string.press_here_to_login); @@ -546,7 +542,9 @@ public class NavigationDrawerRecyclerViewAdapter extends RecyclerView.Adapter<Re } } - public void updateKarma(int karma) { + public void updateAccountInfo(String profileImageUrl, String bannerImageUrl, int karma) { + this.profileImageUrl = profileImageUrl; + this.bannerImageUrl = bannerImageUrl; this.karma = karma; notifyItemChanged(0); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/GetCurrentAccount.java b/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/GetCurrentAccount.java deleted file mode 100644 index 3cab64a2..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/GetCurrentAccount.java +++ /dev/null @@ -1,23 +0,0 @@ -package ml.docilealligator.infinityforreddit.asynctasks; - -import android.os.Handler; - -import java.util.concurrent.Executor; - -import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; -import ml.docilealligator.infinityforreddit.account.Account; - -public class GetCurrentAccount { - - public static void getCurrentAccount(Executor executor, Handler handler, RedditDataRoomDatabase redditDataRoomDatabase, - GetCurrentAccountAsyncTaskListener getCurrentAccountAsyncTaskListener) { - executor.execute(() -> { - Account account = redditDataRoomDatabase.accountDao().getCurrentAccount(); - handler.post(() -> getCurrentAccountAsyncTaskListener.success(account)); - }); - } - - public interface GetCurrentAccountAsyncTaskListener { - void success(Account account); - } -} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/SwitchAccount.java b/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/SwitchAccount.java index 94afd539..f5443973 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/SwitchAccount.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/SwitchAccount.java @@ -20,7 +20,8 @@ public class SwitchAccount { Account account = redditDataRoomDatabase.accountDao().getCurrentAccount(); currentAccountSharedPreferences.edit() .putString(SharedPreferencesUtils.ACCESS_TOKEN, account.getAccessToken()) - .putString(SharedPreferencesUtils.ACCOUNT_NAME, account.getAccountName()).apply(); + .putString(SharedPreferencesUtils.ACCOUNT_NAME, account.getAccountName()) + .putString(SharedPreferencesUtils.ACCOUNT_IMAGE_URL, account.getProfileImageUrl()).apply(); handler.post(() -> switchAccountListener.switched(account)); }); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/SwitchToAnonymousMode.java b/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/SwitchToAnonymousMode.java index ad1e7534..c14b7131 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/SwitchToAnonymousMode.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/SwitchToAnonymousMode.java @@ -7,7 +7,6 @@ import java.util.concurrent.Executor; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.account.AccountDao; -import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; public class SwitchToAnonymousMode { public static void switchToAnonymousMode(RedditDataRoomDatabase redditDataRoomDatabase, SharedPreferences currentAccountSharedPreferences, @@ -20,7 +19,7 @@ public class SwitchToAnonymousMode { } accountDao.markAllAccountsNonCurrent(); - currentAccountSharedPreferences.edit().remove(SharedPreferencesUtils.ACCESS_TOKEN).remove(SharedPreferencesUtils.ACCOUNT_NAME).apply(); + currentAccountSharedPreferences.edit().clear().apply(); handler.post(switchToAnonymousAccountAsyncTaskListener::logoutSuccess); }); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/CommentsListingFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/CommentsListingFragment.java index 35e8290c..9183fc1a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/CommentsListingFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/CommentsListingFragment.java @@ -9,7 +9,6 @@ import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.Drawable; import android.os.Build; import android.os.Bundle; -import android.os.Handler; import android.view.HapticFeedbackConstants; import android.view.LayoutInflater; import android.view.View; @@ -42,13 +41,12 @@ import butterknife.ButterKnife; import ml.docilealligator.infinityforreddit.FragmentCommunicator; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.NetworkState; -import ml.docilealligator.infinityforreddit.RecyclerViewContentScrollingInterface; import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.RecyclerViewContentScrollingInterface; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.SortType; import ml.docilealligator.infinityforreddit.activities.BaseActivity; import ml.docilealligator.infinityforreddit.adapters.CommentsListingRecyclerViewAdapter; -import ml.docilealligator.infinityforreddit.asynctasks.GetCurrentAccount; import ml.docilealligator.infinityforreddit.comment.CommentViewModel; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; @@ -66,9 +64,6 @@ public class CommentsListingFragment extends Fragment implements FragmentCommuni public static final String EXTRA_ACCOUNT_NAME = "EAN"; public static final String EXTRA_ARE_SAVED_COMMENTS = "EISC"; - private static final String NULL_ACCESS_TOKEN_STATE = "NATS"; - private static final String ACCESS_TOKEN_STATE = "ATS"; - @BindView(R.id.coordinator_layout_comments_listing_fragment) CoordinatorLayout mCoordinatorLayout; @BindView(R.id.swipe_refresh_layout_view_comments_listing_fragment) @@ -100,10 +95,12 @@ public class CommentsListingFragment extends Fragment implements FragmentCommuni @Named("post_layout") SharedPreferences mPostLayoutSharedPreferences; @Inject + @Named("current_account") + SharedPreferences mCurrentAccountSharedPreferences; + @Inject CustomThemeWrapper customThemeWrapper; @Inject Executor mExecutor; - private boolean mNullAccessToken = false; private String mAccessToken; private RequestManager mGlide; private AppCompatActivity mActivity; @@ -248,33 +245,13 @@ public class CommentsListingFragment extends Fragment implements FragmentCommuni touchHelper.attachToRecyclerView(mCommentRecyclerView); } - if (savedInstanceState == null) { - getCurrentAccountAndBindView(resources); - } else { - mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE); - mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE); + mAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null); - if (!mNullAccessToken && mAccessToken == null) { - getCurrentAccountAndBindView(resources); - } else { - bindView(resources); - } - } + bindView(resources); return rootView; } - private void getCurrentAccountAndBindView(Resources resources) { - GetCurrentAccount.getCurrentAccount(mExecutor, new Handler(), mRedditDataRoomDatabase, account -> { - if (account == null) { - mNullAccessToken = true; - } else { - mAccessToken = account.getAccessToken(); - } - bindView(resources); - }); - } - private void bindView(Resources resources) { mLinearLayoutManager = new LinearLayoutManager(mActivity); mCommentRecyclerView.setLayoutManager(mLinearLayoutManager); @@ -382,13 +359,6 @@ public class CommentsListingFragment extends Fragment implements FragmentCommuni } @Override - public void onSaveInstanceState(@NonNull Bundle outState) { - super.onSaveInstanceState(outState); - outState.putString(ACCESS_TOKEN_STATE, mAccessToken); - outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken); - } - - @Override public void refresh() { mFetchCommentInfoLinearLayout.setVisibility(View.GONE); mCommentViewModel.refresh(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/SharedPreferencesUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/SharedPreferencesUtils.java index ecfb2ba3..5fa5699c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/SharedPreferencesUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/SharedPreferencesUtils.java @@ -259,6 +259,7 @@ public class SharedPreferencesUtils { public static final String CURRENT_ACCOUNT_SHARED_PREFERENCES_FILE = "ml.docilealligator.infinityforreddit.current_account"; public static final String ACCOUNT_NAME = "account_name"; public static final String ACCESS_TOKEN = "access_token"; + public static final String ACCOUNT_IMAGE_URL = "account_image_url"; //Legacy Settings public static final String MAIN_PAGE_TAB_1_TITLE_LEGACY = "main_page_tab_1_title"; |