diff options
author | Alex Ning <chineseperson5@gmail.com> | 2019-12-17 13:33:15 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2019-12-17 13:33:15 +0000 |
commit | e69c26db19b4940acdd25573a0b92c67eef0e076 (patch) | |
tree | 44b7b1b847f6e4688b717b4fa216a6854277e28f /app/src/main/java | |
parent | cdd25f3405b7a41e796507612e83b394a1479d64 (diff) | |
download | infinity-for-reddit-e69c26db19b4940acdd25573a0b92c67eef0e076.tar infinity-for-reddit-e69c26db19b4940acdd25573a0b92c67eef0e076.tar.gz infinity-for-reddit-e69c26db19b4940acdd25573a0b92c67eef0e076.tar.bz2 infinity-for-reddit-e69c26db19b4940acdd25573a0b92c67eef0e076.tar.lz infinity-for-reddit-e69c26db19b4940acdd25573a0b92c67eef0e076.tar.xz infinity-for-reddit-e69c26db19b4940acdd25573a0b92c67eef0e076.tar.zst infinity-for-reddit-e69c26db19b4940acdd25573a0b92c67eef0e076.zip |
Swipe to refresh in SubscribedThingActivity and MultiRedditListingActivity.
Diffstat (limited to 'app/src/main/java')
8 files changed, 116 insertions, 17 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MainActivity.java index 11e1480f..6b592d72 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MainActivity.java @@ -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 diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MultiRedditListingActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MultiRedditListingActivity.java index 63aaffdb..a4d9d6d0 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MultiRedditListingActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MultiRedditListingActivity.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.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(); } }); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SubscribedThingListingActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SubscribedThingListingActivity.java index 1f0c1e20..061c3c06 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SubscribedThingListingActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SubscribedThingListingActivity.java @@ -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(); + } } } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/FollowedUsersListingFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/FollowedUsersListingFragment.java index 43603177..93f2b36b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/FollowedUsersListingFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/FollowedUsersListingFragment.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.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); + } } 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 8e579689..f2c9f494 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java @@ -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; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/SubscribedSubredditsListingFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/SubscribedSubredditsListingFragment.java index ceb4aeed..9f0dd711 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/SubscribedSubredditsListingFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/SubscribedSubredditsListingFragment.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.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); + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FragmentCommunicator.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FragmentCommunicator.java index 07cda1bb..e67ae8ca 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FragmentCommunicator.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FragmentCommunicator.java @@ -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() { + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java index fc207a02..7244cb85 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java @@ -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<>(); |