diff options
author | Alex Ning <chineseperson5@gmail.com> | 2020-11-09 09:01:53 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2020-11-09 09:01:53 +0000 |
commit | 4de6887423f7ba499873b8e2a6984b1efd701cd2 (patch) | |
tree | 5547b01cd740d7b00458998382968049e4ef1e82 /app/src/main | |
parent | d37a386291c89e3740a762dbc99fe621fb8faac3 (diff) | |
download | infinity-for-reddit-4de6887423f7ba499873b8e2a6984b1efd701cd2.tar infinity-for-reddit-4de6887423f7ba499873b8e2a6984b1efd701cd2.tar.gz infinity-for-reddit-4de6887423f7ba499873b8e2a6984b1efd701cd2.tar.bz2 infinity-for-reddit-4de6887423f7ba499873b8e2a6984b1efd701cd2.tar.lz infinity-for-reddit-4de6887423f7ba499873b8e2a6984b1efd701cd2.tar.xz infinity-for-reddit-4de6887423f7ba499873b8e2a6984b1efd701cd2.tar.zst infinity-for-reddit-4de6887423f7ba499873b8e2a6984b1efd701cd2.zip |
Selecting swipe actions is avavilable.
Diffstat (limited to 'app/src/main')
9 files changed, 147 insertions, 32 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SettingsActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SettingsActivity.java index bd6f7ef1..10b9b034 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SettingsActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SettingsActivity.java @@ -97,6 +97,9 @@ public class SettingsActivity extends BaseActivity implements private void getCurrentAccountAndBindView(Bundle savedInstanceState) { new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> { + if (getSupportFragmentManager().isDestroyed()) { + return; + } if (account == null) { mNullAccountName = true; } else { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java index 0a80f67e..1928febf 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java @@ -1312,18 +1312,34 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView return null; } - public void onItemSwipe(RecyclerView.ViewHolder viewHolder, int direction) { + public void onItemSwipe(RecyclerView.ViewHolder viewHolder, int direction, int swipeLeftAction, int swipeRightAction) { if (viewHolder instanceof PostBaseViewHolder) { if (direction == ItemTouchHelper.LEFT || direction == ItemTouchHelper.START) { - ((PostBaseViewHolder) viewHolder).upvoteButton.performClick(); + if (swipeLeftAction == SharedPreferencesUtils.SWIPE_ACITON_UPVOTE) { + ((PostBaseViewHolder) viewHolder).upvoteButton.performClick(); + } else if (swipeLeftAction == SharedPreferencesUtils.SWIPE_ACITON_DOWNVOTE) { + ((PostBaseViewHolder) viewHolder).downvoteButton.performClick(); + } } else { - ((PostBaseViewHolder) viewHolder).downvoteButton.performClick(); + if (swipeRightAction == SharedPreferencesUtils.SWIPE_ACITON_UPVOTE) { + ((PostBaseViewHolder) viewHolder).upvoteButton.performClick(); + } else if (swipeRightAction == SharedPreferencesUtils.SWIPE_ACITON_DOWNVOTE) { + ((PostBaseViewHolder) viewHolder).downvoteButton.performClick(); + } } } else if (viewHolder instanceof PostCompactBaseViewHolder) { if (direction == ItemTouchHelper.LEFT || direction == ItemTouchHelper.START) { - ((PostCompactBaseViewHolder) viewHolder).upvoteButton.performClick(); + if (swipeLeftAction == SharedPreferencesUtils.SWIPE_ACITON_UPVOTE) { + ((PostCompactBaseViewHolder) viewHolder).upvoteButton.performClick(); + } else if (swipeLeftAction == SharedPreferencesUtils.SWIPE_ACITON_DOWNVOTE) { + ((PostCompactBaseViewHolder) viewHolder).downvoteButton.performClick(); + } } else { - ((PostCompactBaseViewHolder) viewHolder).downvoteButton.performClick(); + if (swipeRightAction == SharedPreferencesUtils.SWIPE_ACITON_UPVOTE) { + ((PostCompactBaseViewHolder) viewHolder).upvoteButton.performClick(); + } else if (swipeRightAction == SharedPreferencesUtils.SWIPE_ACITON_DOWNVOTE) { + ((PostCompactBaseViewHolder) viewHolder).downvoteButton.performClick(); + } } } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Event/ChangeSwipeActionEvent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Event/ChangeSwipeActionEvent.java new file mode 100644 index 00000000..fabda6ff --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Event/ChangeSwipeActionEvent.java @@ -0,0 +1,11 @@ +package ml.docilealligator.infinityforreddit.Event; + +public class ChangeSwipeActionEvent { + public int swipeLeftAction; + public int swipeRightAction; + + public ChangeSwipeActionEvent(int swipeLeftAction, int swipeRightAction) { + this.swipeLeftAction = swipeLeftAction; + this.swipeRightAction = swipeRightAction; + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java index e431341a..d5ad7878 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java @@ -14,7 +14,6 @@ import android.os.Build; import android.os.Bundle; import android.os.CountDownTimer; import android.os.Handler; -import android.os.Vibrator; import android.util.DisplayMetrics; import android.view.HapticFeedbackConstants; import android.view.KeyEvent; @@ -87,6 +86,7 @@ import ml.docilealligator.infinityforreddit.Event.ChangeShowAbsoluteNumberOfVote import ml.docilealligator.infinityforreddit.Event.ChangeShowElapsedTimeEvent; import ml.docilealligator.infinityforreddit.Event.ChangeSpoilerBlurEvent; import ml.docilealligator.infinityforreddit.Event.ChangeStartAutoplayVisibleAreaOffsetEvent; +import ml.docilealligator.infinityforreddit.Event.ChangeSwipeActionEvent; import ml.docilealligator.infinityforreddit.Event.ChangeSwipeActionThresholdEvent; import ml.docilealligator.infinityforreddit.Event.ChangeTimeFormatEvent; import ml.docilealligator.infinityforreddit.Event.ChangeVibrateWhenActionTriggeredEvent; @@ -202,11 +202,13 @@ public class PostFragment extends Fragment implements FragmentCommunicator { private int maxPosition = -1; private int postLayout; private SortType sortType; - private ColorDrawable backgroundLeft; - private ColorDrawable backgroundRight; - private Drawable drawableLeft; - private Drawable drawableRight; - private float swipeActionThreshold = 0.3f; + private ColorDrawable backgroundSwipeRight; + private ColorDrawable backgroundSwipeLeft; + private Drawable drawableSwipeRight; + private Drawable drawableSwipeLeft; + private int swipeLeftAction; + private int swipeRightAction; + private float swipeActionThreshold; private ItemTouchHelper touchHelper; private ArrayList<SubredditFilter> subredditFilterList; @@ -415,6 +417,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { savePostFeedScrolledPosition = mSharedPreferences.getBoolean(SharedPreferencesUtils.SAVE_FRONT_PAGE_SCROLLED_POSITION, false); vibrateWhenActionTriggered = mSharedPreferences.getBoolean(SharedPreferencesUtils.VIBRATE_WHEN_ACTION_TRIGGERED, true); boolean enableSwipeAction = mSharedPreferences.getBoolean(SharedPreferencesUtils.ENABLE_SWIPE_ACTION, false); + swipeActionThreshold = Float.parseFloat(mSharedPreferences.getString(SharedPreferencesUtils.SWIPE_ACTION_THRESHOLD, "0.3")); Locale locale = getResources().getConfiguration().locale; if (postType == PostDataSource.TYPE_SEARCH) { @@ -689,11 +692,10 @@ public class PostFragment extends Fragment implements FragmentCommunicator { ((ActivityToolbarInterface) activity).displaySortType(); } - Vibrator v = (Vibrator) activity.getSystemService(Context.VIBRATOR_SERVICE); - backgroundLeft = new ColorDrawable(customThemeWrapper.getDownvoted()); - backgroundRight = new ColorDrawable(customThemeWrapper.getUpvoted()); - drawableLeft = ResourcesCompat.getDrawable(activity.getResources(), R.drawable.ic_arrow_downward_black_24dp, null); - drawableRight = ResourcesCompat.getDrawable(activity.getResources(), R.drawable.ic_arrow_upward_black_24dp, null); + 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; @@ -723,7 +725,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { touchHelper.attachToRecyclerView(null); touchHelper.attachToRecyclerView(mPostRecyclerView); if (mAdapter != null) { - mAdapter.onItemSwipe(viewHolder, direction); + mAdapter.onItemSwipe(viewHolder, direction, swipeLeftAction, swipeRightAction); } } } @@ -741,18 +743,18 @@ public class PostFragment extends Fragment implements FragmentCommunicator { viewHolder.itemView.setHapticFeedbackEnabled(true); viewHolder.itemView.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY, HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING); } - backgroundLeft.setBounds(0, itemView.getTop(), itemView.getRight(), itemView.getBottom()); + backgroundSwipeRight.setBounds(0, itemView.getTop(), itemView.getRight(), itemView.getBottom()); } else { exceedThreshold = false; - backgroundLeft.setBounds(0, 0, 0, 0); + backgroundSwipeRight.setBounds(0, 0, 0, 0); } - drawableLeft.setBounds(itemView.getLeft() + ((int) dX) - horizontalOffset - drawableLeft.getIntrinsicWidth(), - (itemView.getBottom() + itemView.getTop() - drawableLeft.getIntrinsicHeight()) / 2, + drawableSwipeRight.setBounds(itemView.getLeft() + ((int) dX) - horizontalOffset - drawableSwipeRight.getIntrinsicWidth(), + (itemView.getBottom() + itemView.getTop() - drawableSwipeRight.getIntrinsicHeight()) / 2, itemView.getLeft() + ((int) dX) - horizontalOffset, - (itemView.getBottom() + itemView.getTop() + drawableLeft.getIntrinsicHeight()) / 2); - backgroundLeft.draw(c); - drawableLeft.draw(c); + (itemView.getBottom() + itemView.getTop() + drawableSwipeRight.getIntrinsicHeight()) / 2); + backgroundSwipeRight.draw(c); + drawableSwipeRight.draw(c); } else if (dX < 0) { if (-dX > (itemView.getRight() - itemView.getLeft()) * swipeActionThreshold) { if (!exceedThreshold) { @@ -760,17 +762,17 @@ public class PostFragment extends Fragment implements FragmentCommunicator { viewHolder.itemView.setHapticFeedbackEnabled(true); viewHolder.itemView.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY, HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING); } - backgroundRight.setBounds(0, itemView.getTop(), itemView.getRight(), itemView.getBottom()); + backgroundSwipeLeft.setBounds(0, itemView.getTop(), itemView.getRight(), itemView.getBottom()); } else { exceedThreshold = false; - backgroundRight.setBounds(0, 0, 0, 0); + backgroundSwipeLeft.setBounds(0, 0, 0, 0); } - drawableRight.setBounds(itemView.getRight() + ((int) dX) + horizontalOffset, - (itemView.getBottom() + itemView.getTop() - drawableRight.getIntrinsicHeight()) / 2, - itemView.getRight() + ((int) dX) + horizontalOffset + drawableRight.getIntrinsicWidth(), - (itemView.getBottom() + itemView.getTop() + drawableRight.getIntrinsicHeight()) / 2); - backgroundRight.draw(c); - drawableRight.draw(c); + drawableSwipeLeft.setBounds(itemView.getRight() + ((int) dX) + horizontalOffset, + (itemView.getBottom() + itemView.getTop() - drawableSwipeLeft.getIntrinsicHeight()) / 2, + itemView.getRight() + ((int) dX) + horizontalOffset + drawableSwipeLeft.getIntrinsicWidth(), + (itemView.getBottom() + itemView.getTop() + drawableSwipeLeft.getIntrinsicHeight()) / 2); + backgroundSwipeLeft.draw(c); + drawableSwipeLeft.draw(c); } } @@ -879,6 +881,24 @@ public class PostFragment extends Fragment implements FragmentCommunicator { mPostViewModel.changeSortType(sortType); } + private void initializeSwipeActionDrawable() { + if (swipeRightAction == SharedPreferencesUtils.SWIPE_ACITON_DOWNVOTE) { + backgroundSwipeRight = new ColorDrawable(customThemeWrapper.getDownvoted()); + drawableSwipeRight = ResourcesCompat.getDrawable(activity.getResources(), R.drawable.ic_arrow_downward_black_24dp, null); + } else { + backgroundSwipeRight = new ColorDrawable(customThemeWrapper.getUpvoted()); + drawableSwipeRight = ResourcesCompat.getDrawable(activity.getResources(), R.drawable.ic_arrow_upward_black_24dp, null); + } + + if (swipeLeftAction == SharedPreferencesUtils.SWIPE_ACITON_UPVOTE) { + backgroundSwipeLeft = new ColorDrawable(customThemeWrapper.getUpvoted()); + drawableSwipeLeft = ResourcesCompat.getDrawable(activity.getResources(), R.drawable.ic_arrow_upward_black_24dp, null); + } else { + backgroundSwipeLeft = new ColorDrawable(customThemeWrapper.getDownvoted()); + drawableSwipeLeft = ResourcesCompat.getDrawable(activity.getResources(), R.drawable.ic_arrow_downward_black_24dp, null); + } + } + @Override public void onAttach(@NonNull Context context) { super.onAttach(context); @@ -1315,6 +1335,13 @@ 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(); + } + private void refreshAdapter() { int previousPosition = -1; if (mLinearLayoutManager != null) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/SwipeActionPreferenceFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/SwipeActionPreferenceFragment.java index 78844f06..4fe474ec 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/SwipeActionPreferenceFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/SwipeActionPreferenceFragment.java @@ -10,6 +10,7 @@ import org.greenrobot.eventbus.EventBus; import ml.docilealligator.infinityforreddit.Event.ChangeDisableSwipingBetweenTabsEvent; import ml.docilealligator.infinityforreddit.Event.ChangeEnableSwipeActionSwitchEvent; +import ml.docilealligator.infinityforreddit.Event.ChangeSwipeActionEvent; import ml.docilealligator.infinityforreddit.Event.ChangeSwipeActionThresholdEvent; import ml.docilealligator.infinityforreddit.Event.ChangeVibrateWhenActionTriggeredEvent; import ml.docilealligator.infinityforreddit.R; @@ -22,6 +23,8 @@ public class SwipeActionPreferenceFragment extends PreferenceFragmentCompat { setPreferencesFromResource(R.xml.swipe_action_preferences, rootKey); SwitchPreference enableSwipeActionSwitch = findPreference(SharedPreferencesUtils.ENABLE_SWIPE_ACTION); + ListPreference swipeLeftActionListPreference = findPreference(SharedPreferencesUtils.SWIPE_LEFT_ACTION); + ListPreference swipeRightActionListPreference = findPreference(SharedPreferencesUtils.SWIPE_RIGHT_ACTION); SwitchPreference vibrateWhenActionTriggeredSwitch = findPreference(SharedPreferencesUtils.VIBRATE_WHEN_ACTION_TRIGGERED); SwitchPreference disableSwipingBetweenTabsSwitch = findPreference(SharedPreferencesUtils.DISABLE_SWIPING_BETWEEN_TABS); ListPreference swipeActionThresholdListPreference = findPreference(SharedPreferencesUtils.SWIPE_ACTION_THRESHOLD); @@ -32,6 +35,29 @@ public class SwipeActionPreferenceFragment extends PreferenceFragmentCompat { return true; }); } + + if (swipeLeftActionListPreference != null) { + swipeLeftActionListPreference.setOnPreferenceChangeListener((preference, newValue) -> { + if (swipeRightActionListPreference != null) { + EventBus.getDefault().post(new ChangeSwipeActionEvent(Integer.parseInt((String) newValue), Integer.parseInt(swipeRightActionListPreference.getValue()))); + } else { + EventBus.getDefault().post(new ChangeSwipeActionEvent(Integer.parseInt((String) newValue), -1)); + } + return true; + }); + } + + if (swipeRightActionListPreference != null) { + swipeRightActionListPreference.setOnPreferenceChangeListener((preference, newValue) -> { + if (swipeLeftActionListPreference != null) { + EventBus.getDefault().post(new ChangeSwipeActionEvent(Integer.parseInt(swipeLeftActionListPreference.getValue()), Integer.parseInt((String) newValue))); + } else { + EventBus.getDefault().post(new ChangeSwipeActionEvent(-1, Integer.parseInt((String) newValue))); + } + return true; + }); + } + if (vibrateWhenActionTriggeredSwitch != null) { vibrateWhenActionTriggeredSwitch.setOnPreferenceChangeListener((preference, newValue) -> { EventBus.getDefault().post(new ChangeVibrateWhenActionTriggeredEvent((Boolean) newValue)); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java index b4d36d1b..6b441e27 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java @@ -140,6 +140,10 @@ public class SharedPreferencesUtils { public static final String UFO_CAPTURING_ANIMATION = "ufo_capturing_animation"; public static final String HIDE_SUBREDDIT_DESCRIPTION = "hide_subreddit_description"; public static final String DISABLE_IMAGE_PREVIEW = "disable_image_preview"; + public static final String SWIPE_LEFT_ACTION = "swipe_left_action"; + public static final String SWIPE_RIGHT_ACTION = "swipe_right_action"; + public static final int SWIPE_ACITON_UPVOTE = 0; + public static final int SWIPE_ACITON_DOWNVOTE = 1; public static final String MAIN_PAGE_TABS_SHARED_PREFERENCES_FILE = "ml.docilealligator.infinityforreddit.main_page_tabs"; public static final String MAIN_PAGE_TAB_COUNT = "_main_page_tab_count"; diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml index c26ce4f4..767e2fb9 100644 --- a/app/src/main/res/values/arrays.xml +++ b/app/src/main/res/values/arrays.xml @@ -285,4 +285,14 @@ <item>2</item> </string-array> + <string-array name="settings_swipe_actions"> + <item>Upvote</item> + <item>Downvote</item> + </string-array> + + <string-array name="settings_swipe_actions_values"> + <item>0</item> + <item>1</item> + </string-array> + </resources>
\ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index bf0a7866..19768479 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -504,6 +504,8 @@ <string name="settings_credits_ufo_capturing_animation_title">UFO Capturing Animation</string> <string name="settings_hide_subreddit_description_title">Hide Subreddit Description</string> <string name="settings_disable_image_preview_title">Disable Image Preview in Data Saving Mode</string> + <string name="settings_swipe_action_swipe_left_title">Swipe Left</string> + <string name="settings_swipe_action_swipe_right_title">Swipe Right</string> <string name="no_link_available">Cannot get the link</string> diff --git a/app/src/main/res/xml/swipe_action_preferences.xml b/app/src/main/res/xml/swipe_action_preferences.xml index 60a34c1b..36326765 100644 --- a/app/src/main/res/xml/swipe_action_preferences.xml +++ b/app/src/main/res/xml/swipe_action_preferences.xml @@ -6,6 +6,22 @@ app:key="enable_swipe_action" app:title="@string/settings_enable_swipe_action_title" /> + <ListPreference + app:defaultValue="0" + app:entries="@array/settings_swipe_actions" + app:entryValues="@array/settings_swipe_actions_values" + app:key="swipe_left_action" + app:title="@string/settings_swipe_action_swipe_left_title" + app:useSimpleSummaryProvider="true" /> + + <ListPreference + app:defaultValue="1" + app:entries="@array/settings_swipe_actions" + app:entryValues="@array/settings_swipe_actions_values" + app:key="swipe_right_action" + app:title="@string/settings_swipe_action_swipe_right_title" + app:useSimpleSummaryProvider="true" /> + <SwitchPreference app:defaultValue="true" app:key="vibrate_when_action_triggered" |