From 725533afddf6b6b2a61833954e0f004381464366 Mon Sep 17 00:00:00 2001 From: Dima-1 Date: Mon, 1 Jun 2020 19:16:09 +0300 Subject: [PATCH] Icon categories adapter refactoring --- .../editors/IconCategoriesAdapter.java | 107 ++++++++++++++++++ .../editors/PointEditorFragmentNew.java | 84 +++----------- 2 files changed, 121 insertions(+), 70 deletions(-) create mode 100644 OsmAnd/src/net/osmand/plus/mapcontextmenu/editors/IconCategoriesAdapter.java diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/editors/IconCategoriesAdapter.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/editors/IconCategoriesAdapter.java new file mode 100644 index 0000000000..9f53b0f49c --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/editors/IconCategoriesAdapter.java @@ -0,0 +1,107 @@ +package net.osmand.plus.mapcontextmenu.editors; + +import android.graphics.drawable.GradientDrawable; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.LinearLayout; +import android.widget.TextView; + +import androidx.annotation.NonNull; +import androidx.appcompat.content.res.AppCompatResources; +import androidx.core.content.ContextCompat; +import androidx.recyclerview.widget.RecyclerView; + +import net.osmand.AndroidUtils; +import net.osmand.plus.OsmandApplication; +import net.osmand.plus.R; + +import java.util.List; + +import static net.osmand.util.Algorithms.capitalizeFirstLetter; + + +public class IconCategoriesAdapter extends RecyclerView.Adapter { + + private List items; + private OsmandApplication app; + private boolean nightMode; + private IconCategoriesAdapterListener listenerCategory; + + public IconCategoriesAdapter(OsmandApplication app) { + this.app = app; + nightMode = app.getDaynightHelper().isNightModeForMapControls(); + } + + public void setItems(List items) { + this.items = items; + } + + @NonNull + @Override + public NameViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + View view; + view = LayoutInflater.from(parent.getContext()).inflate(R.layout.point_editor_icon_category_item, parent, false); + return new NameViewHolder(view); + } + + @Override + public void onBindViewHolder(@NonNull NameViewHolder holder, final int position) { + final String category = items.get(holder.getAdapterPosition()); + TextView textView = holder.buttonText; + int activeColorResId = nightMode ? R.color.active_color_primary_dark : R.color.active_color_primary_light; + if (category.equals(listenerCategory.getSelectedItem())) { + AndroidUtils.setBackground(holder.button, app.getUIUtilities().getPaintedIcon(R.drawable.bg_select_icon_group_button, + ContextCompat.getColor(app, activeColorResId))); + textView.setTextColor(ContextCompat.getColor(app, R.color.color_white)); + } else { + textView.setTextColor(ContextCompat.getColor(app, R.color.preference_category_title)); + GradientDrawable buttonBackground = (GradientDrawable) AppCompatResources.getDrawable(app, + R.drawable.bg_select_icon_group_button).mutate(); + buttonBackground.setStroke(AndroidUtils.dpToPx(app, 1), ContextCompat.getColor(app, + nightMode ? R.color.stroked_buttons_and_links_outline_dark + : R.color.stroked_buttons_and_links_outline_light)); + buttonBackground.setColor(ContextCompat.getColor(app, R.color.color_transparent)); + AndroidUtils.setBackground(holder.button, buttonBackground); + } + textView.setText(capitalizeFirstLetter(category)); + holder.button.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if (listenerCategory != null) { + listenerCategory.onItemClick(category); + } + } + }); + } + + @Override + public int getItemCount() { + return items.size(); + } + + int getItemPosition(String name) { + return items.indexOf(name); + } + + public void setListenerCategory(IconCategoriesAdapterListener listenerCategory) { + this.listenerCategory = listenerCategory; + } + + public interface IconCategoriesAdapterListener { + void onItemClick(String item); + + String getSelectedItem(); + } + + static class NameViewHolder extends RecyclerView.ViewHolder { + final TextView buttonText; + final LinearLayout button; + + NameViewHolder(@NonNull View itemView) { + super(itemView); + buttonText = itemView.findViewById(R.id.button_text); + button = itemView.findViewById(R.id.button_container); + } + } +} diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/editors/PointEditorFragmentNew.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/editors/PointEditorFragmentNew.java index 80f6889114..08c8473b75 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/editors/PointEditorFragmentNew.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/editors/PointEditorFragmentNew.java @@ -19,7 +19,6 @@ import android.view.inputmethod.InputMethodManager; import android.widget.EditText; import android.widget.FrameLayout; import android.widget.ImageView; -import android.widget.LinearLayout; import android.widget.ScrollView; import android.widget.TextView; @@ -63,7 +62,6 @@ import java.util.Set; import static net.osmand.data.FavouritePoint.*; import static net.osmand.plus.FavouritesDbHelper.FavoriteGroup.PERSONAL_CATEGORY; import static net.osmand.plus.FavouritesDbHelper.FavoriteGroup.isPersonalCategoryDisplayName; -import static net.osmand.util.Algorithms.capitalizeFirstLetter; public abstract class PointEditorFragmentNew extends BaseOsmAndFragment { @@ -463,8 +461,21 @@ public abstract class PointEditorFragmentNew extends BaseOsmAndFragment { e.printStackTrace(); } } - IconCategoriesAdapter iconCategoriesAdapter = new IconCategoriesAdapter(); + IconCategoriesAdapter iconCategoriesAdapter = new IconCategoriesAdapter(app); iconCategoriesAdapter.setItems(new ArrayList<>(iconCategories.keySet())); + iconCategoriesAdapter.setListenerCategory(new IconCategoriesAdapter.IconCategoriesAdapterListener() { + @Override + public void onItemClick(String item) { + selectedIconCategory = item; + createIconForCategory(); + updateIconSelector(selectedIcon, PointEditorFragmentNew.this.view); + } + + @Override + public String getSelectedItem() { + return selectedIconCategory; + } + }); RecyclerView iconCategoriesRecyclerView = view.findViewById(R.id.group_name_recycler_view); iconCategoriesRecyclerView.setAdapter(iconCategoriesAdapter); iconCategoriesRecyclerView.setLayoutManager(new LinearLayoutManager(app, RecyclerView.HORIZONTAL, false)); @@ -894,71 +905,4 @@ public abstract class PointEditorFragmentNew extends BaseOsmAndFragment { groupButton = itemView.findViewById(R.id.outlineRect); } } - - class IconCategoriesAdapter extends RecyclerView.Adapter { - - List items; - - public void setItems(List items) { - this.items = items; - } - - @NonNull - @Override - public NameViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { - View view; - view = LayoutInflater.from(parent.getContext()).inflate(R.layout.point_editor_icon_category_item, parent, false); - return new NameViewHolder(view); - } - - @Override - public void onBindViewHolder(@NonNull NameViewHolder holder, final int position) { - final String category = items.get(position); - TextView textView = holder.buttonText; - int activeColorResId = nightMode ? R.color.active_color_primary_dark : R.color.active_color_primary_light; - if (category.equals(selectedIconCategory)) { - AndroidUtils.setBackground(holder.button, app.getUIUtilities().getPaintedIcon(R.drawable.bg_select_icon_group_button, - ContextCompat.getColor(app, activeColorResId))); - textView.setTextColor(ContextCompat.getColor(app, R.color.color_white)); - } else { - textView.setTextColor(ContextCompat.getColor(app, R.color.preference_category_title)); - GradientDrawable buttonBackground = (GradientDrawable) AppCompatResources.getDrawable(app, - R.drawable.bg_select_icon_group_button).mutate(); - buttonBackground.setStroke(AndroidUtils.dpToPx(app, 1), ContextCompat.getColor(app, - nightMode ? R.color.stroked_buttons_and_links_outline_dark - : R.color.stroked_buttons_and_links_outline_light)); - buttonBackground.setColor(ContextCompat.getColor(app, R.color.color_transparent)); - AndroidUtils.setBackground(holder.button, buttonBackground); - } - textView.setText(capitalizeFirstLetter(category)); - holder.button.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - selectedIconCategory = category; - createIconForCategory(); - updateIconSelector(selectedIcon, PointEditorFragmentNew.this.view); - } - }); - } - - @Override - public int getItemCount() { - return items.size(); - } - - int getItemPosition(String name) { - return items.indexOf(name); - } - } - - static class NameViewHolder extends RecyclerView.ViewHolder { - final TextView buttonText; - final LinearLayout button; - - NameViewHolder(@NonNull View itemView) { - super(itemView); - buttonText = itemView.findViewById(R.id.button_text); - button = itemView.findViewById(R.id.button_container); - } - } }