quick action create/edit fragment & add action from map

This commit is contained in:
Rosty 2016-12-27 17:03:21 +02:00
parent dfe575d5ec
commit 8573f7f66a
13 changed files with 577 additions and 88 deletions

View file

@ -3,7 +3,7 @@
android:shape="rectangle">
<gradient
android:startColor="@color/popup_bg_color"
android:startColor="#c8202020"
android:endColor="@color/color_transparent" android:angle="-90"/>
</shape>

View file

@ -0,0 +1,104 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--<LinearLayout-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="56dp">-->
<!--<ImageButton-->
<!--android:id="@+id/btnBack"-->
<!--android:layout_width="56dp"-->
<!--android:layout_height="56dp"-->
<!--android:layout_gravity="center_vertical"-->
<!--android:layout_marginLeft="16dp"-->
<!--android:layout_marginRight="16dp"-->
<!--android:scaleType="centerInside"-->
<!--android:tint="@color/color_white"-->
<!--android:background="?android:selectableItemBackground"-->
<!--android:src="@drawable/ic_action_flag_dark" />-->
<!---->
<!--</LinearLayout>-->
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="56dp"
android:contentInsetStart="72dp"
android:contentInsetLeft="72dp"
app:contentInsetLeft="72dp"
app:contentInsetStart="72dp"
android:background="@color/actionbar_light_color"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="72dp"
android:background="@color/actionbar_light_color">
<ImageView
android:id="@+id/image"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="16dp"
android:scaleType="centerInside"
android:tint="@color/color_white"
android:layout_marginTop="24dp"
android:src="@drawable/ic_action_flag_dark" />
<android.support.v7.widget.AppCompatEditText
android:id="@+id/name"
android:theme="@style/WhiteControls"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textColor="@color/color_white"
android:textColorHint="@color/white_80_transparent"
android:textSize="@dimen/default_list_text_size_large"
android:hint="@string/quick_action_name_hint"
android:layout_marginStart="-4dp"
android:maxLines="1"
android:layout_marginRight="16dp"
android:layout_alignLeft="@+id/title"
android:layout_below="@+id/title" />
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textColor="@color/white_50_transparent"
android:textSize="@dimen/default_sub_text_size"
android:text="@string/text_name"
android:layout_marginRight="16dp"
android:maxLines="1"
android:layout_marginLeft="32dp"
android:layout_toEndOf="@+id/image"
android:layout_toRightOf="@+id/image" />
</RelativeLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/container"
android:background="@color/dashboard_background"
android:layout_weight="1"/>
<Button
android:id="@+id/btnApply"
android:layout_width="match_parent"
android:layout_height="52dp"
android:text="Apply"
android:textAllCaps="true"
android:textColor="@color/color_white"
android:textSize="@dimen/default_list_text_size"
android:gravity="center"
android:foreground="?android:selectableItemBackground"
android:background="@color/dashboard_blue"/>
</LinearLayout>

View file

@ -2468,6 +2468,7 @@ If you need help with OsmAnd application, please contact our support team: suppo
<string name="quick_action_item_screen">Screen %d</string>
<string name="quick_action_add_marker">Add marker</string>
<string name="quick_action_new_action">Add action</string>
<string name="quick_action_edit_action">Edit action</string>
<string name="quick_action_add_favorite">Add favorite</string>
<string name="dialog_add_action_title">Add Action</string>
<string name="dialog_add_action_subhead">Create item</string>
@ -2477,4 +2478,6 @@ If you need help with OsmAnd application, please contact our support team: suppo
<string name="quick_favorites_show_favorites_dialog">Show favorites dialog</string>
<string name="quick_favorites_name_preset">Name preset</string>
<string name="quick_favorites_name_description">Leave field blank and OsmAnd will use the address or name of a place for the favorite point</string>
<string name="quick_action_name_hint">Action name</string>
<string name="text_name">Name</string>
</resources>

View file

@ -445,4 +445,10 @@
<item name="android:windowMinWidthMinor">90%</item>
</style>
<style name="WhiteControls" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorControlNormal">@color/color_white</item>
<item name="colorControlActivated">@color/color_white</item>
<item name="colorControlHighlight">@color/color_white</item>
</style>
</resources>

View file

