fix scroll to point and editing after orientation change

This commit is contained in:
Chumva 2018-07-02 15:15:27 +03:00
parent 3eb4472c2f
commit 1a312dc2a9

View file

@ -93,6 +93,7 @@ public class CoordinateInputDialogFragment extends DialogFragment implements Osm
public static final String TAG = "CoordinateInputDialogFragment"; public static final String TAG = "CoordinateInputDialogFragment";
public static final String ADDED_POINTS_NUMBER_KEY = "added_points_number_key"; public static final String ADDED_POINTS_NUMBER_KEY = "added_points_number_key";
private static final String SELECTED_POINT_KEY = "selected_point_key";
private static final double SOFT_KEYBOARD_MIN_DETECTION_SIZE = 0.15; private static final double SOFT_KEYBOARD_MIN_DETECTION_SIZE = 0.15;
private GPXFile newGpxFile; private GPXFile newGpxFile;
@ -252,6 +253,12 @@ public class CoordinateInputDialogFragment extends DialogFragment implements Osm
@Nullable @Nullable
@Override @Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
if (savedInstanceState != null) {
int pos = savedInstanceState.getInt(SELECTED_POINT_KEY, -1);
if (pos != -1) {
selectedWpt = adapter.getItem(pos);
}
}
orientationPortrait = AndroidUiHelper.isOrientationPortrait(getActivity()); orientationPortrait = AndroidUiHelper.isOrientationPortrait(getActivity());
Fragment optionsFragment = getChildFragmentManager().findFragmentByTag(CoordinateInputBottomSheetDialogFragment.TAG); Fragment optionsFragment = getChildFragmentManager().findFragmentByTag(CoordinateInputBottomSheetDialogFragment.TAG);
@ -442,19 +449,6 @@ public class CoordinateInputDialogFragment extends DialogFragment implements Osm
final View keyboardLayout = mainView.findViewById(R.id.keyboard_layout); final View keyboardLayout = mainView.findViewById(R.id.keyboard_layout);
keyboardLayout.setBackgroundResource(lightTheme keyboardLayout.setBackgroundResource(lightTheme
? R.drawable.bg_bottom_menu_light : R.drawable.bg_coordinate_input_keyboard_dark); ? R.drawable.bg_bottom_menu_light : R.drawable.bg_coordinate_input_keyboard_dark);
keyboardLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int height = keyboardLayout.getHeight();
if (keyboardViewHeight != height) {
keyboardViewHeight = height;
if (isOsmandKeyboardCurrentlyVisible() && selectedWpt == null) {
scrollToPoint(adapter.getItemCount() - 1);
}
}
}
});
View keyboardView = mainView.findViewById(R.id.keyboard_view); View keyboardView = mainView.findViewById(R.id.keyboard_view);
@ -518,6 +512,13 @@ public class CoordinateInputDialogFragment extends DialogFragment implements Osm
if (!isOsmandKeyboardOn() && isOsmandKeyboardCurrentlyVisible()) { if (!isOsmandKeyboardOn() && isOsmandKeyboardCurrentlyVisible()) {
changeOsmandKeyboardVisibility(false); changeOsmandKeyboardVisibility(false);
} }
if (selectedWpt == null) {
if ((isOsmandKeyboardCurrentlyVisible() || softKeyboardShown)) {
scrollToLastPoint();
}
} else {
enterEditingMode(selectedWpt);
}
mainView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { mainView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override @Override
@ -533,13 +534,38 @@ public class CoordinateInputDialogFragment extends DialogFragment implements Osm
shouldShowOsmandKeyboard = false; shouldShowOsmandKeyboard = false;
} }
} else if (!softKeyboardShown && softKeyboardVisible && selectedWpt == null) { } else if (!softKeyboardShown && softKeyboardVisible && selectedWpt == null) {
scrollToPoint(adapter.getItemCount() - 1); scrollToLastPoint();
} }
softKeyboardShown = softKeyboardVisible; softKeyboardShown = softKeyboardVisible;
int height = keyboardLayout.getHeight();
if (height > keyboardViewHeight) {
keyboardViewHeight = height;
if (isOsmandKeyboardCurrentlyVisible()) {
if (selectedWpt == null && adapter.getItemCount() > 1) {
scrollToLastPoint();
} else {
setPaddingToRecyclerViewBottom(keyboardViewHeight);
}
}
}
} }
}); });
} }
@Override
public void onSaveInstanceState(Bundle outState) {
if (selectedWpt != null) {
outState.putInt(SELECTED_POINT_KEY, adapter.getItemPosition(selectedWpt));
}
super.onSaveInstanceState(outState);
}
private void scrollToLastPoint() {
scrollToPoint(adapter.getItemCount() - 1);
}
private void scrollToPoint(WptPt point) { private void scrollToPoint(WptPt point) {
if (point != null) { if (point != null) {
scrollToPoint(adapter.getItemPosition(point)); scrollToPoint(adapter.getItemPosition(point));
@ -551,10 +577,10 @@ public class CoordinateInputDialogFragment extends DialogFragment implements Osm
if ((position < 0) || !(itemsSize > 1) || (itemsSize < position)) { if ((position < 0) || !(itemsSize > 1) || (itemsSize < position)) {
return; return;
} }
if (isOsmandKeyboardCurrentlyVisible()) { if (isOsmandKeyboardCurrentlyVisible() && keyboardViewHeight > 0) {
setPaddingToRecyclerViewBottom(keyboardViewHeight); setPaddingToRecyclerViewBottom(keyboardViewHeight);
} }
recyclerView.scrollToPosition(position); ((LinearLayoutManager) recyclerView.getLayoutManager()).scrollToPositionWithOffset(position, 0);
} }
private void setPaddingToRecyclerViewBottom(int padding) { private void setPaddingToRecyclerViewBottom(int padding) {
@ -676,6 +702,7 @@ public class CoordinateInputDialogFragment extends DialogFragment implements Osm
@Override @Override
public void onPause() { public void onPause() {
super.onPause(); super.onPause();
keyboardViewHeight = 0;
stopLocationUpdate(); stopLocationUpdate();
} }
@ -1085,7 +1112,7 @@ public class CoordinateInputDialogFragment extends DialogFragment implements Osm
} }
adapter.removeItem(position); adapter.removeItem(position);
hasUnsavedChanges = true; hasUnsavedChanges = true;
snackbar = Snackbar.make(mainView, getString(R.string.coord_input_point_deleted, wpt.name), Snackbar.LENGTH_LONG) snackbar = Snackbar.make(mainView, getString(R.string.point_deleted, wpt.name), Snackbar.LENGTH_LONG)
.setAction(R.string.shared_string_undo, new View.OnClickListener() { .setAction(R.string.shared_string_undo, new View.OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
@ -1131,7 +1158,11 @@ public class CoordinateInputDialogFragment extends DialogFragment implements Osm
} else { } else {
mainView.findViewById(R.id.keyboard_layout).setVisibility(visibility); mainView.findViewById(R.id.keyboard_layout).setVisibility(visibility);
} }
if (!show) { if (show) {
if (selectedWpt == null) {
scrollToLastPoint();
}
} else {
setPaddingToRecyclerViewBottom(AndroidUtils.dpToPx(getMyApplication(), 72)); setPaddingToRecyclerViewBottom(AndroidUtils.dpToPx(getMyApplication(), 72));
} }
} }
@ -1180,7 +1211,7 @@ public class CoordinateInputDialogFragment extends DialogFragment implements Osm
selectedWpt = wptPt; selectedWpt = wptPt;
Format format = getMyApplication().getSettings().COORDS_INPUT_FORMAT.get(); Format format = getMyApplication().getSettings().COORDS_INPUT_FORMAT.get();
double lat = Math.abs(wptPt.lat); double lat = Math.abs(wptPt.lat);
double lon = Math.abs(wptPt.lat); double lon = Math.abs(wptPt.lon);
if (format == Format.DD_MM_MMM || format == Format.DD_MM_MMMM) { if (format == Format.DD_MM_MMM || format == Format.DD_MM_MMMM) {
int accuracy = format.getThirdPartSymbolsCount(); int accuracy = format.getThirdPartSymbolsCount();
updateInputsDdm(true, CoordinateInputFormats.ddToDdm(lat), accuracy); updateInputsDdm(true, CoordinateInputFormats.ddToDdm(lat), accuracy);
@ -1298,7 +1329,7 @@ public class CoordinateInputDialogFragment extends DialogFragment implements Osm
dismissEditingMode(); dismissEditingMode();
} else { } else {
addWpt(getGpx(), null, name, null, 0, lat, lon); addWpt(getGpx(), null, name, null, 0, lat, lon);
scrollToPoint(adapter.getItemCount() - 1); scrollToLastPoint();
} }
adapter.notifyDataSetChanged(); adapter.notifyDataSetChanged();
clearInputs(); clearInputs();