Merge pull request #4800 from osmandapp/notes_landscape

OSM Notes
This commit is contained in:
Alexander Sytnyk 2017-11-23 17:32:26 +02:00 committed by GitHub
commit 3807156e22
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 243 additions and 101 deletions

View file

@ -4,7 +4,6 @@
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical"> android:orientation="vertical">
<include <include

View file

@ -1,14 +1,21 @@
package net.osmand.plus.osmedit; package net.osmand.plus.osmedit;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.system.Os;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.StyleSpan;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.CheckBox; import android.widget.CheckBox;
import android.widget.FrameLayout;
import android.widget.ImageButton; import android.widget.ImageButton;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView; import android.widget.TextView;
import net.osmand.osm.PoiType; import net.osmand.osm.PoiType;
@ -20,18 +27,25 @@ import net.osmand.util.Algorithms;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
public class OsmEditsAdapter extends ArrayAdapter<OsmPoint> { public class OsmEditsAdapter extends ArrayAdapter<Object> {
public static final int TYPE_HEADER = 0;
private static final int TYPE_ITEM = 1;
private static final int TYPE_COUNT = 2;
private OsmandApplication app; private OsmandApplication app;
private List<Object> items;
private boolean selectionMode; private boolean selectionMode;
private List<OsmPoint> selectedOsmEdits; private List<OsmPoint> selectedOsmEdits;
private boolean portrait;
private OsmEditsAdapterListener listener; private OsmEditsAdapterListener listener;
public OsmEditsAdapter(OsmandApplication app, @NonNull List<OsmPoint> points) { public OsmEditsAdapter(OsmandApplication app, @NonNull List<Object> items) {
super(app, 0, points); super(app, 0, items);
this.app = app; this.app = app;
this.items = items;
} }
public boolean isSelectionMode() { public boolean isSelectionMode() {
@ -46,6 +60,10 @@ public class OsmEditsAdapter extends ArrayAdapter<OsmPoint> {
this.selectedOsmEdits = selectedOsmEdits; this.selectedOsmEdits = selectedOsmEdits;
} }
public void setPortrait(boolean portrait) {
this.portrait = portrait;
}
public void setAdapterListener(OsmEditsAdapterListener listener) { public void setAdapterListener(OsmEditsAdapterListener listener) {
this.listener = listener; this.listener = listener;
} }
@ -53,18 +71,112 @@ public class OsmEditsAdapter extends ArrayAdapter<OsmPoint> {
@NonNull @NonNull
@Override @Override
public View getView(int position, View convertView, @NonNull ViewGroup parent) { public View getView(int position, View convertView, @NonNull ViewGroup parent) {
View view = convertView; if (portrait) {
if (view == null) { if (convertView == null) {
view = LayoutInflater.from(getContext()).inflate(R.layout.note_list_item, parent, false); if (position == 0) {
OsmEditViewHolder holder = new OsmEditViewHolder(view); convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item_header, parent, false);
view.setTag(holder); HeaderViewHolder holder = new HeaderViewHolder(convertView);
convertView.setTag(holder);
} else {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.note_list_item, parent, false);
OsmEditViewHolder holder = new OsmEditViewHolder(convertView);
convertView.setTag(holder);
}
}
if (position == 0) {
bindHeaderViewHolder((HeaderViewHolder) convertView.getTag());
} else {
final Object item = getItem(position);
if (item instanceof OsmPoint) {
final OsmEditViewHolder holder = (OsmEditViewHolder) convertView.getTag();
bindOsmEditViewHolder(holder, (OsmPoint) item, position);
}
} }
final OsmPoint osmEdit = getItem(position);
if (osmEdit != null) { return convertView;
final OsmEditViewHolder holder = (OsmEditViewHolder) view.getTag(); } else {
int margin = app.getResources().getDimensionPixelSize(R.dimen.content_padding);
int sideMargin = app.getResources().getDisplayMetrics().widthPixels / 10;
holder.titleTextView.setText(OsmEditingPlugin.getName(osmEdit)); FrameLayout fl = new FrameLayout(getContext());
LinearLayout ll = new LinearLayout(getContext());
ll.setOrientation(LinearLayout.VERTICAL);
ll.setBackgroundResource(app.getSettings().isLightContent() ? R.drawable.bg_card_light : R.drawable.bg_card_dark);
fl.addView(ll);
((FrameLayout.LayoutParams) ll.getLayoutParams()).setMargins(sideMargin, margin, sideMargin, margin);
HeaderViewHolder headerViewHolder = new HeaderViewHolder(LayoutInflater.from(getContext()).inflate(R.layout.list_item_header, parent, false));
bindHeaderViewHolder(headerViewHolder);
ll.addView(headerViewHolder.mainView);
for (int i = 0; i < items.size(); i++) {
Object item = getItem(i);
if (item instanceof OsmPoint) {
OsmEditViewHolder viewHolder = new OsmEditViewHolder(LayoutInflater.from(getContext()).inflate(R.layout.note_list_item, parent, false));
bindOsmEditViewHolder(viewHolder, (OsmPoint) item, i);
ll.addView(viewHolder.mainView);
}
}
return fl;
}
}
@Override
public int getCount() {
if (portrait) {
return super.getCount();
} else {
return getHeadersCount();
}
}
@Override
public int getItemViewType(int position) {
Object item = getItem(position);
if (item instanceof OsmPoint) {
return TYPE_ITEM;
}
return (int) item;
}
@Override
public int getViewTypeCount() {
return TYPE_COUNT;
}
private int getHeadersCount() {
int count = 0;
for (int i = 0; i < items.size(); i++) {
Object item = items.get(i);
if (item instanceof Integer) {
count++;
}
}
return count;
}
private void bindHeaderViewHolder(final HeaderViewHolder holder) {
holder.topDivider.setVisibility(portrait ? View.VISIBLE : View.GONE);
holder.title.setText(R.string.your_edits);
holder.checkBox.setChecked(isAllSelected());
if (selectionMode) {
holder.checkBox.setVisibility(View.VISIBLE);
holder.checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (listener != null) {
listener.onHeaderCheckboxClick(holder.checkBox.isChecked());
}
}
});
} else {
holder.checkBox.setVisibility(View.GONE);
}
}
private void bindOsmEditViewHolder(final OsmEditViewHolder holder, final OsmPoint osmEdit, int position) {
holder.titleTextView.setText(getTitle(osmEdit));
holder.descriptionTextView.setText(getDescription(osmEdit)); holder.descriptionTextView.setText(getDescription(osmEdit));
Drawable icon = getIcon(osmEdit); Drawable icon = getIcon(osmEdit);
if (icon != null) { if (icon != null) {
@ -111,11 +223,21 @@ public class OsmEditsAdapter extends ArrayAdapter<OsmPoint> {
} }
}); });
boolean showDivider = getCount() > 1 && position != getCount() - 1; boolean showDivider = getItemsCount() > 1 && position != getItemsCount() - 1;
holder.bottomDivider.setVisibility(showDivider ? View.VISIBLE : View.GONE); holder.bottomDivider.setVisibility(showDivider ? View.VISIBLE : View.GONE);
} }
return view; private int getItemsCount() {
return items.size();
}
private SpannableString getTitle(OsmPoint osmPoint) {
SpannableString title = new SpannableString(OsmEditingPlugin.getName(osmPoint));
if (TextUtils.isEmpty(title)) {
title = SpannableString.valueOf(getCategory(osmPoint));
title.setSpan(new StyleSpan(Typeface.ITALIC), 0, title.length(), 0);
}
return title;
} }
private Drawable getIcon(OsmPoint point) { private Drawable getIcon(OsmPoint point) {
@ -158,6 +280,26 @@ public class OsmEditsAdapter extends ArrayAdapter<OsmPoint> {
return null; return null;
} }
private boolean isAllSelected() {
for (int i = 0; i < items.size(); i++) {
Object item = items.get(i);
if (item instanceof OsmPoint) {
if (!selectedOsmEdits.contains(item)) {
return false;
}
}
}
return true;
}
private String getCategory(OsmPoint point) {
String category = "";
if (point.getGroup() == OsmPoint.Group.POI) {
category = ((OpenstreetmapPoint) point).getEntity().getTag(EditPoiData.POI_TYPE_TAG);
}
return category;
}
private String getDescription(OsmPoint point) { private String getDescription(OsmPoint point) {
String action = ""; String action = "";
if (point.getAction() == OsmPoint.Action.CREATE) { if (point.getAction() == OsmPoint.Action.CREATE) {
@ -170,10 +312,7 @@ public class OsmEditsAdapter extends ArrayAdapter<OsmPoint> {
action = getContext().getString(R.string.shared_string_edited); action = getContext().getString(R.string.shared_string_edited);
} }
String category = ""; String category = getCategory(point);
if (point.getGroup() == OsmPoint.Group.POI) {
category = ((OpenstreetmapPoint) point).getEntity().getTag(EditPoiData.POI_TYPE_TAG);
}
String comment = ""; String comment = "";
if (point.getGroup() == OsmPoint.Group.BUG) { if (point.getGroup() == OsmPoint.Group.BUG) {
@ -197,6 +336,20 @@ public class OsmEditsAdapter extends ArrayAdapter<OsmPoint> {
return description; return description;
} }
private class HeaderViewHolder {
View mainView;
View topDivider;
CheckBox checkBox;
TextView title;
HeaderViewHolder(View view) {
mainView = view;
topDivider = view.findViewById(R.id.top_divider);
checkBox = (CheckBox) view.findViewById(R.id.check_box);
title = (TextView) view.findViewById(R.id.title_text_view);
}
}
private class OsmEditViewHolder { private class OsmEditViewHolder {
View mainView; View mainView;
ImageView icon; ImageView icon;
@ -219,6 +372,8 @@ public class OsmEditsAdapter extends ArrayAdapter<OsmPoint> {
public interface OsmEditsAdapterListener { public interface OsmEditsAdapterListener {
void onHeaderCheckboxClick(boolean checked);
void onItemSelect(OsmPoint point, boolean checked); void onItemSelect(OsmPoint point, boolean checked);
void onItemShowMap(OsmPoint point); void onItemShowMap(OsmPoint point);

View file

@ -21,7 +21,6 @@ import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.ViewStub; import android.view.ViewStub;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.EditText; import android.widget.EditText;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.ListView; import android.widget.ListView;
@ -64,7 +63,6 @@ public class OsmEditsFragment extends OsmAndListFragment implements SendPoiDialo
private OsmEditingPlugin plugin; private OsmEditingPlugin plugin;
private View footerView; private View footerView;
private View headerView;
private View emptyView; private View emptyView;
private List<OsmPoint> osmEdits = new ArrayList<>(); private List<OsmPoint> osmEdits = new ArrayList<>();
@ -133,6 +131,12 @@ public class OsmEditsFragment extends OsmAndListFragment implements SendPoiDialo
return listAdapter; return listAdapter;
} }
private void recreateAdapterData() {
listAdapter.clear();
listAdapter.addAll(createItemsList());
listAdapter.notifyDataSetChanged();
}
private void selectAll() { private void selectAll() {
for (int i = 0; i < osmEdits.size(); i++) { for (int i = 0; i < osmEdits.size(); i++) {
OsmPoint point = osmEdits.get(i); OsmPoint point = osmEdits.get(i);
@ -285,7 +289,7 @@ public class OsmEditsFragment extends OsmAndListFragment implements SendPoiDialo
private void updateSelectionMode(ActionMode m) { private void updateSelectionMode(ActionMode m) {
updateSelectionTitle(m); updateSelectionTitle(m);
refreshSelectAll(); listAdapter.notifyDataSetChanged();
} }
private void updateSelectionTitle(ActionMode m) { private void updateSelectionTitle(ActionMode m) {
@ -296,28 +300,9 @@ public class OsmEditsFragment extends OsmAndListFragment implements SendPoiDialo
} }
} }
private void refreshSelectAll() {
View view = getView();
if (view == null) {
return;
}
CheckBox selectAll = (CheckBox) view.findViewById(R.id.check_box);
for (int i = 0; i < osmEdits.size(); i++) {
OsmPoint point = osmEdits.get(i);
if (!osmEditsSelected.contains(point)) {
selectAll.setChecked(false);
return;
}
}
selectAll.setChecked(true);
}
private void enableSelectionMode(boolean selectionMode) { private void enableSelectionMode(boolean selectionMode) {
listAdapter.setSelectionMode(selectionMode); listAdapter.setSelectionMode(selectionMode);
//noinspection ConstantConditions //noinspection ConstantConditions
if (headerView != null) {
headerView.findViewById(R.id.check_box).setVisibility(selectionMode ? View.VISIBLE : View.GONE);
}
((FavoritesActivity) getActivity()).setToolbarVisibility(!selectionMode && AndroidUiHelper.isOrientationPortrait(getActivity())); ((FavoritesActivity) getActivity()).setToolbarVisibility(!selectionMode && AndroidUiHelper.isOrientationPortrait(getActivity()));
((FavoritesActivity) getActivity()).updateListViewFooter(footerView); ((FavoritesActivity) getActivity()).updateListViewFooter(footerView);
} }
@ -348,6 +333,7 @@ public class OsmEditsFragment extends OsmAndListFragment implements SendPoiDialo
} }
private void fetchData() { private void fetchData() {
boolean portrait = AndroidUiHelper.isOrientationPortrait(getActivity());
osmEdits = new ArrayList<>(); osmEdits = new ArrayList<>();
List<OpenstreetmapPoint> l1 = plugin.getDBPOI().getOpenstreetmapPoints(); List<OpenstreetmapPoint> l1 = plugin.getDBPOI().getOpenstreetmapPoints();
List<OsmNotesPoint> l2 = plugin.getDBBug().getOsmbugsPoints(); List<OsmNotesPoint> l2 = plugin.getDBBug().getOsmbugsPoints();
@ -357,32 +343,24 @@ public class OsmEditsFragment extends OsmAndListFragment implements SendPoiDialo
listView.setDivider(null); listView.setDivider(null);
listView.setEmptyView(emptyView); listView.setEmptyView(emptyView);
if (osmEdits.size() > 0) { if (osmEdits.size() > 0 && footerView == null && portrait) {
if (footerView == null) {
footerView = getActivity().getLayoutInflater().inflate(R.layout.list_shadow_footer, listView, false); footerView = getActivity().getLayoutInflater().inflate(R.layout.list_shadow_footer, listView, false);
listView.addFooterView(footerView); listView.addFooterView(footerView);
} }
if (headerView == null) { List<Object> items = createItemsList();
headerView = getActivity().getLayoutInflater().inflate(R.layout.list_item_header, listView, false); listAdapter = new OsmEditsAdapter(getMyApplication(), items);
listView.addHeaderView(headerView); listAdapter.setSelectedOsmEdits(osmEditsSelected);
((TextView) headerView.findViewById(R.id.title_text_view)).setText(R.string.your_edits); listAdapter.setAdapterListener(new OsmEditsAdapter.OsmEditsAdapterListener() {
final CheckBox selectAll = (CheckBox) headerView.findViewById(R.id.check_box);
selectAll.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onHeaderCheckboxClick(boolean checked) {
if (selectAll.isChecked()) { if (checked) {
selectAll(); selectAll();
} else { } else {
deselectAll(); deselectAll();
} }
updateSelectionTitle(actionMode); updateSelectionTitle(actionMode);
} }
});
}
}
listAdapter = new OsmEditsAdapter(getMyApplication(), osmEdits);
listAdapter.setSelectedOsmEdits(osmEditsSelected);
listAdapter.setAdapterListener(new OsmEditsAdapter.OsmEditsAdapterListener() {
@Override @Override
public void onItemSelect(OsmPoint point, boolean checked) { public void onItemSelect(OsmPoint point, boolean checked) {
if (checked) { if (checked) {
@ -403,9 +381,19 @@ public class OsmEditsFragment extends OsmAndListFragment implements SendPoiDialo
openPopUpMenu(note); openPopUpMenu(note);
} }
}); });
listAdapter.setPortrait(portrait);
listView.setAdapter(listAdapter); listView.setAdapter(listAdapter);
} }
private List<Object> createItemsList() {
List<Object> items = new ArrayList<>();
if (!osmEdits.isEmpty()) {
items.add(OsmEditsAdapter.TYPE_HEADER);
items.addAll(osmEdits);
}
return items;
}
private void showBugDialog(final OsmNotesPoint point) { private void showBugDialog(final OsmNotesPoint point) {
final View view = LayoutInflater.from(getActivity()).inflate(R.layout.open_bug, null); final View view = LayoutInflater.from(getActivity()).inflate(R.layout.open_bug, null);
view.findViewById(R.id.user_name_field).setVisibility(View.GONE); view.findViewById(R.id.user_name_field).setVisibility(View.GONE);
@ -527,7 +515,7 @@ public class OsmEditsFragment extends OsmAndListFragment implements SendPoiDialo
osmEdits.remove(osmPoint); osmEdits.remove(osmPoint);
} }
} }
listAdapter.notifyDataSetChanged(); recreateAdapterData();
} }
}; };
dialog.show(getActivity().getSupportFragmentManager(), ProgressDialogFragment.TAG); dialog.show(getActivity().getSupportFragmentManager(), ProgressDialogFragment.TAG);
@ -546,7 +534,7 @@ public class OsmEditsFragment extends OsmAndListFragment implements SendPoiDialo
private void deletePoint(OsmPoint osmPoint) { private void deletePoint(OsmPoint osmPoint) {
osmEdits.remove(osmPoint); osmEdits.remove(osmPoint);
listAdapter.notifyDataSetChanged(); recreateAdapterData();
} }
private void notifyDataSetChanged() { private void notifyDataSetChanged() {