From a4583f1ecfe38099af544e1834a2911d1a46c5c1 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Fri, 9 Aug 2019 11:29:46 +0800 Subject: Prevent loading user and subreddit data after orientation change if they were fetched before. Refresh current fragment in SearchResultActivity instead of all fragments when refreshing. --- .../infinityforreddit/SearchResultActivity.java | 18 +++--- .../ViewSubredditDetailActivity.java | 75 +++++++++++++++------- .../infinityforreddit/ViewUserDetailActivity.java | 70 ++++++++++++++------ 3 files changed, 112 insertions(+), 51 deletions(-) (limited to 'app/src/main/java/ml/docilealligator') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SearchResultActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SearchResultActivity.java index eb07265a..7e663e03 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SearchResultActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SearchResultActivity.java @@ -268,14 +268,16 @@ public class SearchResultActivity extends AppCompatActivity implements SearchPos } public void refresh() { - if(postFragment != null) { - ((FragmentCommunicator) postFragment).refresh(); - } - if(subredditListingFragment != null) { - ((FragmentCommunicator) subredditListingFragment).refresh(); - } - if (userListingFragment != null) { - ((FragmentCommunicator) userListingFragment).refresh(); + switch (viewPager.getCurrentItem()) { + case 0: + ((FragmentCommunicator) postFragment).refresh(); + break; + case 1: + ((FragmentCommunicator) subredditListingFragment).refresh(); + break; + case 2: + ((FragmentCommunicator) userListingFragment).refresh(); + break; } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java index c0bb4513..4c734251 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java @@ -42,6 +42,7 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So public static final String EXTRA_SUBREDDIT_NAME_KEY = "ESN"; + private static final String FETCH_SUBREDDIT_INFO_STATE = "FSIS"; 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"; @@ -65,6 +66,7 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So private String mAccessToken; private String mAccountName; private String subredditName; + private boolean mFetchSubredditInfoSuccess = false; private boolean subscriptionReady = false; private boolean isInLazyMode = false; @@ -97,9 +99,12 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So ((Infinity) getApplication()).getAppComponent().inject(this); + subredditName = getIntent().getExtras().getString(EXTRA_SUBREDDIT_NAME_KEY); + if(savedInstanceState == null) { getCurrentAccountAndBindView(); } else { + mFetchSubredditInfoSuccess = savedInstanceState.getBoolean(FETCH_SUBREDDIT_INFO_STATE); mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE); mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE); mAccountName = savedInstanceState.getString(ACCOUNT_NAME_STATE); @@ -114,6 +119,8 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So } } + fetchSubredditData(); + postTypeBottomSheetFragment = new PostTypeBottomSheetFragment(); sortTypeBottomSheetFragment = new SortTypeBottomSheetFragment(); @@ -130,7 +137,6 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So statusBarHeight = getResources().getDimensionPixelSize(resourceId); } - subredditName = getIntent().getExtras().getString(EXTRA_SUBREDDIT_NAME_KEY); String title = "r/" + subredditName; subredditNameTextView.setText(title); @@ -200,21 +206,6 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So } }); - FetchSubredditData.fetchSubredditData(mRetrofit, subredditName, new FetchSubredditData.FetchSubredditDataListener() { - @Override - public void onFetchSubredditDataSuccess(SubredditData subredditData, int nCurrentOnlineSubscribers) { - new InsertSubredditDataAsyncTask(mRedditDataRoomDatabase, subredditData) - .execute(); - String nOnlineSubscribers = getString(R.string.online_subscribers_number_detail, nCurrentOnlineSubscribers); - nOnlineSubscribersTextView.setText(nOnlineSubscribers); - } - - @Override - public void onFetchSubredditDataFail() { - - } - }); - fab.setOnClickListener(view -> postTypeBottomSheetFragment.show(getSupportFragmentManager(), postTypeBottomSheetFragment.getTag())); } @@ -230,6 +221,26 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So }).execute(); } + private void fetchSubredditData() { + if(!mFetchSubredditInfoSuccess) { + FetchSubredditData.fetchSubredditData(mRetrofit, subredditName, new FetchSubredditData.FetchSubredditDataListener() { + @Override + public void onFetchSubredditDataSuccess(SubredditData subredditData, int nCurrentOnlineSubscribers) { + new InsertSubredditDataAsyncTask(mRedditDataRoomDatabase, subredditData, () -> { + nOnlineSubscribersTextView.setText(getString(R.string.online_subscribers_number_detail, nCurrentOnlineSubscribers)); + mFetchSubredditInfoSuccess = true; + }).execute(); + } + + @Override + public void onFetchSubredditDataFail() { + makeSnackbar(R.string.cannot_fetch_subreddit_info, true); + mFetchSubredditInfoSuccess = false; + } + }); + } + } + private void bindView(boolean initializeFragment) { subscribeSubredditChip.setOnClickListener(view -> { if(subscriptionReady) { @@ -242,13 +253,13 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So public void onSubredditSubscriptionSuccess() { subscribeSubredditChip.setText(R.string.unsubscribe); subscribeSubredditChip.setChipBackgroundColor(getResources().getColorStateList(R.color.colorAccent)); - makeSnackbar(R.string.subscribed); + makeSnackbar(R.string.subscribed, false); subscriptionReady = true; } @Override public void onSubredditSubscriptionFail() { - makeSnackbar(R.string.subscribe_failed); + makeSnackbar(R.string.subscribe_failed, false); subscriptionReady = true; } }); @@ -260,13 +271,13 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So public void onSubredditSubscriptionSuccess() { subscribeSubredditChip.setText(R.string.subscribe); subscribeSubredditChip.setChipBackgroundColor(getResources().getColorStateList(R.color.backgroundColorPrimaryDark)); - makeSnackbar(R.string.unsubscribed); + makeSnackbar(R.string.unsubscribed, false); subscriptionReady = true; } @Override public void onSubredditSubscriptionFail() { - makeSnackbar(R.string.unsubscribe_failed); + makeSnackbar(R.string.unsubscribe_failed, false); subscriptionReady = true; } }); @@ -367,6 +378,7 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So @Override protected void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); + outState.putBoolean(FETCH_SUBREDDIT_INFO_STATE, mFetchSubredditInfoSuccess); outState.putBoolean(IS_IN_LAZY_MODE_STATE, isInLazyMode); outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken); outState.putString(ACCESS_TOKEN_STATE, mAccessToken); @@ -374,8 +386,13 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So getSupportFragmentManager().putFragment(outState, FRAGMENT_OUT_STATE_KEY, mFragment); } - private void makeSnackbar(int resId) { - Snackbar.make(coordinatorLayout, resId, Snackbar.LENGTH_SHORT).show(); + private void makeSnackbar(int resId, boolean retry) { + if(retry) { + Snackbar.make(coordinatorLayout, resId, Snackbar.LENGTH_SHORT).setAction(R.string.retry, + view -> fetchSubredditData()).show(); + } else { + Snackbar.make(coordinatorLayout, resId, Snackbar.LENGTH_SHORT).show(); + } } @Override @@ -411,12 +428,19 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So private static class InsertSubredditDataAsyncTask extends AsyncTask { + interface InsertSubredditDataAsyncTaskListener { + void insertSuccess(); + } + private SubredditDao mSubredditDao; private SubredditData subredditData; + private InsertSubredditDataAsyncTaskListener insertSubredditDataAsyncTaskListener; - InsertSubredditDataAsyncTask(RedditDataRoomDatabase db, SubredditData subredditData) { + InsertSubredditDataAsyncTask(RedditDataRoomDatabase db, SubredditData subredditData, + InsertSubredditDataAsyncTaskListener insertSubredditDataAsyncTaskListener) { mSubredditDao = db.subredditDao(); this.subredditData = subredditData; + this.insertSubredditDataAsyncTaskListener = insertSubredditDataAsyncTaskListener; } @Override @@ -424,5 +448,10 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So mSubredditDao.insert(subredditData); return null; } + + @Override + protected void onPostExecute(Void aVoid) { + insertSubredditDataAsyncTaskListener.insertSuccess(); + } } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java index d8f983c9..1901f179 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java @@ -45,6 +45,7 @@ public class ViewUserDetailActivity extends AppCompatActivity { public static final String EXTRA_USER_NAME_KEY = "EUNK"; + 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"; @@ -74,6 +75,7 @@ public class ViewUserDetailActivity extends AppCompatActivity { private String mAccountName; private String username; private boolean subscriptionReady = false; + private boolean mFetchUserInfoSuccess = false; private boolean isInLazyMode = false; private int expandedTabTextColor; private int expandedTabBackgroundColor; @@ -102,9 +104,12 @@ public class ViewUserDetailActivity extends AppCompatActivity { ((Infinity) getApplication()).getAppComponent().inject(this); + username = getIntent().getExtras().getString(EXTRA_USER_NAME_KEY); + if(savedInstanceState == null) { 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); @@ -117,6 +122,8 @@ public class ViewUserDetailActivity extends AppCompatActivity { } } + fetchUserInfo(); + params = (AppBarLayout.LayoutParams) collapsingToolbarLayout.getLayoutParams(); //Get status bar height @@ -126,7 +133,6 @@ public class ViewUserDetailActivity extends AppCompatActivity { statusBarHeight = getResources().getDimensionPixelSize(resourceId); } - username = getIntent().getExtras().getString(EXTRA_USER_NAME_KEY); String title = "u/" + username; userNameTextView.setText(title); @@ -217,13 +223,13 @@ public class ViewUserDetailActivity extends AppCompatActivity { public void onUserFollowingSuccess() { subscribeUserChip.setText(R.string.unfollow); subscribeUserChip.setChipBackgroundColor(getResources().getColorStateList(R.color.colorAccent)); - makeSnackbar(R.string.followed); + makeSnackbar(R.string.followed, false); subscriptionReady = true; } @Override public void onUserFollowingFail() { - makeSnackbar(R.string.follow_failed); + makeSnackbar(R.string.follow_failed, false); subscriptionReady = true; } }); @@ -234,13 +240,13 @@ public class ViewUserDetailActivity extends AppCompatActivity { public void onUserFollowingSuccess() { subscribeUserChip.setText(R.string.follow); subscribeUserChip.setChipBackgroundColor(getResources().getColorStateList(R.color.backgroundColorPrimaryDark)); - makeSnackbar(R.string.unfollowed); + makeSnackbar(R.string.unfollowed, false); subscriptionReady = true; } @Override public void onUserFollowingFail() { - makeSnackbar(R.string.unfollow_failed); + makeSnackbar(R.string.unfollow_failed, false); subscriptionReady = true; } }); @@ -276,18 +282,6 @@ public class ViewUserDetailActivity extends AppCompatActivity { karmaTextView.setText(karma); } }); - - FetchUserData.fetchUserData(mRetrofit, username, new FetchUserData.FetchUserDataListener() { - @Override - public void onFetchUserDataSuccess(UserData userData) { - new InsertUserDataAsyncTask(mRedditDataRoomDatabase.userDao(), userData).execute(); - } - - @Override - public void onFetchUserDataFailed() { - makeSnackbar(R.string.cannot_fetch_user_info); - } - }); } private void getCurrentAccountAndInitializeViewPager() { @@ -309,6 +303,24 @@ public class ViewUserDetailActivity extends AppCompatActivity { tabLayout.setupWithViewPager(viewPager); } + private void fetchUserInfo() { + if(!mFetchUserInfoSuccess) { + FetchUserData.fetchUserData(mRetrofit, username, new FetchUserData.FetchUserDataListener() { + @Override + public void onFetchUserDataSuccess(UserData userData) { + new InsertUserDataAsyncTask(mRedditDataRoomDatabase.userDao(), userData, + () -> mFetchUserInfoSuccess = true).execute(); + } + + @Override + public void onFetchUserDataFailed() { + makeSnackbar(R.string.cannot_fetch_user_info, true); + mFetchUserInfoSuccess = false; + } + }); + } + } + @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.view_user_detail_activity, menu); @@ -369,14 +381,20 @@ public class ViewUserDetailActivity extends AppCompatActivity { @Override protected void onSaveInstanceState(@NonNull Bundle outState) { 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); } - private void makeSnackbar(int resId) { - Snackbar.make(coordinatorLayout, resId, Snackbar.LENGTH_SHORT).show(); + private void makeSnackbar(int resId, boolean retry) { + if(retry) { + Snackbar.make(coordinatorLayout, resId, Snackbar.LENGTH_SHORT).setAction(R.string.retry, + view -> fetchUserInfo()).show(); + } else { + Snackbar.make(coordinatorLayout, resId, Snackbar.LENGTH_SHORT).show(); + } } public abstract static class AppBarStateChangeListener implements AppBarLayout.OnOffsetChangedListener { @@ -419,12 +437,19 @@ public class ViewUserDetailActivity extends AppCompatActivity { private static class InsertUserDataAsyncTask extends AsyncTask { + interface InsertUserDataAsyncTaskListener { + void insertSuccess(); + } + private UserDao userDao; private UserData subredditData; + private InsertUserDataAsyncTaskListener insertUserDataAsyncTaskListener; - InsertUserDataAsyncTask(UserDao userDao, UserData userData) { + InsertUserDataAsyncTask(UserDao userDao, UserData userData, + InsertUserDataAsyncTaskListener insertUserDataAsyncTaskListener) { this.userDao = userDao; this.subredditData = userData; + this.insertUserDataAsyncTaskListener = insertUserDataAsyncTaskListener; } @Override @@ -432,6 +457,11 @@ public class ViewUserDetailActivity extends AppCompatActivity { userDao.insert(subredditData); return null; } + + @Override + protected void onPostExecute(Void aVoid) { + insertUserDataAsyncTaskListener.insertSuccess(); + } } private class SectionsPagerAdapter extends FragmentPagerAdapter { -- cgit v1.2.3