Lazy mode is available in FilteredThingActivity. Fixed a bug related to lazy mode. Fixed several bugs in ViewModels. Minor bugs fixed.

This commit is contained in:
Alex Ning 2019-08-30 16:22:41 +08:00
parent e3d672f395
commit b34fcf1c6d
16 changed files with 151 additions and 91 deletions

View File

@ -227,6 +227,9 @@ public class AccountPostsActivity extends AppCompatActivity implements UserThing
userThingSortTypeBottomSheetFragment.show(getSupportFragmentManager(), userThingSortTypeBottomSheetFragment.getTag()); userThingSortTypeBottomSheetFragment.show(getSupportFragmentManager(), userThingSortTypeBottomSheetFragment.getTag());
return true; return true;
case R.id.action_refresh_account_posts_activity: case R.id.action_refresh_account_posts_activity:
if(mMenu != null) {
mMenu.findItem(R.id.action_lazy_mode_account_posts_activity).setTitle(R.string.action_start_lazy_mode);
}
if(mFragment != null) { if(mFragment != null) {
((PostFragment) mFragment).refresh(); ((PostFragment) mFragment).refresh();
} }

View File

@ -106,20 +106,6 @@ public class CommentsListingFragment extends Fragment implements FragmentCommuni
mCommentViewModel = new ViewModelProvider(this, factory).get(CommentViewModel.class); mCommentViewModel = new ViewModelProvider(this, factory).get(CommentViewModel.class);
mCommentViewModel.getComments().observe(this, comments -> mAdapter.submitList(comments)); mCommentViewModel.getComments().observe(this, comments -> mAdapter.submitList(comments));
mCommentViewModel.getInitialLoadingState().observe(this, networkState -> {
if(networkState.getStatus().equals(NetworkState.Status.SUCCESS)) {
mProgressBar.setVisibility(View.GONE);
mFetchCommentInfoLinearLayout.setVisibility(View.GONE);
} else if(networkState.getStatus().equals(NetworkState.Status.FAILED)) {
mProgressBar.setVisibility(View.GONE);
mFetchCommentInfoLinearLayout.setOnClickListener(view -> mCommentViewModel.refresh());
showErrorView(R.string.load_comments_failed);
} else {
mFetchCommentInfoLinearLayout.setVisibility(View.GONE);
mProgressBar.setVisibility(View.VISIBLE);
}
});
mCommentViewModel.hasComment().observe(this, hasComment -> { mCommentViewModel.hasComment().observe(this, hasComment -> {
mProgressBar.setVisibility(View.GONE); mProgressBar.setVisibility(View.GONE);
if(hasComment) { if(hasComment) {
@ -132,6 +118,18 @@ public class CommentsListingFragment extends Fragment implements FragmentCommuni
} }
}); });
mCommentViewModel.getInitialLoadingState().observe(this, networkState -> {
if(networkState.getStatus().equals(NetworkState.Status.SUCCESS)) {
mProgressBar.setVisibility(View.GONE);
} else if(networkState.getStatus().equals(NetworkState.Status.FAILED)) {
mProgressBar.setVisibility(View.GONE);
mFetchCommentInfoLinearLayout.setOnClickListener(view -> refresh());
showErrorView(R.string.load_comments_failed);
} else {
mProgressBar.setVisibility(View.VISIBLE);
}
});
mCommentViewModel.getPaginationNetworkState().observe(this, networkState -> { mCommentViewModel.getPaginationNetworkState().observe(this, networkState -> {
mAdapter.setNetworkState(networkState); mAdapter.setNetworkState(networkState);
}); });
@ -151,13 +149,14 @@ public class CommentsListingFragment extends Fragment implements FragmentCommuni
@Override @Override
public void refresh() { public void refresh() {
mFetchCommentInfoLinearLayout.setVisibility(View.GONE);
mCommentViewModel.refresh(); mCommentViewModel.refresh();
mAdapter.setNetworkState(null); mAdapter.setNetworkState(null);
} }
private void showErrorView(int stringResId) { private void showErrorView(int stringResId) {
mProgressBar.setVisibility(View.GONE);
if(activity != null && isAdded()) { if(activity != null && isAdded()) {
mProgressBar.setVisibility(View.GONE);
mFetchCommentInfoLinearLayout.setVisibility(View.VISIBLE); mFetchCommentInfoLinearLayout.setVisibility(View.VISIBLE);
mFetchCommentInfoTextView.setText(stringResId); mFetchCommentInfoTextView.setText(stringResId);
mGlide.load(R.drawable.error_image).into(mFetchCommentInfoImageView); mGlide.load(R.drawable.error_image).into(mFetchCommentInfoImageView);

View File

@ -19,6 +19,7 @@ import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import com.google.android.material.appbar.AppBarLayout; import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.appbar.CollapsingToolbarLayout;
import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.Subscribe;
@ -43,19 +44,24 @@ public class FilteredThingActivity extends AppCompatActivity implements SortType
static final String EXTRA_SORT_TYPE = "EST"; static final String EXTRA_SORT_TYPE = "EST";
static final String EXTRA_USER_WHERE = "EUW"; 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 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 FRAGMENT_OUT_STATE = "FOS"; private static final String FRAGMENT_OUT_STATE = "FOS";
@BindView(R.id.appbar_layout_filtered_posts_activity) AppBarLayout appBarLayout; @BindView(R.id.appbar_layout_filtered_posts_activity) AppBarLayout appBarLayout;
@BindView(R.id.collapsing_toolbar_layout_filtered_posts_activity) CollapsingToolbarLayout collapsingToolbarLayout;
@BindView(R.id.toolbar_filtered_posts_activity) Toolbar toolbar; @BindView(R.id.toolbar_filtered_posts_activity) Toolbar toolbar;
private boolean isInLazyMode = false;
private boolean mNullAccessToken = false; private boolean mNullAccessToken = false;
private String mAccessToken; private String mAccessToken;
private String name; private String name;
private int postType; private int postType;
private Fragment mFragment; private Fragment mFragment;
private Menu mMenu;
private AppBarLayout.LayoutParams params;
private SortTypeBottomSheetFragment bestSortTypeBottomSheetFragment; private SortTypeBottomSheetFragment bestSortTypeBottomSheetFragment;
private SortTypeBottomSheetFragment popularAndAllSortTypeBottomSheetFragment; private SortTypeBottomSheetFragment popularAndAllSortTypeBottomSheetFragment;
@ -72,7 +78,7 @@ public class FilteredThingActivity extends AppCompatActivity implements SortType
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_filtered_posts); setContentView(R.layout.activity_filtered_thing);
ButterKnife.bind(this); ButterKnife.bind(this);
@ -142,12 +148,15 @@ public class FilteredThingActivity extends AppCompatActivity implements SortType
setSupportActionBar(toolbar); setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true);
params = (AppBarLayout.LayoutParams) collapsingToolbarLayout.getLayoutParams();
name = getIntent().getStringExtra(EXTRA_NAME); name = getIntent().getStringExtra(EXTRA_NAME);
postType = getIntent().getIntExtra(EXTRA_POST_TYPE, PostDataSource.TYPE_FRONT_PAGE); postType = getIntent().getIntExtra(EXTRA_POST_TYPE, PostDataSource.TYPE_FRONT_PAGE);
int filter = getIntent().getIntExtra(EXTRA_FILTER, Post.TEXT_TYPE); int filter = getIntent().getIntExtra(EXTRA_FILTER, Post.TEXT_TYPE);
String sortType = getIntent().getStringExtra(EXTRA_SORT_TYPE); String sortType = getIntent().getStringExtra(EXTRA_SORT_TYPE);
if(savedInstanceState != null) { if(savedInstanceState != null) {
isInLazyMode = savedInstanceState.getBoolean(IS_IN_LAZY_MODE_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);
if(!mNullAccessToken && mAccessToken == null) { if(!mNullAccessToken && mAccessToken == null) {
@ -259,6 +268,17 @@ public class FilteredThingActivity extends AppCompatActivity implements SortType
@Override @Override
public boolean onCreateOptionsMenu(Menu menu) { public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.filtered_posts_activity, menu); getMenuInflater().inflate(R.menu.filtered_posts_activity, menu);
mMenu = menu;
MenuItem lazyModeItem = mMenu.findItem(R.id.action_lazy_mode_filtered_thing_activity);
if(isInLazyMode) {
lazyModeItem.setTitle(R.string.action_stop_lazy_mode);
params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_NO_SCROLL);
collapsingToolbarLayout.setLayoutParams(params);
} else {
lazyModeItem.setTitle(R.string.action_start_lazy_mode);
params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS);
collapsingToolbarLayout.setLayoutParams(params);
}
return true; return true;
} }
@ -268,7 +288,7 @@ public class FilteredThingActivity extends AppCompatActivity implements SortType
case android.R.id.home: case android.R.id.home:
finish(); finish();
return true; return true;
case R.id.action_sort_filtered_posts_activity: case R.id.action_sort_filtered_thing_activity:
switch (postType) { switch (postType) {
case PostDataSource.TYPE_FRONT_PAGE: case PostDataSource.TYPE_FRONT_PAGE:
bestSortTypeBottomSheetFragment.show(getSupportFragmentManager(), bestSortTypeBottomSheetFragment.getTag()); bestSortTypeBottomSheetFragment.show(getSupportFragmentManager(), bestSortTypeBottomSheetFragment.getTag());
@ -287,8 +307,30 @@ public class FilteredThingActivity extends AppCompatActivity implements SortType
userThingSortTypeBottomSheetFragment.show(getSupportFragmentManager(), userThingSortTypeBottomSheetFragment.getTag()); userThingSortTypeBottomSheetFragment.show(getSupportFragmentManager(), userThingSortTypeBottomSheetFragment.getTag());
} }
return true; return true;
case R.id.action_refresh_filtered_posts_activity: case R.id.action_refresh_filtered_thing_activity:
if(mMenu != null) {
mMenu.findItem(R.id.action_lazy_mode_filtered_thing_activity).setTitle(R.string.action_start_lazy_mode);
}
if(mFragment instanceof FragmentCommunicator) {
((FragmentCommunicator) mFragment).refresh(); ((FragmentCommunicator) mFragment).refresh();
}
return true;
case R.id.action_lazy_mode_filtered_thing_activity:
MenuItem lazyModeItem = mMenu.findItem(R.id.action_lazy_mode_filtered_thing_activity);
if(isInLazyMode) {
((FragmentCommunicator) mFragment).stopLazyMode();
isInLazyMode = false;
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 {
if(((FragmentCommunicator) mFragment).startLazyMode()) {
isInLazyMode = true;
lazyModeItem.setTitle(R.string.action_stop_lazy_mode);
params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_NO_SCROLL);
collapsingToolbarLayout.setLayoutParams(params);
}
}
return true; return true;
} }
return false; return false;
@ -300,6 +342,7 @@ public class FilteredThingActivity extends AppCompatActivity implements SortType
if (mFragment != null) { if (mFragment != null) {
getSupportFragmentManager().putFragment(outState, FRAGMENT_OUT_STATE, mFragment); getSupportFragmentManager().putFragment(outState, FRAGMENT_OUT_STATE, mFragment);
} }
outState.putBoolean(IS_IN_LAZY_MODE_STATE, isInLazyMode);
outState.putString(ACCESS_TOKEN_STATE, mAccessToken); outState.putString(ACCESS_TOKEN_STATE, mAccessToken);
outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken); outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken);
} }

