diff --git a/OsmAnd/res/layout/profile_list_item.xml b/OsmAnd/res/layout/profile_list_item.xml index eec9ff448d..cf08529e6d 100644 --- a/OsmAnd/res/layout/profile_list_item.xml +++ b/OsmAnd/res/layout/profile_list_item.xml @@ -1,104 +1,96 @@ + xmlns:tools="http://schemas.android.com/tools" + android:id="@+id/profile_settings" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:background="?attr/selectableItemBackground" + android:descendantFocusability="blocksDescendants" + android:gravity="center" + android:orientation="horizontal"> - + - + + - + - + - + - - + - - android:orientation="horizontal"> + - + - - + + - + - - - - + \ No newline at end of file diff --git a/OsmAnd/res/values/sizes.xml b/OsmAnd/res/values/sizes.xml index 2b6b24f3b3..3cf69a10d3 100644 --- a/OsmAnd/res/values/sizes.xml +++ b/OsmAnd/res/values/sizes.xml @@ -343,9 +343,7 @@ 3dp 4dp - 64dp - 24dp 18dp \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/profiles/AppModesBottomSheetDialogFragment.java b/OsmAnd/src/net/osmand/plus/profiles/AppModesBottomSheetDialogFragment.java index f9715e1e5e..8a676783c2 100644 --- a/OsmAnd/src/net/osmand/plus/profiles/AppModesBottomSheetDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/profiles/AppModesBottomSheetDialogFragment.java @@ -23,11 +23,12 @@ import java.util.List; import java.util.Set; import net.osmand.AndroidUtils; import net.osmand.plus.ApplicationMode; +import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.base.MenuBottomSheetDialogFragment; import net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem; import net.osmand.plus.base.bottomsheetmenu.simpleitems.TitleItem; -import net.osmand.plus.profiles.ProfileMenuAdapter.ProfileListener; +import net.osmand.plus.profiles.ProfileMenuAdapter.ProfileMenuAdapterListener; import net.osmand.util.Algorithms; public class AppModesBottomSheetDialogFragment extends MenuBottomSheetDialogFragment { @@ -40,7 +41,7 @@ public class AppModesBottomSheetDialogFragment extends MenuBottomSheetDialogFrag private ProfileMenuAdapter adapter; private RecyclerView recyclerView; - private ProfileListener listener; + private ProfileMenuAdapterListener listener; private UpdateMapRouteMenuListener updateMapRouteMenuListener; @Override @@ -55,7 +56,7 @@ public class AppModesBottomSheetDialogFragment extends MenuBottomSheetDialogFrag public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) { themeRes = nightMode ? R.style.OsmandDarkTheme : R.style.OsmandLightTheme; - adapter = new ProfileMenuAdapter(allModes, selectedModes, getMyApplication(), true); + adapter = new ProfileMenuAdapter(allModes, selectedModes, getMyApplication(), getString(R.string.shared_string_manage)); recyclerView = new RecyclerView(getContext()); recyclerView = (RecyclerView) View .inflate(new ContextThemeWrapper(getContext(), themeRes), R.layout.recyclerview, null); @@ -75,27 +76,37 @@ public class AppModesBottomSheetDialogFragment extends MenuBottomSheetDialogFrag @Override public void onResume() { super.onResume(); - listener = new ProfileListener() { - @Override - public void changeProfileStatus(ApplicationMode item, boolean isSelected) { - if (isSelected) { - selectedModes.add(item); - } else { - selectedModes.remove(item); + if (listener == null) { + listener = new ProfileMenuAdapterListener() { + @Override + public void onProfileSelected(ApplicationMode item, boolean selected) { + if (selected) { + selectedModes.add(item); + } else { + selectedModes.remove(item); + } + ApplicationMode.changeProfileStatus(item, selected, getMyApplication()); } - ApplicationMode.changeProfileStatus(item, isSelected, getMyApplication()); - } - @Override - public void editProfile(ApplicationMode item) { - Intent intent = new Intent(getActivity(), EditProfileActivity.class); - intent.putExtra(PROFILE_STRING_KEY, item.getStringKey()); - if (!Algorithms.isEmpty(item.getUserProfileName())) { - intent.putExtra(IS_USER_PROFILE, true); + @Override + public void onProfilePressed(ApplicationMode item) { + Intent intent = new Intent(getActivity(), EditProfileActivity.class); + intent.putExtra(PROFILE_STRING_KEY, item.getStringKey()); + if (!Algorithms.isEmpty(item.getUserProfileName())) { + intent.putExtra(IS_USER_PROFILE, true); + } + startActivity(intent); } - startActivity(intent); - } - }; + + @Override + public void onButtonPressed() { + OsmandApplication app = requiredMyApplication(); + Intent intent = new Intent(app, SettingsProfileActivity.class); + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + app.startActivity(intent); + } + }; + } adapter.setListener(listener); allModes = ApplicationMode.allPossibleValues(); allModes.remove(ApplicationMode.DEFAULT); diff --git a/OsmAnd/src/net/osmand/plus/profiles/ProfileMenuAdapter.java b/OsmAnd/src/net/osmand/plus/profiles/ProfileMenuAdapter.java index 950c87d56b..eae1caffc6 100644 --- a/OsmAnd/src/net/osmand/plus/profiles/ProfileMenuAdapter.java +++ b/OsmAnd/src/net/osmand/plus/profiles/ProfileMenuAdapter.java @@ -1,14 +1,17 @@ package net.osmand.plus.profiles; -import android.content.Intent; import android.support.annotation.ColorRes; import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.SwitchCompat; import android.view.LayoutInflater; +import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; +import android.widget.CompoundButton; +import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; @@ -26,22 +29,25 @@ public class ProfileMenuAdapter extends RecyclerView.Adapter private List items = new ArrayList<>(); private Set selectedItems; - private ProfileListener listener; + @Nullable + private ProfileMenuAdapterListener listener; private final OsmandApplication app; @ColorRes private int selectedIconColorRes; - private boolean isBottomSheet = false; - private static final String MANAGE_BTN = "manage_button"; + private boolean bottomButton; + private String bottomButtonText; + private static final String BUTTON_ITEM = "button_item"; public ProfileMenuAdapter(List items, Set selectedItems, - OsmandApplication app, boolean isBottomSheet) { + OsmandApplication app, String bottomButtonText) { this.items.addAll(items); - if (isBottomSheet) { - this.items.add(MANAGE_BTN); + if (bottomButton) { + this.items.add(BUTTON_ITEM); } this.app = app; this.selectedItems = selectedItems; - this.isBottomSheet = isBottomSheet; + this.bottomButton = !Algorithms.isEmpty(bottomButtonText); + this.bottomButtonText = bottomButtonText; selectedIconColorRes = isNightMode(app) ? R.color.active_buttons_and_links_dark : R.color.active_buttons_and_links_light; @@ -51,12 +57,7 @@ public class ProfileMenuAdapter extends RecyclerView.Adapter return items; } - public void addItem(ApplicationMode profileItem) { - items.add(profileItem); - notifyDataSetChanged(); - } - - public void setListener(ProfileListener listener) { + public void setListener(@Nullable ProfileMenuAdapterListener listener) { this.listener = listener; } @@ -64,8 +65,8 @@ public class ProfileMenuAdapter extends RecyclerView.Adapter this.items.clear(); this.selectedItems.clear(); this.items.addAll(newList); - if (isBottomSheet) { - items.add(MANAGE_BTN); + if (bottomButton) { + items.add(BUTTON_ITEM); } this.selectedItems.addAll(selectedItems); notifyDataSetChanged(); @@ -79,55 +80,22 @@ public class ProfileMenuAdapter extends RecyclerView.Adapter @NonNull @Override public ProfileViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { - View itemView = LayoutInflater.from(parent.getContext()) - .inflate(R.layout.profile_list_item, parent, false); - final ProfileViewHolder holder = new ProfileViewHolder(itemView); - holder.itemView.findViewById(R.id.compound_button).setOnClickListener( - new OnClickListener() { - @Override - public void onClick(View v) { - int pos = holder.getAdapterPosition(); - if ( pos != RecyclerView.NO_POSITION) { - Object o = items.get(pos); - if (o instanceof ApplicationMode) { - final ApplicationMode item = (ApplicationMode) o; - int iconRes = item.getParent() == null - ? item.getSmallIconDark() - : item.getIconRes(app); - - if (iconRes == 0 || iconRes == -1) { - iconRes = R.drawable.ic_action_world_globe; - } - - if (!selectedItems.contains(item)) { - selectedItems.add(item); - holder.icon.setImageDrawable( - app.getUIUtilities().getIcon(iconRes, selectedIconColorRes)); - } else { - selectedItems.remove(item); - holder.icon.setImageDrawable( - app.getUIUtilities().getIcon(iconRes, R.color.icon_color)); - } - listener.changeProfileStatus(item, holder.aSwitch.isChecked()); - } - } - } - }); - return holder; + View itemView = + LayoutInflater.from(parent.getContext()).inflate(R.layout.profile_list_item, parent, false); + return new ProfileViewHolder(itemView); } @Override public void onBindViewHolder(@NonNull final ProfileViewHolder holder, int position) { Object obj = items.get(position); - if (obj instanceof ApplicationMode) { holder.divider.setVisibility(View.VISIBLE); holder.icon.setVisibility(View.VISIBLE); holder.descr.setVisibility(View.VISIBLE); - holder.aSwitch.setVisibility(View.VISIBLE); + holder.switcher.setVisibility(View.VISIBLE); holder.menuIcon.setVisibility(View.VISIBLE); final ApplicationMode item = (ApplicationMode) obj; - if (isBottomSheet) { + if (bottomButton) { holder.divider.setBackgroundColor(isNightMode(app) ? app.getResources().getColor(R.color.divider_dark) : app.getResources().getColor(R.color.divider_light)); @@ -147,45 +115,21 @@ public class ProfileMenuAdapter extends RecyclerView.Adapter ? R.color.main_font_dark : R.color.main_font_light)); - int iconRes = item.getParent() == null - ? item.getSmallIconDark() - : item.getIconRes(app); - - if (iconRes == 0 || iconRes == -1) { - iconRes = R.drawable.ic_action_world_globe; - } - - if (selectedItems.contains(item)) { - holder.aSwitch.setChecked(true); - holder.icon - .setImageDrawable(app.getUIUtilities().getIcon(iconRes, selectedIconColorRes)); - } else { - holder.aSwitch.setChecked(false); - holder.icon.setImageDrawable(app.getUIUtilities().getIcon(iconRes, R.color.icon_color)); - } - - holder.profileOptions.setOnClickListener(new OnClickListener() { - @Override - public void onClick(View v) { - listener.editProfile(item); - } - }); + holder.initSwitcher = true; + holder.switcher.setChecked(selectedItems.contains(item)); + holder.initSwitcher = false; + updateViewHolder(holder, item); } else { final String title = (String) obj; - if (title.equals("manage_button")) - holder.divider.setVisibility(View.INVISIBLE); + if (title.equals(BUTTON_ITEM)) { + holder.divider.setVisibility(View.INVISIBLE); + } holder.icon.setVisibility(View.INVISIBLE); holder.descr.setVisibility(View.GONE); - holder.aSwitch.setVisibility(View.GONE); + holder.switcher.setVisibility(View.GONE); holder.menuIcon.setVisibility(View.GONE); holder.title.setTextColor(app.getResources().getColor(selectedIconColorRes)); - holder.title.setText(R.string.shared_string_manage); - holder.profileOptions.setOnClickListener(new OnClickListener() { - @Override - public void onClick(View v) { - app.startActivity(new Intent(app, SettingsProfileActivity.class)); - } - }); + holder.title.setText(bottomButtonText); } } @@ -194,6 +138,18 @@ public class ProfileMenuAdapter extends RecyclerView.Adapter return items.size(); } + private void updateViewHolder(ProfileViewHolder holder, ApplicationMode mode) { + int iconRes = mode.getParent() == null ? mode.getSmallIconDark() : mode.getIconRes(app); + if (iconRes == 0 || iconRes == -1) { + iconRes = R.drawable.ic_action_world_globe; + } + if (selectedItems.contains(mode)) { + holder.icon.setImageDrawable(app.getUIUtilities().getIcon(iconRes, selectedIconColorRes)); + } else { + holder.icon.setImageDrawable(app.getUIUtilities().getIcon(iconRes, R.color.icon_color)); + } + } + private static boolean isNightMode(OsmandApplication ctx) { return !ctx.getSettings().isLightContent(); } @@ -201,28 +157,65 @@ public class ProfileMenuAdapter extends RecyclerView.Adapter class ProfileViewHolder extends RecyclerView.ViewHolder { TextView title, descr; - SwitchCompat aSwitch; + SwitchCompat switcher; ImageView icon, menuIcon; LinearLayout profileOptions; View divider; + boolean initSwitcher; + ProfileViewHolder(View itemView) { super(itemView); title = itemView.findViewById(R.id.title); descr = itemView.findViewById(R.id.description); - aSwitch = itemView.findViewById(R.id.compound_button); + switcher = itemView.findViewById(R.id.compound_button); icon = itemView.findViewById(R.id.icon); profileOptions = itemView.findViewById(R.id.profile_settings); divider = itemView.findViewById(R.id.divider_bottom); menuIcon = itemView.findViewById(R.id.menu_image); + + profileOptions.setOnClickListener(new OnClickListener() { + @Override + public void onClick(View v) { + int pos = getAdapterPosition(); + if (pos != RecyclerView.NO_POSITION && listener != null) { + Object o = items.get(pos); + if (o instanceof ApplicationMode) { + listener.onProfilePressed((ApplicationMode) o); + } else { + listener.onButtonPressed(); + } + } + } + }); + switcher.setOnCheckedChangeListener(new OnCheckedChangeListener() { + @Override + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + int pos = getAdapterPosition(); + if (pos != RecyclerView.NO_POSITION && listener != null && !initSwitcher) { + Object o = items.get(pos); + if (o instanceof ApplicationMode) { + final ApplicationMode item = (ApplicationMode) o; + if (isChecked) { + selectedItems.add(item); + } else { + selectedItems.remove(item); + } + updateViewHolder(ProfileViewHolder.this, item); + listener.onProfileSelected(item, isChecked); + } + } + } + }); } } - public interface ProfileListener { + public interface ProfileMenuAdapterListener { - void changeProfileStatus(ApplicationMode item, boolean isSelected); + void onProfileSelected(ApplicationMode item, boolean selected); - void editProfile(ApplicationMode item); + void onProfilePressed(ApplicationMode item); + + void onButtonPressed(); } } - diff --git a/OsmAnd/src/net/osmand/plus/profiles/SettingsProfileFragment.java b/OsmAnd/src/net/osmand/plus/profiles/SettingsProfileFragment.java index 03840efa8a..4fab17d00c 100644 --- a/OsmAnd/src/net/osmand/plus/profiles/SettingsProfileFragment.java +++ b/OsmAnd/src/net/osmand/plus/profiles/SettingsProfileFragment.java @@ -25,7 +25,7 @@ import net.osmand.PlatformUtil; import net.osmand.plus.ApplicationMode; import net.osmand.plus.R; import net.osmand.plus.base.BaseOsmAndFragment; -import net.osmand.plus.profiles.ProfileMenuAdapter.ProfileListener; +import net.osmand.plus.profiles.ProfileMenuAdapter.ProfileMenuAdapterListener; import net.osmand.plus.profiles.SelectProfileBottomSheetDialogFragment.SelectProfileListener; import net.osmand.util.Algorithms; import org.apache.commons.logging.Log; @@ -43,7 +43,7 @@ public class SettingsProfileFragment extends BaseOsmAndFragment { private RecyclerView recyclerView; private LinearLayout addNewProfileBtn; - ProfileListener listener = null; + ProfileMenuAdapterListener listener = null; SelectProfileListener typeListener = null; private List allAppModes; @@ -85,7 +85,7 @@ public class SettingsProfileFragment extends BaseOsmAndFragment { } }); - adapter = new ProfileMenuAdapter(allAppModes, availableAppModes, getMyApplication(), false); + adapter = new ProfileMenuAdapter(allAppModes, availableAppModes, getMyApplication(), null); recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); recyclerView.setAdapter(adapter); return view; @@ -95,19 +95,19 @@ public class SettingsProfileFragment extends BaseOsmAndFragment { public void onResume() { super.onResume(); if (listener == null) { - listener = new ProfileListener() { + listener = new ProfileMenuAdapterListener() { @Override - public void changeProfileStatus(ApplicationMode am, boolean isSelected) { - if(isSelected) { + public void onProfileSelected(ApplicationMode am, boolean selected) { + if(selected) { availableAppModes.add(am); } else { availableAppModes.remove(am); } - ApplicationMode.changeProfileStatus(am, isSelected, getMyApplication()); + ApplicationMode.changeProfileStatus(am, selected, getMyApplication()); } @Override - public void editProfile(ApplicationMode item) { + public void onProfilePressed(ApplicationMode item) { Intent intent = new Intent(getActivity(), EditProfileActivity.class); intent.putExtra(PROFILE_STRING_KEY, item.getStringKey()); if (!Algorithms.isEmpty(item.getUserProfileName())) { @@ -115,6 +115,10 @@ public class SettingsProfileFragment extends BaseOsmAndFragment { } startActivity(intent); } + + @Override + public void onButtonPressed() { + } }; } adapter.setListener(listener);