forked from organicmaps/organicmaps
Merge pull request #720 from trashkalmar/mm-2101-bookmark-doesnot-save-on-rotate-master
[android] fix: Bookmark description is not saved if rotate the device.
This commit is contained in:
commit
e469f88d28
3 changed files with 68 additions and 29 deletions
|
@ -739,6 +739,7 @@ public class MwmActivity extends BaseMwmFragmentActivity
|
|||
{
|
||||
super.onResumeFragments();
|
||||
RoutingController.get().restore();
|
||||
mPlacePage.restore();
|
||||
}
|
||||
|
||||
private void adjustZoomButtons()
|
||||
|
|
|
@ -11,24 +11,27 @@ import android.view.ViewGroup;
|
|||
import android.view.WindowManager;
|
||||
import android.widget.EditText;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.mapswithme.maps.R;
|
||||
import com.mapswithme.maps.base.BaseMwmDialogFragment;
|
||||
import com.mapswithme.maps.bookmarks.data.Bookmark;
|
||||
import com.mapswithme.util.StringUtils;
|
||||
import com.mapswithme.util.UiUtils;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
|
||||
public class EditDescriptionFragment extends BaseMwmDialogFragment
|
||||
{
|
||||
public static final String EXTRA_DESCRIPTION = "ExtraDescription";
|
||||
public static final String EXTRA_BOOKMARK = "bookmark";
|
||||
|
||||
private EditText mEtDescription;
|
||||
private Bookmark mBookmark;
|
||||
|
||||
public interface OnDescriptionSaveListener
|
||||
public interface OnDescriptionSavedListener
|
||||
{
|
||||
void onSave(String description);
|
||||
void onSaved(Bookmark bookmark);
|
||||
}
|
||||
|
||||
private OnDescriptionSaveListener mListener;
|
||||
private WeakReference<OnDescriptionSavedListener> mListener;
|
||||
|
||||
public EditDescriptionFragment() {}
|
||||
|
||||
|
@ -51,7 +54,8 @@ public class EditDescriptionFragment extends BaseMwmDialogFragment
|
|||
{
|
||||
super.onViewCreated(view, savedInstanceState);
|
||||
|
||||
String description = getArguments().getString(EXTRA_DESCRIPTION);
|
||||
mBookmark = getArguments().getParcelable(EXTRA_BOOKMARK);
|
||||
String description = mBookmark.getBookmarkDescription();
|
||||
|
||||
if (StringUtils.isHtml(description))
|
||||
{
|
||||
|
@ -68,9 +72,9 @@ public class EditDescriptionFragment extends BaseMwmDialogFragment
|
|||
getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
|
||||
}
|
||||
|
||||
public void setSaveDescriptionListener(OnDescriptionSaveListener listener)
|
||||
public void setSaveDescriptionListener(OnDescriptionSavedListener listener)
|
||||
{
|
||||
mListener = listener;
|
||||
mListener = new WeakReference<>(listener);
|
||||
}
|
||||
|
||||
private void initToolbar(View view)
|
||||
|
@ -99,8 +103,21 @@ public class EditDescriptionFragment extends BaseMwmDialogFragment
|
|||
|
||||
private void saveDescription()
|
||||
{
|
||||
mBookmark.setParams(mBookmark.getName(), null, mEtDescription.getText().toString());
|
||||
|
||||
if (mListener != null)
|
||||
mListener.onSave(mEtDescription.getText().toString());
|
||||
{
|
||||
OnDescriptionSavedListener listener = mListener.get();
|
||||
if (listener != null)
|
||||
listener.onSaved(mBookmark);
|
||||
}
|
||||
dismiss();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDetach()
|
||||
{
|
||||
super.onDetach();
|
||||
mListener = null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,8 +11,10 @@ import android.net.Uri;
|
|||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.app.FragmentActivity;
|
||||
import android.support.v4.app.FragmentManager;
|
||||
import android.support.v7.widget.Toolbar;
|
||||
import android.text.TextUtils;
|
||||
import android.util.AttributeSet;
|
||||
|
@ -267,6 +269,12 @@ public class PlacePageView extends RelativeLayout implements View.OnClickListene
|
|||
mAnimationController.initialHide();
|
||||
}
|
||||
|
||||
public void restore()
|
||||
{
|
||||
if (mMapObject != null)
|
||||
subscribeBookmarkEditFragment(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onTouchEvent(@NonNull MotionEvent event)
|
||||
{
|
||||
|
@ -647,6 +655,34 @@ public class PlacePageView extends RelativeLayout implements View.OnClickListene
|
|||
bookmark.setParams(name, null, bookmark.getBookmarkDescription());
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds listener to {@link EditDescriptionFragment} to catch notification about bookmark description edit is complete.
|
||||
* <br/>When the user rotates device screen the listener is lost, so we must re-subscribe again.
|
||||
* @param fragment if specified - explicitely subscribe to this fragment. Otherwise try to find the fragment by hands.
|
||||
*/
|
||||
private void subscribeBookmarkEditFragment(@Nullable EditDescriptionFragment fragment)
|
||||
{
|
||||
if (fragment == null)
|
||||
{
|
||||
FragmentManager fm = ((FragmentActivity)getContext()).getSupportFragmentManager();
|
||||
fragment = (EditDescriptionFragment)fm.findFragmentByTag(EditDescriptionFragment.class.getName());
|
||||
}
|
||||
|
||||
if (fragment == null)
|
||||
return;
|
||||
|
||||
fragment.setSaveDescriptionListener(new EditDescriptionFragment.OnDescriptionSavedListener()
|
||||
{
|
||||
@Override
|
||||
public void onSaved(Bookmark bookmark)
|
||||
{
|
||||
final Bookmark updatedBookmark = BookmarkManager.INSTANCE.getBookmark(bookmark.getCategoryId(), bookmark.getBookmarkId());
|
||||
setMapObject(updatedBookmark);
|
||||
Statistics.INSTANCE.trackDescriptionChanged();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(View v)
|
||||
{
|
||||
|
@ -717,19 +753,12 @@ public class PlacePageView extends RelativeLayout implements View.OnClickListene
|
|||
case R.id.btn__edit_html_bookmark:
|
||||
saveBookmarkNameIfUpdated();
|
||||
final Bundle args = new Bundle();
|
||||
final Bookmark bookmark = (Bookmark) mMapObject;
|
||||
args.putString(EditDescriptionFragment.EXTRA_DESCRIPTION, bookmark.getBookmarkDescription());
|
||||
final EditDescriptionFragment fragment = (EditDescriptionFragment) Fragment.instantiate(getContext(), EditDescriptionFragment.class.getName(), args);
|
||||
args.putParcelable(EditDescriptionFragment.EXTRA_BOOKMARK, mMapObject);
|
||||
String name = EditDescriptionFragment.class.getName();
|
||||
final EditDescriptionFragment fragment = (EditDescriptionFragment) Fragment.instantiate(getContext(), name, args);
|
||||
fragment.setArguments(args);
|
||||
fragment.setSaveDescriptionListener(new EditDescriptionFragment.OnDescriptionSaveListener()
|
||||
{
|
||||
@Override
|
||||
public void onSave(String description)
|
||||
{
|
||||
updateDescription(bookmark, description);
|
||||
}
|
||||
});
|
||||
fragment.show(((FragmentActivity) getContext()).getSupportFragmentManager(), null);
|
||||
fragment.show(((FragmentActivity) getContext()).getSupportFragmentManager(), name);
|
||||
subscribeBookmarkEditFragment(fragment);
|
||||
break;
|
||||
case R.id.from:
|
||||
if (RoutingController.get().setStartPoint(mMapObject))
|
||||
|
@ -742,14 +771,6 @@ public class PlacePageView extends RelativeLayout implements View.OnClickListene
|
|||
}
|
||||
}
|
||||
|
||||
private void updateDescription(Bookmark bookmark, String description)
|
||||
{
|
||||
bookmark.setParams(bookmark.getName(), null, description);
|
||||
final Bookmark updatedBookmark = BookmarkManager.INSTANCE.getBookmark(bookmark.getCategoryId(), bookmark.getBookmarkId());
|
||||
setMapObject(updatedBookmark);
|
||||
Statistics.INSTANCE.trackDescriptionChanged();
|
||||
}
|
||||
|
||||
private void followUrl(String url)
|
||||
{
|
||||
final Intent intent = new Intent(Intent.ACTION_VIEW);
|
||||
|
|
Loading…
Add table
Reference in a new issue