diff options
author | Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> | 2024-06-18 01:06:35 +0000 |
---|---|---|
committer | Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> | 2024-06-18 01:06:35 +0000 |
commit | d644c1c03215932d12b85a223eddf5f3f750d143 (patch) | |
tree | e551f9fe9167cd1c4e69e2c3e78e0f0ae947f23e /app/src/main/java | |
parent | e186c42a1f9f07cc335d533d9624d5807d6c1258 (diff) | |
download | infinity-for-reddit-d644c1c03215932d12b85a223eddf5f3f750d143.tar infinity-for-reddit-d644c1c03215932d12b85a223eddf5f3f750d143.tar.gz infinity-for-reddit-d644c1c03215932d12b85a223eddf5f3f750d143.tar.bz2 infinity-for-reddit-d644c1c03215932d12b85a223eddf5f3f750d143.tar.lz infinity-for-reddit-d644c1c03215932d12b85a223eddf5f3f750d143.tar.xz infinity-for-reddit-d644c1c03215932d12b85a223eddf5f3f750d143.tar.zst infinity-for-reddit-d644c1c03215932d12b85a223eddf5f3f750d143.zip |
Fix wrong coordinates when saving the position of MovableFloatingActionButton in some Android versions.
Diffstat (limited to 'app/src/main/java')
2 files changed, 34 insertions, 14 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 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(); } } |