From e0a9a02157153cbfd27ad6dacbfbabba158a278d Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Fri, 15 Nov 2019 17:23:59 +0800 Subject: Swiping to refresh posts is now available. --- app/src/main/AndroidManifest.xml | 2 +- .../Activity/ViewPostDetailActivity.java | 20 +++++++++- .../Activity/ViewSubredditDetailActivity.java | 2 +- .../infinityforreddit/Fragment/PostFragment.java | 43 ++++++++++++---------- app/src/main/res/layout/fragment_post.xml | 25 ++++++------- app/src/main/res/values/styles.xml | 5 +++ 6 files changed, 59 insertions(+), 38 deletions(-) (limited to 'app/src') diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index c7883829..3ddc6003 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -238,7 +238,7 @@ + android:theme="@style/AppTheme.NoActionBarWithTransparentBackground" /> = Build.VERSION_CODES.O_MR1 diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewSubredditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewSubredditDetailActivity.java index 4c50c45c..5a8cbaff 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewSubredditDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewSubredditDetailActivity.java @@ -58,13 +58,13 @@ import ml.docilealligator.infinityforreddit.PostDataSource; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.ReadMessage; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; -import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; import ml.docilealligator.infinityforreddit.SortType; import ml.docilealligator.infinityforreddit.SortTypeSelectionCallback; import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditDao; import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditData; import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditViewModel; import ml.docilealligator.infinityforreddit.SubredditSubscription; +import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; import pl.droidsonroids.gif.GifImageView; import retrofit2.Retrofit; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java index 214f77cb..eba9f607 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java @@ -32,10 +32,10 @@ import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.LinearSmoothScroller; import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.StaggeredGridLayoutManager; +import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; import com.bumptech.glide.Glide; import com.bumptech.glide.RequestManager; -import com.lsjwzh.widget.materialloadingprogressbar.CircleProgressBar; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; @@ -62,8 +62,8 @@ import ml.docilealligator.infinityforreddit.PostDataSource; import ml.docilealligator.infinityforreddit.PostViewModel; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; -import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; import ml.docilealligator.infinityforreddit.SortType; +import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; import retrofit2.Retrofit; @@ -84,10 +84,10 @@ public class PostFragment extends Fragment implements FragmentCommunicator { private static final String IS_IN_LAZY_MODE_STATE = "IILMS"; private static final String RECYCLER_VIEW_POSITION_STATE = "RVPS"; + @BindView(R.id.swipe_refresh_layout_post_fragment) + SwipeRefreshLayout mSwipeRefreshLayout; @BindView(R.id.recycler_view_post_fragment) RecyclerView mPostRecyclerView; - @BindView(R.id.progress_bar_post_fragment) - CircleProgressBar mProgressBar; @BindView(R.id.fetch_post_info_linear_layout_post_fragment) LinearLayout mFetchPostInfoLinearLayout; @BindView(R.id.fetch_post_info_image_view_post_fragment) @@ -131,6 +131,9 @@ public class PostFragment extends Fragment implements FragmentCommunicator { if (mPostRecyclerView.getAdapter() != null) { ((PostRecyclerViewAdapter) mPostRecyclerView.getAdapter()).setCanStartActivity(true); } + if (isInLazyMode && isLazyModePaused) { + resumeLazyMode(false); + } } @Override @@ -226,6 +229,12 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } }; + mSwipeRefreshLayout.setOnRefreshListener(() -> { + mPostViewModel.refresh(); + }); + + mSwipeRefreshLayout.setColorSchemeResources(R.color.colorAccent); + if (savedInstanceState != null) { int recyclerViewPosition = savedInstanceState.getInt(RECYCLER_VIEW_POSITION_STATE); if (recyclerViewPosition > 0) { @@ -465,11 +474,13 @@ public class PostFragment extends Fragment implements FragmentCommunicator { mPostRecyclerView.setAdapter(mAdapter); 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.hasPost().observe(this, hasPost -> { this.hasPost = hasPost; - mProgressBar.setVisibility(View.GONE); + mSwipeRefreshLayout.setRefreshing(false); if (hasPost) { mFetchPostInfoLinearLayout.setVisibility(View.GONE); } else { @@ -485,13 +496,13 @@ public class PostFragment extends Fragment implements FragmentCommunicator { mPostViewModel.getInitialLoadingState().observe(this, networkState -> { if (networkState.getStatus().equals(NetworkState.Status.SUCCESS)) { - mProgressBar.setVisibility(View.GONE); + mSwipeRefreshLayout.setRefreshing(false); } else if (networkState.getStatus().equals(NetworkState.Status.FAILED)) { - mProgressBar.setVisibility(View.GONE); + mSwipeRefreshLayout.setRefreshing(false); mFetchPostInfoLinearLayout.setOnClickListener(view -> refresh()); showErrorView(R.string.load_posts_error); } else { - mProgressBar.setVisibility(View.VISIBLE); + mSwipeRefreshLayout.setRefreshing(true); } }); @@ -541,7 +552,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { private void showErrorView(int stringResId) { if (activity != null && isAdded()) { - mProgressBar.setVisibility(View.GONE); + mSwipeRefreshLayout.setRefreshing(false); mFetchPostInfoLinearLayout.setVisibility(View.VISIBLE); mFetchPostInfoTextView.setText(stringResId); mGlide.load(R.drawable.error_image).into(mFetchPostInfoImageView); @@ -689,16 +700,8 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } @Override - public void onStart() { - super.onStart(); - if (isInLazyMode && isLazyModePaused) { - resumeLazyMode(false); - } - } - - @Override - public void onStop() { - super.onStop(); + public void onPause() { + super.onPause(); if (isInLazyMode) { pauseLazyMode(false); } diff --git a/app/src/main/res/layout/fragment_post.xml b/app/src/main/res/layout/fragment_post.xml index 56a9290f..263929d6 100644 --- a/app/src/main/res/layout/fragment_post.xml +++ b/app/src/main/res/layout/fragment_post.xml @@ -5,22 +5,19 @@ android:layout_height="match_parent" tools:application="ml.docilealligator.infinityforreddit.Fragment.PostFragment"> - - - + android:background="@color/backgroundColor"> + + + + true + +