@ -30,6 +30,7 @@ import net.osmand.plus.activities.MapActivity.ShowQuickSearchMode;
import net.osmand.plus.helpers.GpxUiHelper;
import net.osmand.plus.poi.PoiFiltersHelper;
import net.osmand.plus.poi.PoiUIFilter;
import net.osmand.plus.quickaction.QuickActionRegistry;
import net.osmand.plus.rastermaps.OsmandRasterMapsPlugin;
import net.osmand.plus.render.MapVectorLayer;
import net.osmand.plus.render.RenderingIcons;
@ -84,12 +85,18 @@ public class MapActivityLayers {
private MapQuickActionLayer mapQuickActionLayer;
private DownloadedRegionsLayer downloadedRegionsLayer;
private MapWidgetRegistry mapWidgetRegistry;
private QuickActionRegistry quickActionRegistry;
private StateChangedListener<Integer> transparencyListener;
public MapActivityLayers(MapActivity activity) {
this.activity = activity;
this.mapWidgetRegistry = new MapWidgetRegistry(activity.getMyApplication().getSettings());
this.quickActionRegistry = new QuickActionRegistry(activity.getMyApplication().getSettings());
}
public QuickActionRegistry getQuickActionRegistry() {
return quickActionRegistry;
}
public MapWidgetRegistry getMapWidgetRegistry() {

View file

@ -17,6 +17,7 @@ import android.widget.ImageView;
import android.widget.TextView;
import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import java.util.List;
@ -28,8 +29,6 @@ public class AddQuickActionDialog extends DialogFragment {
public static final String TAG = AddQuickActionDialog.class.getSimpleName();
protected QuickAction.QuickActionSelectionListener selectionListener;
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
@ -60,6 +59,11 @@ public class AddQuickActionDialog extends DialogFragment {
return root;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
private List<QuickAction> data;
@ -104,8 +108,8 @@ public class AddQuickActionDialog extends DialogFragment {
@Override
public void onClick(View view) {
if (selectionListener != null)
selectionListener.onActionSelected(action);
CreateEditActionDialog dialog = CreateEditActionDialog.newInstance(action.type);
dialog.show(getFragmentManager(), AddQuickActionDialog.TAG);
dismiss();
}

View file

@ -0,0 +1,201 @@
package net.osmand.plus.quickaction;
import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v7.view.ContextThemeWrapper;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageView;
import net.osmand.plus.IconsCache;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
/**
* Created by rosty on 12/27/16.
*/
public class CreateEditActionDialog extends DialogFragment {
public static final String TAG = CreateEditActionDialog.class.getSimpleName();
public static final String KEY_ACTION_ID = "action_id";
public static final String KEY_ACTION_TYPE = "action_type";
public static final String KEY_ACTION_IS_NEW = "action_is_new";
public static CreateEditActionDialog newInstance(long id) {
Bundle args = new Bundle();
args.putLong(KEY_ACTION_ID, id);
CreateEditActionDialog dialog = new CreateEditActionDialog();
dialog.setArguments(args);
return dialog;
}
public static CreateEditActionDialog newInstance(int type) {
Bundle args = new Bundle();
args.putInt(KEY_ACTION_TYPE, type);
CreateEditActionDialog dialog = new CreateEditActionDialog();
dialog.setArguments(args);
return dialog;
}
private QuickActionRegistry quickActionRegistry;
private QuickAction action;
private boolean isNew;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = new Dialog(new ContextThemeWrapper(getActivity(), R.style.OsmandLightTheme), getTheme());
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setLayout(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
return dialog;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NORMAL, R.style.OsmandLightTheme);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
return inflater.inflate(R.layout.quick_action_create_edit_dialog, parent, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
quickActionRegistry = ((MapActivity) getActivity()).getMapLayers().getQuickActionRegistry();
long actionId = savedInstanceState == null
? getArguments().getLong(KEY_ACTION_ID)
: savedInstanceState.getLong(KEY_ACTION_ID);
int type = savedInstanceState == null
? getArguments().getInt(KEY_ACTION_TYPE)
: savedInstanceState.getInt(KEY_ACTION_TYPE);
isNew = savedInstanceState == null
? isNew = actionId == 0
: savedInstanceState.getBoolean(KEY_ACTION_IS_NEW);
action = isNew
? QuickActionFactory.newActionByType(type)
: quickActionRegistry.getQuickAction(actionId);
setupToolbar(view);
setupHeader(view, savedInstanceState);
setupFooter(view);
action.drawUI((ViewGroup) view.findViewById(R.id.container));
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putLong(KEY_ACTION_ID, action.getId());
outState.putInt(KEY_ACTION_TYPE, action.type);
outState.putBoolean(KEY_ACTION_IS_NEW, isNew);
}
private void setupToolbar(View root) {
Toolbar toolbar = (Toolbar) root.findViewById(R.id.toolbar);
toolbar.setTitle(isNew
? R.string.quick_action_new_action
: R.string.quick_action_edit_action);
toolbar.setTitleTextColor(Color.WHITE);
toolbar.setNavigationIcon(getIconsCache().getIcon(
R.drawable.abc_ic_ab_back_mtrl_am_alpha,
R.color.color_white));
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
}
private void setupHeader(View root, Bundle savedInstanceState){
ImageView image = (ImageView) root.findViewById(R.id.image);
EditText name = (EditText) root.findViewById(R.id.name);
name.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
action.setName(charSequence.toString());
}
@Override
public void afterTextChanged(Editable editable) {
}
});
if (savedInstanceState == null) name.setText(action.getName(getContext()));
else action.setName(name.getText().toString());
image.setImageResource(action.iconRes);
}
private void setupFooter(View root){
root.findViewById(R.id.btnApply).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
action.fillParams();
quickActionRegistry.addQuickAction(action);
quickActionRegistry.getUpdatesListener().onActionsUpdated();
dismiss();
}
});
}
private OsmandApplication getApplication(){
return (OsmandApplication)(getContext().getApplicationContext());
}
private IconsCache getIconsCache(){
return getApplication().getIconsCache();
}
}

