From d644c1c03215932d12b85a223eddf5f3f750d143 Mon Sep 17 00:00:00 2001 From: Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> Date: Mon, 17 Jun 2024 21:06:35 -0400 Subject: Fix wrong coordinates when saving the position of MovableFloatingActionButton in some Android versions. --- .../activities/ViewPostDetailActivity.java | 15 +++++----- .../customviews/MovableFloatingActionButton.java | 33 ++++++++++++++++++---- 2 files changed, 34 insertions(+), 14 deletions(-) (limited to 'app/src/main') 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 06e166f3..5a84d0ce 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPostDetailActivity.java @@ -267,14 +267,17 @@ public class ViewPostDetailActivity extends BaseActivity implements SortTypeSele fetchMorePosts(false); } + binding.fabViewPostDetailActivity.bindRequiredData( + Build.VERSION.SDK_INT >= Build.VERSION_CODES.R ? getDisplay() : null, + mPostDetailsSharedPreferences, + getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT + ); + binding.fabViewPostDetailActivity.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { binding.fabViewPostDetailActivity.getViewTreeObserver().removeOnGlobalLayoutListener(this); - binding.fabViewPostDetailActivity.setCoordinates( - Build.VERSION.SDK_INT >= Build.VERSION_CODES.R ? getDisplay() : null, - mPostDetailsSharedPreferences, - getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT); + binding.fabViewPostDetailActivity.setCoordinates(); } }); checkNewAccountAndBindView(savedInstanceState); @@ -787,10 +790,6 @@ public class ViewPostDetailActivity extends BaseActivity implements SortTypeSele @Override protected void onDestroy() { EventBus.getDefault().unregister(this); - 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); BigImageViewer.imageLoader().cancelAll(); 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 76ce3931..1a279ab7 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/customviews/MovableFloatingActionButton.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/customviews/MovableFloatingActionButton.java @@ -23,6 +23,12 @@ public class MovableFloatingActionButton extends FloatingActionButton implements private float downRawX, downRawY; private float dX, dY; + @Nullable + private Display display; + @Nullable + private SharedPreferences postDetailsSharedPreferences; + private boolean portrait; + public MovableFloatingActionButton(Context context) { super(context); init(); @@ -102,6 +108,7 @@ public class MovableFloatingActionButton extends FloatingActionButton implements .setDuration(0) .start(); + saveCoordinates(newX, newY); return true; } else if (action == MotionEvent.ACTION_UP) { if (longClicked) { @@ -144,7 +151,17 @@ public class MovableFloatingActionButton extends FloatingActionButton implements setY(newY); } - public void setCoordinates(@Nullable Display display, SharedPreferences postDetailsSharedPreferences, boolean portrait) { + public void bindRequiredData(@Nullable Display display, SharedPreferences postDetailsSharedPreferences, boolean portrait) { + this.display = display; + this.postDetailsSharedPreferences = postDetailsSharedPreferences; + this.portrait = portrait; + } + + public void setCoordinates() { + if (postDetailsSharedPreferences == null) { + return; + } + if (portrait) { if (postDetailsSharedPreferences.contains(SharedPreferencesUtils.getPostDetailFabPortraitX(display)) && postDetailsSharedPreferences.contains(SharedPreferencesUtils.getPostDetailFabPortraitY(display))) { @@ -160,14 +177,18 @@ public class MovableFloatingActionButton extends FloatingActionButton implements } } - public void saveCoordinates(@Nullable Display display, SharedPreferences postDetailsSharedPreferences, boolean portrait) { + private void saveCoordinates(float x, float y) { + if (postDetailsSharedPreferences == null) { + return; + } + if (portrait) { - postDetailsSharedPreferences.edit().putFloat(SharedPreferencesUtils.getPostDetailFabPortraitX(display), getX()) - .putFloat(SharedPreferencesUtils.getPostDetailFabPortraitY(display), getY()) + postDetailsSharedPreferences.edit().putFloat(SharedPreferencesUtils.getPostDetailFabPortraitX(display), x) + .putFloat(SharedPreferencesUtils.getPostDetailFabPortraitY(display), y) .apply(); } else { - postDetailsSharedPreferences.edit().putFloat(SharedPreferencesUtils.getPostDetailFabLandscapeX(display), getX()) - .putFloat(SharedPreferencesUtils.getPostDetailFabLandscapeY(display), getY()) + postDetailsSharedPreferences.edit().putFloat(SharedPreferencesUtils.getPostDetailFabLandscapeX(display), x) + .putFloat(SharedPreferencesUtils.getPostDetailFabLandscapeY(display), y) .apply(); } } -- cgit v1.2.3