diff options
Diffstat (limited to 'app/src/main/java/ml/docilealligator/infinityforreddit')
7 files changed, 468 insertions, 102 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java index d2797aad..e8ad4767 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java @@ -22,6 +22,7 @@ import ml.docilealligator.infinityforreddit.activities.InboxActivity; import ml.docilealligator.infinityforreddit.activities.LinkResolverActivity; import ml.docilealligator.infinityforreddit.activities.LoginActivity; import ml.docilealligator.infinityforreddit.activities.MainActivity; +import ml.docilealligator.infinityforreddit.activities.MultiredditSelectionActivity; import ml.docilealligator.infinityforreddit.activities.PostFilterUsageListingActivity; import ml.docilealligator.infinityforreddit.activities.PostFilterPreferenceActivity; import ml.docilealligator.infinityforreddit.activities.PostImageActivity; @@ -239,4 +240,6 @@ public interface AppComponent { void inject(PostFilterUsageListingActivity postFilterUsageListingActivity); void inject(SearchUsersResultActivity searchUsersResultActivity); + + void inject(MultiredditSelectionActivity multiredditSelectionActivity); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MultiredditSelectionActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MultiredditSelectionActivity.java new file mode 100644 index 00000000..adc68363 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MultiredditSelectionActivity.java @@ -0,0 +1,250 @@ +package ml.docilealligator.infinityforreddit.activities; + +import android.app.Activity; +import android.content.Intent; +import android.content.SharedPreferences; +import android.os.Build; +import android.os.Bundle; +import android.os.Handler; +import android.view.MenuItem; +import android.view.View; +import android.view.Window; +import android.view.WindowManager; +import android.widget.Toast; + +import androidx.annotation.NonNull; +import androidx.appcompat.widget.Toolbar; +import androidx.coordinatorlayout.widget.CoordinatorLayout; +import androidx.fragment.app.Fragment; + +import com.google.android.material.appbar.AppBarLayout; +import com.r0adkll.slidr.Slidr; + +import org.greenrobot.eventbus.EventBus; +import org.greenrobot.eventbus.Subscribe; + +import java.util.ArrayList; +import java.util.concurrent.Executor; + +import javax.inject.Inject; +import javax.inject.Named; + +import butterknife.BindView; +import butterknife.ButterKnife; +import ml.docilealligator.infinityforreddit.ActivityToolbarInterface; +import ml.docilealligator.infinityforreddit.Infinity; +import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; +import ml.docilealligator.infinityforreddit.asynctasks.GetCurrentAccount; +import ml.docilealligator.infinityforreddit.asynctasks.InsertMultiRedditAsyncTask; +import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; +import ml.docilealligator.infinityforreddit.events.SwitchAccountEvent; +import ml.docilealligator.infinityforreddit.fragments.MultiRedditListingFragment; +import ml.docilealligator.infinityforreddit.multireddit.FetchMyMultiReddits; +import ml.docilealligator.infinityforreddit.multireddit.MultiReddit; +import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; +import retrofit2.Retrofit; + +public class MultiredditSelectionActivity extends BaseActivity implements ActivityToolbarInterface { + + static final String EXTRA_RETURN_MULTIREDDIT = "ERM"; + + private static final String INSERT_SUBSCRIBED_MULTIREDDIT_STATE = "ISSS"; + private static final String NULL_ACCESS_TOKEN_STATE = "NATS"; + private static final String ACCESS_TOKEN_STATE = "ATS"; + private static final String ACCOUNT_NAME_STATE = "ANS"; + private static final String FRAGMENT_OUT_STATE = "FOS"; + + @BindView(R.id.coordinator_layout_multireddit_selection_activity) + CoordinatorLayout coordinatorLayout; + @BindView(R.id.appbar_layout_multireddit_selection_activity) + AppBarLayout appBarLayout; + @BindView(R.id.toolbar_multireddit_selection_activity) + Toolbar toolbar; + @Inject + @Named("oauth") + Retrofit mOauthRetrofit; + @Inject + RedditDataRoomDatabase mRedditDataRoomDatabase; + @Inject + @Named("default") + SharedPreferences mSharedPreferences; + @Inject + CustomThemeWrapper mCustomThemeWrapper; + @Inject + Executor mExecutor; + private boolean mNullAccessToken = false; + private String mAccessToken; + private String mAccountName; + private boolean mInsertSuccess = false; + private Fragment mFragment; + + @Override + protected void onCreate(Bundle savedInstanceState) { + ((Infinity) getApplication()).getAppComponent().inject(this); + + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_multireddit_selection); + + ButterKnife.bind(this); + + EventBus.getDefault().register(this); + + applyCustomTheme(); + + if (mSharedPreferences.getBoolean(SharedPreferencesUtils.SWIPE_RIGHT_TO_GO_BACK, true)) { + Slidr.attach(this); + } + + if (Build.VERSION.SDK_INT >= 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<MultiReddit> 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,79 +905,114 @@ 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); outState.putBoolean(FETCH_SUBREDDIT_INFO_STATE, mFetchSubredditInfoSuccess); 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.R; +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; 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 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<MultiReddit> mMultiReddits; private List<MultiReddit> 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<MultiReddit> 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<String> readAllMessages(@HeaderMap Map<String, String> headers); + + + @FormUrlEncoded + @PUT("/api/multi{multipath}r/{subredditName}") + Call<String> addSubredditToMultiReddit(@HeaderMap Map<String, String> headers, @FieldMap Map<String, String> 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<String, String> params = new HashMap<>(); + params.put(APIUtils.MODEL_KEY, "{\"name\":\"" + subredditName + "\"}"); + oauthRetrofit.create(RedditAPI.class).addSubredditToMultiReddit(APIUtils.getOAuthHeader(accessToken), params, multipath, subredditName) + .enqueue(new Callback<String>() { + @Override + public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { + if (response.isSuccessful()) { + addSubredditOrUserToMultiRedditListener.success(); + } else { + addSubredditOrUserToMultiRedditListener.failed(response.code()); + } + } + + @Override + public void onFailure(@NonNull Call<String> 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.FragmentCommunicator; +import ml.docilealligator.infinityforreddit.Infinity; +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.FragmentCommunicator; -import ml.docilealligator.infinityforreddit.Infinity; +import ml.docilealligator.infinityforreddit.multireddit.MultiReddit; import ml.docilealligator.infinityforreddit.multireddit.MultiRedditViewModel; -import ml.docilealligator.infinityforreddit.R; -import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; 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 |