View file

@ -1,34 +1,47 @@
package net.osmand.plus.quickaction;
import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.DrawableRes;
import android.support.annotation.StringRes;
import android.view.ViewGroup;
import net.osmand.plus.activities.MapActivity;
import java.util.HashMap;
public class QuickAction implements Parcelable {
public class QuickAction {
public interface QuickActionSelectionListener {
void onActionSelected(QuickAction action);
}
protected int id;
protected int type;
protected long id;
protected @StringRes int nameRes;
protected @DrawableRes int iconRes;
private String name;
private HashMap<String, String> params;
protected QuickAction() {
this.id = System.currentTimeMillis();
}
protected QuickAction(int type) {
this.id = System.currentTimeMillis();
this.type = type;
}
public QuickAction(QuickAction quickAction) {
this.type = quickAction.type;
this.id = quickAction.id;
this.nameRes = quickAction.nameRes;
this.iconRes = quickAction.iconRes;
this.name = quickAction.name;
this.params = quickAction.params;
}
@ -40,48 +53,52 @@ public class QuickAction implements Parcelable {
return iconRes;
}
public int getId() {
public long getId() {
return id;
}
public String getName(Context context) {
return name == null || name.isEmpty() ? nameRes > 0 ? context.getString(nameRes) : "" : name;
}
public HashMap<String, String> getParams() {
return params;
}
public void execute(){};
public void setName(String name) {
this.name = name;
}
public void setParams(HashMap<String, String> params) {
this.params = params;
}
public void execute(MapActivity activity){};
public void drawUI(ViewGroup parent){};
public void fillParams(){};
@Override
public int describeContents() {
return 0;
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
QuickAction action = (QuickAction) o;
if (type != action.type) return false;
if (id != action.id) return false;
return true;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(this.id);
dest.writeInt(this.nameRes);
dest.writeInt(this.iconRes);
dest.writeSerializable(this.params);
public int hashCode() {
int result = type;
result = 31 * result + (int) (id ^ (id >>> 32));
result = 31 * result + nameRes;
result = 31 * result + iconRes;
result = 31 * result + (name != null ? name.hashCode() : 0);
return result;
}
protected QuickAction(Parcel in) {
this.id = in.readInt();
this.nameRes = in.readInt();
this.iconRes = in.readInt();
this.params = (HashMap<String, String>) in.readSerializable();
}
public static final Parcelable.Creator<QuickAction> CREATOR = new Parcelable.Creator<QuickAction>() {
@Override
public QuickAction createFromParcel(Parcel source) {
return new QuickAction(source);
}
@Override
public QuickAction[] newArray(int size) {
return new QuickAction[size];
}
};
}

View file

@ -7,6 +7,7 @@ import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import java.lang.reflect.Type;
import java.util.ArrayList;
@ -14,13 +15,13 @@ import java.util.List;
public class QuickActionFactory {
public String quickActionListToString(ArrayList<QuickAction> quickActions){
public String quickActionListToString(List<QuickAction> quickActions){
String json = new Gson().toJson(quickActions);
return json;
}
public static List<QuickAction> parseActiveActionsList(String json) {
public List<QuickAction> parseActiveActionsList(String json) {
Type type = new TypeToken<List<QuickAction>>(){}.getType();
ArrayList<QuickAction> quickActions = new Gson().fromJson(json, type);
@ -38,29 +39,41 @@ public class QuickActionFactory {
return quickActions;
}
public QuickAction produceAction(QuickAction quickAction){
public static QuickAction newActionByType(int type){
if (quickAction.id == NewAction.ID) {
switch (type){
return new NewAction(quickAction);
case NewAction.TYPE: return new NewAction();
} else if (quickAction.id == MarkerAction.ID) {
case MarkerAction.TYPE: return new MarkerAction();
return new MarkerAction(quickAction);
case FavoriteAction.TYPE: return new FavoriteAction();
} else if (quickAction.id == FavoriteAction.ID){
default: return new QuickAction();
}
}
return new FavoriteAction(quickAction);
public static QuickAction produceAction(QuickAction quickAction){
} else return quickAction;
switch (quickAction.type){
case NewAction.TYPE: return new NewAction(quickAction);
case MarkerAction.TYPE: return new MarkerAction(quickAction);
case FavoriteAction.TYPE: return new FavoriteAction(quickAction);
default: return quickAction;
}
}
public static class NewAction extends QuickAction {
public static final int ID = 1;
public static final int TYPE = 1;
protected NewAction(){
id = ID;
id = System.currentTimeMillis();
type = TYPE;
nameRes = R.string.quick_action_new_action;
iconRes = R.drawable.ic_action_plus;
}
@ -70,9 +83,10 @@ public class QuickActionFactory {
}
@Override
public void execute() {
public void execute(MapActivity activity) {
//TODO do some action
AddQuickActionDialog dialog = new AddQuickActionDialog();
dialog.show(activity.getSupportFragmentManager(), AddQuickActionDialog.TAG);
}
@Override
@ -84,10 +98,11 @@ public class QuickActionFactory {
public static class MarkerAction extends QuickAction {
public static final int ID = 2;
public static final int TYPE = 2;
private MarkerAction(){
id = ID;
id = System.currentTimeMillis();
type = TYPE;
nameRes = R.string.quick_action_add_marker;
iconRes = R.drawable.ic_action_flag_dark;
}
@ -97,7 +112,7 @@ public class QuickActionFactory {
}
@Override
public void execute() {
public void execute(MapActivity activity) {
//TODO do some action
}
@ -111,10 +126,11 @@ public class QuickActionFactory {
public static class FavoriteAction extends QuickAction {
public static final int ID = 3;
public static final int TYPE = 3;
public FavoriteAction() {
id = ID;
id = System.currentTimeMillis();
type = TYPE;
nameRes = R.string.quick_action_add_favorite;
iconRes = R.drawable.ic_action_fav_dark;
}
@ -124,7 +140,7 @@ public class QuickActionFactory {
}
@Override
public void execute() {
public void execute(MapActivity activity) {
//TODO do some action
}

View file

@ -40,45 +40,47 @@ import static android.util.TypedValue.COMPLEX_UNIT_DIP;
* Created by okorsun on 20.12.16.
*/
public class QuickActionListFragment extends BaseOsmAndFragment implements QuickAction.QuickActionSelectionListener {
public class QuickActionListFragment extends BaseOsmAndFragment implements QuickActionRegistry.QuickActionUpdatesListener{
public static final String TAG = QuickActionListFragment.class.getSimpleName();
RecyclerView quickActionRV;
FloatingActionButton fab;
QuickActionFactory quickActionFactory = new QuickActionFactory();
QuickActionAdapter adapter;
ItemTouchHelper touchHelper;
QuickActionAdapter adapter;
ItemTouchHelper touchHelper;
QuickActionRegistry quickActionRegistry;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.quick_action_list, container, false);
quickActionRV = (RecyclerView) view.findViewById(R.id.recycler_view);
fab = (FloatingActionButton) view.findViewById(R.id.fabButton);
setUpQuickActionRV();
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AddQuickActionDialog dialog = new AddQuickActionDialog();
dialog.show(getFragmentManager(), AddQuickActionDialog.TAG);
dialog.selectionListener = QuickActionListFragment.this;
}
});
setUpToolbar(view);
Fragment dialog = getFragmentManager().findFragmentByTag(AddQuickActionDialog.TAG);
if (dialog != null && dialog instanceof AddQuickActionDialog)
((AddQuickActionDialog) dialog).selectionListener = this;
return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
quickActionRegistry = getMapActivity().getMapLayers().getQuickActionRegistry();
setUpToolbar(view);
setUpQuickActionRV();
}
private void setUpQuickActionRV() {
adapter = new QuickActionAdapter(new OnStartDragListener() {
@Override
@ -89,11 +91,10 @@ public class QuickActionListFragment extends BaseOsmAndFragment implements Quick
quickActionRV.setAdapter(adapter);
quickActionRV.setLayoutManager(new LinearLayoutManager(getContext()));
ItemTouchHelper.Callback touchHelperCallback = new QuickActionItemTouchHelperCallback(adapter);
touchHelper = new ItemTouchHelper(touchHelperCallback);
touchHelper.attachToRecyclerView(quickActionRV);
adapter.addItems(getSavedActions());
adapter.addItems(quickActionRegistry.getQuickActions());
quickActionRV.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
@ -124,19 +125,17 @@ public class QuickActionListFragment extends BaseOsmAndFragment implements Quick
@Override
public void onResume() {
super.onResume();
getMapActivity().disableDrawer();
quickActionRegistry.setUpdatesListener(this);
}
@Override
public void onPause() {
super.onPause();
getMapActivity().enableDrawer();
}
private List<QuickAction> getSavedActions() {
String actionsJson = getMyApplication().getSettings().QUICK_ACTION_LIST.get();
return quickActionFactory.parseActiveActionsList(actionsJson);
quickActionRegistry.setUpdatesListener(null);
}
private MapActivity getMapActivity() {
@ -144,8 +143,7 @@ public class QuickActionListFragment extends BaseOsmAndFragment implements Quick
}
private void saveQuickActions(){
String json = quickActionFactory.quickActionListToString((ArrayList<QuickAction>) adapter.getQuickActions());
getMyApplication().getSettings().QUICK_ACTION_LIST.set(json);
quickActionRegistry.updateQuickActions(adapter.getQuickActions());
}
void createAndShowDeleteDialog(final int itemPosition, final String itemName) {
@ -170,9 +168,8 @@ public class QuickActionListFragment extends BaseOsmAndFragment implements Quick
}
@Override
public void onActionSelected(QuickAction action) {
adapter.addItem(action);
saveQuickActions();
public void onActionsUpdated() {
adapter.addItems(quickActionRegistry.getQuickActions());
}
public class QuickActionAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements QuickActionItemTouchHelperCallback.OnItemMoveCallback {
@ -205,7 +202,7 @@ public class QuickActionListFragment extends BaseOsmAndFragment implements Quick
if (viewType == SCREEN_ITEM_TYPE) {
final QuickActionItemVH itemVH = (QuickActionItemVH) holder;
itemVH.title.setText(item.getNameRes());
itemVH.title.setText(item.getName(getContext()));
itemVH.subTitle.setText(getResources().getString(R.string.quick_action_item_action, getActionPosition(position)));
itemVH.icon.setImageDrawable(getMyApplication().getIconsCache().getThemedIcon(item.getIconRes()));
@ -243,7 +240,7 @@ public class QuickActionListFragment extends BaseOsmAndFragment implements Quick
@Override
public int getItemViewType(int position) {
return itemsList.get(position).getId() == 0 ? SCREEN_HEADER_TYPE : SCREEN_ITEM_TYPE;
return itemsList.get(position).type == 0 ? SCREEN_HEADER_TYPE : SCREEN_ITEM_TYPE;
}
public void deleteItem(int position) {

View file

@ -0,0 +1,107 @@
package net.osmand.plus.quickaction;
import net.osmand.plus.OsmandSettings;
import java.util.ArrayList;
import java.util.List;
/**
* Created by rosty on 12/27/16.
*/
public class QuickActionRegistry {
public interface QuickActionUpdatesListener{
void onActionsUpdated();
}
private final QuickActionFactory factory;
private final OsmandSettings settings;
private final List<QuickAction> quickActions;
private QuickActionUpdatesListener updatesListener;
public QuickActionRegistry(OsmandSettings settings) {
this.factory = new QuickActionFactory();
this.settings = settings;
quickActions = factory.parseActiveActionsList(settings.QUICK_ACTION_LIST.get());
}
public void setUpdatesListener(QuickActionUpdatesListener updatesListener) {
this.updatesListener = updatesListener;
}
public QuickActionUpdatesListener getUpdatesListener() {
return updatesListener;
}
public List<QuickAction> getQuickActions() {
ArrayList<QuickAction> actions = new ArrayList<>();
actions.addAll(quickActions);
return actions;
}
public void addQuickAction(QuickAction action){
quickActions.add(action);
settings.QUICK_ACTION_LIST.set(factory.quickActionListToString(quickActions));
}
public void deleteQuickAction(QuickAction action){
int index = quickActions.indexOf(action);
if (index >= 0) quickActions.remove(index);
settings.QUICK_ACTION_LIST.set(factory.quickActionListToString(quickActions));
}
public void deleteQuickAction(int id){
int index = -1;
for (QuickAction action: quickActions){
if (action.id == id)
index = quickActions.indexOf(action);
}
if (index >= 0) quickActions.remove(index);
settings.QUICK_ACTION_LIST.set(factory.quickActionListToString(quickActions));
}
public void updateQuickAction(QuickAction action){
int index = quickActions.indexOf(action);
if (index >= 0) quickActions.set(index, action);
settings.QUICK_ACTION_LIST.set(factory.quickActionListToString(quickActions));
}
public void updateQuickActions(List<QuickAction> quickActions){
this.quickActions.clear();
this.quickActions.addAll(quickActions);
settings.QUICK_ACTION_LIST.set(factory.quickActionListToString(this.quickActions));
}
public QuickAction getQuickAction(long id){
for (QuickAction action: quickActions){
if (action.id == id) return action;
}
return null;
}
}

View file

@ -186,7 +186,7 @@ public class QuickActionsWidget extends LinearLayout {
.getIcon(action.getIconRes(), R.color.icon_color_dark));
((TextView) view.findViewById(R.id.title))
.setText(action.getNameRes());
.setText(action.getName(getContext()));
view.setOnClickListener(new OnClickListener() {
@Override

View file

@ -9,18 +9,21 @@ import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings;
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 net.osmand.plus.quickaction.QuickActionsWidget;
/**
* Created by okorsun on 23.12.16.
*/
public class MapQuickActionLayer extends OsmandMapLayer {
public class MapQuickActionLayer extends OsmandMapLayer implements QuickActionRegistry.QuickActionUpdatesListener, QuickAction.QuickActionSelectionListener{
private final MapActivity mapActivity;
private final OsmandApplication app;
private final OsmandSettings settings;
private final QuickActionRegistry quickActionRegistry;
private ImageButton quickActionButton;
private QuickActionsWidget quickActionsWidget;
@ -29,6 +32,7 @@ public class MapQuickActionLayer extends OsmandMapLayer {
this.mapActivity = activity;
app = activity.getMyApplication();
settings = activity.getMyApplication().getSettings();
quickActionRegistry = activity.getMapLayers().getQuickActionRegistry();
}
@ -41,8 +45,20 @@ public class MapQuickActionLayer extends OsmandMapLayer {
quickActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
quickActionsWidget.setVisibility(quickActionsWidget.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
quickActionsWidget.setActions(QuickActionFactory.parseActiveActionsList(settings.QUICK_ACTION_LIST.get()));
if (quickActionsWidget.getVisibility() == View.VISIBLE){
quickActionsWidget.setVisibility(View.GONE);
quickActionRegistry.setUpdatesListener(null);
quickActionsWidget.setSelectionListener(null);
}else {
quickActionsWidget.setActions(quickActionRegistry.getQuickActions());
quickActionsWidget.setVisibility(View.VISIBLE);
quickActionRegistry.setUpdatesListener(MapQuickActionLayer.this);
quickActionsWidget.setSelectionListener(MapQuickActionLayer.this);
}
}
});
}
@ -65,4 +81,15 @@ public class MapQuickActionLayer extends OsmandMapLayer {
public boolean drawInScreenPixels() {
return true;
}
@Override
public void onActionsUpdated() {
quickActionsWidget.setActions(quickActionRegistry.getQuickActions());
}
@Override
public void onActionSelected(QuickAction action) {
action.execute(mapActivity);
}
}