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.

This commit is contained in:
Alex Ning 2019-08-09 11:29:46 +08:00
parent d408a47dba
commit a4583f1ecf
3 changed files with 112 additions and 51 deletions

View File

@ -268,14 +268,16 @@ public class SearchResultActivity extends AppCompatActivity implements SearchPos
} }
public void refresh() { public void refresh() {
if(postFragment != null) { switch (viewPager.getCurrentItem()) {
case 0:
((FragmentCommunicator) postFragment).refresh(); ((FragmentCommunicator) postFragment).refresh();
} break;
if(subredditListingFragment != null) { case 1:
((FragmentCommunicator) subredditListingFragment).refresh(); ((FragmentCommunicator) subredditListingFragment).refresh();
} break;
if (userListingFragment != null) { case 2:
((FragmentCommunicator) userListingFragment).refresh(); ((FragmentCommunicator) userListingFragment).refresh();
break;
} }
} }

View File

@ -42,6 +42,7 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So
public static final String EXTRA_SUBREDDIT_NAME_KEY = "ESN"; 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 FRAGMENT_OUT_STATE_KEY = "FOSK";
private static final String IS_IN_LAZY_MODE_STATE = "IILMS"; private static final String IS_IN_LAZY_MODE_STATE = "IILMS";
private static final String NULL_ACCESS_TOKEN_STATE = "NATS"; 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 mAccessToken;
private String mAccountName; private String mAccountName;
private String subredditName; private String subredditName;
private boolean mFetchSubredditInfoSuccess = false;
private boolean subscriptionReady = false; private boolean subscriptionReady = false;
private boolean isInLazyMode = false; private boolean isInLazyMode = false;
@ -97,9 +99,12 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So
((Infinity) getApplication()).getAppComponent().inject(this); ((Infinity) getApplication()).getAppComponent().inject(this);
subredditName = getIntent().getExtras().getString(EXTRA_SUBREDDIT_NAME_KEY);
if(savedInstanceState == null) { if(savedInstanceState == null) {
getCurrentAccountAndBindView(); getCurrentAccountAndBindView();
} else { } else {
mFetchSubredditInfoSuccess = savedInstanceState.getBoolean(FETCH_SUBREDDIT_INFO_STATE);
mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE); mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE);
mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE); mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE);
mAccountName = savedInstanceState.getString(ACCOUNT_NAME_STATE); mAccountName = savedInstanceState.getString(ACCOUNT_NAME_STATE);
@ -114,6 +119,8 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So
} }
} }
fetchSubredditData();
postTypeBottomSheetFragment = new PostTypeBottomSheetFragment(); postTypeBottomSheetFragment = new PostTypeBottomSheetFragment();
sortTypeBottomSheetFragment = new SortTypeBottomSheetFragment(); sortTypeBottomSheetFragment = new SortTypeBottomSheetFragment();
@ -130,7 +137,6 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So
statusBarHeight = getResources().getDimensionPixelSize(resourceId); statusBarHeight = getResources().getDimensionPixelSize(resourceId);
} }
subredditName = getIntent().getExtras().getString(EXTRA_SUBREDDIT_NAME_KEY);
String title = "r/" + subredditName; String title = "r/" + subredditName;
subredditNameTextView.setText(title); 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())); fab.setOnClickListener(view -> postTypeBottomSheetFragment.show(getSupportFragmentManager(), postTypeBottomSheetFragment.getTag()));
} }
@ -230,6 +221,26 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So
}).execute(); }).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) { private void bindView(boolean initializeFragment) {
subscribeSubredditChip.setOnClickListener(view -> { subscribeSubredditChip.setOnClickListener(view -> {
if(subscriptionReady) { if(subscriptionReady) {
@ -242,13 +253,13 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So
public void onSubredditSubscriptionSuccess() { public void onSubredditSubscriptionSuccess() {
subscribeSubredditChip.setText(R.string.unsubscribe); subscribeSubredditChip.setText(R.string.unsubscribe);
subscribeSubredditChip.setChipBackgroundColor(getResources().getColorStateList(R.color.colorAccent)); subscribeSubredditChip.setChipBackgroundColor(getResources().getColorStateList(R.color.colorAccent));
makeSnackbar(R.string.subscribed); makeSnackbar(R.string.subscribed, false);
subscriptionReady = true; subscriptionReady = true;
} }
@Override @Override
public void onSubredditSubscriptionFail() { public void onSubredditSubscriptionFail() {
makeSnackbar(R.string.subscribe_failed); makeSnackbar(R.string.subscribe_failed, false);
subscriptionReady = true; subscriptionReady = true;
} }
}); });
@ -260,13 +271,13 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So
public void onSubredditSubscriptionSuccess() { public void onSubredditSubscriptionSuccess() {
subscribeSubredditChip.setText(R.string.subscribe); subscribeSubredditChip.setText(R.string.subscribe);
subscribeSubredditChip.setChipBackgroundColor(getResources().getColorStateList(R.color.backgroundColorPrimaryDark)); subscribeSubredditChip.setChipBackgroundColor(getResources().getColorStateList(R.color.backgroundColorPrimaryDark));
makeSnackbar(R.string.unsubscribed); makeSnackbar(R.string.unsubscribed, false);
subscriptionReady = true; subscriptionReady = true;
} }
@Override @Override
public void onSubredditSubscriptionFail() { public void onSubredditSubscriptionFail() {
makeSnackbar(R.string.unsubscribe_failed); makeSnackbar(R.string.unsubscribe_failed, false);
subscriptionReady = true; subscriptionReady = true;
} }
}); });
@ -367,6 +378,7 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So
@Override @Override
protected void onSaveInstanceState(@NonNull Bundle outState) { protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
outState.putBoolean(FETCH_SUBREDDIT_INFO_STATE, mFetchSubredditInfoSuccess);
outState.putBoolean(IS_IN_LAZY_MODE_STATE, isInLazyMode); outState.putBoolean(IS_IN_LAZY_MODE_STATE, isInLazyMode);
outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken); outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken);
outState.putString(ACCESS_TOKEN_STATE, mAccessToken); outState.putString(ACCESS_TOKEN_STATE, mAccessToken);
@ -374,9 +386,14 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So
getSupportFragmentManager().putFragment(outState, FRAGMENT_OUT_STATE_KEY, mFragment); getSupportFragmentManager().putFragment(outState, FRAGMENT_OUT_STATE_KEY, mFragment);
} }
private void makeSnackbar(int resId) { 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(); Snackbar.make(coordinatorLayout, resId, Snackbar.LENGTH_SHORT).show();
} }
}
@Override @Override
public void sortTypeSelected(String sortType) { public void sortTypeSelected(String sortType) {
@ -411,12 +428,19 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So
private static class InsertSubredditDataAsyncTask extends AsyncTask<Void, Void, Void> { private static class InsertSubredditDataAsyncTask extends AsyncTask<Void, Void, Void> {
interface InsertSubredditDataAsyncTaskListener {
void insertSuccess();
}
private SubredditDao mSubredditDao; private SubredditDao mSubredditDao;
private SubredditData subredditData; private SubredditData subredditData;
private InsertSubredditDataAsyncTaskListener insertSubredditDataAsyncTaskListener;
InsertSubredditDataAsyncTask(RedditDataRoomDatabase db, SubredditData subredditData) { InsertSubredditDataAsyncTask(RedditDataRoomDatabase db, SubredditData subredditData,
InsertSubredditDataAsyncTaskListener insertSubredditDataAsyncTaskListener) {
mSubredditDao = db.subredditDao(); mSubredditDao = db.subredditDao();
this.subredditData = subredditData; this.subredditData = subredditData;
this.insertSubredditDataAsyncTaskListener = insertSubredditDataAsyncTaskListener;
} }
@Override @Override
@ -424,5 +448,10 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So
mSubredditDao.insert(subredditData); mSubredditDao.insert(subredditData);
return null; return null;
} }
@Override
protected void onPostExecute(Void aVoid) {
insertSubredditDataAsyncTaskListener.insertSuccess();
}
} }
} }

