Display a MaterialLoadingProgressBar instead of a ProgressBar when loading posts. Correctly handle the visibility of the MaterialLoadingProgressBar when loading posts.

This commit is contained in:
Alex Ning 2018-12-27 15:14:10 +08:00
parent 807ddf5a6e
commit 93a9ad9ece
9 changed files with 53 additions and 35 deletions

Binary file not shown.

View File

@ -71,4 +71,5 @@ dependencies {
implementation 'com.github.santalu:aspect-ratio-imageview:1.0.6'
implementation 'com.felipecsl:gifimageview:2.2.0'
implementation "android.arch.paging:runtime:1.0.1"
implementation "com.lsjwzh:materialloadingprogressbar:0.5.8-RELEASE"
}

View File

@ -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;
}

View File

@ -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;
}
}

View File

@ -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();
}
}*/
}
}

View File

@ -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;

View File

@ -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;

View File

@ -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"

View File

@ -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"