diff options
3 files changed, 71 insertions, 2 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 6676cca8..3716ef60 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPostDetailActivity.java @@ -6,6 +6,7 @@ import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.content.res.ColorStateList; +import android.content.res.Configuration; import android.os.Build; import android.os.Bundle; import android.os.Handler; @@ -13,6 +14,7 @@ import android.os.Looper; import android.view.KeyEvent; import android.view.MenuItem; import android.view.View; +import android.view.ViewTreeObserver; import android.view.Window; import android.view.WindowManager; import android.widget.Toast; @@ -108,6 +110,9 @@ public class ViewPostDetailActivity extends BaseActivity implements SortTypeSele @Named("current_account") SharedPreferences mCurrentAccountSharedPreferences; @Inject + @Named("post_details") + SharedPreferences mPostDetailsSharedPreferences; + @Inject CustomThemeWrapper mCustomThemeWrapper; @Inject Executor mExecutor; @@ -262,6 +267,14 @@ public class ViewPostDetailActivity extends BaseActivity implements SortTypeSele fetchMorePosts(false); } + binding.fabViewPostDetailActivity.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { + @Override + public void onGlobalLayout() { + binding.fabViewPostDetailActivity.getViewTreeObserver().removeOnGlobalLayoutListener(this); + binding.fabViewPostDetailActivity.setCoordinates(mPostDetailsSharedPreferences, + getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT); + } + }); checkNewAccountAndBindView(savedInstanceState); } @@ -772,6 +785,8 @@ public class ViewPostDetailActivity extends BaseActivity implements SortTypeSele @Override protected void onDestroy() { EventBus.getDefault().unregister(this); + binding.fabViewPostDetailActivity.saveCoordinates(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 f9d3dcff..65b5f1be 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/customviews/MovableFloatingActionButton.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/customviews/MovableFloatingActionButton.java @@ -1,6 +1,7 @@ package ml.docilealligator.infinityforreddit.customviews; import android.content.Context; +import android.content.SharedPreferences; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; @@ -8,6 +9,8 @@ import android.view.ViewGroup; import com.google.android.material.floatingactionbutton.FloatingActionButton; +import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; + public class MovableFloatingActionButton extends FloatingActionButton implements View.OnTouchListener { private final static float CLICK_DRAG_TOLERANCE = 50; private long downTime = 0; @@ -38,7 +41,7 @@ public class MovableFloatingActionButton extends FloatingActionButton implements @Override public boolean onTouch(View view, MotionEvent motionEvent) { - ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams)view.getLayoutParams(); + ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) view.getLayoutParams(); int action = motionEvent.getAction(); if (action == MotionEvent.ACTION_DOWN) { @@ -78,7 +81,7 @@ public class MovableFloatingActionButton extends FloatingActionButton implements int viewWidth = view.getWidth(); int viewHeight = view.getHeight(); - View viewParent = (View)view.getParent(); + View viewParent = (View) view.getParent(); int parentWidth = viewParent.getWidth(); int parentHeight = viewParent.getHeight(); @@ -112,10 +115,57 @@ public class MovableFloatingActionButton extends FloatingActionButton implements if (Math.abs(upDX) < CLICK_DRAG_TOLERANCE && Math.abs(upDY) < CLICK_DRAG_TOLERANCE) { return System.currentTimeMillis() - downTime >= 300 ? performLongClick() : performClick(); } else { + return true; } } else { return super.onTouchEvent(motionEvent); } } + + private void setPositionEnsureVisibility(float newX, float newY) { + ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) getLayoutParams(); + View viewParent = (View) getParent(); + int parentWidth = viewParent.getWidth(); + int parentHeight = viewParent.getHeight(); + int viewWidth = getWidth(); + int viewHeight = getHeight(); + + newX = Math.max(layoutParams.leftMargin, newX); // Don't allow the FAB past the left hand side of the parent + newX = Math.min(parentWidth - viewWidth - layoutParams.rightMargin, newX); // Don't allow the FAB past the right hand side of the parent + + newY = Math.max(layoutParams.topMargin, newY); // Don't allow the FAB past the top of the parent + newY = Math.min(parentHeight - viewHeight - layoutParams.bottomMargin, newY); // Don't allow the FAB past the bottom of the parent + + setX(newX); + setY(newY); + } + + public void setCoordinates(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)); + } + } 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)); + } + } + } + + public void saveCoordinates(SharedPreferences postDetailsSharedPreferences, boolean portrait) { + if (portrait) { + postDetailsSharedPreferences.edit().putFloat(SharedPreferencesUtils.POST_DETAIL_FAB_PORTRAIT_X, getX()) + .putFloat(SharedPreferencesUtils.POST_DETAIL_FAB_PORTRAIT_Y, getY()) + .apply(); + } else { + postDetailsSharedPreferences.edit().putFloat(SharedPreferencesUtils.POST_DETAIL_FAB_LANDSCAPE_X, getX()) + .putFloat(SharedPreferencesUtils.POST_DETAIL_FAB_LANDSCAPE_Y, 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 4343eaad..56a8c6f0 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/SharedPreferencesUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/SharedPreferencesUtils.java @@ -215,6 +215,10 @@ 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"; 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"; |