quick action add favorite functionality (completed/need to test)

This commit is contained in:
Rosty 2016-12-29 13:05:23 +02:00
parent a87c28240b
commit 4b64b70d75
11 changed files with 228 additions and 25 deletions

View file

@ -178,6 +178,7 @@
android:layout_marginLeft="8dp"
android:layout_marginRight="16dp"
android:layout_marginTop="8dp"
android:focusable="false"
android:text="@string/shared_string_favorites"
android:drawableRight="@drawable/ic_action_arrow_drop_down"
android:editable="false"/>

View file

@ -2483,4 +2483,6 @@ If you need help with OsmAnd application, please contact our support team: suppo
<string name="text_name">Name</string>
<string name="quick_action_add_marker_discr">Tap on action will add marker to the specified location.</string>
<string name="quick_action_favorite_dialog">Show favorite dialog</string>
<string name="favorite_autofill_toast_text">" is saved to "</string>
<string name="favorite_empty_place_name">Place</string>
</resources>

View file

@ -130,6 +130,7 @@
<item name="colorPrimary">@color/osmand_orange</item>
<item name="colorPrimaryDark">@color/osmand_orange_dark</item>
<item name="colorAccent">@color/dashboard_blue</item>
<item name="android:actionModeBackground">@color/actionbar_light_color</item>
<item name="android:actionBarStyle">@style/Widget.Styled.ActionBarLight</item>

View file

