Swipe to refresh in SubscribedThingActivity and MultiRedditListingActivity.

This commit is contained in:
Alex Ning 2019-12-17 21:33:15 +08:00
parent cdd25f3405
commit e69c26db19
12 changed files with 150 additions and 28 deletions

View File

@ -965,7 +965,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
private PostFragment allPostFragment;
SectionsPagerAdapter(FragmentManager fm) {
super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
super(fm);
}
@NonNull

View File

@ -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.MenuItem;
import android.view.View;
import android.view.ViewGroup;
@ -13,12 +14,14 @@ import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.widget.Toolbar;
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;
@ -55,6 +58,8 @@ public class MultiRedditListingActivity extends BaseActivity {
AppBarLayout mAppBarLayout;
@BindView(R.id.toolbar_multi_reddit_listing_activity)
Toolbar mToolbar;
@BindView(R.id.swipe_refresh_layout_multi_reddit_listing_activity)
SwipeRefreshLayout mSwipeRefreshLayout;
@BindView(R.id.recycler_view_multi_reddit_listing_activity)
RecyclerView mRecyclerView;
@BindView(R.id.fetch_multi_reddit_listing_info_linear_layout_multi_reddit_listing_activity)
@ -137,6 +142,13 @@ public class MultiRedditListingActivity extends BaseActivity {
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mSwipeRefreshLayout.setOnRefreshListener(this::loadMultiReddits);
TypedValue typedValue = new TypedValue();
getTheme().resolveAttribute(R.attr.cardViewBackgroundColor, typedValue, true);
mSwipeRefreshLayout.setProgressBackgroundColorSchemeColor(typedValue.data);
mSwipeRefreshLayout.setColorSchemeResources(R.color.colorAccent);
if (savedInstanceState != null) {
mInsertSuccess = savedInstanceState.getBoolean(INSERT_MULTI_REDDIT_STATE);
mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE);
@ -202,15 +214,21 @@ public class MultiRedditListingActivity extends BaseActivity {
}
private void loadMultiReddits() {
mSwipeRefreshLayout.setRefreshing(true);
GetMultiReddit.getMyMultiReddits(mOauthRetrofit, mAccessToken, new GetMultiReddit.GetMultiRedditListener() {
@Override
public void success(ArrayList<MultiReddit> multiReddits) {
new InsertMultiRedditAsyncTask(mRedditDataRoomDatabase, multiReddits, () -> mInsertSuccess = true).execute();
new InsertMultiRedditAsyncTask(mRedditDataRoomDatabase, multiReddits, () -> {
mInsertSuccess = true;
mSwipeRefreshLayout.setRefreshing(false);
}).execute();
}
@Override
public void failed() {
mInsertSuccess = false;
mSwipeRefreshLayout.setRefreshing(false);
Toast.makeText(MultiRedditListingActivity.this, R.string.error_loading_multi_reddit_list, Toast.LENGTH_SHORT).show();
}
});
}

View File

@ -10,6 +10,7 @@ import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.widget.Toolbar;
@ -38,13 +39,14 @@ import ml.docilealligator.infinityforreddit.Event.SwitchAccountEvent;
import ml.docilealligator.infinityforreddit.FetchSubscribedThing;
import ml.docilealligator.infinityforreddit.Fragment.FollowedUsersListingFragment;
import ml.docilealligator.infinityforreddit.Fragment.SubscribedSubredditsListingFragment;
import ml.docilealligator.infinityforreddit.FragmentCommunicator;
import ml.docilealligator.infinityforreddit.Infinity;
import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils;
import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditData;
import ml.docilealligator.infinityforreddit.SubscribedSubredditDatabase.SubscribedSubredditData;
import ml.docilealligator.infinityforreddit.SubscribedUserDatabase.SubscribedUserData;
import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils;
import retrofit2.Retrofit;
public class SubscribedThingListingActivity extends BaseActivity {
@ -170,7 +172,7 @@ public class SubscribedThingListingActivity extends BaseActivity {
viewPager.setOffscreenPageLimit(2);
tabLayout.setupWithViewPager(viewPager);
loadSubscriptions();
loadSubscriptions(false);
}
@Override
@ -198,8 +200,8 @@ public class SubscribedThingListingActivity extends BaseActivity {
EventBus.getDefault().unregister(this);
}
private void loadSubscriptions() {
if (!mInsertSuccess) {
public void loadSubscriptions(boolean forceLoad) {
if (!(!forceLoad && mInsertSuccess)) {
FetchSubscribedThing.fetchSubscribedThing(mOauthRetrofit, mAccessToken, mAccountName, null,
new ArrayList<>(), new ArrayList<>(),
new ArrayList<>(),
@ -214,12 +216,18 @@ public class SubscribedThingListingActivity extends BaseActivity {
subscribedSubredditData,
subscribedUserData,
subredditData,
() -> mInsertSuccess = true).execute();
() -> {
mInsertSuccess = true;
sectionsPagerAdapter.stopRefreshProgressbar();
}).execute();
}
@Override
public void onFetchSubscribedThingFail() {
mInsertSuccess = false;
sectionsPagerAdapter.stopRefreshProgressbar();
Toast.makeText(SubscribedThingListingActivity.this,
R.string.error_loading_subscriptions, Toast.LENGTH_SHORT).show();
}
});
}
@ -232,6 +240,9 @@ public class SubscribedThingListingActivity extends BaseActivity {
private class SectionsPagerAdapter extends FragmentPagerAdapter {
private SubscribedSubredditsListingFragment subscribedSubredditsListingFragment;
private FollowedUsersListingFragment followedUsersListingFragment;
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@ -279,7 +290,23 @@ public class SubscribedThingListingActivity extends BaseActivity {
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
return super.instantiateItem(container, position);
Fragment fragment = (Fragment) super.instantiateItem(container, position);
if (position == 0) {
subscribedSubredditsListingFragment = (SubscribedSubredditsListingFragment) fragment;
} else if (position == 1) {
followedUsersListingFragment = (FollowedUsersListingFragment) fragment;
}
return fragment;
}
public void stopRefreshProgressbar() {
if (subscribedSubredditsListingFragment != null) {
((FragmentCommunicator) subscribedSubredditsListingFragment).stopRefreshProgressbar();
}
if (followedUsersListingFragment != null) {
((FragmentCommunicator) followedUsersListingFragment).stopRefreshProgressbar();
}
}
}
}

View File

@ -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.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -16,6 +17,7 @@ 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;
@ -25,7 +27,9 @@ import javax.inject.Named;
import butterknife.BindView;
import butterknife.ButterKnife;
import ml.docilealligator.infinityforreddit.Activity.SubscribedThingListingActivity;
import ml.docilealligator.infinityforreddit.Adapter.FollowedUsersRecyclerViewAdapter;
import ml.docilealligator.infinityforreddit.FragmentCommunicator;
import ml.docilealligator.infinityforreddit.Infinity;
import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
@ -36,11 +40,13 @@ import retrofit2.Retrofit;
/**
* A simple {@link Fragment} subclass.
*/
public class FollowedUsersListingFragment extends Fragment {
public class FollowedUsersListingFragment extends Fragment implements FragmentCommunicator {
public static final String EXTRA_ACCOUNT_NAME = "EAN";
public static final String EXTRA_ACCESS_TOKEN = "EAT";
@BindView(R.id.swipe_refresh_layout_followed_users_listing_fragment)
SwipeRefreshLayout mSwipeRefreshLayout;
@BindView(R.id.recycler_view_followed_users_listing_fragment)
RecyclerView mRecyclerView;
@BindView(R.id.no_subscriptions_linear_layout_followed_users_listing_fragment)
@ -84,6 +90,16 @@ public class FollowedUsersListingFragment extends Fragment {
mGlide = Glide.with(this);
if (mActivity instanceof SubscribedThingListingActivity) {
mSwipeRefreshLayout.setOnRefreshListener(() -> ((SubscribedThingListingActivity) mActivity).loadSubscriptions(true));
TypedValue typedValue = new TypedValue();
mActivity.getTheme().resolveAttribute(R.attr.cardViewBackgroundColor, typedValue, true);
mSwipeRefreshLayout.setProgressBackgroundColorSchemeColor(typedValue.data);
mSwipeRefreshLayout.setColorSchemeResources(R.color.colorAccent);
} else {
mSwipeRefreshLayout.setEnabled(false);
}
mRecyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
FollowedUsersRecyclerViewAdapter adapter = new FollowedUsersRecyclerViewAdapter(mActivity,
mOauthRetrofit, mRedditDataRoomDatabase, getArguments().getString(EXTRA_ACCESS_TOKEN));
@ -94,6 +110,7 @@ public class FollowedUsersListingFragment extends Fragment {
.get(SubscribedUserViewModel.class);
mSubscribedUserViewModel.getAllSubscribedUsers().observe(this, subscribedUserData -> {
mSwipeRefreshLayout.setRefreshing(false);
if (subscribedUserData == null || subscribedUserData.size() == 0) {
mRecyclerView.setVisibility(View.GONE);
mLinearLayout.setVisibility(View.VISIBLE);
@ -107,6 +124,7 @@ public class FollowedUsersListingFragment extends Fragment {
});
mSubscribedUserViewModel.getAllFavoriteSubscribedUsers().observe(this, favoriteSubscribedUserData -> {
mSwipeRefreshLayout.setRefreshing(false);
if (favoriteSubscribedUserData != null && favoriteSubscribedUserData.size() > 0) {
mLinearLayout.setVisibility(View.GONE);
mRecyclerView.setVisibility(View.VISIBLE);
@ -118,4 +136,8 @@ public class FollowedUsersListingFragment extends Fragment {
return rootView;
}
@Override
public void stopRefreshProgressbar() {
mSwipeRefreshLayout.setRefreshing(false);
}
}

View File

@ -12,7 +12,6 @@ import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.util.Log;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.view.LayoutInflater;

View File

@ -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.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -17,6 +18,7 @@ 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;
@ -27,7 +29,9 @@ import javax.inject.Named;
import butterknife.BindView;
import butterknife.ButterKnife;
import ml.docilealligator.infinityforreddit.Activity.SubredditSelectionActivity;
import ml.docilealligator.infinityforreddit.Activity.SubscribedThingListingActivity;
import ml.docilealligator.infinityforreddit.Adapter.SubscribedSubredditsRecyclerViewAdapter;
import ml.docilealligator.infinityforreddit.FragmentCommunicator;
import ml.docilealligator.infinityforreddit.Infinity;
import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
@ -38,7 +42,7 @@ import retrofit2.Retrofit;
/**
* A simple {@link Fragment} subclass.
*/
public class SubscribedSubredditsListingFragment extends Fragment {
public class SubscribedSubredditsListingFragment extends Fragment implements FragmentCommunicator {
public static final String EXTRA_ACCOUNT_NAME = "EAN";
public static final String EXTRA_ACCESS_TOKEN = "EAT";
@ -46,6 +50,8 @@ public class SubscribedSubredditsListingFragment extends Fragment {
public static final String EXTRA_IS_SUBREDDIT_SELECTION = "EISS";
public static final String EXTRA_EXTRA_CLEAR_SELECTION = "EECS";
@BindView(R.id.swipe_refresh_layout_subscribed_subreddits_listing_fragment)
SwipeRefreshLayout mSwipeRefreshLayout;
@BindView(R.id.recycler_view_subscribed_subreddits_listing_fragment)
RecyclerView mRecyclerView;
@BindView(R.id.no_subscriptions_linear_layout_subreddits_listing_fragment)
@ -92,6 +98,16 @@ public class SubscribedSubredditsListingFragment extends Fragment {
mGlide = Glide.with(this);
if (mActivity instanceof SubscribedThingListingActivity) {
mSwipeRefreshLayout.setOnRefreshListener(() -> ((SubscribedThingListingActivity) mActivity).loadSubscriptions(true));
TypedValue typedValue = new TypedValue();
mActivity.getTheme().resolveAttribute(R.attr.cardViewBackgroundColor, typedValue, true);
mSwipeRefreshLayout.setProgressBackgroundColorSchemeColor(typedValue.data);
mSwipeRefreshLayout.setColorSchemeResources(R.color.colorAccent);
} else {
mSwipeRefreshLayout.setEnabled(false);
}
mRecyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
SubscribedSubredditsRecyclerViewAdapter adapter;
@ -108,6 +124,7 @@ public class SubscribedSubredditsListingFragment extends Fragment {
new SubscribedSubredditViewModel.Factory(mActivity.getApplication(), mRedditDataRoomDatabase, accountName))
.get(SubscribedSubredditViewModel.class);
mSubscribedSubredditViewModel.getAllSubscribedSubreddits().observe(this, subscribedSubredditData -> {
mSwipeRefreshLayout.setRefreshing(false);
if (subscribedSubredditData == null || subscribedSubredditData.size() == 0) {
mRecyclerView.setVisibility(View.GONE);
mLinearLayout.setVisibility(View.VISIBLE);
@ -123,6 +140,7 @@ public class SubscribedSubredditsListingFragment extends Fragment {
});
mSubscribedSubredditViewModel.getAllFavoriteSubscribedSubreddits().observe(this, favoriteSubscribedSubredditData -> {
mSwipeRefreshLayout.setRefreshing(false);
if (favoriteSubscribedSubredditData != null && favoriteSubscribedSubredditData.size() > 0) {
mLinearLayout.setVisibility(View.GONE);
mRecyclerView.setVisibility(View.VISIBLE);
@ -134,4 +152,9 @@ public class SubscribedSubredditsListingFragment extends Fragment {
return rootView;
}
@Override
public void stopRefreshProgressbar() {
mSwipeRefreshLayout.setRefreshing(false);
}
}

View File

@ -1,9 +1,12 @@
package ml.docilealligator.infinityforreddit;
public interface FragmentCommunicator {
void refresh();
default void refresh() {
}
default boolean handleKeyDown(int keyCode) { return false; }
default boolean handleKeyDown(int keyCode) {
return false;
}
default void changeNSFW(boolean nsfw) {
}
@ -25,6 +28,9 @@ public interface FragmentCommunicator {
return false;
}
default void changePostLayout(int postLayout) { }
default void changePostLayout(int postLayout) {
}
default void stopRefreshProgressbar() {
}
}

View File

@ -68,10 +68,14 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> {
if (postType == TYPE_SUBREDDIT) {
this.subredditOrUserName = path;
} else {
if (path.endsWith("/")) {
multiRedditPath = path + sortType.getType().value;
if (sortType != null) {
if (path.endsWith("/")) {
multiRedditPath = path + sortType.getType().value;
} else {
multiRedditPath = path + "/" + sortType.getType().value;
}
} else {
multiRedditPath = path + "/" + sortType.getType().value;
multiRedditPath = path;
}
}
paginationNetworkStateLiveData = new MutableLiveData<>();

View File

@ -31,11 +31,18 @@
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view_multi_reddit_listing_activity"
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/swipe_refresh_layout_multi_reddit_listing_activity"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view_multi_reddit_listing_activity"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
<LinearLayout
android:id="@+id/fetch_multi_reddit_listing_info_linear_layout_multi_reddit_listing_activity"

View File

@ -5,11 +5,18 @@
android:layout_height="match_parent"
tools:application=".FollowedUsersListingFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view_followed_users_listing_fragment"
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/swipe_refresh_layout_followed_users_listing_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false" />
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view_followed_users_listing_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
<LinearLayout
android:id="@+id/no_subscriptions_linear_layout_followed_users_listing_fragment"

View File

@ -5,11 +5,18 @@
android:layout_height="match_parent"
tools:application=".SubscribedSubredditsListingFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view_subscribed_subreddits_listing_fragment"
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/swipe_refresh_layout_subscribed_subreddits_listing_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false" />
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view_subscribed_subreddits_listing_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
<LinearLayout
android:id="@+id/no_subscriptions_linear_layout_subreddits_listing_fragment"

View File

@ -375,4 +375,6 @@
<string name="elapsed_time_years_ago">%1$d Years</string>
<string name="error_getting_multi_reddit_data">Error getting multireddit data</string>
<string name="error_loading_multi_reddit_list">Cannot sync multireddits</string>
<string name="error_loading_subscriptions">Cannot sync subscriptions</string>
</resources>