Skip to content

Commit

Permalink
Reset FAB position option in ViewPostDetailActivity.
Browse files Browse the repository at this point in the history
  • Loading branch information
Docile-Alligator committed Sep 8, 2024
1 parent 72036e4 commit a2390b7
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import android.os.Handler;
import android.os.Looper;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewTreeObserver;
Expand Down Expand Up @@ -287,9 +288,7 @@ public void onGlobalLayout() {
}

public void setTitle(String title) {
if (binding.toolbarViewPostDetailActivity != null) {
binding.toolbarViewPostDetailActivity.setTitle(title);
}
binding.toolbarViewPostDetailActivity.setTitle(title);
}

public void showFab() {
Expand Down Expand Up @@ -737,11 +736,21 @@ public void onProvidePostListToViewPostDetailActivityEvent(ProvidePostListToView
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.view_post_detail_activity, menu);
applyMenuItemTheme(menu);
return true;
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if (item.getItemId() == android.R.id.home) {
onBackPressed();
return true;
} else if (item.getItemId() == R.id.action_reset_fab_position_view_post_detail_activity) {
binding.fabViewPostDetailActivity.resetCoordinates();
return true;
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,13 @@ public boolean onTouch(View view, MotionEvent motionEvent) {
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

saveCoordinates(newX, newY);

view.animate()
.x(newX)
.y(newY)
.setDuration(0)
.start();

saveCoordinates(newX, newY);
return true;
} else if (action == MotionEvent.ACTION_UP) {
if (longClicked) {
Expand Down Expand Up @@ -177,6 +177,29 @@ public void setCoordinates() {
}
}

public void resetCoordinates() {
if (portrait) {
if (postDetailsSharedPreferences != null) {
postDetailsSharedPreferences
.edit()
.remove(SharedPreferencesUtils.getPostDetailFabPortraitX(display))
.remove(SharedPreferencesUtils.getPostDetailFabPortraitY(display))
.apply();
}
} else {
if (postDetailsSharedPreferences != null) {
postDetailsSharedPreferences
.edit()
.remove(SharedPreferencesUtils.getPostDetailFabLandscapeX(display))
.remove(SharedPreferencesUtils.getPostDetailFabLandscapeY(display))
.apply();
}
}

setTranslationX(0);
setTranslationY(0);
}

private void saveCoordinates(float x, float y) {
if (postDetailsSharedPreferences == null) {
return;
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/res/menu/view_post_detail_activity.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_reset_fab_position_view_post_detail_activity"
android:orderInCategory="100"
android:title="@string/action_reset_fab_position"
app:showAsAction="never" />
</menu>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
<string name="action_contact_mods">Contact Mods</string>
<string name="action_more_options">More Options</string>
<string name="action_add_to_home_screen">Add to Home screen</string>
<string name="action_reset_fab_position">Reset FAB position</string>

<string name="parse_json_response_error">Error occurred when parsing the JSON response</string>
<string name="retrieve_token_error">Error Retrieving the token</string>
Expand Down

0 comments on commit a2390b7

Please sign in to comment.