View File

@ -706,6 +706,9 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
startActivity(intent); startActivity(intent);
return true; return true;
case R.id.action_refresh_main_activity: case R.id.action_refresh_main_activity:
if(mMenu != null) {
mMenu.findItem(R.id.action_lazy_mode_main_activity).setTitle(R.string.action_start_lazy_mode);
}
sectionsPagerAdapter.refresh(); sectionsPagerAdapter.refresh();
mFetchUserInfoSuccess = false; mFetchUserInfoSuccess = false;
loadUserData(); loadUserData();

View File

@ -1,5 +1,6 @@
package ml.docilealligator.infinityforreddit; package ml.docilealligator.infinityforreddit;
import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.MutableLiveData;
import androidx.paging.DataSource; import androidx.paging.DataSource;
@ -75,6 +76,7 @@ class PostDataSourceFactory extends DataSource.Factory {
this.nsfw = nsfw; this.nsfw = nsfw;
} }
@NonNull
@Override @Override
public DataSource create() { public DataSource create() {
if(postType == PostDataSource.TYPE_FRONT_PAGE) { if(postType == PostDataSource.TYPE_FRONT_PAGE) {

View File

@ -389,20 +389,6 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
mPostViewModel = new ViewModelProvider(this, factory).get(PostViewModel.class); mPostViewModel = new ViewModelProvider(this, factory).get(PostViewModel.class);
mPostViewModel.getPosts().observe(this, posts -> mAdapter.submitList(posts)); mPostViewModel.getPosts().observe(this, posts -> mAdapter.submitList(posts));
mPostViewModel.getInitialLoadingState().observe(this, networkState -> {
if(networkState.getStatus().equals(NetworkState.Status.SUCCESS)) {
mProgressBar.setVisibility(View.GONE);
mFetchPostInfoLinearLayout.setVisibility(View.GONE);
} else if(networkState.getStatus().equals(NetworkState.Status.FAILED)) {
mProgressBar.setVisibility(View.GONE);
mFetchPostInfoLinearLayout.setOnClickListener(view -> mPostViewModel.refresh());
showErrorView(R.string.load_posts_error);
} else {
mFetchPostInfoLinearLayout.setVisibility(View.GONE);
mProgressBar.setVisibility(View.VISIBLE);
}
});
mPostViewModel.hasPost().observe(this, hasPost -> { mPostViewModel.hasPost().observe(this, hasPost -> {
this.hasPost = hasPost; this.hasPost = hasPost;
mProgressBar.setVisibility(View.GONE); mProgressBar.setVisibility(View.GONE);
@ -418,6 +404,18 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
} }
}); });
mPostViewModel.getInitialLoadingState().observe(this, networkState -> {
if(networkState.getStatus().equals(NetworkState.Status.SUCCESS)) {
mProgressBar.setVisibility(View.GONE);
} else if(networkState.getStatus().equals(NetworkState.Status.FAILED)) {
mProgressBar.setVisibility(View.GONE);
mFetchPostInfoLinearLayout.setOnClickListener(view -> refresh());
showErrorView(R.string.load_posts_error);
} else {
mProgressBar.setVisibility(View.VISIBLE);
}
});
mPostViewModel.getPaginationNetworkState().observe(this, networkState -> mAdapter.setNetworkState(networkState)); mPostViewModel.getPaginationNetworkState().observe(this, networkState -> mAdapter.setNetworkState(networkState));
return rootView; return rootView;
@ -452,14 +450,15 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
stopLazyMode(); stopLazyMode();
} }
mFetchPostInfoLinearLayout.setVisibility(View.GONE);
hasPost = false; hasPost = false;
mPostViewModel.refresh(); mPostViewModel.refresh();
mAdapter.setNetworkState(null); mAdapter.setNetworkState(null);
} }
private void showErrorView(int stringResId) { private void showErrorView(int stringResId) {
mProgressBar.setVisibility(View.GONE);
if(activity != null && isAdded()) { if(activity != null && isAdded()) {
mProgressBar.setVisibility(View.GONE);
mFetchPostInfoLinearLayout.setVisibility(View.VISIBLE); mFetchPostInfoLinearLayout.setVisibility(View.VISIBLE);
mFetchPostInfoTextView.setText(stringResId); mFetchPostInfoTextView.setText(stringResId);
mGlide.load(R.drawable.error_image).into(mFetchPostInfoImageView); mGlide.load(R.drawable.error_image).into(mFetchPostInfoImageView);
@ -494,6 +493,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
public void stopLazyMode() { public void stopLazyMode() {
isInLazyMode = false; isInLazyMode = false;
isLazyModePaused = false; isLazyModePaused = false;
lazyModeRunnable.resetOldPosition();
lazyModeHandler.removeCallbacks(lazyModeRunnable); lazyModeHandler.removeCallbacks(lazyModeRunnable);
resumeLazyModeCountDownTimer.cancel(); resumeLazyModeCountDownTimer.cancel();
window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

View File

@ -104,7 +104,8 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo
@Override @Override
public boolean areContentsTheSame(@NonNull Post post, @NonNull Post t1) { public boolean areContentsTheSame(@NonNull Post post, @NonNull Post t1) {
return post.getTitle().equals(t1.getTitle()); return post.getTitle().equals(t1.getTitle()) && post.getSelfText().equals(t1.getSelfText())
&& post.getScore() == t1.getScore();
} }
}; };
@ -357,7 +358,7 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo
loadImage(holder, post); loadImage(holder, post);
} }
if(mPostType == PostDataSource.TYPE_SUBREDDIT && post.isStickied()) { if(mPostType == PostDataSource.TYPE_SUBREDDIT && !mDisplaySubredditName && post.isStickied()) {
((DataViewHolder) holder).stickiedPostImageView.setVisibility(View.VISIBLE); ((DataViewHolder) holder).stickiedPostImageView.setVisibility(View.VISIBLE);
mGlide.load(R.drawable.thumbtack).into(((DataViewHolder) holder).stickiedPostImageView); mGlide.load(R.drawable.thumbtack).into(((DataViewHolder) holder).stickiedPostImageView);
} }

View File

@ -48,10 +48,9 @@ public class PostViewModel extends ViewModel {
(new PagedList.Config.Builder()) (new PagedList.Config.Builder())
.setEnablePlaceholders(false) .setEnablePlaceholders(false)
.setPageSize(25) .setPageSize(25)
.setInitialLoadSizeHint(75)
.build(); .build();
posts = Transformations.switchMap(NSFWAndSortTypeLiveData, sort -> { posts = Transformations.switchMap(NSFWAndSortTypeLiveData, nsfwAndSort -> {
postDataSourceFactory.changeNSFWAndSortType(nsfwLiveData.getValue(), sortTypeLiveData.getValue()); postDataSourceFactory.changeNSFWAndSortType(nsfwLiveData.getValue(), sortTypeLiveData.getValue());
return (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).build(); return (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).build();
}); });
@ -80,10 +79,9 @@ public class PostViewModel extends ViewModel {
(new PagedList.Config.Builder()) (new PagedList.Config.Builder())
.setEnablePlaceholders(false) .setEnablePlaceholders(false)
.setPageSize(25) .setPageSize(25)
.setInitialLoadSizeHint(75)
.build(); .build();
posts = Transformations.switchMap(NSFWAndSortTypeLiveData, sort -> { posts = Transformations.switchMap(NSFWAndSortTypeLiveData, nsfwAndSort -> {
postDataSourceFactory.changeNSFWAndSortType(nsfwLiveData.getValue(), sortTypeLiveData.getValue()); postDataSourceFactory.changeNSFWAndSortType(nsfwLiveData.getValue(), sortTypeLiveData.getValue());
return (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).build(); return (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).build();
}); });
@ -112,10 +110,9 @@ public class PostViewModel extends ViewModel {
(new PagedList.Config.Builder()) (new PagedList.Config.Builder())
.setEnablePlaceholders(false) .setEnablePlaceholders(false)
.setPageSize(25) .setPageSize(25)
.setInitialLoadSizeHint(75)
.build(); .build();
posts = Transformations.switchMap(NSFWAndSortTypeLiveData, sort -> { posts = Transformations.switchMap(NSFWAndSortTypeLiveData, nsfwAndSort -> {
postDataSourceFactory.changeNSFWAndSortType(nsfwLiveData.getValue(), sortTypeLiveData.getValue()); postDataSourceFactory.changeNSFWAndSortType(nsfwLiveData.getValue(), sortTypeLiveData.getValue());
return (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).build(); return (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).build();
}); });
@ -144,10 +141,9 @@ public class PostViewModel extends ViewModel {
(new PagedList.Config.Builder()) (new PagedList.Config.Builder())
.setEnablePlaceholders(false) .setEnablePlaceholders(false)
.setPageSize(25) .setPageSize(25)
.setInitialLoadSizeHint(75)
.build(); .build();
posts = Transformations.switchMap(sortTypeLiveData, sort -> { posts = Transformations.switchMap(NSFWAndSortTypeLiveData, nsfwAndSort -> {
postDataSourceFactory.changeNSFWAndSortType(nsfwLiveData.getValue(), sortTypeLiveData.getValue()); postDataSourceFactory.changeNSFWAndSortType(nsfwLiveData.getValue(), sortTypeLiveData.getValue());
return (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).build(); return (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).build();
}); });

View File

@ -127,20 +127,6 @@ public class SubredditListingFragment extends Fragment implements FragmentCommun
mSubredditListingViewModel = new ViewModelProvider(this, factory).get(SubredditListingViewModel.class); mSubredditListingViewModel = new ViewModelProvider(this, factory).get(SubredditListingViewModel.class);
mSubredditListingViewModel.getSubreddits().observe(this, subredditData -> mAdapter.submitList(subredditData)); mSubredditListingViewModel.getSubreddits().observe(this, subredditData -> mAdapter.submitList(subredditData));
mSubredditListingViewModel.getInitialLoadingState().observe(this, networkState -> {
if(networkState.getStatus().equals(NetworkState.Status.SUCCESS)) {
mProgressBar.setVisibility(View.GONE);
mFetchSubredditListingInfoLinearLayout.setVisibility(View.GONE);
} else if(networkState.getStatus().equals(NetworkState.Status.FAILED)) {
mProgressBar.setVisibility(View.GONE);
mFetchSubredditListingInfoLinearLayout.setOnClickListener(view -> mSubredditListingViewModel.refresh());
showErrorView(R.string.search_subreddits_error);
} else {
mFetchSubredditListingInfoLinearLayout.setVisibility(View.GONE);
mProgressBar.setVisibility(View.VISIBLE);
}
});
mSubredditListingViewModel.hasSubredditLiveData().observe(this, hasSubreddit -> { mSubredditListingViewModel.hasSubredditLiveData().observe(this, hasSubreddit -> {
mProgressBar.setVisibility(View.GONE); mProgressBar.setVisibility(View.GONE);
if(hasSubreddit) { if(hasSubreddit) {
@ -153,6 +139,18 @@ public class SubredditListingFragment extends Fragment implements FragmentCommun
} }
}); });
mSubredditListingViewModel.getInitialLoadingState().observe(this, networkState -> {
if(networkState.getStatus().equals(NetworkState.Status.SUCCESS)) {
mProgressBar.setVisibility(View.GONE);
} else if(networkState.getStatus().equals(NetworkState.Status.FAILED)) {
mProgressBar.setVisibility(View.GONE);
mFetchSubredditListingInfoLinearLayout.setOnClickListener(view -> refresh());
showErrorView(R.string.search_subreddits_error);
} else {
mProgressBar.setVisibility(View.VISIBLE);
}
});
mSubredditListingViewModel.getPaginationNetworkState().observe(this, networkState -> { mSubredditListingViewModel.getPaginationNetworkState().observe(this, networkState -> {
mAdapter.setNetworkState(networkState); mAdapter.setNetworkState(networkState);
}); });
@ -161,8 +159,8 @@ public class SubredditListingFragment extends Fragment implements FragmentCommun
} }
private void showErrorView(int stringResId) { private void showErrorView(int stringResId) {
mProgressBar.setVisibility(View.GONE);
if(getActivity() != null && isAdded()) { if(getActivity() != null && isAdded()) {
mProgressBar.setVisibility(View.GONE);
mFetchSubredditListingInfoLinearLayout.setVisibility(View.VISIBLE); mFetchSubredditListingInfoLinearLayout.setVisibility(View.VISIBLE);
mFetchSubredditListingInfoTextView.setText(stringResId); mFetchSubredditListingInfoTextView.setText(stringResId);
Glide.with(this).load(R.drawable.error_image).into(mFetchSubredditListingInfoImageView); Glide.with(this).load(R.drawable.error_image).into(mFetchSubredditListingInfoImageView);
@ -175,6 +173,7 @@ public class SubredditListingFragment extends Fragment implements FragmentCommun
@Override @Override
public void refresh() { public void refresh() {
mFetchSubredditListingInfoLinearLayout.setVisibility(View.GONE);
mSubredditListingViewModel.refresh(); mSubredditListingViewModel.refresh();
mAdapter.setNetworkState(null); mAdapter.setNetworkState(null);
} }

View File

@ -106,20 +106,6 @@ public class UserListingFragment extends Fragment implements FragmentCommunicato
mUserListingViewModel = new ViewModelProvider(this, factory).get(UserListingViewModel.class); mUserListingViewModel = new ViewModelProvider(this, factory).get(UserListingViewModel.class);
mUserListingViewModel.getUsers().observe(this, UserData -> mAdapter.submitList(UserData)); mUserListingViewModel.getUsers().observe(this, UserData -> mAdapter.submitList(UserData));
mUserListingViewModel.getInitialLoadingState().observe(this, networkState -> {
if(networkState.getStatus().equals(NetworkState.Status.SUCCESS)) {
mProgressBar.setVisibility(View.GONE);
mFetchUserListingInfoLinearLayout.setVisibility(View.GONE);
} else if(networkState.getStatus().equals(NetworkState.Status.FAILED)) {
mProgressBar.setVisibility(View.GONE);
mFetchUserListingInfoLinearLayout.setOnClickListener(view -> mUserListingViewModel.refresh());
showErrorView(R.string.search_users_error);
} else {
mFetchUserListingInfoLinearLayout.setVisibility(View.GONE);
mProgressBar.setVisibility(View.VISIBLE);
}
});
mUserListingViewModel.hasUser().observe(this, hasUser -> { mUserListingViewModel.hasUser().observe(this, hasUser -> {
mProgressBar.setVisibility(View.GONE); mProgressBar.setVisibility(View.GONE);
if(hasUser) { if(hasUser) {
@ -132,6 +118,18 @@ public class UserListingFragment extends Fragment implements FragmentCommunicato
} }
}); });
mUserListingViewModel.getInitialLoadingState().observe(this, networkState -> {
if(networkState.getStatus().equals(NetworkState.Status.SUCCESS)) {
mProgressBar.setVisibility(View.GONE);
} else if(networkState.getStatus().equals(NetworkState.Status.FAILED)) {
mProgressBar.setVisibility(View.GONE);
mFetchUserListingInfoLinearLayout.setOnClickListener(view -> refresh());
showErrorView(R.string.search_users_error);
} else {
mProgressBar.setVisibility(View.VISIBLE);
}
});
mUserListingViewModel.getPaginationNetworkState().observe(this, networkState -> { mUserListingViewModel.getPaginationNetworkState().observe(this, networkState -> {
mAdapter.setNetworkState(networkState); mAdapter.setNetworkState(networkState);
}); });
@ -140,8 +138,8 @@ public class UserListingFragment extends Fragment implements FragmentCommunicato
} }
private void showErrorView(int stringResId) { private void showErrorView(int stringResId) {
mProgressBar.setVisibility(View.GONE);
if(getActivity() != null && isAdded()) { if(getActivity() != null && isAdded()) {
mProgressBar.setVisibility(View.GONE);
mFetchUserListingInfoLinearLayout.setVisibility(View.VISIBLE); mFetchUserListingInfoLinearLayout.setVisibility(View.VISIBLE);
mFetchUserListingInfoTextView.setText(stringResId); mFetchUserListingInfoTextView.setText(stringResId);
Glide.with(this).load(R.drawable.error_image).into(mFetchUserListingInfoImageView); Glide.with(this).load(R.drawable.error_image).into(mFetchUserListingInfoImageView);
@ -154,6 +152,7 @@ public class UserListingFragment extends Fragment implements FragmentCommunicato
@Override @Override
public void refresh() { public void refresh() {
mFetchUserListingInfoLinearLayout.setVisibility(View.GONE);
mUserListingViewModel.refresh(); mUserListingViewModel.refresh();
mAdapter.setNetworkState(null); mAdapter.setNetworkState(null);
} }

View File

@ -226,20 +226,6 @@ public class ViewMessageActivity extends AppCompatActivity {
mMessageViewModel = new ViewModelProvider(this, factory).get(MessageViewModel.class); mMessageViewModel = new ViewModelProvider(this, factory).get(MessageViewModel.class);
mMessageViewModel.getMessages().observe(this, messages -> mAdapter.submitList(messages)); mMessageViewModel.getMessages().observe(this, messages -> mAdapter.submitList(messages));
mMessageViewModel.getInitialLoadingState().observe(this, networkState -> {
if(networkState.getStatus().equals(NetworkState.Status.SUCCESS)) {
mProgressBar.setVisibility(View.GONE);
mFetchMessageInfoLinearLayout.setVisibility(View.GONE);
} else if(networkState.getStatus().equals(NetworkState.Status.FAILED)) {
mProgressBar.setVisibility(View.GONE);
mFetchMessageInfoLinearLayout.setOnClickListener(view -> mMessageViewModel.refresh());
showErrorView(R.string.load_messages_failed);
} else {
mFetchMessageInfoLinearLayout.setVisibility(View.GONE);
mProgressBar.setVisibility(View.VISIBLE);
}
});
mMessageViewModel.hasMessage().observe(this, hasMessage -> { mMessageViewModel.hasMessage().observe(this, hasMessage -> {
mProgressBar.setVisibility(View.GONE); mProgressBar.setVisibility(View.GONE);
if(hasMessage) { if(hasMessage) {
@ -252,6 +238,22 @@ public class ViewMessageActivity extends AppCompatActivity {
} }
}); });
mMessageViewModel.getInitialLoadingState().observe(this, networkState -> {
if(networkState.getStatus().equals(NetworkState.Status.SUCCESS)) {
mProgressBar.setVisibility(View.GONE);
} else if(networkState.getStatus().equals(NetworkState.Status.FAILED)) {
mProgressBar.setVisibility(View.GONE);
mFetchMessageInfoLinearLayout.setOnClickListener(view -> {
mFetchMessageInfoLinearLayout.setVisibility(View.GONE);
mMessageViewModel.refresh();
mAdapter.setNetworkState(null);
});
showErrorView(R.string.load_messages_failed);
} else {
mProgressBar.setVisibility(View.VISIBLE);
}
});
mMessageViewModel.getPaginationNetworkState().observe(this, networkState -> { mMessageViewModel.getPaginationNetworkState().observe(this, networkState -> {
mAdapter.setNetworkState(networkState); mAdapter.setNetworkState(networkState);
}); });

View File

@ -478,6 +478,9 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So
startActivity(intent); startActivity(intent);
break; break;
case R.id.action_refresh_view_subreddit_detail_activity: case R.id.action_refresh_view_subreddit_detail_activity:
if(mMenu != null) {
mMenu.findItem(R.id.action_lazy_mode_view_subreddit_detail_activity).setTitle(R.string.action_start_lazy_mode);
}
if(mFragment instanceof FragmentCommunicator) { if(mFragment instanceof FragmentCommunicator) {
((FragmentCommunicator) mFragment).refresh(); ((FragmentCommunicator) mFragment).refresh();
} }

View File

@ -533,6 +533,9 @@ public class ViewUserDetailActivity extends AppCompatActivity implements UserThi
startActivity(intent); startActivity(intent);
return true; return true;
case R.id.action_refresh_view_user_detail_activity: case R.id.action_refresh_view_user_detail_activity:
if(mMenu != null) {
mMenu.findItem(R.id.action_lazy_mode_view_user_detail_activity).setTitle(R.string.action_start_lazy_mode);
}
sectionsPagerAdapter.refresh(); sectionsPagerAdapter.refresh();
return true; return true;
case R.id.action_lazy_mode_view_user_detail_activity: case R.id.action_lazy_mode_view_user_detail_activity:

View File

@ -13,6 +13,7 @@
android:theme="@style/AppTheme.AppBarOverlay"> android:theme="@style/AppTheme.AppBarOverlay">
<com.google.android.material.appbar.CollapsingToolbarLayout <com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar_layout_filtered_posts_activity"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
app:layout_scrollFlags="scroll|enterAlways" app:layout_scrollFlags="scroll|enterAlways"

View File

@ -2,16 +2,22 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android" <menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"> xmlns:app="http://schemas.android.com/apk/res-auto">
<item <item
android:id="@+id/action_sort_filtered_posts_activity" android:id="@+id/action_sort_filtered_thing_activity"
android:orderInCategory="1" android:orderInCategory="1"
android:title="@string/action_sort" android:title="@string/action_sort"
android:icon="@drawable/ic_outline_sort_24px" android:icon="@drawable/ic_outline_sort_24px"
app:showAsAction="ifRoom" /> app:showAsAction="ifRoom" />
<item <item
android:id="@+id/action_refresh_filtered_posts_activity" android:id="@+id/action_refresh_filtered_thing_activity"
android:orderInCategory="2" android:orderInCategory="2"
android:title="@string/action_refresh" android:title="@string/action_refresh"
android:icon="@drawable/ic_search_white_24dp" android:icon="@drawable/ic_search_white_24dp"
app:showAsAction="never" /> app:showAsAction="never" />
<item
android:id="@+id/action_lazy_mode_filtered_thing_activity"
android:orderInCategory="3"
android:title="@string/action_start_lazy_mode"
app:showAsAction="never" />
</menu> </menu>

View File

@ -62,7 +62,7 @@
<string name="vote_failed">Vote failed</string> <string name="vote_failed">Vote failed</string>
<string name="refresh_post_failed">Error refreshing the post</string> <string name="refresh_post_failed">Error refreshing the post</string>
<string name="load_messages_failed">Error loading messages</string> <string name="load_messages_failed">Error loading messages.\nTap to retry.</string>
<string name="no_messages">Empty</string> <string name="no_messages">Empty</string>
<string name="nsfw">NSFW</string> <string name="nsfw">NSFW</string>