From 93a9ad9ece6df6cd210ca03be26bd74a760cb90f Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Thu, 27 Dec 2018 15:14:10 +0800 Subject: Display a MaterialLoadingProgressBar instead of a ProgressBar when loading posts. Correctly handle the visibility of the MaterialLoadingProgressBar when loading posts. --- .../infinityforreddit/PostDataSource.java | 4 +-- .../infinityforreddit/PostDataSourceFactory.java | 5 ++-- .../infinityforreddit/PostFragment.java | 28 +++++++++++-------- .../infinityforreddit/PostViewModel.java | 31 +++++++++++++--------- .../infinityforreddit/ViewPostDetailActivity.java | 3 ++- .../main/res/layout/activity_view_post_detail.xml | 10 ++++--- app/src/main/res/layout/fragment_post.xml | 6 +++-- 7 files changed, 52 insertions(+), 35 deletions(-) (limited to 'app/src') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java index 48d21bf0..9409de52 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java @@ -41,11 +41,11 @@ class PostDataSource extends PageKeyedDataSource { initialLoading = new MutableLiveData(); } - public MutableLiveData getNetworkState() { + MutableLiveData getNetworkState() { return networkState; } - public MutableLiveData getInitialLoading() { + MutableLiveData getInitialLoading() { return initialLoading; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSourceFactory.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSourceFactory.java index 358802dc..db05e0e0 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSourceFactory.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSourceFactory.java @@ -2,7 +2,6 @@ package ml.docilealligator.infinityforreddit; import android.arch.lifecycle.MutableLiveData; import android.arch.paging.DataSource; -import android.arch.paging.PageKeyedDataSource; import java.util.Locale; @@ -15,7 +14,7 @@ class PostDataSourceFactory extends DataSource.Factory { private boolean isBestPost; private String subredditName; - private MutableLiveData> mutableLiveData; + private MutableLiveData mutableLiveData; PostDataSourceFactory(Retrofit retrofit, String accessToken, Locale locale, boolean isBestPost) { this.retrofit = retrofit; @@ -45,7 +44,7 @@ class PostDataSourceFactory extends DataSource.Factory { return postDataSource; } - public MutableLiveData> getMutableLiveData() { + public MutableLiveData getMutableLiveData() { return mutableLiveData; } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java index f6e86fc8..f7dd68ef 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java @@ -7,6 +7,7 @@ import android.content.SharedPreferences; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.design.widget.CoordinatorLayout; +import android.support.design.widget.Snackbar; import android.support.v4.app.Fragment; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; @@ -15,7 +16,9 @@ import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.LinearLayout; -import android.widget.ProgressBar; + +import com.bumptech.glide.Glide; +import com.lsjwzh.widget.materialloadingprogressbar.CircleProgressBar; import javax.inject.Inject; import javax.inject.Named; @@ -31,15 +34,10 @@ public class PostFragment extends Fragment implements FragmentCommunicator { static final String SUBREDDIT_NAME_KEY = "SNK"; static final String IS_BEST_POST_KEY = "IBPK"; - private static final String LAST_ITEM_STATE = "LIS"; - private static final String LOADING_STATE_STATE = "LSS"; - private static final String LOAD_SUCCESS_STATE = "LOSS"; - private static final String IS_REFRESH_STATE = "IRS"; - private CoordinatorLayout mCoordinatorLayout; private RecyclerView mPostRecyclerView; private LinearLayoutManager mLinearLayoutManager; - private ProgressBar mProgressBar; + private CircleProgressBar mProgressBar; private LinearLayout mFetchPostErrorLinearLayout; private ImageView mFetchPostErrorImageView; @@ -134,6 +132,15 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } mPostViewModel = ViewModelProviders.of(this, factory).get(PostViewModel.class); mPostViewModel.getPosts().observe(this, posts -> mAdapter.submitList(posts)); + mPostViewModel.getInitialLoadingState().observe(this, networkState -> { + if(networkState.getStatus().equals(NetworkState.Status.SUCCESS)) { + mProgressBar.setVisibility(View.GONE); + } else if(networkState.getStatus().equals(NetworkState.Status.FAILED)) { + showErrorView(); + } else { + mProgressBar.setVisibility(View.VISIBLE); + } + }); return rootView; } @@ -143,7 +150,6 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } - /* private void showErrorView() { mProgressBar.setVisibility(View.GONE); if(mIsBestPost) { @@ -156,14 +162,14 @@ public class PostFragment extends Fragment implements FragmentCommunicator { snackbar.setAction(R.string.retry, new View.OnClickListener() { @Override public void onClick(View view) { - if (mIsBestPost) { + /*if (mIsBestPost) { fetchBestPost(); } else { fetchPost(); - } + }*/ } }); snackbar.show(); } - }*/ + } } \ No newline at end of file diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java index 3d3596c9..474972dc 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java @@ -1,10 +1,10 @@ package ml.docilealligator.infinityforreddit; import android.arch.lifecycle.LiveData; +import android.arch.lifecycle.Transformations; import android.arch.lifecycle.ViewModel; import android.arch.lifecycle.ViewModelProvider; import android.arch.paging.LivePagedListBuilder; -import android.arch.paging.PageKeyedDataSource; import android.arch.paging.PagedList; import android.support.annotation.NonNull; @@ -15,18 +15,19 @@ import retrofit2.Retrofit; public class PostViewModel extends ViewModel { private Executor executor; - LiveData networkState; - LiveData> posts; - LiveData> liveDataSource; + private LiveData networkState; + private LiveData initialLoadingState; + private LiveData> posts; public PostViewModel(Retrofit retrofit, String accessToken, Locale locale, boolean isBestPost) { //executor = Executors.newFixedThreadPool(5); PostDataSourceFactory postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, locale, isBestPost); - /*networkState = Transformations.switchMap(postDataSourceFactory.getMutableLiveData(), - (Function>) PostDataSource::getNetworkState);*/ - liveDataSource = postDataSourceFactory.getMutableLiveData(); + initialLoadingState = Transformations.switchMap(postDataSourceFactory.getMutableLiveData(), + dataSource -> dataSource.getInitialLoading()); + networkState = Transformations.switchMap(postDataSourceFactory.getMutableLiveData(), + dataSource -> dataSource.getNetworkState()); PagedList.Config pagedListConfig = (new PagedList.Config.Builder()) .setEnablePlaceholders(false) @@ -40,9 +41,11 @@ public class PostViewModel extends ViewModel { //executor = Executors.newFixedThreadPool(5); PostDataSourceFactory postDataSourceFactory = new PostDataSourceFactory(retrofit, locale, isBestPost, subredditName); - /*networkState = Transformations.switchMap(postDataSourceFactory.getMutableLiveData(), - (Function>) PostDataSource::getNetworkState);*/ - liveDataSource = postDataSourceFactory.getMutableLiveData(); + + initialLoadingState = Transformations.switchMap(postDataSourceFactory.getMutableLiveData(), + dataSource -> dataSource.getInitialLoading()); + networkState = Transformations.switchMap(postDataSourceFactory.getMutableLiveData(), + dataSource -> dataSource.getNetworkState()); PagedList.Config pagedListConfig = (new PagedList.Config.Builder()) @@ -57,9 +60,13 @@ public class PostViewModel extends ViewModel { return posts; } - /*public LiveData getNetworkState() { + LiveData getNetworkState() { return networkState; - }*/ + } + + public LiveData getInitialLoadingState() { + return initialLoadingState; + } public static class Factory extends ViewModelProvider.NewInstanceFactory { private Retrofit retrofit; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java index 5140ac94..b1d7af06 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java @@ -31,6 +31,7 @@ import com.bumptech.glide.load.engine.GlideException; import com.bumptech.glide.request.RequestListener; import com.bumptech.glide.request.RequestOptions; import com.bumptech.glide.request.target.Target; +import com.lsjwzh.widget.materialloadingprogressbar.CircleProgressBar; import com.multilevelview.MultiLevelRecyclerView; import com.santalu.aspectratioimageview.AspectRatioImageView; @@ -83,7 +84,7 @@ public class ViewPostDetailActivity extends AppCompatActivity { @BindView(R.id.minus_button_view_post_detail) ImageView mDownvoteButton; @BindView(R.id.share_button_view_post_detail) ImageView mShareButton; - @BindView(R.id.comment_progress_bar_view_post_detail) ProgressBar mCommentProgressbar; + @BindView(R.id.comment_progress_bar_view_post_detail) CircleProgressBar mCommentProgressbar; @BindView(R.id.comment_card_view_view_post_detail) CardView mCommentCardView; @BindView(R.id.recycler_view_view_post_detail) MultiLevelRecyclerView mRecyclerView; diff --git a/app/src/main/res/layout/activity_view_post_detail.xml b/app/src/main/res/layout/activity_view_post_detail.xml index 56b8db56..c9020ae9 100644 --- a/app/src/main/res/layout/activity_view_post_detail.xml +++ b/app/src/main/res/layout/activity_view_post_detail.xml @@ -258,13 +258,15 @@ - + android:layout_marginTop="16dp" + android:layout_marginBottom="16dp" + app:mlpb_progress_stoke_width="3dp" + app:mlpb_progress_color="@color/colorAccent" + android:layout_gravity="center_horizontal"/> - + app:mlpb_progress_stoke_width="3dp" + app:mlpb_progress_color="@color/colorAccent" + android:layout_gravity="center_horizontal"/>