diff options
author | Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> | 2024-06-17 15:32:23 +0000 |
---|---|---|
committer | Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> | 2024-06-17 15:32:23 +0000 |
commit | e186c42a1f9f07cc335d533d9624d5807d6c1258 (patch) | |
tree | f6a67f1c750f230004b80546ad9959b6fa2ce6be /app | |
parent | c4953363601ceec4185c020f78c959fe3812d0ca (diff) | |
download | infinity-for-reddit-e186c42a1f9f07cc335d533d9624d5807d6c1258.tar infinity-for-reddit-e186c42a1f9f07cc335d533d9624d5807d6c1258.tar.gz infinity-for-reddit-e186c42a1f9f07cc335d533d9624d5807d6c1258.tar.bz2 infinity-for-reddit-e186c42a1f9f07cc335d533d9624d5807d6c1258.tar.lz infinity-for-reddit-e186c42a1f9f07cc335d533d9624d5807d6c1258.tar.xz infinity-for-reddit-e186c42a1f9f07cc335d533d9624d5807d6c1258.tar.zst infinity-for-reddit-e186c42a1f9f07cc335d533d9624d5807d6c1258.zip |
Support multiple displays (e.g. foldable phone) when saving the position of fab in PostDetailActivity.
Diffstat (limited to 'app')
3 files changed, 63 insertions, 20 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPostDetailActivity.java index 3716ef60..06e166f3 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPostDetailActivity.java @@ -271,7 +271,9 @@ public class ViewPostDetailActivity extends BaseActivity implements SortTypeSele @Override public void onGlobalLayout() { binding.fabViewPostDetailActivity.getViewTreeObserver().removeOnGlobalLayoutListener(this); - binding.fabViewPostDetailActivity.setCoordinates(mPostDetailsSharedPreferences, + binding.fabViewPostDetailActivity.setCoordinates( + Build.VERSION.SDK_INT >= Build.VERSION_CODES.R ? getDisplay() : null, + mPostDetailsSharedPreferences, getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT); } }); @@ -785,7 +787,9 @@ public class ViewPostDetailActivity extends BaseActivity implements SortTypeSele @Override protected void onDestroy() { EventBus.getDefault().unregister(this); - binding.fabViewPostDetailActivity.saveCoordinates(mPostDetailsSharedPreferences, + binding.fabViewPostDetailActivity.saveCoordinates( + Build.VERSION.SDK_INT >= Build.VERSION_CODES.R ? getDisplay() : null, + mPostDetailsSharedPreferences, getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT); super.onDestroy(); Bridge.clear(this); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/customviews/MovableFloatingActionButton.java b/app/src/main/java/ml/docilealligator/infinityforreddit/customviews/MovableFloatingActionButton.java index 65b5f1be..76ce3931 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/customviews/MovableFloatingActionButton.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/customviews/MovableFloatingActionButton.java @@ -3,10 +3,13 @@ package ml.docilealligator.infinityforreddit.customviews; import android.content.Context; import android.content.SharedPreferences; import android.util.AttributeSet; +import android.view.Display; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; +import androidx.annotation.Nullable; + import com.google.android.material.floatingactionbutton.FloatingActionButton; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; @@ -141,30 +144,30 @@ public class MovableFloatingActionButton extends FloatingActionButton implements setY(newY); } - public void setCoordinates(SharedPreferences postDetailsSharedPreferences, boolean portrait) { + public void setCoordinates(@Nullable Display display, SharedPreferences postDetailsSharedPreferences, boolean portrait) { if (portrait) { - if (postDetailsSharedPreferences.contains(SharedPreferencesUtils.POST_DETAIL_FAB_PORTRAIT_X) - && postDetailsSharedPreferences.contains(SharedPreferencesUtils.POST_DETAIL_FAB_PORTRAIT_Y)) { - setPositionEnsureVisibility(postDetailsSharedPreferences.getFloat(SharedPreferencesUtils.POST_DETAIL_FAB_PORTRAIT_X, 0), - postDetailsSharedPreferences.getFloat(SharedPreferencesUtils.POST_DETAIL_FAB_PORTRAIT_Y, 0)); + if (postDetailsSharedPreferences.contains(SharedPreferencesUtils.getPostDetailFabPortraitX(display)) + && postDetailsSharedPreferences.contains(SharedPreferencesUtils.getPostDetailFabPortraitY(display))) { + setPositionEnsureVisibility(postDetailsSharedPreferences.getFloat(SharedPreferencesUtils.getPostDetailFabPortraitX(display), 0), + postDetailsSharedPreferences.getFloat(SharedPreferencesUtils.getPostDetailFabPortraitY(display), 0)); } } else { - if (postDetailsSharedPreferences.contains(SharedPreferencesUtils.POST_DETAIL_FAB_LANDSCAPE_X) - && postDetailsSharedPreferences.contains(SharedPreferencesUtils.POST_DETAIL_FAB_LANDSCAPE_Y)) { - setPositionEnsureVisibility(postDetailsSharedPreferences.getFloat(SharedPreferencesUtils.POST_DETAIL_FAB_LANDSCAPE_X, 0), - postDetailsSharedPreferences.getFloat(SharedPreferencesUtils.POST_DETAIL_FAB_LANDSCAPE_Y, 0)); + if (postDetailsSharedPreferences.contains(SharedPreferencesUtils.getPostDetailFabLandscapeX(display)) + && postDetailsSharedPreferences.contains(SharedPreferencesUtils.getPostDetailFabLandscapeY(display))) { + setPositionEnsureVisibility(postDetailsSharedPreferences.getFloat(SharedPreferencesUtils.getPostDetailFabLandscapeX(display), 0), + postDetailsSharedPreferences.getFloat(SharedPreferencesUtils.getPostDetailFabLandscapeY(display), 0)); } } } - public void saveCoordinates(SharedPreferences postDetailsSharedPreferences, boolean portrait) { + public void saveCoordinates(@Nullable Display display, SharedPreferences postDetailsSharedPreferences, boolean portrait) { if (portrait) { - postDetailsSharedPreferences.edit().putFloat(SharedPreferencesUtils.POST_DETAIL_FAB_PORTRAIT_X, getX()) - .putFloat(SharedPreferencesUtils.POST_DETAIL_FAB_PORTRAIT_Y, getY()) + postDetailsSharedPreferences.edit().putFloat(SharedPreferencesUtils.getPostDetailFabPortraitX(display), getX()) + .putFloat(SharedPreferencesUtils.getPostDetailFabPortraitY(display), getY()) .apply(); } else { - postDetailsSharedPreferences.edit().putFloat(SharedPreferencesUtils.POST_DETAIL_FAB_LANDSCAPE_X, getX()) - .putFloat(SharedPreferencesUtils.POST_DETAIL_FAB_LANDSCAPE_Y, getY()) + postDetailsSharedPreferences.edit().putFloat(SharedPreferencesUtils.getPostDetailFabLandscapeX(display), getX()) + .putFloat(SharedPreferencesUtils.getPostDetailFabLandscapeY(display), getY()) .apply(); } } 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 56a8c6f0..51ad438b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/SharedPreferencesUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/SharedPreferencesUtils.java @@ -1,5 +1,9 @@ package ml.docilealligator.infinityforreddit.utils; +import android.view.Display; + +import androidx.annotation.Nullable; + /** * Created by alex on 2/23/18. */ @@ -215,10 +219,42 @@ public class SharedPreferencesUtils { public static final String COMMENT_DIVIDER_TYPE = "comment_divider_type"; public static final String SUBSCRIBED_THINGS_SYNC_TIME = "subscribed_things_sync_time"; public static final String COMMENT_FILTER = "comment_filter"; - public static final String POST_DETAIL_FAB_PORTRAIT_X = "fab_portrait_x"; - public static final String POST_DETAIL_FAB_PORTRAIT_Y = "fab_portrait_y"; - public static final String POST_DETAIL_FAB_LANDSCAPE_X = "fab_landscape_x"; - public static final String POST_DETAIL_FAB_LANDSCAPE_Y = "fab_landscape_y"; + private static final String POST_DETAIL_FAB_PORTRAIT_X_BASE = "fab_portrait_x_"; + private static final String POST_DETAIL_FAB_PORTRAIT_Y_BASE = "fab_portrait_y_"; + private static final String POST_DETAIL_FAB_LANDSCAPE_X_BASE = "fab_landscape_x_"; + private static final String POST_DETAIL_FAB_LANDSCAPE_Y_BASE = "fab_landscape_y_"; + + public static String getPostDetailFabPortraitX(@Nullable Display display) { + if (display == null) { + return POST_DETAIL_FAB_PORTRAIT_X_BASE; + } + + return POST_DETAIL_FAB_PORTRAIT_X_BASE + display.getDisplayId(); + } + + public static String getPostDetailFabPortraitY(@Nullable Display display) { + if (display == null) { + return POST_DETAIL_FAB_PORTRAIT_Y_BASE; + } + + return POST_DETAIL_FAB_PORTRAIT_Y_BASE + display.getDisplayId(); + } + + public static String getPostDetailFabLandscapeX(@Nullable Display display) { + if (display == null) { + return POST_DETAIL_FAB_LANDSCAPE_X_BASE; + } + + return POST_DETAIL_FAB_LANDSCAPE_X_BASE + display.getDisplayId(); + } + + public static String getPostDetailFabLandscapeY(@Nullable Display display) { + if (display == null) { + return POST_DETAIL_FAB_LANDSCAPE_Y_BASE; + } + + return POST_DETAIL_FAB_LANDSCAPE_Y_BASE + display.getDisplayId(); + } public static final String DEFAULT_PREFERENCES_FILE = "ml.docilealligator.infinityforreddit_preferences"; public static final String MAIN_PAGE_TABS_SHARED_PREFERENCES_FILE = "ml.docilealligator.infinityforreddit.main_page_tabs"; |