diff options
author | Alex Ning <chineseperson5@gmail.com> | 2019-11-19 08:23:45 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2019-11-19 08:23:45 +0000 |
commit | ca81ec49cf8fba4191673903bd8d7c6ef39394e6 (patch) | |
tree | 99254d20294d147dc6e800e39e3fbc71316d5e62 /app/src/main/java | |
parent | 47b57fd50fc21c796f59cd5165fa7741d7ef944e (diff) | |
download | infinity-for-reddit-ca81ec49cf8fba4191673903bd8d7c6ef39394e6.tar infinity-for-reddit-ca81ec49cf8fba4191673903bd8d7c6ef39394e6.tar.gz infinity-for-reddit-ca81ec49cf8fba4191673903bd8d7c6ef39394e6.tar.bz2 infinity-for-reddit-ca81ec49cf8fba4191673903bd8d7c6ef39394e6.tar.lz infinity-for-reddit-ca81ec49cf8fba4191673903bd8d7c6ef39394e6.tar.xz infinity-for-reddit-ca81ec49cf8fba4191673903bd8d7c6ef39394e6.tar.zst infinity-for-reddit-ca81ec49cf8fba4191673903bd8d7c6ef39394e6.zip |
Swipe down to refresh is available in other activities.
Diffstat (limited to 'app/src/main/java')
6 files changed, 120 insertions, 54 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewMessageActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewMessageActivity.java index 58b8b03e..9007e810 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewMessageActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewMessageActivity.java @@ -5,6 +5,7 @@ import android.content.res.Configuration; import android.content.res.Resources; import android.os.Build; import android.os.Bundle; +import android.util.TypedValue; import android.view.Menu; import android.view.MenuItem; import android.view.View; @@ -22,12 +23,12 @@ import androidx.lifecycle.ViewModelProvider; import androidx.recyclerview.widget.DividerItemDecoration; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; +import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; import com.bumptech.glide.Glide; import com.bumptech.glide.RequestManager; import com.google.android.material.appbar.AppBarLayout; import com.google.android.material.appbar.CollapsingToolbarLayout; -import com.lsjwzh.widget.materialloadingprogressbar.CircleProgressBar; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; @@ -65,10 +66,10 @@ public class ViewMessageActivity extends BaseActivity { AppBarLayout appBarLayout; @BindView(R.id.toolbar_view_message_activity) Toolbar toolbar; - @BindView(R.id.progress_bar_view_message_activity) - CircleProgressBar mProgressBar; + @BindView(R.id.swipe_refresh_layout_view_message_activity) + SwipeRefreshLayout mSwipeRefreshLayout; @BindView(R.id.recycler_view_view_message_activity) - RecyclerView recyclerView; + RecyclerView mRecyclerView; @BindView(R.id.fetch_messages_info_linear_layout_view_message_activity) LinearLayout mFetchMessageInfoLinearLayout; @BindView(R.id.fetch_messages_info_image_view_view_message_activity) @@ -146,7 +147,7 @@ public class ViewMessageActivity extends BaseActivity { int navBarResourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android"); if (navBarResourceId > 0) { - recyclerView.setPadding(0, 0, 0, resources.getDimensionPixelSize(navBarResourceId)); + mRecyclerView.setPadding(0, 0, 0, resources.getDimensionPixelSize(navBarResourceId)); } } @@ -211,10 +212,10 @@ public class ViewMessageActivity extends BaseActivity { mAdapter = new MessageRecyclerViewAdapter(this, mOauthRetrofit, mAccessToken, () -> mMessageViewModel.retryLoadingMore()); LinearLayoutManager layoutManager = new LinearLayoutManager(this); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(mAdapter); + mRecyclerView.setLayoutManager(layoutManager); + mRecyclerView.setAdapter(mAdapter); DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(this, layoutManager.getOrientation()); - recyclerView.addItemDecoration(dividerItemDecoration); + mRecyclerView.addItemDecoration(dividerItemDecoration); MessageViewModel.Factory factory = new MessageViewModel.Factory(mOauthRetrofit, getResources().getConfiguration().locale, mAccessToken, FetchMessages.WHERE_INBOX); @@ -222,7 +223,7 @@ public class ViewMessageActivity extends BaseActivity { mMessageViewModel.getMessages().observe(this, messages -> mAdapter.submitList(messages)); mMessageViewModel.hasMessage().observe(this, hasMessage -> { - mProgressBar.setVisibility(View.GONE); + mSwipeRefreshLayout.setRefreshing(false); if (hasMessage) { mFetchMessageInfoLinearLayout.setVisibility(View.GONE); } else { @@ -235,9 +236,9 @@ public class ViewMessageActivity extends BaseActivity { mMessageViewModel.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); mFetchMessageInfoLinearLayout.setOnClickListener(view -> { mFetchMessageInfoLinearLayout.setVisibility(View.GONE); mMessageViewModel.refresh(); @@ -245,17 +246,24 @@ public class ViewMessageActivity extends BaseActivity { }); showErrorView(R.string.load_messages_failed); } else { - mProgressBar.setVisibility(View.VISIBLE); + mSwipeRefreshLayout.setRefreshing(true); } }); mMessageViewModel.getPaginationNetworkState().observe(this, networkState -> { mAdapter.setNetworkState(networkState); }); + + mSwipeRefreshLayout.setOnRefreshListener(this::onRefresh); + + TypedValue typedValue = new TypedValue(); + getTheme().resolveAttribute(R.attr.cardViewBackgroundColor, typedValue, true); + mSwipeRefreshLayout.setProgressBackgroundColorSchemeColor(typedValue.data); + mSwipeRefreshLayout.setColorSchemeResources(R.color.colorAccent); } private void showErrorView(int stringResId) { - mProgressBar.setVisibility(View.GONE); + mSwipeRefreshLayout.setRefreshing(false); mFetchMessageInfoLinearLayout.setVisibility(View.VISIBLE); mFetchMessageInfoTextView.setText(stringResId); mGlide.load(R.drawable.error_image).into(mFetchMessageInfoImageView); @@ -300,4 +308,8 @@ public class ViewMessageActivity extends BaseActivity { finish(); } } + + private void onRefresh() { + mMessageViewModel.refresh(); + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java index 691eef31..d658a626 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java @@ -6,6 +6,7 @@ import android.content.res.Configuration; import android.content.res.Resources; import android.os.Build; import android.os.Bundle; +import android.util.TypedValue; import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; @@ -15,7 +16,6 @@ import android.view.Window; import android.view.WindowManager; import android.widget.ImageView; import android.widget.LinearLayout; -import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; @@ -26,6 +26,7 @@ import androidx.coordinatorlayout.widget.CoordinatorLayout; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.LinearSmoothScroller; import androidx.recyclerview.widget.RecyclerView; +import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; import com.bumptech.glide.Glide; import com.bumptech.glide.RequestManager; @@ -135,8 +136,8 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS AppBarLayout appBarLayout; @BindView(R.id.toolbar_view_post_detail_activity) Toolbar toolbar; - @BindView(R.id.progress_bar_view_post_detail_activity) - ProgressBar mProgressBar; + @BindView(R.id.swipe_refresh_layout_view_post_detail_activity) + SwipeRefreshLayout mSwipeRefreshLayout; @BindView(R.id.recycler_view_view_post_detail) RecyclerView mRecyclerView; @BindView(R.id.fetch_post_info_linear_layout_view_post_detail_activity) @@ -311,6 +312,13 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS }); } + mSwipeRefreshLayout.setOnRefreshListener(() -> refresh(true, true)); + + TypedValue typedValue = new TypedValue(); + getTheme().resolveAttribute(R.attr.cardViewBackgroundColor, typedValue, true); + mSwipeRefreshLayout.setProgressBackgroundColorSchemeColor(typedValue.data); + mSwipeRefreshLayout.setColorSchemeResources(R.color.colorAccent); + mSmoothScroller = new LinearSmoothScroller(this) { @Override protected int getVerticalSnapPreference() { @@ -510,7 +518,7 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS private void fetchPostAndCommentsById(String subredditId) { mFetchPostInfoLinearLayout.setVisibility(View.GONE); - mProgressBar.setVisibility(View.VISIBLE); + mSwipeRefreshLayout.setRefreshing(true); mGlide.clear(mFetchPostInfoImageView); String sortType = mSharedPreferences.getString(SharedPreferencesUtils.SORT_TYPE_POST_COMMENT, SortType.Type.BEST.value).toLowerCase(); @@ -537,7 +545,7 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS postAndComments.enqueue(new Callback<String>() { @Override public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { - mProgressBar.setVisibility(View.GONE); + mSwipeRefreshLayout.setRefreshing(false); if (response.isSuccessful()) { ParsePost.parsePost(response.body(), mLocale, new ParsePost.ParsePostListener() { @@ -860,6 +868,7 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS mMenu.findItem(R.id.action_view_crosspost_parent_view_post_detail_activity).setVisible(mPost.getCrosspostParentId() != null); } + mSwipeRefreshLayout.setRefreshing(false); } @Override @@ -873,7 +882,7 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS } private void showErrorView(String subredditId) { - mProgressBar.setVisibility(View.GONE); + mSwipeRefreshLayout.setRefreshing(false); mFetchPostInfoLinearLayout.setVisibility(View.VISIBLE); mFetchPostInfoLinearLayout.setOnClickListener(view -> fetchPostAndCommentsById(subredditId)); mFetchPostInfoTextView.setText(R.string.load_post_error); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/CommentsListingFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/CommentsListingFragment.java index b4ada7d5..aa5edb94 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/CommentsListingFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/CommentsListingFragment.java @@ -8,6 +8,7 @@ import android.content.res.Configuration; import android.content.res.Resources; import android.os.Build; import android.os.Bundle; +import android.util.TypedValue; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -21,10 +22,10 @@ import androidx.fragment.app.Fragment; import androidx.lifecycle.ViewModelProvider; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; +import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; import com.bumptech.glide.Glide; import com.bumptech.glide.RequestManager; -import com.lsjwzh.widget.materialloadingprogressbar.CircleProgressBar; import javax.inject.Inject; import javax.inject.Named; @@ -39,8 +40,8 @@ import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.NetworkState; 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; @@ -59,10 +60,10 @@ public class CommentsListingFragment extends Fragment implements FragmentCommuni @BindView(R.id.coordinator_layout_comments_listing_fragment) CoordinatorLayout mCoordinatorLayout; + @BindView(R.id.swipe_refresh_layout_view_comments_listing_fragment) + SwipeRefreshLayout mSwipeRefreshLayout; @BindView(R.id.recycler_view_comments_listing_fragment) RecyclerView mCommentRecyclerView; - @BindView(R.id.progress_bar_comments_listing_fragment) - CircleProgressBar mProgressBar; @BindView(R.id.fetch_comments_info_linear_layout_comments_listing_fragment) LinearLayout mFetchCommentInfoLinearLayout; @BindView(R.id.fetch_comments_info_image_view_comments_listing_fragment) @@ -179,7 +180,7 @@ public class CommentsListingFragment extends Fragment implements FragmentCommuni mCommentViewModel.getComments().observe(this, comments -> mAdapter.submitList(comments)); mCommentViewModel.hasComment().observe(this, hasComment -> { - mProgressBar.setVisibility(View.GONE); + mSwipeRefreshLayout.setRefreshing(false); if (hasComment) { mFetchCommentInfoLinearLayout.setVisibility(View.GONE); } else { @@ -192,17 +193,24 @@ public class CommentsListingFragment extends Fragment implements FragmentCommuni mCommentViewModel.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); mFetchCommentInfoLinearLayout.setOnClickListener(view -> refresh()); showErrorView(R.string.load_comments_failed); } else { - mProgressBar.setVisibility(View.VISIBLE); + mSwipeRefreshLayout.setRefreshing(true); } }); mCommentViewModel.getPaginationNetworkState().observe(this, networkState -> mAdapter.setNetworkState(networkState)); + + mSwipeRefreshLayout.setOnRefreshListener(() -> mCommentViewModel.refresh()); + + TypedValue typedValue = new TypedValue(); + mActivity.getTheme().resolveAttribute(R.attr.cardViewBackgroundColor, typedValue, true); + mSwipeRefreshLayout.setProgressBackgroundColorSchemeColor(typedValue.data); + mSwipeRefreshLayout.setColorSchemeResources(R.color.colorAccent); } public void changeSortType(SortType sortType) { @@ -231,7 +239,7 @@ public class CommentsListingFragment extends Fragment implements FragmentCommuni private void showErrorView(int stringResId) { if (mActivity != null && isAdded()) { - mProgressBar.setVisibility(View.GONE); + mSwipeRefreshLayout.setRefreshing(false); mFetchCommentInfoLinearLayout.setVisibility(View.VISIBLE); mFetchCommentInfoTextView.setText(stringResId); mGlide.load(R.drawable.error_image).into(mFetchCommentInfoImageView); 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 eba9f607..fab04c17 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java @@ -12,6 +12,7 @@ import android.os.Build; import android.os.Bundle; import android.os.CountDownTimer; import android.os.Handler; +import android.util.TypedValue; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -25,6 +26,7 @@ import android.widget.Toast; import androidx.annotation.DimenRes; import androidx.annotation.NonNull; import androidx.coordinatorlayout.widget.CoordinatorLayout; +import androidx.core.content.ContextCompat; import androidx.fragment.app.Fragment; import androidx.lifecycle.ViewModelProvider; import androidx.paging.PagedList; @@ -233,6 +235,9 @@ public class PostFragment extends Fragment implements FragmentCommunicator { mPostViewModel.refresh(); }); + TypedValue typedValue = new TypedValue(); + activity.getTheme().resolveAttribute(R.attr.cardViewBackgroundColor, typedValue, true); + mSwipeRefreshLayout.setProgressBackgroundColorSchemeColor(typedValue.data); mSwipeRefreshLayout.setColorSchemeResources(R.color.colorAccent); if (savedInstanceState != null) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/SubredditListingFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/SubredditListingFragment.java index 3a5955f1..1709fed9 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/SubredditListingFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/SubredditListingFragment.java @@ -2,12 +2,14 @@ package ml.docilealligator.infinityforreddit.Fragment; import android.app.Activity; +import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.res.Configuration; import android.content.res.Resources; import android.os.Build; import android.os.Bundle; +import android.util.TypedValue; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -21,9 +23,9 @@ import androidx.fragment.app.Fragment; import androidx.lifecycle.ViewModelProvider; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; +import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; import com.bumptech.glide.Glide; -import com.lsjwzh.widget.materialloadingprogressbar.CircleProgressBar; import javax.inject.Inject; import javax.inject.Named; @@ -38,9 +40,9 @@ import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.NetworkState; 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.SubredditListingViewModel; +import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; import retrofit2.Retrofit; @@ -58,8 +60,8 @@ public class SubredditListingFragment extends Fragment implements FragmentCommun CoordinatorLayout mCoordinatorLayout; @BindView(R.id.recycler_view_subreddit_listing_fragment) RecyclerView mSubredditListingRecyclerView; - @BindView(R.id.progress_bar_subreddit_listing_fragment) - CircleProgressBar mProgressBar; + @BindView(R.id.swipe_refresh_layout_subreddit_listing_fragment) + SwipeRefreshLayout mSwipeRefreshLayout; @BindView(R.id.fetch_subreddit_listing_info_linear_layout_subreddit_listing_fragment) LinearLayout mFetchSubredditListingInfoLinearLayout; @BindView(R.id.fetch_subreddit_listing_info_image_view_subreddit_listing_fragment) @@ -79,6 +81,7 @@ public class SubredditListingFragment extends Fragment implements FragmentCommun SharedPreferences mSharedPreferences; private LinearLayoutManager mLinearLayoutManager; private SubredditListingRecyclerViewAdapter mAdapter; + private Activity mActivity; public SubredditListingFragment() { // Required empty public constructor @@ -91,9 +94,7 @@ public class SubredditListingFragment extends Fragment implements FragmentCommun // Inflate the layout for this fragment View rootView = inflater.inflate(R.layout.fragment_subreddit_listing, container, false); - Activity activity = getActivity(); - - ((Infinity) activity.getApplication()).getAppComponent().inject(this); + ((Infinity) mActivity.getApplication()).getAppComponent().inject(this); ButterKnife.bind(this, rootView); @@ -119,7 +120,7 @@ public class SubredditListingFragment extends Fragment implements FragmentCommun String sort = mSharedPreferences.getString(SharedPreferencesUtils.SORT_TYPE_SEARCH_SUBREDDIT, SortType.Type.RELEVANCE.value); SortType sortType = new SortType(SortType.Type.valueOf(sort.toUpperCase())); - mAdapter = new SubredditListingRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, + mAdapter = new SubredditListingRecyclerViewAdapter(mActivity, mOauthRetrofit, mRetrofit, accessToken, accountName, mRedditDataRoomDatabase, new SubredditListingRecyclerViewAdapter.Callback() { @Override @@ -130,11 +131,11 @@ public class SubredditListingFragment extends Fragment implements FragmentCommun @Override public void subredditSelected(String subredditName, String iconUrl) { if (isPosting) { - ((SearchSubredditsResultActivity) activity).getSelectedSubreddit(subredditName, iconUrl); + ((SearchSubredditsResultActivity) mActivity).getSelectedSubreddit(subredditName, iconUrl); } else { - Intent intent = new Intent(activity, ViewSubredditDetailActivity.class); + Intent intent = new Intent(mActivity, ViewSubredditDetailActivity.class); intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, subredditName); - activity.startActivity(intent); + mActivity.startActivity(intent); } } }); @@ -146,7 +147,7 @@ public class SubredditListingFragment extends Fragment implements FragmentCommun mSubredditListingViewModel.getSubreddits().observe(this, subredditData -> mAdapter.submitList(subredditData)); mSubredditListingViewModel.hasSubredditLiveData().observe(this, hasSubreddit -> { - mProgressBar.setVisibility(View.GONE); + mSwipeRefreshLayout.setRefreshing(false); if (hasSubreddit) { mFetchSubredditListingInfoLinearLayout.setVisibility(View.GONE); } else { @@ -159,13 +160,13 @@ public class SubredditListingFragment extends Fragment implements FragmentCommun mSubredditListingViewModel.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); mFetchSubredditListingInfoLinearLayout.setOnClickListener(view -> refresh()); showErrorView(R.string.search_subreddits_error); } else { - mProgressBar.setVisibility(View.VISIBLE); + mSwipeRefreshLayout.setRefreshing(true); } }); @@ -173,12 +174,25 @@ public class SubredditListingFragment extends Fragment implements FragmentCommun mAdapter.setNetworkState(networkState); }); + mSwipeRefreshLayout.setOnRefreshListener(() -> mSubredditListingViewModel.refresh()); + + TypedValue typedValue = new TypedValue(); + mActivity.getTheme().resolveAttribute(R.attr.cardViewBackgroundColor, typedValue, true); + mSwipeRefreshLayout.setProgressBackgroundColorSchemeColor(typedValue.data); + mSwipeRefreshLayout.setColorSchemeResources(R.color.colorAccent); + return rootView; } + @Override + public void onAttach(@NonNull Context context) { + super.onAttach(context); + mActivity = (Activity) context; + } + private void showErrorView(int stringResId) { if (getActivity() != null && isAdded()) { - mProgressBar.setVisibility(View.GONE); + mSwipeRefreshLayout.setRefreshing(false); mFetchSubredditListingInfoLinearLayout.setVisibility(View.VISIBLE); mFetchSubredditListingInfoTextView.setText(stringResId); Glide.with(this).load(R.drawable.error_image).into(mFetchSubredditListingInfoImageView); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/UserListingFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/UserListingFragment.java index 697ae757..3e580e09 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/UserListingFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/UserListingFragment.java @@ -1,11 +1,14 @@ package ml.docilealligator.infinityforreddit.Fragment; +import android.app.Activity; +import android.content.Context; import android.content.SharedPreferences; import android.content.res.Configuration; import android.content.res.Resources; import android.os.Build; import android.os.Bundle; +import android.util.TypedValue; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -13,14 +16,15 @@ import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; +import androidx.annotation.NonNull; import androidx.coordinatorlayout.widget.CoordinatorLayout; import androidx.fragment.app.Fragment; import androidx.lifecycle.ViewModelProvider; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; +import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; import com.bumptech.glide.Glide; -import com.lsjwzh.widget.materialloadingprogressbar.CircleProgressBar; import javax.inject.Inject; import javax.inject.Named; @@ -33,9 +37,9 @@ import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.NetworkState; 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.UserListingViewModel; +import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; import retrofit2.Retrofit; @@ -52,8 +56,8 @@ public class UserListingFragment extends Fragment implements FragmentCommunicato CoordinatorLayout mCoordinatorLayout; @BindView(R.id.recycler_view_user_listing_fragment) RecyclerView mUserListingRecyclerView; - @BindView(R.id.progress_bar_user_listing_fragment) - CircleProgressBar mProgressBar; + @BindView(R.id.swipe_refresh_layout_user_listing_fragment) + SwipeRefreshLayout mSwipeRefreshLayout; @BindView(R.id.fetch_user_listing_info_linear_layout_user_listing_fragment) LinearLayout mFetchUserListingInfoLinearLayout; @BindView(R.id.fetch_user_listing_info_image_view_user_listing_fragment) @@ -74,6 +78,7 @@ public class UserListingFragment extends Fragment implements FragmentCommunicato private LinearLayoutManager mLinearLayoutManager; private String mQuery; private UserListingRecyclerViewAdapter mAdapter; + private Activity mActivity; public UserListingFragment() { // Required empty public constructor @@ -86,7 +91,7 @@ public class UserListingFragment extends Fragment implements FragmentCommunicato // Inflate the layout for this fragment View rootView = inflater.inflate(R.layout.fragment_user_listing, container, false); - ((Infinity) getActivity().getApplication()).getAppComponent().inject(this); + ((Infinity) mActivity.getApplication()).getAppComponent().inject(this); ButterKnife.bind(this, rootView); @@ -122,7 +127,7 @@ public class UserListingFragment extends Fragment implements FragmentCommunicato mUserListingViewModel.getUsers().observe(this, UserData -> mAdapter.submitList(UserData)); mUserListingViewModel.hasUser().observe(this, hasUser -> { - mProgressBar.setVisibility(View.GONE); + mSwipeRefreshLayout.setRefreshing(false); if (hasUser) { mFetchUserListingInfoLinearLayout.setVisibility(View.GONE); } else { @@ -135,13 +140,13 @@ public class UserListingFragment extends Fragment implements FragmentCommunicato mUserListingViewModel.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); mFetchUserListingInfoLinearLayout.setOnClickListener(view -> refresh()); showErrorView(R.string.search_users_error); } else { - mProgressBar.setVisibility(View.VISIBLE); + mSwipeRefreshLayout.setRefreshing(true); } }); @@ -149,12 +154,25 @@ public class UserListingFragment extends Fragment implements FragmentCommunicato mAdapter.setNetworkState(networkState); }); + mSwipeRefreshLayout.setOnRefreshListener(() -> mUserListingViewModel.refresh()); + + TypedValue typedValue = new TypedValue(); + mActivity.getTheme().resolveAttribute(R.attr.cardViewBackgroundColor, typedValue, true); + mSwipeRefreshLayout.setProgressBackgroundColorSchemeColor(typedValue.data); + mSwipeRefreshLayout.setColorSchemeResources(R.color.colorAccent); + return rootView; } + @Override + public void onAttach(@NonNull Context context) { + super.onAttach(context); + mActivity = (Activity) context; + } + private void showErrorView(int stringResId) { if (getActivity() != null && isAdded()) { - mProgressBar.setVisibility(View.GONE); + mSwipeRefreshLayout.setRefreshing(false); mFetchUserListingInfoLinearLayout.setVisibility(View.VISIBLE); mFetchUserListingInfoTextView.setText(stringResId); Glide.with(this).load(R.drawable.error_image).into(mFetchUserListingInfoImageView); |