View File

@ -45,6 +45,7 @@ public class ViewUserDetailActivity extends AppCompatActivity {
public static final String EXTRA_USER_NAME_KEY = "EUNK"; 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 NULL_ACCESS_TOKEN_STATE = "NATS";
private static final String ACCESS_TOKEN_STATE = "ATS"; private static final String ACCESS_TOKEN_STATE = "ATS";
private static final String ACCOUNT_NAME_STATE = "ANS"; private static final String ACCOUNT_NAME_STATE = "ANS";
@ -74,6 +75,7 @@ public class ViewUserDetailActivity extends AppCompatActivity {
private String mAccountName; private String mAccountName;
private String username; private String username;
private boolean subscriptionReady = false; private boolean subscriptionReady = false;
private boolean mFetchUserInfoSuccess = false;
private boolean isInLazyMode = false; private boolean isInLazyMode = false;
private int expandedTabTextColor; private int expandedTabTextColor;
private int expandedTabBackgroundColor; private int expandedTabBackgroundColor;
@ -102,9 +104,12 @@ public class ViewUserDetailActivity extends AppCompatActivity {
((Infinity) getApplication()).getAppComponent().inject(this); ((Infinity) getApplication()).getAppComponent().inject(this);
username = getIntent().getExtras().getString(EXTRA_USER_NAME_KEY);
if(savedInstanceState == null) { if(savedInstanceState == null) {
getCurrentAccountAndInitializeViewPager(); getCurrentAccountAndInitializeViewPager();
} else { } else {
mFetchUserInfoSuccess = savedInstanceState.getBoolean(FETCH_USER_INFO_STATE);
mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE); mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE);
mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE); mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE);
mAccountName = savedInstanceState.getString(ACCOUNT_NAME_STATE); mAccountName = savedInstanceState.getString(ACCOUNT_NAME_STATE);
@ -117,6 +122,8 @@ public class ViewUserDetailActivity extends AppCompatActivity {
} }
} }
fetchUserInfo();
params = (AppBarLayout.LayoutParams) collapsingToolbarLayout.getLayoutParams(); params = (AppBarLayout.LayoutParams) collapsingToolbarLayout.getLayoutParams();
//Get status bar height //Get status bar height
@ -126,7 +133,6 @@ public class ViewUserDetailActivity extends AppCompatActivity {
statusBarHeight = getResources().getDimensionPixelSize(resourceId); statusBarHeight = getResources().getDimensionPixelSize(resourceId);
} }
username = getIntent().getExtras().getString(EXTRA_USER_NAME_KEY);
String title = "u/" + username; String title = "u/" + username;
userNameTextView.setText(title); userNameTextView.setText(title);
@ -217,13 +223,13 @@ public class ViewUserDetailActivity extends AppCompatActivity {
public void onUserFollowingSuccess() { public void onUserFollowingSuccess() {
subscribeUserChip.setText(R.string.unfollow); subscribeUserChip.setText(R.string.unfollow);
subscribeUserChip.setChipBackgroundColor(getResources().getColorStateList(R.color.colorAccent)); subscribeUserChip.setChipBackgroundColor(getResources().getColorStateList(R.color.colorAccent));
makeSnackbar(R.string.followed); makeSnackbar(R.string.followed, false);
subscriptionReady = true; subscriptionReady = true;
} }
@Override @Override
public void onUserFollowingFail() { public void onUserFollowingFail() {
makeSnackbar(R.string.follow_failed); makeSnackbar(R.string.follow_failed, false);
subscriptionReady = true; subscriptionReady = true;
} }
}); });
@ -234,13 +240,13 @@ public class ViewUserDetailActivity extends AppCompatActivity {
public void onUserFollowingSuccess() { public void onUserFollowingSuccess() {
subscribeUserChip.setText(R.string.follow); subscribeUserChip.setText(R.string.follow);
subscribeUserChip.setChipBackgroundColor(getResources().getColorStateList(R.color.backgroundColorPrimaryDark)); subscribeUserChip.setChipBackgroundColor(getResources().getColorStateList(R.color.backgroundColorPrimaryDark));
makeSnackbar(R.string.unfollowed); makeSnackbar(R.string.unfollowed, false);
subscriptionReady = true; subscriptionReady = true;
} }
@Override @Override
public void onUserFollowingFail() { public void onUserFollowingFail() {
makeSnackbar(R.string.unfollow_failed); makeSnackbar(R.string.unfollow_failed, false);
subscriptionReady = true; subscriptionReady = true;
} }
}); });
@ -276,18 +282,6 @@ public class ViewUserDetailActivity extends AppCompatActivity {
karmaTextView.setText(karma); 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() { private void getCurrentAccountAndInitializeViewPager() {
@ -309,6 +303,24 @@ public class ViewUserDetailActivity extends AppCompatActivity {
tabLayout.setupWithViewPager(viewPager); 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 @Override
public boolean onCreateOptionsMenu(Menu menu) { public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.view_user_detail_activity, menu); getMenuInflater().inflate(R.menu.view_user_detail_activity, menu);
@ -369,15 +381,21 @@ public class ViewUserDetailActivity extends AppCompatActivity {
@Override @Override
protected void onSaveInstanceState(@NonNull Bundle outState) { protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
outState.putBoolean(FETCH_USER_INFO_STATE, mFetchUserInfoSuccess);
outState.putBoolean(IS_IN_LAZY_MODE_STATE, isInLazyMode); outState.putBoolean(IS_IN_LAZY_MODE_STATE, isInLazyMode);
outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken); outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken);
outState.putString(ACCESS_TOKEN_STATE, mAccessToken); outState.putString(ACCESS_TOKEN_STATE, mAccessToken);
outState.putString(ACCOUNT_NAME_STATE, mAccountName); outState.putString(ACCOUNT_NAME_STATE, mAccountName);
} }
private void makeSnackbar(int resId) { 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(); Snackbar.make(coordinatorLayout, resId, Snackbar.LENGTH_SHORT).show();
} }
}
public abstract static class AppBarStateChangeListener implements AppBarLayout.OnOffsetChangedListener { public abstract static class AppBarStateChangeListener implements AppBarLayout.OnOffsetChangedListener {
// State // State
@ -419,12 +437,19 @@ public class ViewUserDetailActivity extends AppCompatActivity {
private static class InsertUserDataAsyncTask extends AsyncTask<Void, Void, Void> { private static class InsertUserDataAsyncTask extends AsyncTask<Void, Void, Void> {
interface InsertUserDataAsyncTaskListener {
void insertSuccess();
}
private UserDao userDao; private UserDao userDao;
private UserData subredditData; private UserData subredditData;
private InsertUserDataAsyncTaskListener insertUserDataAsyncTaskListener;
InsertUserDataAsyncTask(UserDao userDao, UserData userData) { InsertUserDataAsyncTask(UserDao userDao, UserData userData,
InsertUserDataAsyncTaskListener insertUserDataAsyncTaskListener) {
this.userDao = userDao; this.userDao = userDao;
this.subredditData = userData; this.subredditData = userData;
this.insertUserDataAsyncTaskListener = insertUserDataAsyncTaskListener;
} }
@Override @Override
@ -432,6 +457,11 @@ public class ViewUserDetailActivity extends AppCompatActivity {
userDao.insert(subredditData); userDao.insert(subredditData);
return null; return null;
} }
@Override
protected void onPostExecute(Void aVoid) {
insertUserDataAsyncTaskListener.insertSuccess();
}
} }
private class SectionsPagerAdapter extends FragmentPagerAdapter { private class SectionsPagerAdapter extends FragmentPagerAdapter {