saving quick actions

This commit is contained in:
Korusn Oleksandr 2016-12-22 18:01:24 +02:00
parent c4ebbb4436
commit 891ca8f71c
4 changed files with 281 additions and 19 deletions

View file

@ -0,0 +1,229 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/editor_scroll_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="56dp"
android:orientation="horizontal"
android:gravity="center_vertical"
android:paddingLeft="24dp"
android:paddingRight="16dp">
<android.support.v7.widget.AppCompatTextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAppearance="@style/TextAppearance.ListItemTitle"
tools:text="Some title text"/>
<android.support.v7.widget.SwitchCompat
android:id="@+id/toggle_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"/>
</LinearLayout>
<LinearLayout
android:id="@+id/main_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:orientation="vertical">
<LinearLayout
android:id="@+id/title_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/bg_color"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:baselineAligned="false"
android:orientation="horizontal">
<LinearLayout
android:layout_width="42dp"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/name_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginStart="12dp"
android:layout_marginTop="2dp"
android:scaleType="centerInside"
android:src="@drawable/ic_action_building_number"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/name_caption"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:text="@string/favourites_edit_dialog_name"
android:textColor="?android:textColorSecondary"
android:textSize="@dimen/default_list_text_size"/>
<EditText
android:id="@+id/name_edit"
tools:text="Name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="16dp"
android:layout_marginTop="8dp"
android:imeOptions="actionDone"
android:inputType="text"
android:textColor="?android:textColorPrimary"
android:textColorHint="?android:textColorSecondary"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:orientation="horizontal">
<LinearLayout
android:layout_width="42dp"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/category_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginStart="12dp"
android:layout_marginTop="2dp"
android:scaleType="centerInside"
android:src="@drawable/ic_action_building_number"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/category_caption"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:text="@string/favourites_edit_dialog_name"
android:textColor="?android:textColorSecondary"
android:textSize="@dimen/default_list_text_size"/>
<net.osmand.plus.widgets.AutoCompleteTextViewEx
android:id="@+id/category_edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="16dp"
android:layout_marginTop="8dp"
android:drawableRight="@drawable/ic_action_arrow_drop_down"
android:editable="false"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="48dp"
android:foreground="@drawable/bg_contextmenu_shadow"
android:foregroundGravity="top|fill_horizontal">
<LinearLayout
android:id="@+id/description_info_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:orientation="horizontal">
<LinearLayout
android:layout_width="42dp"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/description_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginStart="12dp"
android:layout_marginTop="10dp"
android:scaleType="center"
android:src="@drawable/ic_action_note_dark"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_weight="1"
android:orientation="vertical">
<EditText
android:id="@+id/description_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="8dp"
android:layout_marginRight="16dp"
android:inputType="textMultiLine"
android:maxLines="8"
android:textColor="?android:textColorPrimary"
android:textColorHint="?android:textColorSecondary"/>
</LinearLayout>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>

View file

@ -25,7 +25,7 @@ public class QuickActionFactory {
Type type = new TypeToken<List<QuickAction>>(){}.getType(); Type type = new TypeToken<List<QuickAction>>(){}.getType();
ArrayList<QuickAction> quickActions = new Gson().fromJson(json, type); ArrayList<QuickAction> quickActions = new Gson().fromJson(json, type);
return quickActions; return quickActions != null ? quickActions : new ArrayList<QuickAction>();
} }
public static List<QuickAction> produceTypeActionsList() { public static List<QuickAction> produceTypeActionsList() {

View file

@ -33,6 +33,12 @@ public class QuickActionItemTouchHelperCallback extends ItemTouchHelper.Callback
return false; return false;
} }
@Override
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
super.clearView(recyclerView, viewHolder);
itemMoveCallback.onViewDropped(recyclerView, viewHolder);
}
@Override @Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) { public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return itemMoveCallback.onMove(recyclerView, viewHolder, target); return itemMoveCallback.onMove(recyclerView, viewHolder, target);
@ -49,5 +55,6 @@ public class QuickActionItemTouchHelperCallback extends ItemTouchHelper.Callback
interface OnItemMoveCallback { interface OnItemMoveCallback {
boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target); boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target);
void onViewDropped(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder);
} }
} }