@ -478,7 +478,7 @@ public class FavouritesDbHelper {
private void recalculateCachedFavPoints(){
public void recalculateCachedFavPoints(){
ArrayList<FavouritePoint> temp = new ArrayList<FavouritePoint>();
for(FavoriteGroup f : favoriteGroups){
temp.addAll(f.points);

View file

@ -41,6 +41,8 @@ public class EditCategoryDialogFragment extends DialogFragment {
FavouritesDbHelper helper;
private SelectCategoryDialogFragment.CategorySelectionListener selectionListener;
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
@ -87,6 +89,10 @@ public class EditCategoryDialogFragment extends DialogFragment {
return builder.create();
}
public void setSelectionListener(SelectCategoryDialogFragment.CategorySelectionListener selectionListener) {
this.selectionListener = selectionListener;
}
@Override
public void onStart()
{
@ -103,10 +109,17 @@ public class EditCategoryDialogFragment extends DialogFragment {
name = nameEdit.getText().toString().trim();
if (!helper.groupExists(name)) {
helper.addEmptyCategory(name, color);
PointEditor editor = ((MapActivity) getActivity()).getContextMenu().getPointEditor(editorTag);
if (editor != null) {
editor.setCategory(name);
}
if (selectionListener != null){
selectionListener.onCategorySelected(name, color);
}
d.dismiss();
} else {
AlertDialog.Builder b = new AlertDialog.Builder(getActivity());

View file

@ -2,6 +2,7 @@ package net.osmand.plus.mapcontextmenu.editors;
import net.osmand.data.FavouritePoint;
import net.osmand.data.LatLon;
import net.osmand.plus.FavouritesDbHelper;
import net.osmand.plus.activities.MapActivity;
import net.osmand.util.Algorithms;
@ -39,6 +40,28 @@ public class FavoritePointEditor extends PointEditor {
FavoritePointEditorFragment.showInstance(mapActivity);
}
public void add(LatLon latLon, String title, String originObjectName, String categoryName, int categoryColor, boolean autoFill) {
if (latLon == null) return;
isNew = true;
if (categoryName != null && !categoryName.isEmpty()) {
FavouritesDbHelper.FavoriteGroup category = mapActivity.getMyApplication().getFavorites().getGroup(categoryName);
if (category == null)
mapActivity.getMyApplication().getFavorites().addEmptyCategory(categoryName, categoryColor);
} else categoryName = "";
favorite = new FavouritePoint(latLon.getLatitude(), latLon.getLongitude(), title, categoryName);
favorite.setDescription("");
favorite.setOriginObjectName(originObjectName);
FavoritePointEditorFragment.showAutoFillInstance(mapActivity);
}
public void edit(FavouritePoint favorite) {
if (favorite == null) {
return;

View file

@ -4,11 +4,13 @@ import android.app.Activity;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
import net.osmand.data.FavouritePoint;
import net.osmand.data.LatLon;
@ -28,6 +30,7 @@ public class FavoritePointEditorFragment extends PointEditorFragment {
private FavoriteGroup group;
FavouritesDbHelper helper;
private boolean autoFill;
private boolean saved;
private int defaultColor;
@ -67,6 +70,23 @@ public class FavoritePointEditorFragment extends PointEditorFragment {
return view;
}
@Override
public void onViewCreated(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;
@ -92,6 +112,8 @@ public class FavoritePointEditorFragment extends PointEditorFragment {
return getString(R.string.shared_string_favorites);
}
public static void showInstance(final MapActivity mapActivity) {
FavoritePointEditor editor = mapActivity.getContextMenu().getFavoritePointEditor();
//int slideInAnim = editor.getSlideInAnimation();
@ -104,6 +126,21 @@ public class FavoritePointEditorFragment extends PointEditorFragment {
.addToBackStack(null).commit();
}
public static void showAutoFillInstance(final MapActivity mapActivity) {
FavoritePointEditor editor = mapActivity.getContextMenu().getFavoritePointEditor();
//int slideInAnim = editor.getSlideInAnimation();
//int slideOutAnim = editor.getSlideOutAnimation();
FavoritePointEditorFragment fragment = new FavoritePointEditorFragment();
fragment.autoFill = true;
mapActivity.getSupportFragmentManager().beginTransaction()
//.setCustomAnimations(slideInAnim, slideOutAnim, slideInAnim, slideOutAnim)
.add(R.id.fragmentContainer, fragment, editor.getFragmentTag())
.addToBackStack(null).commit();
}
@Override
protected boolean wasSaved() {
return saved;

View file

@ -19,6 +19,9 @@ import net.osmand.plus.IconsCache;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.quickaction.QuickAction;
import net.osmand.plus.quickaction.QuickActionFactory;
import net.osmand.plus.quickaction.QuickActionRegistry;
import java.util.List;
@ -28,9 +31,15 @@ public class SelectCategoryDialogFragment extends DialogFragment {
public static final String TAG = "SelectCategoryDialogFragment";
public interface CategorySelectionListener{
void onCategorySelected(String category, int color);
}
private static final String KEY_CTX_SEL_CAT_EDITOR_TAG = "key_ctx_sel_cat_editor_tag";
private String editorTag;
private CategorySelectionListener selectionListener;
@NonNull
@Override
@ -64,10 +73,17 @@ public class SelectCategoryDialogFragment extends DialogFragment {
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PointEditor editor = ((MapActivity) getActivity()).getContextMenu().getPointEditor(editorTag);
if (editor != null) {
editor.setCategory(category.name);
}
if (selectionListener != null) {
selectionListener.onCategorySelected(category.name, category.color);
}
dismiss();
}
});
@ -82,8 +98,9 @@ public class SelectCategoryDialogFragment extends DialogFragment {
@Override
public void onClick(View v) {
dismiss();
DialogFragment dialogFragment = EditCategoryDialogFragment.createInstance(editorTag);
EditCategoryDialogFragment dialogFragment = EditCategoryDialogFragment.createInstance(editorTag);
dialogFragment.show(getActivity().getSupportFragmentManager(), EditCategoryDialogFragment.TAG);
dialogFragment.setSelectionListener(selectionListener);
}
});
ll.addView(itemView);
@ -102,6 +119,10 @@ public class SelectCategoryDialogFragment extends DialogFragment {
return fragment;
}
public void setSelectionListener(CategorySelectionListener selectionListener) {
this.selectionListener = selectionListener;
}
public void saveState(Bundle bundle) {
bundle.putString(KEY_CTX_SEL_CAT_EDITOR_TAG, editorTag);
}

View file

@ -114,8 +114,13 @@ public class CreateEditActionDialog extends DialogFragment {
setupToolbar(view);
setupHeader(view, savedInstanceState);
setupFooter(view);
}
action.drawUI((ViewGroup) view.findViewById(R.id.container), (MapActivity) getActivity());
@Override
public void onResume() {
super.onResume();
action.drawUI((ViewGroup) getView().findViewById(R.id.container), (MapActivity) getActivity());
}
@Override
@ -181,7 +186,7 @@ public class CreateEditActionDialog extends DialogFragment {
@Override
public void onClick(View view) {
action.fillParams(((ViewGroup) root.findViewById(R.id.container)).getChildAt(0));
action.fillParams(((ViewGroup) root.findViewById(R.id.container)).getChildAt(0), (MapActivity) getActivity());
if (isNew) quickActionRegistry.addQuickAction(action);
else quickActionRegistry.updateQuickAction(action);

View file

@ -85,7 +85,7 @@ public class QuickAction {
public void execute(MapActivity activity){};
public void drawUI(ViewGroup parent, MapActivity activity){};
public void fillParams(View root){};
public void fillParams(View root, MapActivity activity){};
@Override

View file

@ -6,6 +6,7 @@ import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.StringRes;
import android.support.v4.app.DialogFragment;
import android.support.v7.widget.SwitchCompat;
import android.view.LayoutInflater;
@ -14,6 +15,8 @@ import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
@ -30,7 +33,9 @@ import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.base.FavoriteImageDrawable;
import net.osmand.plus.dialogs.ProgressDialogFragment;
import net.osmand.plus.mapcontextmenu.editors.EditCategoryDialogFragment;
import net.osmand.plus.mapcontextmenu.editors.FavoritePointEditor;
import net.osmand.plus.mapcontextmenu.editors.FavoritePointEditorFragment;
import net.osmand.plus.mapcontextmenu.editors.SelectCategoryDialogFragment;
import net.osmand.plus.osmedit.OsmEditsUploadListener;
import net.osmand.plus.osmedit.OsmEditsUploadListenerHelper;
@ -188,6 +193,8 @@ public class QuickActionFactory {
public static final String KEY_NAME = "name";
public static final String KEY_DIALOG = "dialog";
public static final String KEY_CATEGORY_NAME = "category_name";
public static final String KEY_CATEGORY_COLOR = "category_color";
private FavoriteAction() {
id = System.currentTimeMillis();
@ -224,54 +231,147 @@ public class QuickActionFactory {
public void geocodingDone(String address) {
progressDialog.dismiss();
activity.getContextMenu().getFavoritePointEditor().add(latLon, address, "");
activity.getContextMenu().getFavoritePointEditor().add(latLon, address, "",
getParams().get(KEY_CATEGORY_NAME),
Integer.valueOf(getParams().get(KEY_CATEGORY_COLOR)),
!Boolean.valueOf(getParams().get(KEY_DIALOG)));
}
}, null);
activity.getMyApplication().getGeocodingLookupService().lookupAddress(lookupRequest);
} else activity.getContextMenu().getFavoritePointEditor().add(latLon, title, "");
} else activity.getContextMenu().getFavoritePointEditor().add(latLon, title, "",
getParams().get(KEY_CATEGORY_NAME),
Integer.valueOf(getParams().get(KEY_CATEGORY_COLOR)),
!Boolean.valueOf(getParams().get(KEY_DIALOG)));
}
@Override
public void drawUI(final ViewGroup parent, MapActivity activity) {
public void drawUI(final ViewGroup parent, final MapActivity activity) {
FavouritesDbHelper helper = activity.getMyApplication().getFavorites();
String category = helper.getFavoriteGroups().size() > 0
? helper.getFavoriteGroups().get(0).name
: activity.getString(R.string.shared_string_favorites);
final View root = LayoutInflater.from(parent.getContext())
.inflate(R.layout.quick_action_add_favorite, parent, false);
View root;
parent.addView(root);
if (parent.getChildCount() == 0) {
AutoCompleteTextViewEx categoryEdit = (AutoCompleteTextViewEx) root.findViewById(R.id.category_edit);
SwitchCompat showDialog = (SwitchCompat) root.findViewById(R.id.saveButton);
ImageView categoryImage = (ImageView) root.findViewById(R.id.category_image);
EditText name = (EditText) root.findViewById(R.id.name_edit);
root = LayoutInflater.from(parent.getContext())
.inflate(R.layout.quick_action_add_favorite, parent, false);
if (!getParams().isEmpty()) {
parent.addView(root);
showDialog.setChecked(Boolean.valueOf(getParams().get(KEY_DIALOG)));
categoryImage.setColorFilter(Integer.valueOf(getParams().get(KEY_CATEGORY_COLOR)));
name.setText(getParams().get(KEY_NAME));
categoryEdit.setText(getParams().get(KEY_CATEGORY_NAME));
} else root = parent.getChildAt(0);
if (getParams().get(KEY_NAME).isEmpty() && Integer.valueOf(getParams().get(KEY_CATEGORY_NAME)) == 0) {
AutoCompleteTextViewEx categoryEdit = (AutoCompleteTextViewEx)
root.findViewById(R.id.category_edit);
categoryEdit.setText(activity.getString(R.string.shared_string_favorites));
categoryImage.setColorFilter(activity.getResources().getColor(R.color.color_favorite));
}
categoryEdit.setText(category);
categoryEdit.setFocusable(false);
} else if (helper.getFavoriteGroups().size() > 0) {
if (!getParams().isEmpty()){
FavouritesDbHelper.FavoriteGroup group = helper.getFavoriteGroups().get(0);
((EditText) root.findViewById(R.id.name_edit)).setText(getParams().get(KEY_NAME));
((SwitchCompat) root.findViewById(R.id.saveButton)).setChecked(Boolean.getBoolean(getParams().get(KEY_DIALOG)));
if (group.name.isEmpty() && group.color == 0) {
group.name = activity.getString(R.string.shared_string_favorites);
categoryEdit.setText(activity.getString(R.string.shared_string_favorites));
categoryImage.setColorFilter(activity.getResources().getColor(R.color.color_favorite));
} else {
categoryEdit.setText(group.name);
categoryImage.setColorFilter(group.color);
}
getParams().put(KEY_CATEGORY_NAME, group.name);
getParams().put(KEY_CATEGORY_COLOR, String.valueOf(group.color));
} else {
categoryEdit.setText(activity.getString(R.string.shared_string_favorites));
categoryImage.setColorFilter(activity.getResources().getColor(R.color.color_favorite));
getParams().put(KEY_CATEGORY_NAME, "");
getParams().put(KEY_CATEGORY_COLOR, "0");
}
categoryEdit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {
SelectCategoryDialogFragment dialogFragment = SelectCategoryDialogFragment.createInstance("");
dialogFragment.show(
activity.getSupportFragmentManager(),
SelectCategoryDialogFragment.TAG);
dialogFragment.setSelectionListener(new SelectCategoryDialogFragment.CategorySelectionListener() {
@Override
public void onCategorySelected(String category, int color) {
fillGroupParams(root, category, color);
}
});
}
});
SelectCategoryDialogFragment dialogFragment = (SelectCategoryDialogFragment)
activity.getSupportFragmentManager().findFragmentByTag(SelectCategoryDialogFragment.TAG);
if (dialogFragment != null) {
dialogFragment.setSelectionListener(new SelectCategoryDialogFragment.CategorySelectionListener() {
@Override
public void onCategorySelected(String category, int color) {
fillGroupParams(root, category, color);
}
});
} else {
EditCategoryDialogFragment dialog = (EditCategoryDialogFragment)
activity.getSupportFragmentManager().findFragmentByTag(EditCategoryDialogFragment.TAG);
if (dialog != null) {
dialogFragment.setSelectionListener(new SelectCategoryDialogFragment.CategorySelectionListener() {
@Override
public void onCategorySelected(String category, int color) {
fillGroupParams(root, category, color);
}
});
}
}
}
@Override
public void fillParams(View root) {
public void fillParams(View root, MapActivity activity) {
getParams().put(KEY_NAME, ((EditText) root.findViewById(R.id.name_edit)).getText().toString());
getParams().put(KEY_DIALOG, Boolean.toString(((SwitchCompat) root.findViewById(R.id.saveButton)).isChecked()));
}
private void fillGroupParams(View root, String name, int color) {
((AutoCompleteTextViewEx) root.findViewById(R.id.category_edit)).setText(name);
getParams().put(KEY_CATEGORY_NAME, name);
if (color > 0) {
((ImageView) root.findViewById(R.id.category_image)).setColorFilter(color);
getParams().put(KEY_CATEGORY_COLOR, String.valueOf(color));
}
}
}
}