From 38c071bd68e1f4b177f36728253e035762f5d38b Mon Sep 17 00:00:00 2001 From: Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> Date: Wed, 16 Oct 2024 16:35:14 -0400 Subject: Start adding PostFragmentBase as the base class for PostFragment and HistoryPostFragment. --- .../activities/AccountPostsActivity.java | 5 +- .../activities/FilteredPostsActivity.java | 12 +- .../activities/ViewMultiRedditDetailActivity.java | 3 +- .../adapters/HistoryPostRecyclerViewAdapter.java | 2 +- .../PostGalleryTypeImageRecyclerViewAdapter.java | 2 +- .../adapters/PostRecyclerViewAdapter.java | 4 +- .../fragments/FragmentCommunicator.java | 24 -- .../fragments/HistoryPostFragment.java | 324 +---------------- .../infinityforreddit/fragments/PostFragment.java | 334 +----------------- .../fragments/PostFragmentBase.java | 383 +++++++++++++++++++++ 10 files changed, 421 insertions(+), 672 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragmentBase.java (limited to 'app/src') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/AccountPostsActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/AccountPostsActivity.java index a54f0c61..338f26d2 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/AccountPostsActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/AccountPostsActivity.java @@ -21,6 +21,7 @@ import javax.inject.Named; import ml.docilealligator.infinityforreddit.fragments.FragmentCommunicator; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.fragments.PostFragmentBase; import ml.docilealligator.infinityforreddit.thing.SortType; import ml.docilealligator.infinityforreddit.thing.SortTypeSelectionCallback; import ml.docilealligator.infinityforreddit.bottomsheetfragments.PostLayoutBottomSheetFragment; @@ -118,7 +119,7 @@ public class AccountPostsActivity extends BaseActivity implements SortTypeSelect @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (mFragment != null) { - return ((FragmentCommunicator) mFragment).handleKeyDown(keyCode) || super.onKeyDown(keyCode, event); + return ((PostFragmentBase) mFragment).handleKeyDown(keyCode) || super.onKeyDown(keyCode, event); } return super.onKeyDown(keyCode, event); @@ -223,7 +224,7 @@ public class AccountPostsActivity extends BaseActivity implements SortTypeSelect public void postLayoutSelected(int postLayout) { if (mFragment != null) { mPostLayoutSharedPreferences.edit().putInt(SharedPreferencesUtils.POST_LAYOUT_USER_POST_BASE + accountName, postLayout).apply(); - ((FragmentCommunicator) mFragment).changePostLayout(postLayout); + ((PostFragmentBase) mFragment).changePostLayout(postLayout); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/FilteredPostsActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/FilteredPostsActivity.java index a350219b..dd8ff575 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/FilteredPostsActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/FilteredPostsActivity.java @@ -23,14 +23,10 @@ import java.util.concurrent.Executor; import javax.inject.Inject; import javax.inject.Named; -import ml.docilealligator.infinityforreddit.fragments.FragmentCommunicator; import ml.docilealligator.infinityforreddit.Infinity; -import ml.docilealligator.infinityforreddit.post.MarkPostAsReadInterface; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RecyclerViewContentScrollingInterface; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; -import ml.docilealligator.infinityforreddit.thing.SortType; -import ml.docilealligator.infinityforreddit.thing.SortTypeSelectionCallback; import ml.docilealligator.infinityforreddit.account.Account; import ml.docilealligator.infinityforreddit.bottomsheetfragments.FilteredThingFABMoreOptionsBottomSheetFragment; import ml.docilealligator.infinityforreddit.bottomsheetfragments.PostLayoutBottomSheetFragment; @@ -42,12 +38,16 @@ import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.customviews.slidr.Slidr; import ml.docilealligator.infinityforreddit.databinding.ActivityFilteredThingBinding; import ml.docilealligator.infinityforreddit.events.SwitchAccountEvent; +import ml.docilealligator.infinityforreddit.fragments.FragmentCommunicator; import ml.docilealligator.infinityforreddit.fragments.PostFragment; +import ml.docilealligator.infinityforreddit.post.MarkPostAsReadInterface; import ml.docilealligator.infinityforreddit.post.Post; import ml.docilealligator.infinityforreddit.post.PostPagingSource; import ml.docilealligator.infinityforreddit.postfilter.PostFilter; import ml.docilealligator.infinityforreddit.readpost.InsertReadPost; import ml.docilealligator.infinityforreddit.subreddit.SubredditViewModel; +import ml.docilealligator.infinityforreddit.thing.SortType; +import ml.docilealligator.infinityforreddit.thing.SortTypeSelectionCallback; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; public class FilteredPostsActivity extends BaseActivity implements SortTypeSelectionCallback, @@ -227,7 +227,7 @@ public class FilteredPostsActivity extends BaseActivity implements SortTypeSelec @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (mFragment != null) { - return ((FragmentCommunicator) mFragment).handleKeyDown(keyCode) || super.onKeyDown(keyCode, event); + return mFragment.handleKeyDown(keyCode) || super.onKeyDown(keyCode, event); } return super.onKeyDown(keyCode, event); @@ -432,7 +432,7 @@ public class FilteredPostsActivity extends BaseActivity implements SortTypeSelec case PostPagingSource.TYPE_SEARCH: mPostLayoutSharedPreferences.edit().putInt(SharedPreferencesUtils.POST_LAYOUT_SEARCH_POST, postLayout).apply(); } - ((FragmentCommunicator) mFragment).changePostLayout(postLayout); + mFragment.changePostLayout(postLayout); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewMultiRedditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewMultiRedditDetailActivity.java index b865414c..8d842ecf 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewMultiRedditDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewMultiRedditDetailActivity.java @@ -36,6 +36,7 @@ import javax.inject.Named; import ml.docilealligator.infinityforreddit.fragments.FragmentCommunicator; import ml.docilealligator.infinityforreddit.Infinity; +import ml.docilealligator.infinityforreddit.fragments.PostFragmentBase; import ml.docilealligator.infinityforreddit.post.MarkPostAsReadInterface; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RecyclerViewContentScrollingInterface; @@ -832,7 +833,7 @@ public class ViewMultiRedditDetailActivity extends BaseActivity implements SortT public void postLayoutSelected(int postLayout) { if (mFragment != null) { mPostLayoutSharedPreferences.edit().putInt(SharedPreferencesUtils.POST_LAYOUT_MULTI_REDDIT_POST_BASE + multiPath, postLayout).apply(); - ((FragmentCommunicator) mFragment).changePostLayout(postLayout); + ((PostFragmentBase) mFragment).changePostLayout(postLayout); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/HistoryPostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/HistoryPostRecyclerViewAdapter.java index 4861e6a6..615dd7d0 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/HistoryPostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/HistoryPostRecyclerViewAdapter.java @@ -2448,7 +2448,7 @@ public class HistoryPostRecyclerViewAdapter extends PagingDataAdapter readPosts; - private final Map subredditOrUserIcons = new HashMap<>(); private int historyType; private FragmentHistoryPostBinding binding; @@ -230,27 +173,14 @@ public class HistoryPostFragment extends Fragment implements FragmentCommunicato ((Infinity) activity.getApplication()).getAppComponent().inject(this); - setHasOptionsMenu(true); + super.onCreateView(inflater, container, savedInstanceState); - EventBus.getDefault().register(this); + setHasOptionsMenu(true); applyTheme(); binding.recyclerViewHistoryPostFragment.addOnWindowFocusChangedListener(this::onWindowFocusChanged); - lazyModeHandler = new Handler(); - - lazyModeInterval = Float.parseFloat(mSharedPreferences.getString(SharedPreferencesUtils.LAZY_MODE_INTERVAL_KEY, "2.5")); - - smoothScroller = new LinearSmoothScroller(activity) { - @Override - protected int getVerticalSnapPreference() { - return LinearSmoothScroller.SNAP_TO_START; - } - }; - - window = activity.getWindow(); - Resources resources = getResources(); if ((activity != null && activity.isImmersiveInterface())) { @@ -263,8 +193,6 @@ public class HistoryPostFragment extends Fragment implements FragmentCommunicato } } - mGlide = Glide.with(activity); - lazyModeRunnable = new LazyModeRunnable() { @Override @@ -294,18 +222,6 @@ public class HistoryPostFragment extends Fragment implements FragmentCommunicato } }; - resumeLazyModeCountDownTimer = new CountDownTimer((long) (lazyModeInterval * 1000), (long) (lazyModeInterval * 1000)) { - @Override - public void onTick(long l) { - - } - - @Override - public void onFinish() { - resumeLazyMode(true); - } - }; - binding.swipeRefreshLayoutHistoryPostFragment.setEnabled(mSharedPreferences.getBoolean(SharedPreferencesUtils.PULL_TO_REFRESH, true)); binding.swipeRefreshLayoutHistoryPostFragment.setOnRefreshListener(this::refresh); @@ -316,10 +232,10 @@ public class HistoryPostFragment extends Fragment implements FragmentCommunicato isInLazyMode = savedInstanceState.getBoolean(IS_IN_LAZY_MODE_STATE); readPosts = savedInstanceState.getStringArrayList(READ_POST_LIST_STATE); postFilter = savedInstanceState.getParcelable(POST_FILTER_STATE); - historyPostFragmentId = savedInstanceState.getLong(POST_FRAGMENT_ID_STATE); + postFragmentId = savedInstanceState.getLong(POST_FRAGMENT_ID_STATE); } else { postFilter = getArguments().getParcelable(EXTRA_FILTER); - historyPostFragmentId = System.currentTimeMillis() + new Random().nextInt(1000); + postFragmentId = System.currentTimeMillis() + new Random().nextInt(1000); } binding.recyclerViewHistoryPostFragment.setOnTouchListener((view, motionEvent) -> { @@ -344,7 +260,6 @@ public class HistoryPostFragment extends Fragment implements FragmentCommunicato historyType = getArguments().getInt(EXTRA_HISTORY_TYPE, HISTORY_TYPE_READ_POSTS); int defaultPostLayout = Integer.parseInt(mSharedPreferences.getString(SharedPreferencesUtils.DEFAULT_POST_LAYOUT_KEY, "0")); - rememberMutingOptionInPostFeed = mSharedPreferences.getBoolean(SharedPreferencesUtils.REMEMBER_MUTING_OPTION_IN_POST_FEED, false); Locale locale = getResources().getConfiguration().locale; if (historyType == HISTORY_TYPE_READ_POSTS) { @@ -428,12 +343,6 @@ public class HistoryPostFragment extends Fragment implements FragmentCommunicato initializeAndBindPostViewModel(); } - vibrateWhenActionTriggered = mSharedPreferences.getBoolean(SharedPreferencesUtils.VIBRATE_WHEN_ACTION_TRIGGERED, true); - swipeActionThreshold = Float.parseFloat(mSharedPreferences.getString(SharedPreferencesUtils.SWIPE_ACTION_THRESHOLD, "0.3")); - swipeRightAction = Integer.parseInt(mSharedPreferences.getString(SharedPreferencesUtils.SWIPE_RIGHT_ACTION, "1")); - swipeLeftAction = Integer.parseInt(mSharedPreferences.getString(SharedPreferencesUtils.SWIPE_LEFT_ACTION, "0")); - initializeSwipeActionDrawable(); - touchHelper = new ItemTouchHelper(new ItemTouchHelper.Callback() { boolean exceedThreshold = false; @@ -562,7 +471,8 @@ public class HistoryPostFragment extends Fragment implements FragmentCommunicato } } - private boolean scrollPostsByCount(int count) { + @Override + protected boolean scrollPostsByCount(int count) { if (mLinearLayoutManager != null) { int pos = mLinearLayoutManager.findFirstVisibleItemPosition(); int targetPosition = pos + count; @@ -573,57 +483,6 @@ public class HistoryPostFragment extends Fragment implements FragmentCommunicato } } - @Override - public boolean handleKeyDown(int keyCode) { - boolean volumeKeysNavigatePosts = mSharedPreferences.getBoolean(SharedPreferencesUtils.VOLUME_KEYS_NAVIGATE_POSTS, false); - if (volumeKeysNavigatePosts) { - switch (keyCode) { - case KeyEvent.KEYCODE_VOLUME_UP: - return scrollPostsByCount(-1); - case KeyEvent.KEYCODE_VOLUME_DOWN: - return scrollPostsByCount(1); - } - } - return false; - } - - private int getNColumns(Resources resources) { - final boolean foldEnabled = mSharedPreferences.getBoolean(SharedPreferencesUtils.ENABLE_FOLD_SUPPORT, false); - if (resources.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { - switch (postLayout) { - case SharedPreferencesUtils.POST_LAYOUT_CARD_2: - return Integer.parseInt(mSharedPreferences.getString(SharedPreferencesUtils.NUMBER_OF_COLUMNS_IN_POST_FEED_PORTRAIT_CARD_LAYOUT_2, "1")); - case SharedPreferencesUtils.POST_LAYOUT_COMPACT: - return Integer.parseInt(mSharedPreferences.getString(SharedPreferencesUtils.NUMBER_OF_COLUMNS_IN_POST_FEED_PORTRAIT_COMPACT_LAYOUT, "1")); - case SharedPreferencesUtils.POST_LAYOUT_GALLERY: - return Integer.parseInt(mSharedPreferences.getString(SharedPreferencesUtils.NUMBER_OF_COLUMNS_IN_POST_FEED_PORTRAIT_GALLERY_LAYOUT, "2")); - default: - if (getResources().getBoolean(R.bool.isTablet)) { - if (foldEnabled) { - return Integer.parseInt(mSharedPreferences.getString(SharedPreferencesUtils.NUMBER_OF_COLUMNS_IN_POST_FEED_PORTRAIT_UNFOLDED, "2")); - } else { - return Integer.parseInt(mSharedPreferences.getString(SharedPreferencesUtils.NUMBER_OF_COLUMNS_IN_POST_FEED_PORTRAIT, "2")); - } - } - return Integer.parseInt(mSharedPreferences.getString(SharedPreferencesUtils.NUMBER_OF_COLUMNS_IN_POST_FEED_PORTRAIT, "1")); - } - } else { - switch (postLayout) { - case SharedPreferencesUtils.POST_LAYOUT_CARD_2: - return Integer.parseInt(mSharedPreferences.getString(SharedPreferencesUtils.NUMBER_OF_COLUMNS_IN_POST_FEED_LANDSCAPE_CARD_LAYOUT_2, "2")); - case SharedPreferencesUtils.POST_LAYOUT_COMPACT: - return Integer.parseInt(mSharedPreferences.getString(SharedPreferencesUtils.NUMBER_OF_COLUMNS_IN_POST_FEED_LANDSCAPE_COMPACT_LAYOUT, "2")); - case SharedPreferencesUtils.POST_LAYOUT_GALLERY: - return Integer.parseInt(mSharedPreferences.getString(SharedPreferencesUtils.NUMBER_OF_COLUMNS_IN_POST_FEED_LANDSCAPE_GALLERY_LAYOUT, "2")); - default: - if (getResources().getBoolean(R.bool.isTablet) && foldEnabled) { - return Integer.parseInt(mSharedPreferences.getString(SharedPreferencesUtils.NUMBER_OF_COLUMNS_IN_POST_FEED_LANDSCAPE_UNFOLDED, "2")); - } - return Integer.parseInt(mSharedPreferences.getString(SharedPreferencesUtils.NUMBER_OF_COLUMNS_IN_POST_FEED_LANDSCAPE, "2")); - } - } - } - private void initializeAndBindPostViewModel() { if (postType == HistoryPostPagingSource.TYPE_READ_POSTS) { mHistoryPostViewModel = new ViewModelProvider(HistoryPostFragment.this, new HistoryPostViewModel.Factory(mExecutor, @@ -678,34 +537,6 @@ public class HistoryPostFragment extends Fragment implements FragmentCommunicato showErrorView(R.string.no_posts); } - private void initializeSwipeActionDrawable() { - if (swipeRightAction == SharedPreferencesUtils.SWIPE_ACITON_DOWNVOTE) { - backgroundSwipeRight = new ColorDrawable(mCustomThemeWrapper.getDownvoted()); - drawableSwipeRight = ResourcesCompat.getDrawable(activity.getResources(), R.drawable.ic_arrow_downward_day_night_24dp, null); - } else { - backgroundSwipeRight = new ColorDrawable(mCustomThemeWrapper.getUpvoted()); - drawableSwipeRight = ResourcesCompat.getDrawable(activity.getResources(), R.drawable.ic_arrow_upward_day_night_24dp, null); - } - - if (swipeLeftAction == SharedPreferencesUtils.SWIPE_ACITON_UPVOTE) { - backgroundSwipeLeft = new ColorDrawable(mCustomThemeWrapper.getUpvoted()); - drawableSwipeLeft = ResourcesCompat.getDrawable(activity.getResources(), R.drawable.ic_arrow_upward_day_night_24dp, null); - } else { - backgroundSwipeLeft = new ColorDrawable(mCustomThemeWrapper.getDownvoted()); - drawableSwipeLeft = ResourcesCompat.getDrawable(activity.getResources(), R.drawable.ic_arrow_downward_day_night_24dp, null); - } - } - - public long getHistoryPostFragmentId() { - return historyPostFragmentId; - } - - @Override - public void onAttach(@NonNull Context context) { - super.onAttach(context); - this.activity = (BaseActivity) context; - } - @Override public void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); @@ -719,7 +550,7 @@ public class HistoryPostFragment extends Fragment implements FragmentCommunicato mStaggeredGridLayoutManager.findFirstVisibleItemPositions(into)[0]); } outState.putParcelable(POST_FILTER_STATE, postFilter); - outState.putLong(POST_FRAGMENT_ID_STATE, historyPostFragmentId); + outState.putLong(POST_FRAGMENT_ID_STATE, postFragmentId); } @Override @@ -822,15 +653,6 @@ public class HistoryPostFragment extends Fragment implements FragmentCommunicato } @Override - public boolean isInLazyMode() { - return isInLazyMode; - } - - @Override - public void changePostLayout(int postLayout) { - changePostLayout(postLayout, false); - } - public void changePostLayout(int postLayout, boolean temporary) { this.postLayout = postLayout; if (!temporary) { @@ -887,38 +709,6 @@ public class HistoryPostFragment extends Fragment implements FragmentCommunicato } } - @Nullable - public Boolean getMasterMutingOption() { - return masterMutingOption; - } - - public void videoAutoplayChangeMutingOption(boolean isMute) { - if (rememberMutingOptionInPostFeed) { - masterMutingOption = isMute; - } - } - - public void loadIcon(String subredditOrUserName, boolean isSubreddit, PostFragment.LoadIconListener loadIconListener) { - if (subredditOrUserIcons.containsKey(subredditOrUserName)) { - loadIconListener.loadIconSuccess(subredditOrUserName, subredditOrUserIcons.get(subredditOrUserName)); - } else { - if (isSubreddit) { - LoadSubredditIcon.loadSubredditIcon(mExecutor, new Handler(), mRedditDataRoomDatabase, - subredditOrUserName, activity.accessToken, activity.accountName, mOauthRetrofit, mRetrofit, - iconImageUrl -> { - subredditOrUserIcons.put(subredditOrUserName, iconImageUrl); - loadIconListener.loadIconSuccess(subredditOrUserName, iconImageUrl); - }); - } else { - LoadUserData.loadUserData(mExecutor, new Handler(), mRedditDataRoomDatabase, subredditOrUserName, - mRetrofit, iconImageUrl -> { - subredditOrUserIcons.put(subredditOrUserName, iconImageUrl); - loadIconListener.loadIconSuccess(subredditOrUserName, iconImageUrl); - }); - } - } - } - private void refreshAdapter() { int previousPosition = -1; if (mLinearLayoutManager != null) { @@ -965,7 +755,6 @@ public class HistoryPostFragment extends Fragment implements FragmentCommunicato @Override public void onDestroy() { - EventBus.getDefault().unregister(this); binding.recyclerViewHistoryPostFragment.addOnWindowFocusChangedListener(null); super.onDestroy(); } @@ -976,6 +765,7 @@ public class HistoryPostFragment extends Fragment implements FragmentCommunicato } } + @Override public boolean isRecyclerViewItemSwipeable(RecyclerView.ViewHolder viewHolder) { if (swipeActionEnabled) { if (viewHolder instanceof HistoryPostRecyclerViewAdapter.PostBaseGalleryTypeViewHolder) { @@ -1169,11 +959,6 @@ public class HistoryPostFragment extends Fragment implements FragmentCommunicato } } - @Subscribe - public void onChangeVibrateWhenActionTriggeredEvent(ChangeVibrateWhenActionTriggeredEvent changeVibrateWhenActionTriggeredEvent) { - vibrateWhenActionTriggered = changeVibrateWhenActionTriggeredEvent.vibrateWhenActionTriggered; - } - @Subscribe public void onChangeEnableSwipeActionSwitchEvent(ChangeEnableSwipeActionSwitchEvent changeEnableSwipeActionSwitchEvent) { if (getNColumns(getResources()) == 1 && touchHelper != null) { @@ -1207,11 +992,6 @@ public class HistoryPostFragment extends Fragment implements FragmentCommunicato } } - @Subscribe - public void onChangeSwipeActionThresholdEvent(ChangeSwipeActionThresholdEvent changeSwipeActionThresholdEvent) { - swipeActionThreshold = changeSwipeActionThresholdEvent.swipeActionThreshold; - } - @Subscribe public void onChangeDataSavingModeEvent(ChangeDataSavingModeEvent changeDataSavingModeEvent) { if (mAdapter != null) { @@ -1242,17 +1022,10 @@ public class HistoryPostFragment extends Fragment implements FragmentCommunicato } } - @Subscribe - public void onChangeSwipeActionEvent(ChangeSwipeActionEvent changeSwipeActionEvent) { - swipeRightAction = changeSwipeActionEvent.swipeRightAction == -1 ? swipeRightAction : changeSwipeActionEvent.swipeRightAction; - swipeLeftAction = changeSwipeActionEvent.swipeLeftAction == -1 ? swipeLeftAction : changeSwipeActionEvent.swipeLeftAction; - initializeSwipeActionDrawable(); - } - @Subscribe public void onNeedForPostListFromPostRecyclerViewAdapterEvent(NeedForPostListFromPostFragmentEvent event) { - if (historyPostFragmentId == event.postFragmentTimeId && mAdapter != null) { - EventBus.getDefault().post(new ProvidePostListToViewPostDetailActivityEvent(historyPostFragmentId, + if (postFragmentId == event.postFragmentTimeId && mAdapter != null) { + EventBus.getDefault().post(new ProvidePostListToViewPostDetailActivityEvent(postFragmentId, new ArrayList<>(mAdapter.snapshot()), HistoryPostPagingSource.TYPE_READ_POSTS, null, null, null, null, null, null, null, postFilter, null, null)); @@ -1299,14 +1072,6 @@ public class HistoryPostFragment extends Fragment implements FragmentCommunicato } } - @Subscribe - public void onChangeRememberMutingOptionInPostFeedEvent(ChangeRememberMutingOptionInPostFeedEvent event) { - rememberMutingOptionInPostFeed = event.rememberMutingOptionInPostFeedEvent; - if (!event.rememberMutingOptionInPostFeedEvent) { - masterMutingOption = null; - } - } - @Subscribe public void onChangeFixedHeightPreviewCardEvent(ChangeFixedHeightPreviewInCardEvent event) { if (mAdapter != null) { @@ -1337,71 +1102,4 @@ public class HistoryPostFragment extends Fragment implements FragmentCommunicato mAdapter.setEasierToWatchInFullScreen(event.easierToWatchInFullScreen); } } - - private static abstract class LazyModeRunnable implements Runnable { - private int currentPosition = -1; - - int getCurrentPosition() { - return currentPosition; - } - - void setCurrentPosition(int currentPosition) { - this.currentPosition = currentPosition; - } - - void incrementCurrentPosition() { - currentPosition++; - } - - void resetOldPosition() { - currentPosition = -1; - } - } - - private static class StaggeredGridLayoutManagerItemOffsetDecoration extends RecyclerView.ItemDecoration { - - private final int mItemOffset; - private final int mNColumns; - - StaggeredGridLayoutManagerItemOffsetDecoration(int itemOffset, int nColumns) { - mItemOffset = itemOffset; - mNColumns = nColumns; - } - - StaggeredGridLayoutManagerItemOffsetDecoration(@NonNull Context context, @DimenRes int itemOffsetId, int nColumns) { - this(context.getResources().getDimensionPixelSize(itemOffsetId), nColumns); - } - - @Override - public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, - @NonNull RecyclerView.State state) { - super.getItemOffsets(outRect, view, parent, state); - - StaggeredGridLayoutManager.LayoutParams layoutParams = (StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams(); - - int spanIndex = layoutParams.getSpanIndex(); - - int halfOffset = mItemOffset / 2; - - if (mNColumns == 2) { - if (spanIndex == 0) { - outRect.set(halfOffset, 0, halfOffset / 2, 0); - } else { - outRect.set(halfOffset / 2, 0, halfOffset, 0); - } - } else if (mNColumns == 3) { - if (spanIndex == 0) { - outRect.set(halfOffset, 0, halfOffset / 2, 0); - } else if (spanIndex == 1) { - outRect.set(halfOffset / 2, 0, halfOffset / 2, 0); - } else { - outRect.set(halfOffset / 2, 0, halfOffset, 0); - } - } - } - } - - public interface LoadIconListener { - void loadIconSuccess(String subredditOrUserName, String iconUrl); - } } \ No newline at end of file diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java index b491fc6e..a544eb66 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java @@ -3,73 +3,52 @@ package ml.docilealligator.infinityforreddit.fragments; import static ml.docilealligator.infinityforreddit.videoautoplay.media.PlaybackInfo.INDEX_UNSET; import static ml.docilealligator.infinityforreddit.videoautoplay.media.PlaybackInfo.TIME_UNSET; -import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; -import android.content.res.Configuration; import android.content.res.Resources; import android.graphics.Canvas; -import android.graphics.Rect; -import android.graphics.drawable.ColorDrawable; -import android.graphics.drawable.Drawable; import android.os.Build; import android.os.Bundle; -import android.os.CountDownTimer; import android.os.Handler; import android.view.HapticFeedbackConstants; -import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; -import android.view.Window; import android.view.WindowManager; import android.widget.Toast; -import androidx.annotation.DimenRes; import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import androidx.core.content.res.ResourcesCompat; -import androidx.fragment.app.Fragment; import androidx.lifecycle.ViewModelProvider; import androidx.paging.ItemSnapshotList; import androidx.paging.LoadState; import androidx.recyclerview.widget.ItemTouchHelper; -import androidx.recyclerview.widget.LinearSmoothScroller; import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.StaggeredGridLayoutManager; import androidx.transition.AutoTransition; import androidx.transition.TransitionManager; -import com.bumptech.glide.Glide; -import com.bumptech.glide.RequestManager; - import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; import java.util.ArrayList; -import java.util.HashMap; import java.util.Locale; -import java.util.Map; import java.util.Random; -import java.util.concurrent.Executor; import javax.inject.Inject; import javax.inject.Named; import javax.inject.Provider; -import ml.docilealligator.infinityforreddit.activities.ActivityToolbarInterface; import ml.docilealligator.infinityforreddit.FetchPostFilterReadPostsAndConcatenatedSubredditNames; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RecyclerViewContentScrollingInterface; -import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; -import ml.docilealligator.infinityforreddit.thing.SortType; import ml.docilealligator.infinityforreddit.account.Account; import ml.docilealligator.infinityforreddit.activities.AccountPostsActivity; import ml.docilealligator.infinityforreddit.activities.AccountSavedThingActivity; +import ml.docilealligator.infinityforreddit.activities.ActivityToolbarInterface; import ml.docilealligator.infinityforreddit.activities.BaseActivity; import ml.docilealligator.infinityforreddit.activities.CustomizePostFilterActivity; import ml.docilealligator.infinityforreddit.activities.FilteredPostsActivity; @@ -77,10 +56,7 @@ import ml.docilealligator.infinityforreddit.activities.ViewSubredditDetailActivi import ml.docilealligator.infinityforreddit.adapters.Paging3LoadingStateAdapter; import ml.docilealligator.infinityforreddit.adapters.PostRecyclerViewAdapter; import ml.docilealligator.infinityforreddit.apis.StreamableAPI; -import ml.docilealligator.infinityforreddit.asynctasks.LoadSubredditIcon; -import ml.docilealligator.infinityforreddit.asynctasks.LoadUserData; import ml.docilealligator.infinityforreddit.bottomsheetfragments.FABMoreOptionsBottomSheetFragment; -import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.customviews.LinearLayoutManagerBugFixed; import ml.docilealligator.infinityforreddit.databinding.FragmentPostBinding; import ml.docilealligator.infinityforreddit.events.ChangeAutoplayNsfwVideosEvent; @@ -107,16 +83,12 @@ import ml.docilealligator.infinityforreddit.events.ChangeOnlyDisablePreviewInVid import ml.docilealligator.infinityforreddit.events.ChangePostFeedMaxResolutionEvent; import ml.docilealligator.infinityforreddit.events.ChangePostLayoutEvent; import ml.docilealligator.infinityforreddit.events.ChangePullToRefreshEvent; -import ml.docilealligator.infinityforreddit.events.ChangeRememberMutingOptionInPostFeedEvent; import ml.docilealligator.infinityforreddit.events.ChangeSavePostFeedScrolledPositionEvent; import ml.docilealligator.infinityforreddit.events.ChangeShowAbsoluteNumberOfVotesEvent; import ml.docilealligator.infinityforreddit.events.ChangeShowElapsedTimeEvent; import ml.docilealligator.infinityforreddit.events.ChangeSpoilerBlurEvent; import ml.docilealligator.infinityforreddit.events.ChangeStartAutoplayVisibleAreaOffsetEvent; -import ml.docilealligator.infinityforreddit.events.ChangeSwipeActionEvent; -import ml.docilealligator.infinityforreddit.events.ChangeSwipeActionThresholdEvent; import ml.docilealligator.infinityforreddit.events.ChangeTimeFormatEvent; -import ml.docilealligator.infinityforreddit.events.ChangeVibrateWhenActionTriggeredEvent; import ml.docilealligator.infinityforreddit.events.ChangeVideoAutoplayEvent; import ml.docilealligator.infinityforreddit.events.ChangeVoteButtonsPositionEvent; import ml.docilealligator.infinityforreddit.events.NeedForPostListFromPostFragmentEvent; @@ -129,6 +101,7 @@ import ml.docilealligator.infinityforreddit.post.PostPagingSource; import ml.docilealligator.infinityforreddit.post.PostViewModel; import ml.docilealligator.infinityforreddit.postfilter.PostFilter; import ml.docilealligator.infinityforreddit.postfilter.PostFilterUsage; +import ml.docilealligator.infinityforreddit.thing.SortType; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; import ml.docilealligator.infinityforreddit.utils.Utils; import ml.docilealligator.infinityforreddit.videoautoplay.ExoCreator; @@ -138,9 +111,9 @@ import retrofit2.Retrofit; /** - * A simple {@link Fragment} subclass. + * A simple {@link PostFragmentBase} subclass. */ -public class PostFragment extends Fragment implements FragmentCommunicator { +public class PostFragment extends PostFragmentBase implements FragmentCommunicator { public static final String EXTRA_NAME = "EN"; public static final String EXTRA_USER_NAME = "EUN"; @@ -160,22 +133,11 @@ public class PostFragment extends Fragment implements FragmentCommunicator { PostViewModel mPostViewModel; @Inject - @Named("no_oauth") - Retrofit mRetrofit; - @Inject - @Named("oauth") - Retrofit mOauthRetrofit; - @Inject @Named("redgifs") Retrofit mRedgifsRetrofit; @Inject Provider mStreamableApiProvider; @Inject - RedditDataRoomDatabase mRedditDataRoomDatabase; - @Inject - @Named("default") - SharedPreferences mSharedPreferences; - @Inject @Named("current_account") SharedPreferences mCurrentAccountSharedPreferences; @Inject @@ -194,32 +156,12 @@ public class PostFragment extends Fragment implements FragmentCommunicator { @Named("post_feed_scrolled_position_cache") SharedPreferences mPostFeedScrolledPositionSharedPreferences; @Inject - CustomThemeWrapper mCustomThemeWrapper; - @Inject ExoCreator mExoCreator; - @Inject - Executor mExecutor; - private RequestManager mGlide; - private BaseActivity activity; - private LinearLayoutManagerBugFixed mLinearLayoutManager; - private StaggeredGridLayoutManager mStaggeredGridLayoutManager; private MenuItem lazyModeItem; - private long postFragmentId; private int postType; - private boolean isInLazyMode = false; - private boolean isLazyModePaused = false; private boolean hasPost = false; private boolean savePostFeedScrolledPosition; - private boolean rememberMutingOptionInPostFeed; - private boolean swipeActionEnabled; - private Boolean masterMutingOption; private PostRecyclerViewAdapter mAdapter; - private RecyclerView.SmoothScroller smoothScroller; - private Window window; - private Handler lazyModeHandler; - private LazyModeRunnable lazyModeRunnable; - private CountDownTimer resumeLazyModeCountDownTimer; - private float lazyModeInterval; private String subredditName; private String username; private String query; @@ -228,20 +170,9 @@ public class PostFragment extends Fragment implements FragmentCommunicator { private String multiRedditPath; private String concatenatedSubredditNames; private int maxPosition = -1; - private int postLayout; private SortType sortType; private PostFilter postFilter; - private ColorDrawable backgroundSwipeRight; - private ColorDrawable backgroundSwipeLeft; - private Drawable drawableSwipeRight; - private Drawable drawableSwipeLeft; - private int swipeLeftAction; - private int swipeRightAction; - private boolean vibrateWhenActionTriggered; - private float swipeActionThreshold; - private ItemTouchHelper touchHelper; private ArrayList readPosts; - private final Map subredditOrUserIcons = new HashMap<>(); private FragmentPostBinding binding; public PostFragment() { @@ -262,7 +193,8 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } } - private boolean scrollPostsByCount(int count) { + @Override + protected boolean scrollPostsByCount(int count) { if (mLinearLayoutManager != null) { int pos = mLinearLayoutManager.findFirstVisibleItemPosition(); int targetPosition = pos + count; @@ -273,20 +205,6 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } } - @Override - public boolean handleKeyDown(int keyCode) { - boolean volumeKeysNavigatePosts = mSharedPreferences.getBoolean(SharedPreferencesUtils.VOLUME_KEYS_NAVIGATE_POSTS, false); - if (volumeKeysNavigatePosts) { - switch (keyCode) { - case KeyEvent.KEYCODE_VOLUME_UP: - return scrollPostsByCount(-1); - case KeyEvent.KEYCODE_VOLUME_DOWN: - return scrollPostsByCount(1); - } - } - return false; - } - @Override public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { @@ -295,27 +213,14 @@ public class PostFragment extends Fragment implements FragmentCommunicator { ((Infinity) activity.getApplication()).getAppComponent().inject(this); - setHasOptionsMenu(true); + super.onCreateView(inflater, container, savedInstanceState); - EventBus.getDefault().register(this); + setHasOptionsMenu(true); applyTheme(); binding.recyclerViewPostFragment.addOnWindowFocusChangedListener(this::onWindowFocusChanged); - lazyModeHandler = new Handler(); - - lazyModeInterval = Float.parseFloat(mSharedPreferences.getString(SharedPreferencesUtils.LAZY_MODE_INTERVAL_KEY, "2.5")); - - smoothScroller = new LinearSmoothScroller(activity) { - @Override - protected int getVerticalSnapPreference() { - return LinearSmoothScroller.SNAP_TO_START; - } - }; - - window = activity.getWindow(); - Resources resources = getResources(); if ((activity != null && activity.isImmersiveInterface())) { @@ -328,8 +233,6 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } } - mGlide = Glide.with(activity); - lazyModeRunnable = new LazyModeRunnable() { @Override @@ -359,18 +262,6 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } }; - resumeLazyModeCountDownTimer = new CountDownTimer((long) (lazyModeInterval * 1000), (long) (lazyModeInterval * 1000)) { - @Override - public void onTick(long l) { - - } - - @Override - public void onFinish() { - resumeLazyMode(true); - } - }; - binding.swipeRefreshLayoutPostFragment.setEnabled(mSharedPreferences.getBoolean(SharedPreferencesUtils.PULL_TO_REFRESH, true)); binding.swipeRefreshLayoutPostFragment.setOnRefreshListener(this::refresh); @@ -412,7 +303,6 @@ public class PostFragment extends Fragment implements FragmentCommunicator { int defaultPostLayout = Integer.parseInt(mSharedPreferences.getString(SharedPreferencesUtils.DEFAULT_POST_LAYOUT_KEY, "0")); savePostFeedScrolledPosition = mSharedPreferences.getBoolean(SharedPreferencesUtils.SAVE_FRONT_PAGE_SCROLLED_POSITION, false); - rememberMutingOptionInPostFeed = mSharedPreferences.getBoolean(SharedPreferencesUtils.REMEMBER_MUTING_OPTION_IN_POST_FEED, false); Locale locale = getResources().getConfiguration().locale; int usage; @@ -1023,12 +913,6 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } } - vibrateWhenActionTriggered = mSharedPreferences.getBoolean(SharedPreferencesUtils.VIBRATE_WHEN_ACTION_TRIGGERED, true); - swipeActionThreshold = Float.parseFloat(mSharedPreferences.getString(SharedPreferencesUtils.SWIPE_ACTION_THRESHOLD, "0.3")); - swipeRightAction = Integer.parseInt(mSharedPreferences.getString(SharedPreferencesUtils.SWIPE_RIGHT_ACTION, "1")); - swipeLeftAction = Integer.parseInt(mSharedPreferences.getString(SharedPreferencesUtils.SWIPE_LEFT_ACTION, "0")); - initializeSwipeActionDrawable(); - touchHelper = new ItemTouchHelper(new ItemTouchHelper.Callback() { boolean exceedThreshold = false; @@ -1142,43 +1026,6 @@ public class PostFragment extends Fragment implements FragmentCommunicator { return binding.getRoot(); } - private int getNColumns(Resources resources) { - final boolean foldEnabled = mSharedPreferences.getBoolean(SharedPreferencesUtils.ENABLE_FOLD_SUPPORT, false); - if (resources.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { - switch (postLayout) { - case SharedPreferencesUtils.POST_LAYOUT_CARD_2: - return Integer.parseInt(mSharedPreferences.getString(SharedPreferencesUtils.NUMBER_OF_COLUMNS_IN_POST_FEED_PORTRAIT_CARD_LAYOUT_2, "1")); - case SharedPreferencesUtils.POST_LAYOUT_COMPACT: - return Integer.parseInt(mSharedPreferences.getString(SharedPreferencesUtils.NUMBER_OF_COLUMNS_IN_POST_FEED_PORTRAIT_COMPACT_LAYOUT, "1")); - case SharedPreferencesUtils.POST_LAYOUT_GALLERY: - return Integer.parseInt(mSharedPreferences.getString(SharedPreferencesUtils.NUMBER_OF_COLUMNS_IN_POST_FEED_PORTRAIT_GALLERY_LAYOUT, "2")); - default: - if (getResources().getBoolean(R.bool.isTablet)) { - if (foldEnabled) { - return Integer.parseInt(mSharedPreferences.getString(SharedPreferencesUtils.NUMBER_OF_COLUMNS_IN_POST_FEED_PORTRAIT_UNFOLDED, "2")); - } else { - return Integer.parseInt(mSharedPreferences.getString(SharedPreferencesUtils.NUMBER_OF_COLUMNS_IN_POST_FEED_PORTRAIT, "2")); - } - } - return Integer.parseInt(mSharedPreferences.getString(SharedPreferencesUtils.NUMBER_OF_COLUMNS_IN_POST_FEED_PORTRAIT, "1")); - } - } else { - switch (postLayout) { - case SharedPreferencesUtils.POST_LAYOUT_CARD_2: - return Integer.parseInt(mSharedPreferences.getString(SharedPreferencesUtils.NUMBER_OF_COLUMNS_IN_POST_FEED_LANDSCAPE_CARD_LAYOUT_2, "2")); - case SharedPreferencesUtils.POST_LAYOUT_COMPACT: - return Integer.parseInt(mSharedPreferences.getString(SharedPreferencesUtils.NUMBER_OF_COLUMNS_IN_POST_FEED_LANDSCAPE_COMPACT_LAYOUT, "2")); - case SharedPreferencesUtils.POST_LAYOUT_GALLERY: - return Integer.parseInt(mSharedPreferences.getString(SharedPreferencesUtils.NUMBER_OF_COLUMNS_IN_POST_FEED_LANDSCAPE_GALLERY_LAYOUT, "2")); - default: - if (getResources().getBoolean(R.bool.isTablet) && foldEnabled) { - return Integer.parseInt(mSharedPreferences.getString(SharedPreferencesUtils.NUMBER_OF_COLUMNS_IN_POST_FEED_LANDSCAPE_UNFOLDED, "2")); - } - return Integer.parseInt(mSharedPreferences.getString(SharedPreferencesUtils.NUMBER_OF_COLUMNS_IN_POST_FEED_LANDSCAPE, "2")); - } - } - } - private void initializeAndBindPostViewModel() { if (postType == PostPagingSource.TYPE_SEARCH) { mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(mExecutor, @@ -1382,34 +1229,6 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } } - private void initializeSwipeActionDrawable() { - if (swipeRightAction == SharedPreferencesUtils.SWIPE_ACITON_DOWNVOTE) { - backgroundSwipeRight = new ColorDrawable(mCustomThemeWrapper.getDownvoted()); - drawableSwipeRight = ResourcesCompat.getDrawable(activity.getResources(), R.drawable.ic_arrow_downward_day_night_24dp, null); - } else { - backgroundSwipeRight = new ColorDrawable(mCustomThemeWrapper.getUpvoted()); - drawableSwipeRight = ResourcesCompat.getDrawable(activity.getResources(), R.drawable.ic_arrow_upward_day_night_24dp, null); - } - - if (swipeLeftAction == SharedPreferencesUtils.SWIPE_ACITON_UPVOTE) { - backgroundSwipeLeft = new ColorDrawable(mCustomThemeWrapper.getUpvoted()); - drawableSwipeLeft = ResourcesCompat.getDrawable(activity.getResources(), R.drawable.ic_arrow_upward_day_night_24dp, null); - } else { - backgroundSwipeLeft = new ColorDrawable(mCustomThemeWrapper.getDownvoted()); - drawableSwipeLeft = ResourcesCompat.getDrawable(activity.getResources(), R.drawable.ic_arrow_downward_day_night_24dp, null); - } - } - - public long getPostFragmentId() { - return postFragmentId; - } - - @Override - public void onAttach(@NonNull Context context) { - super.onAttach(context); - this.activity = (BaseActivity) context; - } - @Override public void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); @@ -1554,15 +1373,6 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } @Override - public boolean isInLazyMode() { - return isInLazyMode; - } - - @Override - public void changePostLayout(int postLayout) { - changePostLayout(postLayout, false); - } - public void changePostLayout(int postLayout, boolean temporary) { this.postLayout = postLayout; if (!temporary) { @@ -1691,6 +1501,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } } + @Override public boolean getIsNsfwSubreddit() { if (activity instanceof ViewSubredditDetailActivity) { return ((ViewSubredditDetailActivity) activity).isNsfwSubreddit(); @@ -1701,38 +1512,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } } - @Nullable - public Boolean getMasterMutingOption() { - return masterMutingOption; - } - - public void videoAutoplayChangeMutingOption(boolean isMute) { - if (rememberMutingOptionInPostFeed) { - masterMutingOption = isMute; - } - } - - public void loadIcon(String subredditOrUserName, boolean isSubreddit, LoadIconListener loadIconListener) { - if (subredditOrUserIcons.containsKey(subredditOrUserName)) { - loadIconListener.loadIconSuccess(subredditOrUserName, subredditOrUserIcons.get(subredditOrUserName)); - } else { - if (isSubreddit) { - LoadSubredditIcon.loadSubredditIcon(mExecutor, new Handler(), mRedditDataRoomDatabase, - subredditOrUserName, activity.accessToken, activity.accountName, mOauthRetrofit, mRetrofit, - iconImageUrl -> { - subredditOrUserIcons.put(subredditOrUserName, iconImageUrl); - loadIconListener.loadIconSuccess(subredditOrUserName, iconImageUrl); - }); - } else { - LoadUserData.loadUserData(mExecutor, new Handler(), mRedditDataRoomDatabase, subredditOrUserName, - mRetrofit, iconImageUrl -> { - subredditOrUserIcons.put(subredditOrUserName, iconImageUrl); - loadIconListener.loadIconSuccess(subredditOrUserName, iconImageUrl); - }); - } - } - } - + @Override public void markPostAsRead(Post post) { if (readPosts == null) { readPosts = new ArrayList<>(); @@ -1946,11 +1726,6 @@ public class PostFragment extends Fragment implements FragmentCommunicator { savePostFeedScrolledPosition = changeSavePostFeedScrolledPositionEvent.savePostFeedScrolledPosition; } - @Subscribe - public void onChangeVibrateWhenActionTriggeredEvent(ChangeVibrateWhenActionTriggeredEvent changeVibrateWhenActionTriggeredEvent) { - vibrateWhenActionTriggered = changeVibrateWhenActionTriggeredEvent.vibrateWhenActionTriggered; - } - @Subscribe public void onChangeEnableSwipeActionSwitchEvent(ChangeEnableSwipeActionSwitchEvent changeEnableSwipeActionSwitchEvent) { if (getNColumns(getResources()) == 1 && touchHelper != null) { @@ -1984,11 +1759,6 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } } - @Subscribe - public void onChangeSwipeActionThresholdEvent(ChangeSwipeActionThresholdEvent changeSwipeActionThresholdEvent) { - swipeActionThreshold = changeSwipeActionThresholdEvent.swipeActionThreshold; - } - @Subscribe public void onChangeDataSavingModeEvent(ChangeDataSavingModeEvent changeDataSavingModeEvent) { if (mAdapter != null) { @@ -2019,13 +1789,6 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } } - @Subscribe - public void onChangeSwipeActionEvent(ChangeSwipeActionEvent changeSwipeActionEvent) { - swipeRightAction = changeSwipeActionEvent.swipeRightAction == -1 ? swipeRightAction : changeSwipeActionEvent.swipeRightAction; - swipeLeftAction = changeSwipeActionEvent.swipeLeftAction == -1 ? swipeLeftAction : changeSwipeActionEvent.swipeLeftAction; - initializeSwipeActionDrawable(); - } - @Subscribe public void onNeedForPostListFromPostRecyclerViewAdapterEvent(NeedForPostListFromPostFragmentEvent event) { if (postFragmentId == event.postFragmentTimeId && mAdapter != null) { @@ -2076,14 +1839,6 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } } - @Subscribe - public void onChangeRememberMutingOptionInPostFeedEvent(ChangeRememberMutingOptionInPostFeedEvent event) { - rememberMutingOptionInPostFeed = event.rememberMutingOptionInPostFeedEvent; - if (!event.rememberMutingOptionInPostFeedEvent) { - masterMutingOption = null; - } - } - @Subscribe public void onChangeFixedHeightPreviewCardEvent(ChangeFixedHeightPreviewInCardEvent event) { if (mAdapter != null) { @@ -2169,7 +1924,6 @@ public class PostFragment extends Fragment implements FragmentCommunicator { @Override public void onDestroy() { - EventBus.getDefault().unregister(this); binding.recyclerViewPostFragment.addOnWindowFocusChangedListener(null); super.onDestroy(); } @@ -2180,6 +1934,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } } + @Override public boolean isRecyclerViewItemSwipeable(RecyclerView.ViewHolder viewHolder) { if (swipeActionEnabled) { if (viewHolder instanceof PostRecyclerViewAdapter.PostBaseGalleryTypeViewHolder) { @@ -2191,71 +1946,4 @@ public class PostFragment extends Fragment implements FragmentCommunicator { return false; } - - private static abstract class LazyModeRunnable implements Runnable { - private int currentPosition = -1; - - int getCurrentPosition() { - return currentPosition; - } - - void setCurrentPosition(int currentPosition) { - this.currentPosition = currentPosition; - } - - void incrementCurrentPosition() { - currentPosition++; - } - - void resetOldPosition() { - currentPosition = -1; - } - } - - private static class StaggeredGridLayoutManagerItemOffsetDecoration extends RecyclerView.ItemDecoration { - - private final int mItemOffset; - private final int mNColumns; - - StaggeredGridLayoutManagerItemOffsetDecoration(int itemOffset, int nColumns) { - mItemOffset = itemOffset; - mNColumns = nColumns; - } - - StaggeredGridLayoutManagerItemOffsetDecoration(@NonNull Context context, @DimenRes int itemOffsetId, int nColumns) { - this(context.getResources().getDimensionPixelSize(itemOffsetId), nColumns); - } - - @Override - public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, - @NonNull RecyclerView.State state) { - super.getItemOffsets(outRect, view, parent, state); - - StaggeredGridLayoutManager.LayoutParams layoutParams = (StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams(); - - int spanIndex = layoutParams.getSpanIndex(); - - int halfOffset = mItemOffset / 2; - - if (mNColumns == 2) { - if (spanIndex == 0) { - outRect.set(halfOffset, 0, halfOffset / 2, 0); - } else { - outRect.set(halfOffset / 2, 0, halfOffset, 0); - } - } else if (mNColumns == 3) { - if (spanIndex == 0) { - outRect.set(halfOffset, 0, halfOffset / 2, 0); - } else if (spanIndex == 1) { - outRect.set(halfOffset / 2, 0, halfOffset / 2, 0); - } else { - outRect.set(halfOffset / 2, 0, halfOffset, 0); - } - } - } - } - - public interface LoadIconListener { - void loadIconSuccess(String subredditOrUserName, String iconUrl); - } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragmentBase.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragmentBase.java new file mode 100644 index 00000000..33363b52 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragmentBase.java @@ -0,0 +1,383 @@ +package ml.docilealligator.infinityforreddit.fragments; + +import android.content.Context; +import android.content.SharedPreferences; +import android.content.res.Configuration; +import android.content.res.Resources; +import android.graphics.Rect; +import android.graphics.drawable.ColorDrawable; +import android.graphics.drawable.Drawable; +import android.os.Bundle; +import android.os.CountDownTimer; +import android.os.Handler; +import android.view.KeyEvent; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.view.Window; + +import androidx.annotation.DimenRes; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.core.content.res.ResourcesCompat; +import androidx.fragment.app.Fragment; +import androidx.recyclerview.widget.ItemTouchHelper; +import androidx.recyclerview.widget.LinearSmoothScroller; +import androidx.recyclerview.widget.RecyclerView; +import androidx.recyclerview.widget.StaggeredGridLayoutManager; + +import com.bumptech.glide.Glide; +import com.bumptech.glide.RequestManager; + +import org.greenrobot.eventbus.EventBus; +import org.greenrobot.eventbus.Subscribe; + +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.Executor; + +import javax.inject.Inject; +import javax.inject.Named; + +import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; +import ml.docilealligator.infinityforreddit.activities.BaseActivity; +import ml.docilealligator.infinityforreddit.asynctasks.LoadSubredditIcon; +import ml.docilealligator.infinityforreddit.asynctasks.LoadUserData; +import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; +import ml.docilealligator.infinityforreddit.customviews.LinearLayoutManagerBugFixed; +import ml.docilealligator.infinityforreddit.events.ChangeRememberMutingOptionInPostFeedEvent; +import ml.docilealligator.infinityforreddit.events.ChangeSwipeActionEvent; +import ml.docilealligator.infinityforreddit.events.ChangeSwipeActionThresholdEvent; +import ml.docilealligator.infinityforreddit.events.ChangeVibrateWhenActionTriggeredEvent; +import ml.docilealligator.infinityforreddit.post.Post; +import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; +import retrofit2.Retrofit; + +public abstract class PostFragmentBase extends Fragment { + + @Inject + @Named("no_oauth") + protected Retrofit mRetrofit; + @Inject + @Named("oauth") + protected Retrofit mOauthRetrofit; + @Inject + @Named("default") + protected SharedPreferences mSharedPreferences; + @Inject + protected RedditDataRoomDatabase mRedditDataRoomDatabase; + @Inject + CustomThemeWrapper mCustomThemeWrapper; + @Inject + protected Executor mExecutor; + protected BaseActivity activity; + protected RequestManager mGlide; + protected Window window; + protected LinearLayoutManagerBugFixed mLinearLayoutManager; + protected StaggeredGridLayoutManager mStaggeredGridLayoutManager; + protected long postFragmentId; + protected boolean rememberMutingOptionInPostFeed; + protected Boolean masterMutingOption; + protected Handler lazyModeHandler; + protected CountDownTimer resumeLazyModeCountDownTimer; + protected RecyclerView.SmoothScroller smoothScroller; + protected LazyModeRunnable lazyModeRunnable; + protected float lazyModeInterval; + protected boolean isInLazyMode = false; + protected boolean isLazyModePaused = false; + protected int postLayout; + protected boolean swipeActionEnabled; + protected ColorDrawable backgroundSwipeRight; + protected ColorDrawable backgroundSwipeLeft; + protected Drawable drawableSwipeRight; + protected Drawable drawableSwipeLeft; + protected boolean vibrateWhenActionTriggered; + protected float swipeActionThreshold; + protected int swipeLeftAction; + protected int swipeRightAction; + protected ItemTouchHelper touchHelper; + protected final Map subredditOrUserIcons = new HashMap<>(); + + public PostFragmentBase() { + // Required empty public constructor + } + + @Nullable + @Override + public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { + EventBus.getDefault().register(this); + + window = activity.getWindow(); + + rememberMutingOptionInPostFeed = mSharedPreferences.getBoolean(SharedPreferencesUtils.REMEMBER_MUTING_OPTION_IN_POST_FEED, false); + + smoothScroller = new LinearSmoothScroller(activity) { + @Override + protected int getVerticalSnapPreference() { + return LinearSmoothScroller.SNAP_TO_START; + } + }; + + lazyModeHandler = new Handler(); + lazyModeInterval = Float.parseFloat(mSharedPreferences.getString(SharedPreferencesUtils.LAZY_MODE_INTERVAL_KEY, "2.5")); + resumeLazyModeCountDownTimer = new CountDownTimer((long) (lazyModeInterval * 1000), (long) (lazyModeInterval * 1000)) { + @Override + public void onTick(long l) { + + } + + @Override + public void onFinish() { + resumeLazyMode(true); + } + }; + + mGlide = Glide.with(activity); + + vibrateWhenActionTriggered = mSharedPreferences.getBoolean(SharedPreferencesUtils.VIBRATE_WHEN_ACTION_TRIGGERED, true); + swipeActionThreshold = Float.parseFloat(mSharedPreferences.getString(SharedPreferencesUtils.SWIPE_ACTION_THRESHOLD, "0.3")); + swipeRightAction = Integer.parseInt(mSharedPreferences.getString(SharedPreferencesUtils.SWIPE_RIGHT_ACTION, "1")); + swipeLeftAction = Integer.parseInt(mSharedPreferences.getString(SharedPreferencesUtils.SWIPE_LEFT_ACTION, "0")); + initializeSwipeActionDrawable(); + + return super.onCreateView(inflater, container, savedInstanceState); + } + + @Override + public void onDestroy() { + EventBus.getDefault().unregister(this); + super.onDestroy(); + } + + @Override + public void onAttach(@NonNull Context context) { + super.onAttach(context); + this.activity = (BaseActivity) context; + } + + public final boolean handleKeyDown(int keyCode) { + boolean volumeKeysNavigatePosts = mSharedPreferences.getBoolean(SharedPreferencesUtils.VOLUME_KEYS_NAVIGATE_POSTS, false); + if (volumeKeysNavigatePosts) { + switch (keyCode) { + case KeyEvent.KEYCODE_VOLUME_UP: + return scrollPostsByCount(-1); + case KeyEvent.KEYCODE_VOLUME_DOWN: + return scrollPostsByCount(1); + } + } + return false; + } + + public final long getPostFragmentId() { + return postFragmentId; + } + + public abstract boolean startLazyMode(); + + public abstract void stopLazyMode(); + + public abstract void resumeLazyMode(boolean resumeNow); + + public abstract void pauseLazyMode(boolean startTimer); + + public final boolean isInLazyMode() { + return isInLazyMode; + } + + protected final int getNColumns(Resources resources) { + final boolean foldEnabled = mSharedPreferences.getBoolean(SharedPreferencesUtils.ENABLE_FOLD_SUPPORT, false); + if (resources.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { + switch (postLayout) { + case SharedPreferencesUtils.POST_LAYOUT_CARD_2: + return Integer.parseInt(mSharedPreferences.getString(SharedPreferencesUtils.NUMBER_OF_COLUMNS_IN_POST_FEED_PORTRAIT_CARD_LAYOUT_2, "1")); + case SharedPreferencesUtils.POST_LAYOUT_COMPACT: + return Integer.parseInt(mSharedPreferences.getString(SharedPreferencesUtils.NUMBER_OF_COLUMNS_IN_POST_FEED_PORTRAIT_COMPACT_LAYOUT, "1")); + case SharedPreferencesUtils.POST_LAYOUT_GALLERY: + return Integer.parseInt(mSharedPreferences.getString(SharedPreferencesUtils.NUMBER_OF_COLUMNS_IN_POST_FEED_PORTRAIT_GALLERY_LAYOUT, "2")); + default: + if (getResources().getBoolean(R.bool.isTablet)) { + if (foldEnabled) { + return Integer.parseInt(mSharedPreferences.getString(SharedPreferencesUtils.NUMBER_OF_COLUMNS_IN_POST_FEED_PORTRAIT_UNFOLDED, "2")); + } else { + return Integer.parseInt(mSharedPreferences.getString(SharedPreferencesUtils.NUMBER_OF_COLUMNS_IN_POST_FEED_PORTRAIT, "2")); + } + } + return Integer.parseInt(mSharedPreferences.getString(SharedPreferencesUtils.NUMBER_OF_COLUMNS_IN_POST_FEED_PORTRAIT, "1")); + } + } else { + switch (postLayout) { + case SharedPreferencesUtils.POST_LAYOUT_CARD_2: + return Integer.parseInt(mSharedPreferences.getString(SharedPreferencesUtils.NUMBER_OF_COLUMNS_IN_POST_FEED_LANDSCAPE_CARD_LAYOUT_2, "2")); + case SharedPreferencesUtils.POST_LAYOUT_COMPACT: + return Integer.parseInt(mSharedPreferences.getString(SharedPreferencesUtils.NUMBER_OF_COLUMNS_IN_POST_FEED_LANDSCAPE_COMPACT_LAYOUT, "2")); + case SharedPreferencesUtils.POST_LAYOUT_GALLERY: + return Integer.parseInt(mSharedPreferences.getString(SharedPreferencesUtils.NUMBER_OF_COLUMNS_IN_POST_FEED_LANDSCAPE_GALLERY_LAYOUT, "2")); + default: + if (getResources().getBoolean(R.bool.isTablet) && foldEnabled) { + return Integer.parseInt(mSharedPreferences.getString(SharedPreferencesUtils.NUMBER_OF_COLUMNS_IN_POST_FEED_LANDSCAPE_UNFOLDED, "2")); + } + return Integer.parseInt(mSharedPreferences.getString(SharedPreferencesUtils.NUMBER_OF_COLUMNS_IN_POST_FEED_LANDSCAPE, "2")); + } + } + } + + public final void changePostLayout(int postLayout) { + changePostLayout(postLayout, false); + } + + public abstract void changePostLayout(int postLayout, boolean temporary); + + public final Boolean getMasterMutingOption() { + return masterMutingOption; + } + + public final void videoAutoplayChangeMutingOption(boolean isMute) { + if (rememberMutingOptionInPostFeed) { + masterMutingOption = isMute; + } + } + + public boolean getIsNsfwSubreddit() { + return false; + } + + public abstract boolean isRecyclerViewItemSwipeable(RecyclerView.ViewHolder viewHolder); + + public final void loadIcon(String subredditOrUserName, boolean isSubreddit, LoadIconListener loadIconListener) { + if (subredditOrUserIcons.containsKey(subredditOrUserName)) { + loadIconListener.loadIconSuccess(subredditOrUserName, subredditOrUserIcons.get(subredditOrUserName)); + } else { + if (isSubreddit) { + LoadSubredditIcon.loadSubredditIcon(mExecutor, new Handler(), mRedditDataRoomDatabase, + subredditOrUserName, activity.accessToken, activity.accountName, mOauthRetrofit, mRetrofit, + iconImageUrl -> { + subredditOrUserIcons.put(subredditOrUserName, iconImageUrl); + loadIconListener.loadIconSuccess(subredditOrUserName, iconImageUrl); + }); + } else { + LoadUserData.loadUserData(mExecutor, new Handler(), mRedditDataRoomDatabase, subredditOrUserName, + mRetrofit, iconImageUrl -> { + subredditOrUserIcons.put(subredditOrUserName, iconImageUrl); + loadIconListener.loadIconSuccess(subredditOrUserName, iconImageUrl); + }); + } + } + } + + public void markPostAsRead(Post post) { + // no-op + } + + protected abstract boolean scrollPostsByCount(int count); + + protected final void initializeSwipeActionDrawable() { + if (swipeRightAction == SharedPreferencesUtils.SWIPE_ACITON_DOWNVOTE) { + backgroundSwipeRight = new ColorDrawable(mCustomThemeWrapper.getDownvoted()); + drawableSwipeRight = ResourcesCompat.getDrawable(activity.getResources(), R.drawable.ic_arrow_downward_day_night_24dp, null); + } else { + backgroundSwipeRight = new ColorDrawable(mCustomThemeWrapper.getUpvoted()); + drawableSwipeRight = ResourcesCompat.getDrawable(activity.getResources(), R.drawable.ic_arrow_upward_day_night_24dp, null); + } + + if (swipeLeftAction == SharedPreferencesUtils.SWIPE_ACITON_UPVOTE) { + backgroundSwipeLeft = new ColorDrawable(mCustomThemeWrapper.getUpvoted()); + drawableSwipeLeft = ResourcesCompat.getDrawable(activity.getResources(), R.drawable.ic_arrow_upward_day_night_24dp, null); + } else { + backgroundSwipeLeft = new ColorDrawable(mCustomThemeWrapper.getDownvoted()); + drawableSwipeLeft = ResourcesCompat.getDrawable(activity.getResources(), R.drawable.ic_arrow_downward_day_night_24dp, null); + } + } + + @Subscribe + public void onChangeRememberMutingOptionInPostFeedEvent(ChangeRememberMutingOptionInPostFeedEvent event) { + rememberMutingOptionInPostFeed = event.rememberMutingOptionInPostFeedEvent; + if (!event.rememberMutingOptionInPostFeedEvent) { + masterMutingOption = null; + } + } + + @Subscribe + public void onChangeSwipeActionEvent(ChangeSwipeActionEvent changeSwipeActionEvent) { + swipeRightAction = changeSwipeActionEvent.swipeRightAction == -1 ? swipeRightAction : changeSwipeActionEvent.swipeRightAction; + swipeLeftAction = changeSwipeActionEvent.swipeLeftAction == -1 ? swipeLeftAction : changeSwipeActionEvent.swipeLeftAction; + initializeSwipeActionDrawable(); + } + + @Subscribe + public void onChangeSwipeActionThresholdEvent(ChangeSwipeActionThresholdEvent changeSwipeActionThresholdEvent) { + swipeActionThreshold = changeSwipeActionThresholdEvent.swipeActionThreshold; + } + + @Subscribe + public void onChangeVibrateWhenActionTriggeredEvent(ChangeVibrateWhenActionTriggeredEvent changeVibrateWhenActionTriggeredEvent) { + vibrateWhenActionTriggered = changeVibrateWhenActionTriggeredEvent.vibrateWhenActionTriggered; + } + + protected static abstract class LazyModeRunnable implements Runnable { + private int currentPosition = -1; + + int getCurrentPosition() { + return currentPosition; + } + + void setCurrentPosition(int currentPosition) { + this.currentPosition = currentPosition; + } + + void incrementCurrentPosition() { + currentPosition++; + } + + void resetOldPosition() { + currentPosition = -1; + } + } + + protected static class StaggeredGridLayoutManagerItemOffsetDecoration extends RecyclerView.ItemDecoration { + + private final int mItemOffset; + private final int mNColumns; + + StaggeredGridLayoutManagerItemOffsetDecoration(int itemOffset, int nColumns) { + mItemOffset = itemOffset; + mNColumns = nColumns; + } + + StaggeredGridLayoutManagerItemOffsetDecoration(@NonNull Context context, @DimenRes int itemOffsetId, int nColumns) { + this(context.getResources().getDimensionPixelSize(itemOffsetId), nColumns); + } + + @Override + public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, + @NonNull RecyclerView.State state) { + super.getItemOffsets(outRect, view, parent, state); + + StaggeredGridLayoutManager.LayoutParams layoutParams = (StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams(); + + int spanIndex = layoutParams.getSpanIndex(); + + int halfOffset = mItemOffset / 2; + + if (mNColumns == 2) { + if (spanIndex == 0) { + outRect.set(halfOffset, 0, halfOffset / 2, 0); + } else { + outRect.set(halfOffset / 2, 0, halfOffset, 0); + } + } else if (mNColumns == 3) { + if (spanIndex == 0) { + outRect.set(halfOffset, 0, halfOffset / 2, 0); + } else if (spanIndex == 1) { + outRect.set(halfOffset / 2, 0, halfOffset / 2, 0); + } else { + outRect.set(halfOffset / 2, 0, halfOffset, 0); + } + } + } + } + + public interface LoadIconListener { + void loadIconSuccess(String subredditOrUserName, String iconUrl); + } +} \ No newline at end of file -- cgit v1.2.3