View file

@ -43,8 +43,8 @@ public class QuickActionListFragment extends BaseOsmAndFragment implements AddQu
FloatingActionButton fab; FloatingActionButton fab;
QuickActionFactory quickActionFactory = new QuickActionFactory(); QuickActionFactory quickActionFactory = new QuickActionFactory();
QuickActionAdapter adapter; QuickActionAdapter adapter;
ItemTouchHelper touchHelper; ItemTouchHelper touchHelper;
@Nullable @Nullable
@Override @Override
@ -59,7 +59,6 @@ public class QuickActionListFragment extends BaseOsmAndFragment implements AddQu
fab.setOnClickListener(new View.OnClickListener() { fab.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
// adapter.addItem(new QuickActionItem(R.string.map_marker, R.drawable.ic_action_flag_dark));
AddQuickActionDialog dialog = new AddQuickActionDialog(); AddQuickActionDialog dialog = new AddQuickActionDialog();
dialog.show(getFragmentManager(), AddQuickActionDialog.TAG); dialog.show(getFragmentManager(), AddQuickActionDialog.TAG);
@ -90,7 +89,7 @@ public class QuickActionListFragment extends BaseOsmAndFragment implements AddQu
ItemTouchHelper.Callback touchHelperCallback = new QuickActionItemTouchHelperCallback(adapter); ItemTouchHelper.Callback touchHelperCallback = new QuickActionItemTouchHelperCallback(adapter);
touchHelper = new ItemTouchHelper(touchHelperCallback); touchHelper = new ItemTouchHelper(touchHelperCallback);
touchHelper.attachToRecyclerView(quickActionRV); touchHelper.attachToRecyclerView(quickActionRV);
adapter.addItems(createMockDada()); adapter.addItems(getSavedActions());
quickActionRV.addOnScrollListener(new RecyclerView.OnScrollListener() { quickActionRV.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override @Override
@ -130,24 +129,31 @@ public class QuickActionListFragment extends BaseOsmAndFragment implements AddQu
getMapActivity().enableDrawer(); getMapActivity().enableDrawer();
} }
private List<QuickAction> createMockDada() { private List<QuickAction> getSavedActions() {
List<QuickAction> result = new ArrayList<>(); String actionsJson = getMyApplication().getSettings().QUICK_ACTION_LIST.get();
for (int i = 0; i < 4; i++) {
result.addAll(quickActionFactory.produceTypeActionsList());
}
return result; return quickActionFactory.parseActiveActionsList(actionsJson);
// List<QuickAction> result = new ArrayList<>();
// for (int i = 0; i < 4; i++) {
// result.addAll(quickActionFactory.produceTypeActionsList());
// }
//
// return result;
} }
private MapActivity getMapActivity() { private MapActivity getMapActivity() {
return (MapActivity) getActivity(); return (MapActivity) getActivity();
} }
private void saveQuickActions(){
String json = quickActionFactory.quickActionListToString((ArrayList<QuickAction>) adapter.getQuickActions());
getMyApplication().getSettings().QUICK_ACTION_LIST.set(json);
}
@Override @Override
public void onActionSelected(QuickAction action) { public void onActionSelected(QuickAction action) {
adapter.addItem(action); adapter.addItem(action);
saveQuickActions();
//TODO save
} }
public class QuickActionAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements QuickActionItemTouchHelperCallback.OnItemMoveCallback { public class QuickActionAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements QuickActionItemTouchHelperCallback.OnItemMoveCallback {
@ -174,7 +180,7 @@ public class QuickActionListFragment extends BaseOsmAndFragment implements AddQu
@Override @Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) { public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
int viewType = getItemViewType(position); int viewType = getItemViewType(position);
QuickAction item = itemsList.get(position); QuickAction item = itemsList.get(position);
if (viewType == SCREEN_ITEM_TYPE) { if (viewType == SCREEN_ITEM_TYPE) {
@ -202,7 +208,8 @@ public class QuickActionListFragment extends BaseOsmAndFragment implements AddQu
}); });
// LinearLayout.LayoutParams dividerParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); // LinearLayout.LayoutParams dividerParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams dividerParams = (LinearLayout.LayoutParams) itemVH.divider.getLayoutParams(); LinearLayout.LayoutParams dividerParams = (LinearLayout.LayoutParams) itemVH.divider.getLayoutParams();
//noinspection ResourceType
dividerParams.setMargins(!isLongDivider(position) ? dpToPx(56f) : 0, 0, 0, 0); dividerParams.setMargins(!isLongDivider(position) ? dpToPx(56f) : 0, 0, 0, 0);
itemVH.divider.setLayoutParams(dividerParams); itemVH.divider.setLayoutParams(dividerParams);
} else { } else {
@ -228,6 +235,12 @@ public class QuickActionListFragment extends BaseOsmAndFragment implements AddQu
itemsList.remove(position); itemsList.remove(position);
notifyItemRemoved(position); notifyItemRemoved(position);
moveHeaders(position);
showFABIfNotScrollable();
saveQuickActions();
}
private void moveHeaders(int position) {
for (int i = position; i < itemsList.size(); i++) { for (int i = position; i < itemsList.size(); i++) {
if (getItemViewType(i) == SCREEN_HEADER_TYPE) { if (getItemViewType(i) == SCREEN_HEADER_TYPE) {
if (i != itemsList.size() - 2) { if (i != itemsList.size() - 2) {
@ -241,8 +254,6 @@ public class QuickActionListFragment extends BaseOsmAndFragment implements AddQu
} }
} }
notifyItemRangeChanged(position, itemsList.size() - position); notifyItemRangeChanged(position, itemsList.size() - position);
showFABIfNotScrollable();
} }
private void showFABIfNotScrollable() { private void showFABIfNotScrollable() {
@ -255,11 +266,21 @@ public class QuickActionListFragment extends BaseOsmAndFragment implements AddQu
fab.show(); fab.show();
} }
public List<QuickAction> getQuickActions() {
List<QuickAction> result = new ArrayList<>();
for (int i = 0; i < itemsList.size(); i++) {
if (getItemViewType(i) == SCREEN_ITEM_TYPE)
result.add(itemsList.get(i));
}
return result;
}
public void addItems(List<QuickAction> data) { public void addItems(List<QuickAction> data) {
List<QuickAction> resultList = new ArrayList<>(); List<QuickAction> resultList = new ArrayList<>();
for (int i = 0; i < data.size(); i++) { for (int i = 0; i < data.size(); i++) {
if (i % ITEMS_IN_GROUP == 0) if (i % ITEMS_IN_GROUP == 0)
resultList.add(createHeader()); //creates header item resultList.add(createHeader());
resultList.add(data.get(i)); resultList.add(data.get(i));
} }
@ -285,7 +306,7 @@ public class QuickActionListFragment extends BaseOsmAndFragment implements AddQu
} }
private boolean isLongDivider(int globalPosition) { private boolean isLongDivider(int globalPosition) {
return getActionPosition(globalPosition) == ITEMS_IN_GROUP || globalPosition == getItemCount() - 1; return getActionPosition(globalPosition) == ITEMS_IN_GROUP || globalPosition == getItemCount() - 1;
} }
public boolean isRecyclerScrollable(RecyclerView recyclerView) { public boolean isRecyclerScrollable(RecyclerView recyclerView) {
@ -325,6 +346,11 @@ public class QuickActionListFragment extends BaseOsmAndFragment implements AddQu
} }
} }
@Override
public void onViewDropped(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
saveQuickActions();
}
public class QuickActionItemVH extends RecyclerView.ViewHolder { public class QuickActionItemVH extends RecyclerView.ViewHolder {
public TextView title; public TextView title;
public TextView subTitle; public TextView subTitle;