From 54e0b2f3327e8858a3f4640eeda5f3d8ec223890 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Tue, 5 Jan 2021 16:00:45 +0800 Subject: [PATCH] Directly adding subreddit to multireddit is available. --- app/src/main/AndroidManifest.xml | 9 +- .../infinityforreddit/AppComponent.java | 3 + .../MultiredditSelectionActivity.java | 250 ++++++++++++++++++ .../ViewSubredditDetailActivity.java | 162 +++++++----- ...MultiRedditListingRecyclerViewAdapter.java | 41 ++- .../infinityforreddit/apis/RedditAPI.java | 7 + .../AddSubredditOrUserToMultiReddit.java | 43 +++ .../fragments/MultiRedditListingFragment.java | 68 +++-- .../layout/activity_multireddit_selection.xml | 41 +++ .../menu/view_subreddit_detail_activity.xml | 8 +- app/src/main/res/values/strings.xml | 6 + 11 files changed, 532 insertions(+), 106 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/activities/MultiredditSelectionActivity.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/AddSubredditOrUserToMultiReddit.java create mode 100644 app/src/main/res/layout/activity_multireddit_selection.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 4cd7c035..4a938525 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -32,7 +32,14 @@ android:theme="@style/AppTheme" android:usesCleartextTraffic="true" tools:replace="android:label"> - + + = Build.VERSION_CODES.M) { + Window window = getWindow(); + + if (isChangeStatusBarIconColor()) { + addOnOffsetChangedListener(appBarLayout); + } + + if (isImmersiveInterface()) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { + coordinatorLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | + View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | + View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION); + } else { + window.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS); + } + adjustToolbar(toolbar); + } + } + + setSupportActionBar(toolbar); + getSupportActionBar().setDisplayHomeAsUpEnabled(true); + + if (savedInstanceState == null) { + getCurrentAccountAndBindView(); + } else { + mInsertSuccess = savedInstanceState.getBoolean(INSERT_SUBSCRIBED_MULTIREDDIT_STATE); + mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE); + mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE); + mAccountName = savedInstanceState.getString(ACCOUNT_NAME_STATE); + + if (!mNullAccessToken && mAccountName == null) { + getCurrentAccountAndBindView(); + } else { + mFragment = getSupportFragmentManager().getFragment(savedInstanceState, FRAGMENT_OUT_STATE); + getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_multireddit_selection_activity, mFragment).commit(); + bindView(false); + } + } + } + + @Override + protected SharedPreferences getDefaultSharedPreferences() { + return mSharedPreferences; + } + + @Override + protected CustomThemeWrapper getCustomThemeWrapper() { + return mCustomThemeWrapper; + } + + @Override + protected void applyCustomTheme() { + coordinatorLayout.setBackgroundColor(mCustomThemeWrapper.getBackgroundColor()); + applyAppBarLayoutAndToolbarTheme(appBarLayout, toolbar); + } + + private void getCurrentAccountAndBindView() { + GetCurrentAccount.getCurrentAccount(mExecutor, new Handler(), mRedditDataRoomDatabase, account -> { + if (account == null) { + mNullAccessToken = true; + } else { + mAccessToken = account.getAccessToken(); + mAccountName = account.getUsername(); + } + bindView(true); + }); + } + + private void bindView(boolean initializeFragment) { + if (isFinishing() || isDestroyed()) { + return; + } + + loadMultiReddits(); + + if (initializeFragment) { + mFragment = new MultiRedditListingFragment(); + Bundle bundle = new Bundle(); + bundle.putString(MultiRedditListingFragment.EXTRA_ACCOUNT_NAME, mAccountName); + bundle.putString(MultiRedditListingFragment.EXTRA_ACCESS_TOKEN, mAccessToken); + bundle.putBoolean(MultiRedditListingFragment.EXTRA_IS_GETTING_MULTIREDDIT_INFO, true); + mFragment.setArguments(bundle); + getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_multireddit_selection_activity, mFragment).commit(); + } + } + + private void loadMultiReddits() { + if (!mInsertSuccess) { + FetchMyMultiReddits.fetchMyMultiReddits(mOauthRetrofit, mAccessToken, new FetchMyMultiReddits.FetchMyMultiRedditsListener() { + @Override + public void success(ArrayList multiReddits) { + new InsertMultiRedditAsyncTask(mRedditDataRoomDatabase, multiReddits, mAccountName, () -> { + mInsertSuccess = true; + ((MultiRedditListingFragment) mFragment).stopRefreshProgressbar(); + }).execute(); + } + + @Override + public void failed() { + mInsertSuccess = false; + ((MultiRedditListingFragment) mFragment).stopRefreshProgressbar(); + Toast.makeText(MultiredditSelectionActivity.this, R.string.error_loading_multi_reddit_list, Toast.LENGTH_SHORT).show(); + } + }); + } + } + + @Override + public boolean onOptionsItemSelected(@NonNull MenuItem item) { + if (item.getItemId() == android.R.id.home) { + finish(); + return true; + } + + return false; + } + + public void getSelectedMultireddit(MultiReddit multiReddit) { + Intent returnIntent = new Intent(); + returnIntent.putExtra(EXTRA_RETURN_MULTIREDDIT, multiReddit); + setResult(Activity.RESULT_OK, returnIntent); + finish(); + } + + @Override + protected void onSaveInstanceState(@NonNull Bundle outState) { + super.onSaveInstanceState(outState); + getSupportFragmentManager().putFragment(outState, FRAGMENT_OUT_STATE, mFragment); + outState.putBoolean(INSERT_SUBSCRIBED_MULTIREDDIT_STATE, mInsertSuccess); + outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken); + outState.putString(ACCESS_TOKEN_STATE, mAccessToken); + outState.putString(ACCOUNT_NAME_STATE, mAccountName); + } + + @Override + protected void onDestroy() { + super.onDestroy(); + EventBus.getDefault().unregister(this); + } + + @Subscribe + public void onAccountSwitchEvent(SwitchAccountEvent event) { + finish(); + } + + @Override + public void onLongPress() { + if (mFragment != null) { + ((MultiRedditListingFragment) mFragment).goBackToTop(); + } + } +} \ No newline at end of file diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java index fce50705..aa4732fa 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java @@ -70,6 +70,7 @@ import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.SortType; import ml.docilealligator.infinityforreddit.SortTypeSelectionCallback; +import ml.docilealligator.infinityforreddit.asynctasks.AddSubredditOrUserToMultiReddit; import ml.docilealligator.infinityforreddit.asynctasks.CheckIsSubscribedToSubredditAsyncTask; import ml.docilealligator.infinityforreddit.asynctasks.GetCurrentAccount; import ml.docilealligator.infinityforreddit.asynctasks.InsertSubredditDataAsyncTask; @@ -87,6 +88,7 @@ import ml.docilealligator.infinityforreddit.events.SwitchAccountEvent; import ml.docilealligator.infinityforreddit.fragments.PostFragment; import ml.docilealligator.infinityforreddit.fragments.SidebarFragment; import ml.docilealligator.infinityforreddit.message.ReadMessage; +import ml.docilealligator.infinityforreddit.multireddit.MultiReddit; import ml.docilealligator.infinityforreddit.post.Post; import ml.docilealligator.infinityforreddit.post.PostDataSource; import ml.docilealligator.infinityforreddit.readpost.InsertReadPost; @@ -117,6 +119,7 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp private static final String ACCOUNT_NAME_STATE = "ANS"; private static final String MESSAGE_FULLNAME_STATE = "MFS"; private static final String NEW_ACCOUNT_NAME_STATE = "NANS"; + private static final int ADD_TO_MULTIREDDIT_REQUEST_CODE = 1; @BindView(R.id.coordinator_layout_view_subreddit_detail_activity) CoordinatorLayout coordinatorLayout; @@ -902,78 +905,113 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp @Override public boolean onOptionsItemSelected(@NonNull MenuItem item) { - switch (item.getItemId()) { - case android.R.id.home: - finish(); - return true; - case R.id.action_sort_view_subreddit_detail_activity: - displaySortTypeBottomSheetFragment(); - return true; - case R.id.action_search_view_subreddit_detail_activity: - Intent intent = new Intent(this, SearchActivity.class); - intent.putExtra(SearchActivity.EXTRA_SUBREDDIT_NAME, subredditName); - startActivity(intent); - return true; - case R.id.action_refresh_view_subreddit_detail_activity: - if (mMenu != null) { - mMenu.findItem(R.id.action_lazy_mode_view_subreddit_detail_activity).setTitle(R.string.action_start_lazy_mode); - } - if (sectionsPagerAdapter != null) { - sectionsPagerAdapter.refresh(); - mFetchSubredditInfoSuccess = false; - fetchSubredditData(); - } - return true; - case R.id.action_lazy_mode_view_subreddit_detail_activity: - if (sectionsPagerAdapter != null) { - MenuItem lazyModeItem = mMenu.findItem(R.id.action_lazy_mode_view_subreddit_detail_activity); - if (isInLazyMode) { - isInLazyMode = false; - sectionsPagerAdapter.stopLazyMode(); - lazyModeItem.setTitle(R.string.action_start_lazy_mode); - params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS | - AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS_COLLAPSED); + int itemId = item.getItemId(); + if (itemId == android.R.id.home) { + finish(); + return true; + } else if (itemId == R.id.action_sort_view_subreddit_detail_activity) { + displaySortTypeBottomSheetFragment(); + return true; + } else if (itemId == R.id.action_search_view_subreddit_detail_activity) { + Intent intent = new Intent(this, SearchActivity.class); + intent.putExtra(SearchActivity.EXTRA_SUBREDDIT_NAME, subredditName); + startActivity(intent); + return true; + } else if (itemId == R.id.action_refresh_view_subreddit_detail_activity) { + if (mMenu != null) { + mMenu.findItem(R.id.action_lazy_mode_view_subreddit_detail_activity).setTitle(R.string.action_start_lazy_mode); + } + if (sectionsPagerAdapter != null) { + sectionsPagerAdapter.refresh(); + mFetchSubredditInfoSuccess = false; + fetchSubredditData(); + } + return true; + } else if (itemId == R.id.action_lazy_mode_view_subreddit_detail_activity) { + if (sectionsPagerAdapter != null) { + MenuItem lazyModeItem = mMenu.findItem(R.id.action_lazy_mode_view_subreddit_detail_activity); + if (isInLazyMode) { + isInLazyMode = false; + sectionsPagerAdapter.stopLazyMode(); + lazyModeItem.setTitle(R.string.action_start_lazy_mode); + params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS | + AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS_COLLAPSED); + collapsingToolbarLayout.setLayoutParams(params); + } else { + isInLazyMode = true; + if (sectionsPagerAdapter.startLazyMode()) { + lazyModeItem.setTitle(R.string.action_stop_lazy_mode); + appBarLayout.setExpanded(false); + params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_EXIT_UNTIL_COLLAPSED); collapsingToolbarLayout.setLayoutParams(params); } else { - isInLazyMode = true; - if (sectionsPagerAdapter.startLazyMode()) { - lazyModeItem.setTitle(R.string.action_stop_lazy_mode); - appBarLayout.setExpanded(false); - params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_EXIT_UNTIL_COLLAPSED); - collapsingToolbarLayout.setLayoutParams(params); - } else { - isInLazyMode = false; - } + isInLazyMode = false; } } + } + return true; + } else if (itemId == R.id.action_change_post_layout_view_subreddit_detail_activity) { + PostLayoutBottomSheetFragment postLayoutBottomSheetFragment = new PostLayoutBottomSheetFragment(); + postLayoutBottomSheetFragment.show(getSupportFragmentManager(), postLayoutBottomSheetFragment.getTag()); + return true; + } else if (itemId == R.id.action_select_user_flair_view_subreddit_detail_activity) { + if (mAccessToken == null) { + Toast.makeText(this, R.string.login_first, Toast.LENGTH_SHORT).show(); return true; - case R.id.action_change_post_layout_view_subreddit_detail_activity: - PostLayoutBottomSheetFragment postLayoutBottomSheetFragment = new PostLayoutBottomSheetFragment(); - postLayoutBottomSheetFragment.show(getSupportFragmentManager(), postLayoutBottomSheetFragment.getTag()); - return true; - case R.id.action_select_user_flair_view_subreddit_detail_activity: - if (mAccessToken == null) { - Toast.makeText(this, R.string.login_first, Toast.LENGTH_SHORT).show(); - return true; - } - Intent selectUserFlairIntent = new Intent(this, SelectUserFlairActivity.class); - selectUserFlairIntent.putExtra(SelectUserFlairActivity.EXTRA_SUBREDDIT_NAME, subredditName); - startActivity(selectUserFlairIntent); - return true; - case R.id.action_share_view_subreddit_detail_activity: - Intent shareIntent = new Intent(Intent.ACTION_SEND); - shareIntent.setType("text/plain"); - shareIntent.putExtra(Intent.EXTRA_TEXT, "https://www.reddit.com/r/" + subredditName); - if (shareIntent.resolveActivity(getPackageManager()) != null) { - startActivity(Intent.createChooser(shareIntent, getString(R.string.share))); - } else { - Toast.makeText(this, R.string.no_app, Toast.LENGTH_SHORT).show(); - } + } + Intent selectUserFlairIntent = new Intent(this, SelectUserFlairActivity.class); + selectUserFlairIntent.putExtra(SelectUserFlairActivity.EXTRA_SUBREDDIT_NAME, subredditName); + startActivity(selectUserFlairIntent); + return true; + } else if (itemId == R.id.action_add_to_multireddit_view_subreddit_detail_activity) { + if (mAccessToken == null) { + Toast.makeText(this, R.string.login_first, Toast.LENGTH_SHORT).show(); return true; + } + Intent intent = new Intent(this, MultiredditSelectionActivity.class); + startActivityForResult(intent, ADD_TO_MULTIREDDIT_REQUEST_CODE); + } else if (itemId == R.id.action_share_view_subreddit_detail_activity) { + Intent shareIntent = new Intent(Intent.ACTION_SEND); + shareIntent.setType("text/plain"); + shareIntent.putExtra(Intent.EXTRA_TEXT, "https://www.reddit.com/r/" + subredditName); + if (shareIntent.resolveActivity(getPackageManager()) != null) { + startActivity(Intent.createChooser(shareIntent, getString(R.string.share))); + } else { + Toast.makeText(this, R.string.no_app, Toast.LENGTH_SHORT).show(); + } + return true; } return false; } + @Override + protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { + super.onActivityResult(requestCode, resultCode, data); + if (requestCode == ADD_TO_MULTIREDDIT_REQUEST_CODE && resultCode == RESULT_OK) { + if (data != null) { + MultiReddit multiReddit = data.getParcelableExtra(MultiredditSelectionActivity.EXTRA_RETURN_MULTIREDDIT); + if (multiReddit != null) { + AddSubredditOrUserToMultiReddit.addSubredditOrUserToMultiReddit(mOauthRetrofit, + mAccessToken, multiReddit.getPath(), subredditName, + new AddSubredditOrUserToMultiReddit.AddSubredditOrUserToMultiRedditListener() { + @Override + public void success() { + Toast.makeText(ViewSubredditDetailActivity.this, + getString(R.string.add_subreddit_or_user_to_multireddit_success, subredditName, multiReddit.getDisplayName()), Toast.LENGTH_LONG).show(); + } + + @Override + public void failed(int code) { + Toast.makeText(ViewSubredditDetailActivity.this, + getString(R.string.add_subreddit_or_user_to_multireddit_failed, subredditName, multiReddit.getDisplayName()), Toast.LENGTH_LONG).show(); + } + }); + } + + } + } + } + @Override protected void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/MultiRedditListingRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/MultiRedditListingRecyclerViewAdapter.java index 7dd20529..5d2d3ef1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/MultiRedditListingRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/MultiRedditListingRecyclerViewAdapter.java @@ -1,7 +1,5 @@ package ml.docilealligator.infinityforreddit.adapters; -import android.content.Intent; -import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -23,13 +21,11 @@ import butterknife.BindView; import butterknife.ButterKnife; import jp.wasabeef.glide.transformations.RoundedCornersTransformation; import me.zhanghai.android.fastscroll.PopupTextProvider; -import ml.docilealligator.infinityforreddit.activities.ViewMultiRedditDetailActivity; -import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; -import ml.docilealligator.infinityforreddit.bottomsheetfragments.MultiRedditOptionsBottomSheetFragment; -import ml.docilealligator.infinityforreddit.multireddit.FavoriteMultiReddit; -import ml.docilealligator.infinityforreddit.multireddit.MultiReddit; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; +import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; +import ml.docilealligator.infinityforreddit.multireddit.FavoriteMultiReddit; +import ml.docilealligator.infinityforreddit.multireddit.MultiReddit; import pl.droidsonroids.gif.GifImageView; import retrofit2.Retrofit; @@ -46,24 +42,29 @@ public class MultiRedditListingRecyclerViewAdapter extends RecyclerView.Adapter< private RequestManager mGlide; private String mAccessToken; - private String mAccountName; private List mMultiReddits; private List mFavoriteMultiReddits; private int mPrimaryTextColor; private int mSecondaryTextColor; + private OnItemClickListener mOnItemClickListener; + + public interface OnItemClickListener { + void onClick(MultiReddit multiReddit); + void onLongClick(MultiReddit multiReddit); + } public MultiRedditListingRecyclerViewAdapter(AppCompatActivity activity, Retrofit oauthRetrofit, RedditDataRoomDatabase redditDataRoomDatabase, CustomThemeWrapper customThemeWrapper, - String accessToken, String accountName) { + String accessToken, OnItemClickListener onItemClickListener) { mActivity = activity; mGlide = Glide.with(activity); mOauthRetrofit = oauthRetrofit; mRedditDataRoomDatabase = redditDataRoomDatabase; mAccessToken = accessToken; - mAccountName = accountName; mPrimaryTextColor = customThemeWrapper.getPrimaryTextColor(); mSecondaryTextColor = customThemeWrapper.getSecondaryTextColor(); + mOnItemClickListener = onItemClickListener; } @Override @@ -176,13 +177,11 @@ public class MultiRedditListingRecyclerViewAdapter extends RecyclerView.Adapter< } }); holder.itemView.setOnClickListener(view -> { - Intent intent = new Intent(mActivity, ViewMultiRedditDetailActivity.class); - intent.putExtra(ViewMultiRedditDetailActivity.EXTRA_MULTIREDDIT_DATA, multiReddit); - mActivity.startActivity(intent); + mOnItemClickListener.onClick(multiReddit); }); holder.itemView.setOnLongClickListener(view -> { - showOptionsBottomSheetFragment(multiReddit); + mOnItemClickListener.onLongClick(multiReddit); return true; }); @@ -264,13 +263,11 @@ public class MultiRedditListingRecyclerViewAdapter extends RecyclerView.Adapter< } }); holder.itemView.setOnClickListener(view -> { - Intent intent = new Intent(mActivity, ViewMultiRedditDetailActivity.class); - intent.putExtra(ViewMultiRedditDetailActivity.EXTRA_MULTIREDDIT_DATA, multiReddit); - mActivity.startActivity(intent); + mOnItemClickListener.onClick(multiReddit); }); holder.itemView.setOnLongClickListener(view -> { - showOptionsBottomSheetFragment(multiReddit); + mOnItemClickListener.onLongClick(multiReddit); return true; }); @@ -311,14 +308,6 @@ public class MultiRedditListingRecyclerViewAdapter extends RecyclerView.Adapter< } } - private void showOptionsBottomSheetFragment(MultiReddit multiReddit) { - MultiRedditOptionsBottomSheetFragment fragment = new MultiRedditOptionsBottomSheetFragment(); - Bundle bundle = new Bundle(); - bundle.putParcelable(MultiRedditOptionsBottomSheetFragment.EXTRA_MULTI_REDDIT, multiReddit); - fragment.setArguments(bundle); - fragment.show(mActivity.getSupportFragmentManager(), fragment.getTag()); - } - public void setMultiReddits(List multiReddits) { mMultiReddits = multiReddits; notifyDataSetChanged(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/apis/RedditAPI.java b/app/src/main/java/ml/docilealligator/infinityforreddit/apis/RedditAPI.java index 516d6e36..1799ff57 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/apis/RedditAPI.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/apis/RedditAPI.java @@ -339,4 +339,11 @@ public interface RedditAPI { @POST("/api/read_all_messages") Call readAllMessages(@HeaderMap Map headers); + + + @FormUrlEncoded + @PUT("/api/multi{multipath}r/{subredditName}") + Call addSubredditToMultiReddit(@HeaderMap Map headers, @FieldMap Map params, + @Path(value = "multipath", encoded = true) String multipath, @Path("subredditName") String subredditName); + } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/AddSubredditOrUserToMultiReddit.java b/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/AddSubredditOrUserToMultiReddit.java new file mode 100644 index 00000000..d69e5f88 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/AddSubredditOrUserToMultiReddit.java @@ -0,0 +1,43 @@ +package ml.docilealligator.infinityforreddit.asynctasks; + +import androidx.annotation.NonNull; + +import java.util.HashMap; +import java.util.Map; + +import ml.docilealligator.infinityforreddit.apis.RedditAPI; +import ml.docilealligator.infinityforreddit.utils.APIUtils; +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; +import retrofit2.Retrofit; + +public class AddSubredditOrUserToMultiReddit { + public interface AddSubredditOrUserToMultiRedditListener { + void success(); + void failed(int code); + } + + public static void addSubredditOrUserToMultiReddit(Retrofit oauthRetrofit, String accessToken, String multipath, + String subredditName, + AddSubredditOrUserToMultiRedditListener addSubredditOrUserToMultiRedditListener) { + Map params = new HashMap<>(); + params.put(APIUtils.MODEL_KEY, "{\"name\":\"" + subredditName + "\"}"); + oauthRetrofit.create(RedditAPI.class).addSubredditToMultiReddit(APIUtils.getOAuthHeader(accessToken), params, multipath, subredditName) + .enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { + if (response.isSuccessful()) { + addSubredditOrUserToMultiRedditListener.success(); + } else { + addSubredditOrUserToMultiRedditListener.failed(response.code()); + } + } + + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + addSubredditOrUserToMultiRedditListener.failed(-1); + } + }); + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/MultiRedditListingFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/MultiRedditListingFragment.java index b9ae3f7b..28f9a0bf 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/MultiRedditListingFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/MultiRedditListingFragment.java @@ -1,6 +1,7 @@ package ml.docilealligator.infinityforreddit.fragments; import android.content.Context; +import android.content.Intent; import android.content.SharedPreferences; import android.content.res.Resources; import android.os.Build; @@ -13,6 +14,7 @@ import android.widget.LinearLayout; import android.widget.TextView; import androidx.annotation.NonNull; +import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.Fragment; import androidx.lifecycle.ViewModelProvider; import androidx.recyclerview.widget.LinearLayoutManager; @@ -28,15 +30,19 @@ import javax.inject.Named; import butterknife.BindView; import butterknife.ButterKnife; import me.zhanghai.android.fastscroll.FastScrollerBuilder; -import ml.docilealligator.infinityforreddit.activities.BaseActivity; -import ml.docilealligator.infinityforreddit.activities.SubscribedThingListingActivity; -import ml.docilealligator.infinityforreddit.adapters.MultiRedditListingRecyclerViewAdapter; -import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.FragmentCommunicator; import ml.docilealligator.infinityforreddit.Infinity; -import ml.docilealligator.infinityforreddit.multireddit.MultiRedditViewModel; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; +import ml.docilealligator.infinityforreddit.activities.BaseActivity; +import ml.docilealligator.infinityforreddit.activities.MultiredditSelectionActivity; +import ml.docilealligator.infinityforreddit.activities.SubscribedThingListingActivity; +import ml.docilealligator.infinityforreddit.activities.ViewMultiRedditDetailActivity; +import ml.docilealligator.infinityforreddit.adapters.MultiRedditListingRecyclerViewAdapter; +import ml.docilealligator.infinityforreddit.bottomsheetfragments.MultiRedditOptionsBottomSheetFragment; +import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; +import ml.docilealligator.infinityforreddit.multireddit.MultiReddit; +import ml.docilealligator.infinityforreddit.multireddit.MultiRedditViewModel; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; import retrofit2.Retrofit; @@ -44,6 +50,7 @@ public class MultiRedditListingFragment extends Fragment implements FragmentComm public static final String EXTRA_ACCOUNT_NAME = "EAN"; public static final String EXTRA_ACCESS_TOKEN = "EAT"; + public static final String EXTRA_IS_GETTING_MULTIREDDIT_INFO = "EIGMI"; @BindView(R.id.swipe_refresh_layout_multi_reddit_listing_fragment) SwipeRefreshLayout mSwipeRefreshLayout; @@ -67,7 +74,7 @@ public class MultiRedditListingFragment extends Fragment implements FragmentComm CustomThemeWrapper mCustomThemeWrapper; public MultiRedditViewModel mMultiRedditViewModel; - private SubscribedThingListingActivity mActivity; + private AppCompatActivity mActivity; private RequestManager mGlide; private LinearLayoutManager mLinearLayoutManager; @@ -86,7 +93,7 @@ public class MultiRedditListingFragment extends Fragment implements FragmentComm applyTheme(); - if ((mActivity instanceof BaseActivity && ((BaseActivity) mActivity).isImmersiveInterface())) { + if ((mActivity != null && ((BaseActivity) mActivity).isImmersiveInterface())) { mRecyclerView.setPadding(0, 0, 0, ((BaseActivity) mActivity).getNavBarHeight()); } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && mSharedPreferences.getBoolean(SharedPreferencesUtils.IMMERSIVE_INTERFACE_KEY, true)) { @@ -99,25 +106,46 @@ public class MultiRedditListingFragment extends Fragment implements FragmentComm String accountName = getArguments().getString(EXTRA_ACCOUNT_NAME); String accessToken = getArguments().getString(EXTRA_ACCESS_TOKEN); + boolean isGettingMultiredditInfo = getArguments().getBoolean(EXTRA_IS_GETTING_MULTIREDDIT_INFO, false); mGlide = Glide.with(this); mLinearLayoutManager = new LinearLayoutManager(mActivity); mRecyclerView.setLayoutManager(mLinearLayoutManager); MultiRedditListingRecyclerViewAdapter adapter = new MultiRedditListingRecyclerViewAdapter(mActivity, - mOauthRetrofit, mRedditDataRoomDatabase, mCustomThemeWrapper, accessToken, accountName); - mRecyclerView.setAdapter(adapter); - mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { + mOauthRetrofit, mRedditDataRoomDatabase, mCustomThemeWrapper, accessToken, new MultiRedditListingRecyclerViewAdapter.OnItemClickListener() { @Override - public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { - super.onScrolled(recyclerView, dx, dy); - if (dy > 0) { - mActivity.fab.hide(); + public void onClick(MultiReddit multiReddit) { + if (mActivity instanceof MultiredditSelectionActivity) { + ((MultiredditSelectionActivity) mActivity).getSelectedMultireddit(multiReddit); } else { - mActivity.fab.show(); + Intent intent = new Intent(mActivity, ViewMultiRedditDetailActivity.class); + intent.putExtra(ViewMultiRedditDetailActivity.EXTRA_MULTIREDDIT_DATA, multiReddit); + mActivity.startActivity(intent); + } + } + + @Override + public void onLongClick(MultiReddit multiReddit) { + if (!isGettingMultiredditInfo) { + showOptionsBottomSheetFragment(multiReddit); } } }); + mRecyclerView.setAdapter(adapter); + if (mActivity instanceof SubscribedThingListingActivity) { + mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { + @Override + public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { + super.onScrolled(recyclerView, dx, dy); + if (dy > 0) { + ((SubscribedThingListingActivity) mActivity).fab.hide(); + } else { + ((SubscribedThingListingActivity) mActivity).fab.show(); + } + } + }); + } new FastScrollerBuilder(mRecyclerView).build(); mMultiRedditViewModel = new ViewModelProvider(this, @@ -149,6 +177,14 @@ public class MultiRedditListingFragment extends Fragment implements FragmentComm return rootView; } + private void showOptionsBottomSheetFragment(MultiReddit multiReddit) { + MultiRedditOptionsBottomSheetFragment fragment = new MultiRedditOptionsBottomSheetFragment(); + Bundle bundle = new Bundle(); + bundle.putParcelable(MultiRedditOptionsBottomSheetFragment.EXTRA_MULTI_REDDIT, multiReddit); + fragment.setArguments(bundle); + fragment.show(mActivity.getSupportFragmentManager(), fragment.getTag()); + } + public void goBackToTop() { if (mLinearLayoutManager != null) { mLinearLayoutManager.scrollToPositionWithOffset(0, 0); @@ -158,7 +194,7 @@ public class MultiRedditListingFragment extends Fragment implements FragmentComm @Override public void onAttach(@NonNull Context context) { super.onAttach(context); - mActivity = (SubscribedThingListingActivity) context; + mActivity = (AppCompatActivity) context; } @Override diff --git a/app/src/main/res/layout/activity_multireddit_selection.xml b/app/src/main/res/layout/activity_multireddit_selection.xml new file mode 100644 index 00000000..4c114742 --- /dev/null +++ b/app/src/main/res/layout/activity_multireddit_selection.xml @@ -0,0 +1,41 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/menu/view_subreddit_detail_activity.xml b/app/src/main/res/menu/view_subreddit_detail_activity.xml index f9a8cffc..1144d8e7 100644 --- a/app/src/main/res/menu/view_subreddit_detail_activity.xml +++ b/app/src/main/res/menu/view_subreddit_detail_activity.xml @@ -41,8 +41,14 @@ app:showAsAction="never" /> + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 9658a65a..d845d04c 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -37,6 +37,8 @@ Customize Post Filter Filtered Posts Post Filter + Users + Select a Multireddit Open navigation drawer Close navigation drawer @@ -76,6 +78,7 @@ Give Award Save to Database Read All Messages + Add to Multireddit Error occurred when parsing the JSON response Error Retrieving the token @@ -998,4 +1001,7 @@ Read all messages successfully Unable to read all messages + %1$s is added to multireddit %2$s + Unable to add %1$s to multireddit %2$s +