From ef2cff684308c0f73ac0d2e3c0ffea1035f734b9 Mon Sep 17 00:00:00 2001 From: Dima-1 Date: Thu, 5 Mar 2020 11:31:19 +0200 Subject: [PATCH] Separate old ui to add and new ui to edit favorites --- ...ment.xml => point_editor_fragment_new.xml} | 0 .../editors/FavoritePointEditor.java | 2 +- .../FavoritePointEditorFragmentNew.java | 344 ++++++++++++++++ .../editors/PointEditorFragment.java | 122 +++--- .../editors/PointEditorFragmentNew.java | 375 ++++++++++++++++++ 5 files changed, 775 insertions(+), 68 deletions(-) rename OsmAnd/res/layout/{new_point_editor_fragment.xml => point_editor_fragment_new.xml} (100%) create mode 100644 OsmAnd/src/net/osmand/plus/mapcontextmenu/editors/FavoritePointEditorFragmentNew.java create mode 100644 OsmAnd/src/net/osmand/plus/mapcontextmenu/editors/PointEditorFragmentNew.java diff --git a/OsmAnd/res/layout/new_point_editor_fragment.xml b/OsmAnd/res/layout/point_editor_fragment_new.xml similarity index 100% rename from OsmAnd/res/layout/new_point_editor_fragment.xml rename to OsmAnd/res/layout/point_editor_fragment_new.xml diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/editors/FavoritePointEditor.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/editors/FavoritePointEditor.java index b7258b63c5..554c9c5f1b 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/editors/FavoritePointEditor.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/editors/FavoritePointEditor.java @@ -73,6 +73,6 @@ public class FavoritePointEditor extends PointEditor { } isNew = false; this.favorite = favorite; - FavoritePointEditorFragment.showInstance(mapActivity); + FavoritePointEditorFragmentNew.showInstance(mapActivity); } } diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/editors/FavoritePointEditorFragmentNew.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/editors/FavoritePointEditorFragmentNew.java new file mode 100644 index 0000000000..56444d59d1 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/editors/FavoritePointEditorFragmentNew.java @@ -0,0 +1,344 @@ +package net.osmand.plus.mapcontextmenu.editors; + +import android.content.Context; +import android.content.DialogInterface; +import android.graphics.drawable.Drawable; +import android.os.Bundle; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.support.v4.app.FragmentActivity; +import android.support.v7.app.AlertDialog; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.Button; + +import net.osmand.data.FavouritePoint; +import net.osmand.data.LatLon; +import net.osmand.plus.FavouritesDbHelper; +import net.osmand.plus.FavouritesDbHelper.FavoriteGroup; +import net.osmand.plus.OsmandApplication; +import net.osmand.plus.R; +import net.osmand.plus.activities.MapActivity; +import net.osmand.plus.base.FavoriteImageDrawable; +import net.osmand.plus.dialogs.FavoriteDialogs; +import net.osmand.plus.mapcontextmenu.MapContextMenu; +import net.osmand.util.Algorithms; + + +public class FavoritePointEditorFragmentNew extends PointEditorFragmentNew { + + @Nullable + private FavoritePointEditor editor; + @Nullable + private FavouritePoint favorite; + @Nullable + private FavoriteGroup group; + @Nullable + FavouritesDbHelper helper; + + private boolean autoFill; + private boolean saved; + private int defaultColor; + + @Override + public void onAttach(Context context) { + super.onAttach(context); + MapActivity mapActivity = getMapActivity(); + if (mapActivity != null) { + helper = mapActivity.getMyApplication().getFavorites(); + editor = mapActivity.getContextMenu().getFavoritePointEditor(); + } + } + + @Override + public void onCreate(final Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + defaultColor = getResources().getColor(R.color.color_favorite); + + FavoritePointEditor editor = getFavoritePointEditor(); + FavouritesDbHelper helper = getHelper(); + if (editor != null && helper != null) { + FavouritePoint favorite = editor.getFavorite(); + this.favorite = favorite; + this.group = helper.getGroup(favorite); + } + } + + @Override + public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + View view = super.onCreateView(inflater, container, savedInstanceState); + FavoritePointEditor editor = getFavoritePointEditor(); + if (view != null && editor != null && editor.isNew()) { + Button replaceButton = (Button) view.findViewById(R.id.replace_button); + replaceButton.setTextColor(getResources().getColor(!editor.isLight() ? R.color.osmand_orange : R.color.map_widget_blue)); + replaceButton.setVisibility(View.VISIBLE); + replaceButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Bundle args = new Bundle(); + args.putSerializable(FavoriteDialogs.KEY_FAVORITE, getFavorite()); + FragmentActivity activity = getActivity(); + if (activity != null) { + FavoriteDialogs.createReplaceFavouriteDialog(activity, args); + } + } + }); + } + return view; + } + + @Override + public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + + if (autoFill) { + +// String name = favorite.getName() != null && !favorite.getName().isEmpty() ? +// favorite.getName() : getString(R.string.favorite_empty_place_name); +// +// String tostText = name + getString(R.string.favorite_autofill_toast_text) + group.name; +// +// Toast.makeText(getContext(), tostText, Toast.LENGTH_SHORT).show(); + + save(true); + } + } + + @Override + public PointEditor getEditor() { + return editor; + } + + public FavoritePointEditor getFavoritePointEditor() { + return editor; + } + + @Nullable + public FavouritePoint getFavorite() { + return favorite; + } + + @Nullable + public FavoriteGroup getGroup() { + return group; + } + + @Nullable + public FavouritesDbHelper getHelper() { + return helper; + } + + @Override + public String getToolbarTitle() { + FavoritePointEditor editor = getFavoritePointEditor(); + if (editor != null) { + if (editor.isNew()) { + return getString(R.string.favourites_context_menu_add); + } else { + return getString(R.string.favourites_context_menu_edit); + } + } + return ""; + } + + @Override + public void setCategory(String name, int color) { + FavouritesDbHelper helper = getHelper(); + if (helper != null) { + FavoriteGroup group = helper.getGroup(FavoriteGroup.convertDisplayNameToGroupIdName(requireContext(), name)); + this.group = group; + super.setCategory(name, group != null ? group.getColor() : 0); + } + } + + @Override + protected String getDefaultCategoryName() { + return getString(R.string.shared_string_favorites); + } + + public static void showInstance(@NonNull MapActivity mapActivity) { + FavoritePointEditor editor = mapActivity.getContextMenu().getFavoritePointEditor(); + //int slideInAnim = editor.getSlideInAnimation(); + //int slideOutAnim = editor.getSlideOutAnimation(); + + if (editor != null) { + FavoritePointEditorFragmentNew fragment = new FavoritePointEditorFragmentNew(); + mapActivity.getSupportFragmentManager().beginTransaction() + //.setCustomAnimations(slideInAnim, slideOutAnim, slideInAnim, slideOutAnim) + .add(R.id.fragmentContainer, fragment, editor.getFragmentTag()) + .addToBackStack(null).commitAllowingStateLoss(); + } + } + + public static void showAutoFillInstance(final MapActivity mapActivity, boolean autoFill) { + FavoritePointEditor editor = mapActivity.getContextMenu().getFavoritePointEditor(); + //int slideInAnim = editor.getSlideInAnimation(); + //int slideOutAnim = editor.getSlideOutAnimation(); + + FavoritePointEditorFragmentNew fragment = new FavoritePointEditorFragmentNew(); + fragment.autoFill = autoFill; + + if (editor != null) { + mapActivity.getSupportFragmentManager().beginTransaction() + //.setCustomAnimations(slideInAnim, slideOutAnim, slideInAnim, slideOutAnim) + .add(R.id.fragmentContainer, fragment, editor.getFragmentTag()) + .addToBackStack(null).commit(); + } + } + + @Override + protected boolean wasSaved() { + return saved; + } + + @Override + protected void save(final boolean needDismiss) { + final FavouritePoint favorite = getFavorite(); + if (favorite != null) { + final FavouritePoint point = new FavouritePoint(favorite.getLatitude(), favorite.getLongitude(), + getNameTextValue(), getCategoryTextValue()); + point.setDescription(getDescriptionTextValue()); + AlertDialog.Builder builder = FavouritesDbHelper.checkDuplicates(point, helper, getMapActivity()); + + if (favorite.getName().equals(point.getName()) && + favorite.getCategory().equals(point.getCategory()) && + Algorithms.stringsEqual(favorite.getDescription(), point.getDescription())) { + + if (needDismiss) { + dismiss(false); + } + return; + } + + if (builder != null && !autoFill) { + builder.setPositiveButton(R.string.shared_string_ok, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + doSave(favorite, point.getName(), point.getCategory(), point.getDescription(), needDismiss); + } + }); + builder.create().show(); + } else { + doSave(favorite, point.getName(), point.getCategory(), point.getDescription(), needDismiss); + } + saved = true; + } + } + + private void doSave(FavouritePoint favorite, String name, String category, String description, boolean needDismiss) { + FavouritesDbHelper helper = getHelper(); + FavoritePointEditor editor = getFavoritePointEditor(); + if (editor != null && helper != null) { + if (editor.isNew()) { + doAddFavorite(name, category, description); + } else { + helper.editFavouriteName(favorite, name, category, description); + } + } + MapActivity mapActivity = getMapActivity(); + if (mapActivity == null) { + return; + } + mapActivity.refreshMap(); + if (needDismiss) { + dismiss(false); + } + + MapContextMenu menu = mapActivity.getContextMenu(); + LatLon latLon = new LatLon(favorite.getLatitude(), favorite.getLongitude()); + if (menu.getLatLon() != null && menu.getLatLon().equals(latLon)) { + menu.update(latLon, favorite.getPointDescription(mapActivity), favorite); + } + } + + private void doAddFavorite(String name, String category, String description) { + OsmandApplication app = getMyApplication(); + FavouritesDbHelper helper = getHelper(); + FavouritePoint favorite = getFavorite(); + if (app != null && favorite != null && helper != null) { + favorite.setName(name); + favorite.setCategory(category); + favorite.setDescription(description); + app.getSettings().LAST_FAV_CATEGORY_ENTERED.set(category); + helper.addFavourite(favorite); + } + } + + @Override + protected void delete(final boolean needDismiss) { + FragmentActivity activity = getActivity(); + final FavouritePoint favorite = getFavorite(); + if (activity != null && favorite != null) { + AlertDialog.Builder builder = new AlertDialog.Builder(activity); + builder.setMessage(getString(R.string.favourites_remove_dialog_msg, favorite.getName())); + builder.setNegativeButton(R.string.shared_string_no, null); + builder.setPositiveButton(R.string.shared_string_yes, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + FavouritesDbHelper helper = getHelper(); + if (helper != null) { + helper.deleteFavourite(favorite); + saved = true; + if (needDismiss) { + dismiss(true); + } else { + MapActivity mapActivity = getMapActivity(); + if (mapActivity != null) { + mapActivity.refreshMap(); + } + } + } + } + }); + builder.create().show(); + } + } + + @Override + public String getHeaderCaption() { + return getString(R.string.favourites_edit_dialog_title); + } + + @Override + public String getNameInitValue() { + FavouritePoint favorite = getFavorite(); + return favorite != null ? favorite.getName() : ""; + } + + @Override + public String getCategoryInitValue() { + FavouritePoint favorite = getFavorite(); + return favorite == null || favorite.getCategory().length() == 0 ? getDefaultCategoryName() : favorite.getCategoryDisplayName(requireContext()); + } + + @Override + public String getDescriptionInitValue() { + FavouritePoint favorite = getFavorite(); + return favorite != null ? favorite.getDescription() : ""; + } + + @Override + public Drawable getNameIcon() { + return FavoriteImageDrawable.getOrCreate(getMapActivity(), getPointColor(), false, getFavorite()); + } + + @Override + public Drawable getCategoryIcon() { + return getPaintedIcon(R.drawable.ic_action_folder_stroke, getPointColor()); + } + + @Override + public int getPointColor() { + int color = 0; + FavoriteGroup group = getGroup(); + if (group != null) { + color = group.getColor(); + } + if (color == 0) { + color = defaultColor; + } + return color; + } +} diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/editors/PointEditorFragment.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/editors/PointEditorFragment.java index b7c361fed7..b3b3e5d27f 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/editors/PointEditorFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/editors/PointEditorFragment.java @@ -31,7 +31,6 @@ import net.osmand.plus.UiUtilities; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.base.BaseOsmAndFragment; import net.osmand.plus.widgets.AutoCompleteTextViewEx; -import net.osmand.plus.widgets.OsmandTextFieldBoxes; import net.osmand.util.Algorithms; import static net.osmand.plus.FavouritesDbHelper.FavoriteGroup.PERSONAL_CATEGORY; @@ -49,7 +48,7 @@ public abstract class PointEditorFragment extends BaseOsmAndFragment { Bundle savedInstanceState) { boolean nightMode = !getMyApplication().getSettings().isLightContent(); - view = UiUtilities.getInflater(getContext(), nightMode).inflate(R.layout.new_point_editor_fragment, container, false); + view = UiUtilities.getInflater(getContext(), nightMode).inflate(R.layout.point_editor_fragment, container, false); PointEditor editor = getEditor(); if (editor == null) { @@ -60,12 +59,14 @@ public abstract class PointEditorFragment extends BaseOsmAndFragment { editor.updateNightMode(); Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar); + toolbar.setBackgroundColor(ContextCompat.getColor(getContext(), !editor.isLight() ? R.color.app_bar_color_dark : R.color.app_bar_color_light)); toolbar.setTitle(getToolbarTitle()); OsmandApplication app = requireMyApplication(); - Drawable icBack = app.getUIUtilities().getIcon(R.drawable.ic_arrow_back, nightMode ? R.color.active_buttons_and_links_text_dark : R.color.description_font_and_bottom_sheet_icons); + Drawable icBack = app.getUIUtilities().getIcon(R.drawable.ic_arrow_back, !editor.isLight() ? R.color.active_buttons_and_links_text_dark : R.color.active_buttons_and_links_text_light); toolbar.setNavigationIcon(icBack); toolbar.setNavigationContentDescription(R.string.access_shared_string_navigate_up); + toolbar.setTitleTextColor(getResources().getColor(getResIdFromAttribute(getMapActivity(), R.attr.pstsTextColor))); toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { @@ -74,8 +75,6 @@ public abstract class PointEditorFragment extends BaseOsmAndFragment { }); int activeColorResId = !editor.isLight() ? R.color.active_color_primary_dark : R.color.active_color_primary_light; - ImageView toolbarAction = (ImageView) view.findViewById(R.id.toolbar_action); - toolbarAction.setImageDrawable(app.getUIUtilities().getIcon(R.drawable.ic_action_replace,activeColorResId)); Button saveButton = (Button) view.findViewById(R.id.save_button); saveButton.setTextColor(getResources().getColor(activeColorResId)); @@ -111,59 +110,61 @@ public abstract class PointEditorFragment extends BaseOsmAndFragment { deleteButton.setVisibility(View.VISIBLE); } -// view.findViewById(R.id.background_layout).setBackgroundResource(!editor.isLight() ? R.color.activity_background_color_dark : R.color.activity_background_color_light); -// view.findViewById(R.id.buttons_layout).setBackgroundResource(!editor.isLight() ? R.color.activity_background_color_dark : R.color.activity_background_color_light); -// view.findViewById(R.id.title_view).setBackgroundResource(!editor.isLight() ? R.color.list_background_color_dark : R.color.list_background_color_light); -// view.findViewById(R.id.description_caption).setBackgroundResource(!editor.isLight() ? R.color.activity_background_color_dark : R.color.activity_background_color_light); + view.findViewById(R.id.background_layout).setBackgroundResource(!editor.isLight() ? R.color.activity_background_color_dark : R.color.activity_background_color_light); + view.findViewById(R.id.buttons_layout).setBackgroundResource(!editor.isLight() ? R.color.activity_background_color_dark : R.color.activity_background_color_light); + view.findViewById(R.id.title_view).setBackgroundResource(!editor.isLight() ? R.color.list_background_color_dark : R.color.list_background_color_light); + view.findViewById(R.id.description_info_view).setBackgroundResource(!editor.isLight() ? R.color.activity_background_color_dark : R.color.activity_background_color_light); - OsmandTextFieldBoxes nameCaption = (OsmandTextFieldBoxes) view.findViewById(R.id.name_caption); - AndroidUtils.setTextSecondaryColor(view.getContext(), nameCaption.getEditText(), !editor.isLight()); - nameCaption.getEditText().setText(getNameCaption()); -// TextView categoryCaption = (TextView) view.findViewById(R.id.category_caption); -// AndroidUtils.setTextSecondaryColor(view.getContext(), categoryCaption, !editor.isLight()); -// categoryCaption.setText(getCategoryCaption()); + TextView nameCaption = (TextView) view.findViewById(R.id.name_caption); + AndroidUtils.setTextSecondaryColor(view.getContext(), nameCaption, !editor.isLight()); + nameCaption.setText(getNameCaption()); + TextView categoryCaption = (TextView) view.findViewById(R.id.category_caption); + AndroidUtils.setTextSecondaryColor(view.getContext(), categoryCaption, !editor.isLight()); + categoryCaption.setText(getCategoryCaption()); nameEdit = (EditText) view.findViewById(R.id.name_edit); AndroidUtils.setTextPrimaryColor(view.getContext(), nameEdit, !editor.isLight()); AndroidUtils.setHintTextSecondaryColor(view.getContext(), nameEdit, !editor.isLight()); nameEdit.setText(getNameInitValue()); -// AutoCompleteTextViewEx categoryEdit = (AutoCompleteTextViewEx) view.findViewById(R.id.category_edit); -// AndroidUtils.setTextPrimaryColor(view.getContext(), categoryEdit, !editor.isLight()); -// categoryEdit.setText(getCategoryInitValue()); -// categoryEdit.setFocusable(false); -// categoryEdit.setOnTouchListener(new View.OnTouchListener() { -// @Override -// public boolean onTouch(final View v, MotionEvent event) { -// if (event.getAction() == MotionEvent.ACTION_UP) { -// DialogFragment dialogFragment = createSelectCategoryDialog(); -// if (dialogFragment != null) { -// dialogFragment.show(getChildFragmentManager(), SelectCategoryDialogFragment.TAG); -// } -// return true; -// } -// return false; -// } -// }); + AutoCompleteTextViewEx categoryEdit = (AutoCompleteTextViewEx) view.findViewById(R.id.category_edit); + AndroidUtils.setTextPrimaryColor(view.getContext(), categoryEdit, !editor.isLight()); + categoryEdit.setText(getCategoryInitValue()); + categoryEdit.setFocusable(false); + categoryEdit.setOnTouchListener(new View.OnTouchListener() { + @Override + public boolean onTouch(final View v, MotionEvent event) { + if (event.getAction() == MotionEvent.ACTION_UP) { + DialogFragment dialogFragment = createSelectCategoryDialog(); + if (dialogFragment != null) { + dialogFragment.show(getChildFragmentManager(), SelectCategoryDialogFragment.TAG); + } + return true; + } + return false; + } + }); -// final EditText descriptionEdit = (EditText) view.findViewById(R.id.description_edit); -// AndroidUtils.setTextPrimaryColor(view.getContext(), descriptionEdit, !editor.isLight()); -// AndroidUtils.setHintTextSecondaryColor(view.getContext(), descriptionEdit, !editor.isLight()); -// if (getDescriptionInitValue() != null) { -// descriptionEdit.setText(getDescriptionInitValue()); -// } + final EditText descriptionEdit = (EditText) view.findViewById(R.id.description_edit); + AndroidUtils.setTextPrimaryColor(view.getContext(), descriptionEdit, !editor.isLight()); + AndroidUtils.setHintTextSecondaryColor(view.getContext(), descriptionEdit, !editor.isLight()); + if (getDescriptionInitValue() != null) { + descriptionEdit.setText(getDescriptionInitValue()); + } -// ImageView nameImage = (ImageView) view.findViewById(R.id.name_image); -// nameImage.setImageDrawable(getNameIcon()); -// ImageView categoryImage = (ImageView) view.findViewById(R.id.category_image); -// categoryImage.setImageDrawable(getCategoryIcon()); + ImageView nameImage = (ImageView) view.findViewById(R.id.name_image); + nameImage.setImageDrawable(getNameIcon()); + ImageView categoryImage = (ImageView) view.findViewById(R.id.category_image); + categoryImage.setImageDrawable(getCategoryIcon()); + ImageView descriptionImage = (ImageView) view.findViewById(R.id.description_image); + descriptionImage.setImageDrawable(getRowIcon(R.drawable.ic_action_note_dark)); if (app.accessibilityEnabled()) { nameCaption.setFocusable(true); -// categoryCaption.setFocusable(true); -// nameEdit.setHint(R.string.access_hint_enter_name); -// categoryEdit.setHint(R.string.access_hint_enter_category); -// descriptionEdit.setHint(R.string.access_hint_enter_description); + categoryCaption.setFocusable(true); + nameEdit.setHint(R.string.access_hint_enter_name); + categoryEdit.setHint(R.string.access_hint_enter_category); + descriptionEdit.setHint(R.string.access_hint_enter_description); } return view; } @@ -206,19 +207,6 @@ public abstract class PointEditorFragment extends BaseOsmAndFragment { nameEdit.requestFocus(); AndroidUtils.softKeyboardDelayed(nameEdit); } - if (Build.VERSION.SDK_INT >= 21) { - Activity activity = getActivity(); - if (activity != null) { - int colorId = getStatusBarColorId(); - if (colorId != -1) { - if (activity instanceof MapActivity) { - ((MapActivity) activity).updateStatusBarColor(); - } else { - activity.getWindow().setStatusBarColor(ContextCompat.getColor(activity, colorId)); - } - } - } - } } @Override @@ -366,15 +354,15 @@ public abstract class PointEditorFragment extends BaseOsmAndFragment { } public String getCategoryTextValue() { -// AutoCompleteTextViewEx categoryEdit = (AutoCompleteTextViewEx) view.findViewById(R.id.category_edit); -// String name = categoryEdit.getText().toString().trim(); -// if (isPersonalCategoryDisplayName(requireContext(), name)) { -// return PERSONAL_CATEGORY; -// } -// if(name.equals(getDefaultCategoryName())) { + AutoCompleteTextViewEx categoryEdit = (AutoCompleteTextViewEx) view.findViewById(R.id.category_edit); + String name = categoryEdit.getText().toString().trim(); + if (isPersonalCategoryDisplayName(requireContext(), name)) { + return PERSONAL_CATEGORY; + } + if(name.equals(getDefaultCategoryName())) { return ""; -// } -// return name; + } + return name; } public String getDescriptionTextValue() { diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/editors/PointEditorFragmentNew.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/editors/PointEditorFragmentNew.java new file mode 100644 index 0000000000..576df72506 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/editors/PointEditorFragmentNew.java @@ -0,0 +1,375 @@ +package net.osmand.plus.mapcontextmenu.editors; + +import android.annotation.TargetApi; +import android.app.Activity; +import android.content.Context; +import android.graphics.drawable.Drawable; +import android.os.Build; +import android.os.Bundle; +import android.os.IBinder; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.support.v4.app.DialogFragment; +import android.support.v4.app.FragmentActivity; +import android.support.v7.widget.Toolbar; +import android.util.TypedValue; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.view.inputmethod.InputMethodManager; +import android.widget.Button; +import android.widget.EditText; +import android.widget.ImageView; + +import net.osmand.AndroidUtils; +import net.osmand.plus.OsmandApplication; +import net.osmand.plus.R; +import net.osmand.plus.UiUtilities; +import net.osmand.plus.activities.MapActivity; +import net.osmand.plus.base.BaseOsmAndFragment; +import net.osmand.plus.widgets.AutoCompleteTextViewEx; +import net.osmand.plus.widgets.OsmandTextFieldBoxes; +import net.osmand.util.Algorithms; + +public abstract class PointEditorFragmentNew extends BaseOsmAndFragment { + + private View view; + private EditText nameEdit; + private boolean cancelled; + private boolean nightMode; + + @TargetApi(Build.VERSION_CODES.HONEYCOMB) + @Override + public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + + nightMode = !getMyApplication().getSettings().isLightContent(); + view = UiUtilities.getInflater(getContext(), nightMode).inflate(R.layout.point_editor_fragment_new, container, false); + + PointEditor editor = getEditor(); + if (editor == null) { + return view; + } + + editor.updateLandscapePortrait(requireActivity()); + editor.updateNightMode(); + + Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar); + toolbar.setTitle(getToolbarTitle()); + + OsmandApplication app = requireMyApplication(); + Drawable icBack = app.getUIUtilities().getIcon(R.drawable.ic_arrow_back, nightMode ? R.color.active_buttons_and_links_text_dark : R.color.description_font_and_bottom_sheet_icons); + toolbar.setNavigationIcon(icBack); + toolbar.setNavigationContentDescription(R.string.access_shared_string_navigate_up); + toolbar.setNavigationOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + dismiss(); + } + }); + + int activeColorResId = !editor.isLight() ? R.color.active_color_primary_dark : R.color.active_color_primary_light; + ImageView toolbarAction = (ImageView) view.findViewById(R.id.toolbar_action); + toolbarAction.setImageDrawable(app.getUIUtilities().getIcon(R.drawable.ic_action_replace, activeColorResId)); + + Button saveButton = (Button) view.findViewById(R.id.save_button); + saveButton.setTextColor(getResources().getColor(activeColorResId)); + saveButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + savePressed(); + } + }); + + Button cancelButton = (Button) view.findViewById(R.id.cancel_button); + cancelButton.setTextColor(getResources().getColor(activeColorResId)); + cancelButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + cancelled = true; + dismiss(); + } + }); + + Button deleteButton = (Button) view.findViewById(R.id.delete_button); + deleteButton.setTextColor(getResources().getColor(activeColorResId)); + deleteButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + deletePressed(); + } + }); + + if (editor.isNew()) { + deleteButton.setVisibility(View.GONE); + } else { + deleteButton.setVisibility(View.VISIBLE); + } + +// view.findViewById(R.id.background_layout).setBackgroundResource(!editor.isLight() ? R.color.activity_background_color_dark : R.color.activity_background_color_light); +// view.findViewById(R.id.buttons_layout).setBackgroundResource(!editor.isLight() ? R.color.activity_background_color_dark : R.color.activity_background_color_light); +// view.findViewById(R.id.title_view).setBackgroundResource(!editor.isLight() ? R.color.list_background_color_dark : R.color.list_background_color_light); +// view.findViewById(R.id.description_caption).setBackgroundResource(!editor.isLight() ? R.color.activity_background_color_dark : R.color.activity_background_color_light); + + OsmandTextFieldBoxes nameCaption = (OsmandTextFieldBoxes) view.findViewById(R.id.name_caption); + AndroidUtils.setTextSecondaryColor(view.getContext(), nameCaption.getEditText(), !editor.isLight()); + nameCaption.getEditText().setText(getNameCaption()); +// TextView categoryCaption = (TextView) view.findViewById(R.id.category_caption); +// AndroidUtils.setTextSecondaryColor(view.getContext(), categoryCaption, !editor.isLight()); +// categoryCaption.setText(getCategoryCaption()); + + nameEdit = (EditText) view.findViewById(R.id.name_edit); + AndroidUtils.setTextPrimaryColor(view.getContext(), nameEdit, !editor.isLight()); + AndroidUtils.setHintTextSecondaryColor(view.getContext(), nameEdit, !editor.isLight()); + nameEdit.setText(getNameInitValue()); +// AutoCompleteTextViewEx categoryEdit = (AutoCompleteTextViewEx) view.findViewById(R.id.category_edit); +// AndroidUtils.setTextPrimaryColor(view.getContext(), categoryEdit, !editor.isLight()); +// categoryEdit.setText(getCategoryInitValue()); +// categoryEdit.setFocusable(false); +// categoryEdit.setOnTouchListener(new View.OnTouchListener() { +// @Override +// public boolean onTouch(final View v, MotionEvent event) { +// if (event.getAction() == MotionEvent.ACTION_UP) { +// DialogFragment dialogFragment = createSelectCategoryDialog(); +// if (dialogFragment != null) { +// dialogFragment.show(getChildFragmentManager(), SelectCategoryDialogFragment.TAG); +// } +// return true; +// } +// return false; +// } +// }); + +// final EditText descriptionEdit = (EditText) view.findViewById(R.id.description_edit); +// AndroidUtils.setTextPrimaryColor(view.getContext(), descriptionEdit, !editor.isLight()); +// AndroidUtils.setHintTextSecondaryColor(view.getContext(), descriptionEdit, !editor.isLight()); +// if (getDescriptionInitValue() != null) { +// descriptionEdit.setText(getDescriptionInitValue()); +// } + +// ImageView nameImage = (ImageView) view.findViewById(R.id.name_image); +// nameImage.setImageDrawable(getNameIcon()); +// ImageView categoryImage = (ImageView) view.findViewById(R.id.category_image); +// categoryImage.setImageDrawable(getCategoryIcon()); + + + if (app.accessibilityEnabled()) { + nameCaption.setFocusable(true); +// categoryCaption.setFocusable(true); +// nameEdit.setHint(R.string.access_hint_enter_name); +// categoryEdit.setHint(R.string.access_hint_enter_category); +// descriptionEdit.setHint(R.string.access_hint_enter_description); + } + return view; + } + + protected EditText getNameEdit() { + return nameEdit; + } + + @Nullable + protected DialogFragment createSelectCategoryDialog() { + PointEditor editor = getEditor(); + if (editor != null) { + return SelectCategoryDialogFragment.createInstance(editor.getFragmentTag()); + } else { + return null; + } + } + + public Drawable getRowIcon(int iconId) { + PointEditor editor = getEditor(); + boolean light = editor == null || editor.isLight(); + return getIcon(iconId, light ? R.color.icon_color_default_light : R.color.icon_color_default_dark); + } + + @Override + public void onStart() { + super.onStart(); + MapActivity mapActivity = getMapActivity(); + if (mapActivity != null) { + mapActivity.getContextMenu().setBaseFragmentVisibility(false); + } + } + + @Override + public void onResume() { + super.onResume(); + PointEditor editor = getEditor(); + if (editor != null && editor.isNew()) { + nameEdit.selectAll(); + nameEdit.requestFocus(); + AndroidUtils.softKeyboardDelayed(nameEdit); + } + } + + @Override + public void onStop() { + super.onStop(); + hideKeyboard(); + MapActivity mapActivity = getMapActivity(); + if (mapActivity != null) { + mapActivity.getContextMenu().setBaseFragmentVisibility(true); + } + } + + @Override + public void onDestroyView() { + PointEditor editor = getEditor(); + if (!wasSaved() && editor != null && !editor.isNew() && !cancelled) { + save(false); + } + super.onDestroyView(); + } + + @Override + public int getStatusBarColorId() { + View view = getView(); + if (view != null && Build.VERSION.SDK_INT >= 23 && !nightMode) { + view.setSystemUiVisibility(view.getSystemUiVisibility() | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); + } + return nightMode ? R.color.list_background_color_dark : R.color.list_background_color_light; + } + + @Override + protected boolean isFullScreenAllowed() { + return false; + } + + private void hideKeyboard() { + FragmentActivity activity = getActivity(); + if (activity != null) { + InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE); + if (inputMethodManager != null) { + View currentFocus = activity.getCurrentFocus(); + if (currentFocus != null) { + IBinder windowToken = currentFocus.getWindowToken(); + if (windowToken != null) { + inputMethodManager.hideSoftInputFromWindow(windowToken, 0); + } + } + } + } + } + + protected void savePressed() { + save(true); + } + + protected void deletePressed() { + delete(true); + } + + protected abstract boolean wasSaved(); + + protected abstract void save(boolean needDismiss); + + protected abstract void delete(boolean needDismiss); + + static int getResIdFromAttribute(final Context ctx, final int attr) { + if (attr == 0) { + return 0; + } + final TypedValue typedvalueattr = new TypedValue(); + ctx.getTheme().resolveAttribute(attr, typedvalueattr, true); + return typedvalueattr.resourceId; + } + + @Nullable + public abstract PointEditor getEditor(); + + public abstract String getToolbarTitle(); + + public void setCategory(String name, int color) { + AutoCompleteTextViewEx categoryEdit = (AutoCompleteTextViewEx) view.findViewById(R.id.category_edit); + String n = name.length() == 0 ? getDefaultCategoryName() : name; + categoryEdit.setText(n); + ImageView categoryImage = (ImageView) view.findViewById(R.id.category_image); + categoryImage.setImageDrawable(getCategoryIcon()); + ImageView nameImage = (ImageView) view.findViewById(R.id.name_image); + nameImage.setImageDrawable(getNameIcon()); + } + + protected String getDefaultCategoryName() { + return getString(R.string.shared_string_none); + } + + @Nullable + protected MapActivity getMapActivity() { + return (MapActivity) getActivity(); + } + + @Nullable + @Override + protected OsmandApplication getMyApplication() { + if (getActivity() == null) { + return null; + } + return (OsmandApplication) getActivity().getApplication(); + } + + public void dismiss() { + dismiss(false); + } + + public void dismiss(boolean includingMenu) { + MapActivity mapActivity = getMapActivity(); + if (mapActivity != null) { + if (includingMenu) { + mapActivity.getSupportFragmentManager().popBackStack(); + mapActivity.getContextMenu().close(); + } else { + mapActivity.getSupportFragmentManager().popBackStack(); + } + } + } + + public abstract String getHeaderCaption(); + + public String getNameCaption() { + return getString(R.string.shared_string_name); + } + + public String getCategoryCaption() { + return getString(R.string.favourites_edit_dialog_category); + } + + public abstract String getNameInitValue(); + + public abstract String getCategoryInitValue(); + + public abstract String getDescriptionInitValue(); + + public abstract Drawable getNameIcon(); + + public abstract Drawable getCategoryIcon(); + + public abstract int getPointColor(); + + public String getNameTextValue() { + EditText nameEdit = (EditText) view.findViewById(R.id.name_edit); + return nameEdit.getText().toString().trim(); + } + + public String getCategoryTextValue() { +// AutoCompleteTextViewEx categoryEdit = (AutoCompleteTextViewEx) view.findViewById(R.id.category_edit); +// String name = categoryEdit.getText().toString().trim(); +// if (isPersonalCategoryDisplayName(requireContext(), name)) { +// return PERSONAL_CATEGORY; +// } +// if(name.equals(getDefaultCategoryName())) { + return ""; +// } +// return name; + } + + public String getDescriptionTextValue() { + EditText descriptionEdit = (EditText) view.findViewById(R.id.description_edit); + String res = descriptionEdit.getText().toString().trim(); + return Algorithms.isEmpty(res) ? null : res; + } + + protected Drawable getPaintedIcon(int iconId, int color) { + return getPaintedContentIcon(iconId, color); + } +}