diff options
author | Alex Ning <chineseperson5@gmail.com> | 2021-02-17 13:44:59 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2021-02-17 13:44:59 +0000 |
commit | 52d6691ae7ea7d9d5a19bede87b913f33e0645a3 (patch) | |
tree | da85b2f9454e28a8629cafa9552cd6cb9f4334ee /app/src/main/java/ml | |
parent | 9b9a1aa308659fc2f031f468002eeff15323b9d6 (diff) | |
download | infinity-for-reddit-52d6691ae7ea7d9d5a19bede87b913f33e0645a3.tar infinity-for-reddit-52d6691ae7ea7d9d5a19bede87b913f33e0645a3.tar.gz infinity-for-reddit-52d6691ae7ea7d9d5a19bede87b913f33e0645a3.tar.bz2 infinity-for-reddit-52d6691ae7ea7d9d5a19bede87b913f33e0645a3.tar.lz infinity-for-reddit-52d6691ae7ea7d9d5a19bede87b913f33e0645a3.tar.xz infinity-for-reddit-52d6691ae7ea7d9d5a19bede87b913f33e0645a3.tar.zst infinity-for-reddit-52d6691ae7ea7d9d5a19bede87b913f33e0645a3.zip |
Customize the number of columns in Post Feed for every post layout.
Diffstat (limited to 'app/src/main/java/ml')
6 files changed, 150 insertions, 77 deletions
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 b514b160..cbce964b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java @@ -319,25 +319,6 @@ public class PostFragment extends Fragment implements FragmentCommunicator { activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); int windowWidth = displayMetrics.widthPixels; - int nColumns; - if (resources.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { - nColumns = Integer.parseInt(mSharedPreferences.getString(SharedPreferencesUtils.NUMBER_OF_COLUMNS_IN_POST_FEED_PORTRAIT, "1")); - } else { - nColumns = Integer.parseInt(mSharedPreferences.getString(SharedPreferencesUtils.NUMBER_OF_COLUMNS_IN_POST_FEED_LANDSCAPE, "2")); - } - - if (nColumns == 1) { - mLinearLayoutManager = new LinearLayoutManager(activity); - mPostRecyclerView.setLayoutManager(mLinearLayoutManager); - } else { - mStaggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL); - mPostRecyclerView.setLayoutManager(mStaggeredGridLayoutManager); - StaggeredGridLayoutManagerItemOffsetDecoration itemDecoration = - new StaggeredGridLayoutManagerItemOffsetDecoration(activity, R.dimen.staggeredLayoutManagerItemOffset); - mPostRecyclerView.addItemDecoration(itemDecoration); - windowWidth /= 2; - } - mGlide = Glide.with(activity); lazyModeRunnable = new LazyModeRunnable() { @@ -747,6 +728,19 @@ public class PostFragment extends Fragment implements FragmentCommunicator { }); } + int nColumns = getNColumns(resources); + if (nColumns == 1) { + mLinearLayoutManager = new LinearLayoutManager(activity); + mPostRecyclerView.setLayoutManager(mLinearLayoutManager); + } else { + mStaggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL); + mPostRecyclerView.setLayoutManager(mStaggeredGridLayoutManager); + StaggeredGridLayoutManagerItemOffsetDecoration itemDecoration = + new StaggeredGridLayoutManagerItemOffsetDecoration(activity, R.dimen.staggeredLayoutManagerItemOffset); + mPostRecyclerView.addItemDecoration(itemDecoration); + windowWidth /= 2; + } + mAdapter.setHideReadPostsIndex(hideReadPostsIndex); if (activity instanceof ActivityToolbarInterface) { @@ -919,6 +913,28 @@ public class PostFragment extends Fragment implements FragmentCommunicator { return rootView; } + private int getNColumns(Resources resources) { + if (resources.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { + switch (postLayout) { + 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: + return Integer.parseInt(mSharedPreferences.getString(SharedPreferencesUtils.NUMBER_OF_COLUMNS_IN_POST_FEED_PORTRAIT, "1")); + } + } else { + switch (postLayout) { + 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: + return Integer.parseInt(mSharedPreferences.getString(SharedPreferencesUtils.NUMBER_OF_COLUMNS_IN_POST_FEED_LANDSCAPE, "2")); + } + } + } + private void initializeAndBindPostViewModel(String accessToken) { if (postType == PostDataSource.TYPE_SEARCH) { mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(accessToken == null ? mRetrofit : mOauthRetrofit, accessToken, @@ -1237,6 +1253,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { @Override public void changePostLayout(int postLayout) { + this.postLayout = postLayout; switch (postType) { case PostDataSource.TYPE_FRONT_PAGE: mPostLayoutSharedPreferences.edit().putInt(SharedPreferencesUtils.POST_LAYOUT_FRONT_PAGE_POST, postLayout).apply(); @@ -1255,6 +1272,23 @@ public class PostFragment extends Fragment implements FragmentCommunicator { break; } + int nColumns = getNColumns(getResources()); + if (nColumns == 1) { + mLinearLayoutManager = new LinearLayoutManager(activity); + mPostRecyclerView.setLayoutManager(mLinearLayoutManager); + mStaggeredGridLayoutManager = null; + if (mPostRecyclerView.getItemDecorationCount() > 0) { + mPostRecyclerView.removeItemDecorationAt(0); + } + } else if (nColumns == 2) { + mStaggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL); + mPostRecyclerView.setLayoutManager(mStaggeredGridLayoutManager); + StaggeredGridLayoutManagerItemOffsetDecoration itemDecoration = + new StaggeredGridLayoutManagerItemOffsetDecoration(activity, R.dimen.staggeredLayoutManagerItemOffset); + mPostRecyclerView.addItemDecoration(itemDecoration); + mLinearLayoutManager = null; + } + if (mAdapter != null) { mAdapter.setPostLayout(postLayout); refreshAdapter(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/settings/CommentPreferenceFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/settings/CommentPreferenceFragment.java new file mode 100644 index 00000000..a5d41885 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/settings/CommentPreferenceFragment.java @@ -0,0 +1,15 @@ +package ml.docilealligator.infinityforreddit.settings; + +import android.os.Bundle; + +import androidx.preference.PreferenceFragmentCompat; + +import ml.docilealligator.infinityforreddit.R; + +public class CommentPreferenceFragment extends PreferenceFragmentCompat { + + @Override + public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { + setPreferencesFromResource(R.xml.comment_preferences, rootKey); + } +}
\ No newline at end of file diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/settings/GesturesAndButtonsPreferenceFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/settings/GesturesAndButtonsPreferenceFragment.java index cd151ad3..882455ef 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/settings/GesturesAndButtonsPreferenceFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/settings/GesturesAndButtonsPreferenceFragment.java @@ -35,7 +35,7 @@ public class GesturesAndButtonsPreferenceFragment extends PreferenceFragmentComp @Override public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { - setPreferencesFromResource(R.xml.gestures_and_buttons_preference, rootKey); + setPreferencesFromResource(R.xml.gestures_and_buttons_preferences, rootKey); ((Infinity) activity.getApplication()).getAppComponent().inject(this); SwitchPreference lockJumpToNextTopLevelCommentButtonSwitch = diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/settings/InterfacePreferenceFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/settings/InterfacePreferenceFragment.java index e873f8ba..2ef43ebf 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/settings/InterfacePreferenceFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/settings/InterfacePreferenceFragment.java @@ -4,39 +4,26 @@ package ml.docilealligator.infinityforreddit.settings; import android.os.Build; import android.os.Bundle; -import androidx.preference.ListPreference; import androidx.preference.Preference; import androidx.preference.PreferenceFragmentCompat; import androidx.preference.SwitchPreference; import org.greenrobot.eventbus.EventBus; -import ml.docilealligator.infinityforreddit.events.ChangeCompactLayoutToolbarHiddenByDefaultEvent; -import ml.docilealligator.infinityforreddit.events.ChangeDefaultPostLayoutEvent; -import ml.docilealligator.infinityforreddit.events.ChangeLongPressToHideToolbarInCompactLayoutEvent; -import ml.docilealligator.infinityforreddit.events.ChangeShowAbsoluteNumberOfVotesEvent; +import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.events.ChangeVoteButtonsPositionEvent; import ml.docilealligator.infinityforreddit.events.RecreateActivityEvent; -import ml.docilealligator.infinityforreddit.events.ShowDividerInCompactLayoutPreferenceEvent; -import ml.docilealligator.infinityforreddit.events.ShowThumbnailOnTheRightInCompactLayoutEvent; -import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; public class InterfacePreferenceFragment extends PreferenceFragmentCompat { @Override public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { - setPreferencesFromResource(R.xml.interface_preference, rootKey); + setPreferencesFromResource(R.xml.interface_preferences, rootKey); Preference immersiveInterfaceEntryPreference = findPreference(SharedPreferencesUtils.IMMERSIVE_INTERFACE_ENTRY_KEY); SwitchPreference bottomAppBarSwitch = findPreference(SharedPreferencesUtils.BOTTOM_APP_BAR_KEY); SwitchPreference voteButtonsOnTheRightSwitch = findPreference(SharedPreferencesUtils.VOTE_BUTTONS_ON_THE_RIGHT_KEY); - ListPreference defaultPostLayoutSwitch = findPreference(SharedPreferencesUtils.DEFAULT_POST_LAYOUT_KEY); - SwitchPreference showDividerInCompactLayout = findPreference(SharedPreferencesUtils.SHOW_DIVIDER_IN_COMPACT_LAYOUT); - SwitchPreference showThumbnailOnTheRightInCompactLayout = findPreference(SharedPreferencesUtils.SHOW_THUMBNAIL_ON_THE_LEFT_IN_COMPACT_LAYOUT); - SwitchPreference showAbsoluteNumberOfVotes = findPreference(SharedPreferencesUtils.SHOW_ABSOLUTE_NUMBER_OF_VOTES); - SwitchPreference longPressToHideToolbarInCompactLayoutSwitch = findPreference(SharedPreferencesUtils.LONG_PRESS_TO_HIDE_TOOLBAR_IN_COMPACT_LAYOUT); - SwitchPreference postCompactLayoutToolbarHiddenByDefaultSwitch = findPreference(SharedPreferencesUtils.POST_COMPACT_LAYOUT_TOOLBAR_HIDDEN_BY_DEFAULT); if (immersiveInterfaceEntryPreference != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { immersiveInterfaceEntryPreference.setVisible(true); @@ -55,47 +42,5 @@ public class InterfacePreferenceFragment extends PreferenceFragmentCompat { return true; }); } - - if (defaultPostLayoutSwitch != null) { - defaultPostLayoutSwitch.setOnPreferenceChangeListener((preference, newValue) -> { - EventBus.getDefault().post(new ChangeDefaultPostLayoutEvent(Integer.parseInt((String) newValue))); - return true; - }); - } - - if (showDividerInCompactLayout != null) { - showDividerInCompactLayout.setOnPreferenceChangeListener((preference, newValue) -> { - EventBus.getDefault().post(new ShowDividerInCompactLayoutPreferenceEvent((Boolean) newValue)); - return true; - }); - } - - if (showThumbnailOnTheRightInCompactLayout != null) { - showThumbnailOnTheRightInCompactLayout.setOnPreferenceChangeListener((preference, newValue) -> { - EventBus.getDefault().post(new ShowThumbnailOnTheRightInCompactLayoutEvent((Boolean) newValue)); - return true; - }); - } - - if (showAbsoluteNumberOfVotes != null) { - showAbsoluteNumberOfVotes.setOnPreferenceChangeListener((preference, newValue) -> { - EventBus.getDefault().post(new ChangeShowAbsoluteNumberOfVotesEvent((Boolean) newValue)); - return true; - }); - } - - if (longPressToHideToolbarInCompactLayoutSwitch != null) { - longPressToHideToolbarInCompactLayoutSwitch.setOnPreferenceChangeListener((preference, newValue) -> { - EventBus.getDefault().post(new ChangeLongPressToHideToolbarInCompactLayoutEvent((Boolean) newValue)); - return true; - }); - } - - if (postCompactLayoutToolbarHiddenByDefaultSwitch != null) { - postCompactLayoutToolbarHiddenByDefaultSwitch.setOnPreferenceChangeListener((preference, newValue) -> { - EventBus.getDefault().post(new ChangeCompactLayoutToolbarHiddenByDefaultEvent((Boolean) newValue)); - return true; - }); - } } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/settings/PostPreferenceFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/settings/PostPreferenceFragment.java new file mode 100644 index 00000000..08011d3a --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/settings/PostPreferenceFragment.java @@ -0,0 +1,75 @@ +package ml.docilealligator.infinityforreddit.settings; + +import android.os.Bundle; + +import androidx.preference.ListPreference; +import androidx.preference.PreferenceFragmentCompat; +import androidx.preference.SwitchPreference; + +import org.greenrobot.eventbus.EventBus; + +import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.events.ChangeCompactLayoutToolbarHiddenByDefaultEvent; +import ml.docilealligator.infinityforreddit.events.ChangeDefaultPostLayoutEvent; +import ml.docilealligator.infinityforreddit.events.ChangeLongPressToHideToolbarInCompactLayoutEvent; +import ml.docilealligator.infinityforreddit.events.ChangeShowAbsoluteNumberOfVotesEvent; +import ml.docilealligator.infinityforreddit.events.ShowDividerInCompactLayoutPreferenceEvent; +import ml.docilealligator.infinityforreddit.events.ShowThumbnailOnTheRightInCompactLayoutEvent; +import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; + +public class PostPreferenceFragment extends PreferenceFragmentCompat { + + @Override + public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { + setPreferencesFromResource(R.xml.post_preferences, rootKey); + + ListPreference defaultPostLayoutSwitch = findPreference(SharedPreferencesUtils.DEFAULT_POST_LAYOUT_KEY); + SwitchPreference showDividerInCompactLayout = findPreference(SharedPreferencesUtils.SHOW_DIVIDER_IN_COMPACT_LAYOUT); + SwitchPreference showThumbnailOnTheRightInCompactLayout = findPreference(SharedPreferencesUtils.SHOW_THUMBNAIL_ON_THE_LEFT_IN_COMPACT_LAYOUT); + SwitchPreference showAbsoluteNumberOfVotes = findPreference(SharedPreferencesUtils.SHOW_ABSOLUTE_NUMBER_OF_VOTES); + SwitchPreference longPressToHideToolbarInCompactLayoutSwitch = findPreference(SharedPreferencesUtils.LONG_PRESS_TO_HIDE_TOOLBAR_IN_COMPACT_LAYOUT); + SwitchPreference postCompactLayoutToolbarHiddenByDefaultSwitch = findPreference(SharedPreferencesUtils.POST_COMPACT_LAYOUT_TOOLBAR_HIDDEN_BY_DEFAULT); + + if (defaultPostLayoutSwitch != null) { + defaultPostLayoutSwitch.setOnPreferenceChangeListener((preference, newValue) -> { + EventBus.getDefault().post(new ChangeDefaultPostLayoutEvent(Integer.parseInt((String) newValue))); + return true; + }); + } + + if (showDividerInCompactLayout != null) { + showDividerInCompactLayout.setOnPreferenceChangeListener((preference, newValue) -> { + EventBus.getDefault().post(new ShowDividerInCompactLayoutPreferenceEvent((Boolean) newValue)); + return true; + }); + } + + if (showThumbnailOnTheRightInCompactLayout != null) { + showThumbnailOnTheRightInCompactLayout.setOnPreferenceChangeListener((preference, newValue) -> { + EventBus.getDefault().post(new ShowThumbnailOnTheRightInCompactLayoutEvent((Boolean) newValue)); + return true; + }); + } + + if (showAbsoluteNumberOfVotes != null) { + showAbsoluteNumberOfVotes.setOnPreferenceChangeListener((preference, newValue) -> { + EventBus.getDefault().post(new ChangeShowAbsoluteNumberOfVotesEvent((Boolean) newValue)); + return true; + }); + } + + if (longPressToHideToolbarInCompactLayoutSwitch != null) { + longPressToHideToolbarInCompactLayoutSwitch.setOnPreferenceChangeListener((preference, newValue) -> { + EventBus.getDefault().post(new ChangeLongPressToHideToolbarInCompactLayoutEvent((Boolean) newValue)); + return true; + }); + } + + if (postCompactLayoutToolbarHiddenByDefaultSwitch != null) { + postCompactLayoutToolbarHiddenByDefaultSwitch.setOnPreferenceChangeListener((preference, newValue) -> { + EventBus.getDefault().post(new ChangeCompactLayoutToolbarHiddenByDefaultEvent((Boolean) newValue)); + return true; + }); + } + } +}
\ No newline at end of file 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 94127163..26c64308 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/SharedPreferencesUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/SharedPreferencesUtils.java @@ -79,6 +79,10 @@ public class SharedPreferencesUtils { public static final String SHOW_THUMBNAIL_ON_THE_LEFT_IN_COMPACT_LAYOUT = "show_thumbnail_on_the_left_in_compact_layout"; public static final String NUMBER_OF_COLUMNS_IN_POST_FEED_PORTRAIT = "number_of_columns_in_post_feed_portrait"; public static final String NUMBER_OF_COLUMNS_IN_POST_FEED_LANDSCAPE = "number_of_columns_in_post_feed_landscape"; + public static final String NUMBER_OF_COLUMNS_IN_POST_FEED_PORTRAIT_COMPACT_LAYOUT = "number_of_columns_in_post_feed_portrait_compact_layout"; + public static final String NUMBER_OF_COLUMNS_IN_POST_FEED_LANDSCAPE_COMPACT_LAYOUT = "number_of_columns_in_post_feed_landscape_compact_layout"; + public static final String NUMBER_OF_COLUMNS_IN_POST_FEED_PORTRAIT_GALLERY_LAYOUT = "number_of_columns_in_post_feed_portrait_gallery_layout"; + public static final String NUMBER_OF_COLUMNS_IN_POST_FEED_LANDSCAPE_GALLERY_LAYOUT = "number_of_columns_in_post_feed_landscape_gallery_layout"; public static final String SWIPE_RIGHT_TO_GO_BACK = "swipe_to_go_back_from_post_detail"; public static final String VOLUME_KEYS_NAVIGATE_COMMENTS = "volume_keys_navigate_comments"; public static final String VOLUME_KEYS_NAVIGATE_POSTS = "volume_keys_navigate_posts"; |