diff options
author | Alex Ning <chineseperson5@gmail.com> | 2018-12-27 07:14:10 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2018-12-27 07:14:10 +0000 |
commit | 93a9ad9ece6df6cd210ca03be26bd74a760cb90f (patch) | |
tree | 5ebef6b4902899154e779f256803944595e59db9 /app/src | |
parent | 807ddf5a6e595d2368579008bb8132296be54ea2 (diff) | |
download | infinity-for-reddit-93a9ad9ece6df6cd210ca03be26bd74a760cb90f.tar infinity-for-reddit-93a9ad9ece6df6cd210ca03be26bd74a760cb90f.tar.gz infinity-for-reddit-93a9ad9ece6df6cd210ca03be26bd74a760cb90f.tar.bz2 infinity-for-reddit-93a9ad9ece6df6cd210ca03be26bd74a760cb90f.tar.lz infinity-for-reddit-93a9ad9ece6df6cd210ca03be26bd74a760cb90f.tar.xz infinity-for-reddit-93a9ad9ece6df6cd210ca03be26bd74a760cb90f.tar.zst infinity-for-reddit-93a9ad9ece6df6cd210ca03be26bd74a760cb90f.zip |
Display a MaterialLoadingProgressBar instead of a ProgressBar when loading posts. Correctly handle the visibility of the MaterialLoadingProgressBar when loading posts.
Diffstat (limited to '')
7 files changed, 52 insertions, 35 deletions
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<String, Post> { 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<PageKeyedDataSource<String, Post>> mutableLiveData; + private MutableLiveData<PostDataSource> 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<PageKeyedDataSource<String, Post>> getMutableLiveData() { + public MutableLiveData<PostDataSource> 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> networkState; - LiveData<PagedList<Post>> posts; - LiveData<PageKeyedDataSource<String, Post>> liveDataSource; + private LiveData<NetworkState> networkState; + private LiveData<NetworkState> initialLoadingState; + private LiveData<PagedList<Post>> 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, LiveData<NetworkState>>) 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, LiveData<NetworkState>>) 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<NetworkState> getNetworkState() { + LiveData<NetworkState> getNetworkState() { return networkState; - }*/ + } + + public LiveData<NetworkState> 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.support.v7.widget.CardView> - <ProgressBar + <com.lsjwzh.widget.materialloadingprogressbar.CircleProgressBar android:id="@+id/comment_progress_bar_view_post_detail" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:layout_gravity="center" - android:layout_margin="16dp" - android:visibility="gone"/> + 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"/> <android.support.v7.widget.CardView android:id="@+id/comment_card_view_view_post_detail" diff --git a/app/src/main/res/layout/fragment_post.xml b/app/src/main/res/layout/fragment_post.xml index 431a2292..9493bb8f 100644 --- a/app/src/main/res/layout/fragment_post.xml +++ b/app/src/main/res/layout/fragment_post.xml @@ -6,13 +6,15 @@ android:id="@+id/coordinator_layout_post_fragment" tools:context="ml.docilealligator.infinityforreddit.PostFragment"> - <ProgressBar + <com.lsjwzh.widget.materialloadingprogressbar.CircleProgressBar android:id="@+id/progress_bar_post_fragment" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:layout_marginBottom="16dp" - android:layout_gravity="center" /> + app:mlpb_progress_stoke_width="3dp" + app:mlpb_progress_color="@color/colorAccent" + android:layout_gravity="center_horizontal"/> <android.support.v7.widget.RecyclerView android:id="@+id/recycler_view_post_fragment" |