diff --git a/OsmAnd/src/net/osmand/plus/profiles/AppModesBottomSheetDialogFragment.java b/OsmAnd/src/net/osmand/plus/profiles/AppModesBottomSheetDialogFragment.java index dc3a0bfb97..f9715e1e5e 100644 --- a/OsmAnd/src/net/osmand/plus/profiles/AppModesBottomSheetDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/profiles/AppModesBottomSheetDialogFragment.java @@ -55,7 +55,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(), listener, true); + adapter = new ProfileMenuAdapter(allModes, selectedModes, getMyApplication(), true); recyclerView = new RecyclerView(getContext()); recyclerView = (RecyclerView) View .inflate(new ContextThemeWrapper(getContext(), themeRes), R.layout.recyclerview, null); diff --git a/OsmAnd/src/net/osmand/plus/profiles/ProfileMenuAdapter.java b/OsmAnd/src/net/osmand/plus/profiles/ProfileMenuAdapter.java index 2712de65d1..d4a35469dc 100644 --- a/OsmAnd/src/net/osmand/plus/profiles/ProfileMenuAdapter.java +++ b/OsmAnd/src/net/osmand/plus/profiles/ProfileMenuAdapter.java @@ -34,21 +34,11 @@ public class ProfileMenuAdapter extends RecyclerView.Adapter private static final String MANAGE_BTN = "manage_button"; public ProfileMenuAdapter(List items, Set selectedItems, - OsmandApplication app, ProfileListener listener) { + OsmandApplication app, boolean isBottomSheet) { this.items.addAll(items); - this.listener = listener; - this.app = app; - this.selectedItems = selectedItems; - selectedIconColorRes = isNightMode(app) - ? R.color.active_buttons_and_links_dark - : R.color.active_buttons_and_links_light; - } - - public ProfileMenuAdapter(List items, Set selectedItems, - OsmandApplication app, ProfileListener listener, boolean isBottomSheet) { - this.items.addAll(items); - this.items.add(MANAGE_BTN); - this.listener = listener; + if (isBottomSheet) { + this.items.add(MANAGE_BTN); + } this.app = app; this.selectedItems = selectedItems; this.isBottomSheet = isBottomSheet; @@ -71,9 +61,9 @@ public class ProfileMenuAdapter extends RecyclerView.Adapter } public void updateItemsList(List newList, Set selectedItems) { - items.clear(); + this.items.clear(); this.selectedItems.clear(); - items.addAll(newList); + this.items.addAll(newList); if (isBottomSheet) { items.add(MANAGE_BTN); } @@ -91,7 +81,35 @@ public class ProfileMenuAdapter extends RecyclerView.Adapter public ProfileViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View itemView = LayoutInflater.from(parent.getContext()) .inflate(R.layout.profile_list_item, parent, false); - return new ProfileViewHolder(itemView); + final ProfileViewHolder holder = new ProfileViewHolder(itemView); + + holder.itemView.findViewById(R.id.compound_button).setOnClickListener( + new OnClickListener() { + @Override + public void onClick(View v) { + Object o = items.get(holder.getAdapterPosition()); + 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; } @Override @@ -133,8 +151,6 @@ public class ProfileMenuAdapter extends RecyclerView.Adapter iconRes = R.drawable.ic_action_world_globe; } - final int ficon = iconRes; - if (selectedItems.contains(item)) { holder.aSwitch.setChecked(true); holder.icon @@ -144,19 +160,6 @@ public class ProfileMenuAdapter extends RecyclerView.Adapter holder.icon.setImageDrawable(app.getUIUtilities().getIcon(iconRes, R.color.icon_color)); } - holder.aSwitch.setOnClickListener(new OnClickListener() { - @Override - public void onClick(View v) { - listener.changeProfileStatus(item, holder.aSwitch.isChecked()); - if (selectedItems.contains(item)) { - holder.icon.setImageDrawable(app.getUIUtilities() - .getIcon(ficon, selectedIconColorRes)); - } else { - holder.icon.setImageDrawable( - app.getUIUtilities().getIcon(ficon, R.color.icon_color)); - } - } - }); holder.profileOptions.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { diff --git a/OsmAnd/src/net/osmand/plus/profiles/SettingsProfileFragment.java b/OsmAnd/src/net/osmand/plus/profiles/SettingsProfileFragment.java index cbe39c0a5b..03840efa8a 100644 --- a/OsmAnd/src/net/osmand/plus/profiles/SettingsProfileFragment.java +++ b/OsmAnd/src/net/osmand/plus/profiles/SettingsProfileFragment.java @@ -65,28 +65,6 @@ public class SettingsProfileFragment extends BaseOsmAndFragment { public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { - listener = new ProfileListener() { - @Override - public void changeProfileStatus(ApplicationMode am, boolean isSelected) { - if(isSelected) { - availableAppModes.add(am); - } else { - availableAppModes.remove(am); - } - ApplicationMode.changeProfileStatus(am, 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); - } - startActivity(intent); - } - }; - View view = inflater.inflate(R.layout.profiles_list_fragment, container, false); recyclerView = view.findViewById(R.id.profiles_list); addNewProfileBtn = view.findViewById(R.id.add_profile_btn); @@ -107,7 +85,7 @@ public class SettingsProfileFragment extends BaseOsmAndFragment { } }); - adapter = new ProfileMenuAdapter(allAppModes, availableAppModes, getMyApplication(), listener); + adapter = new ProfileMenuAdapter(allAppModes, availableAppModes, getMyApplication(), false); recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); recyclerView.setAdapter(adapter); return view; @@ -116,6 +94,30 @@ public class SettingsProfileFragment extends BaseOsmAndFragment { @Override public void onResume() { super.onResume(); + if (listener == null) { + listener = new ProfileListener() { + @Override + public void changeProfileStatus(ApplicationMode am, boolean isSelected) { + if(isSelected) { + availableAppModes.add(am); + } else { + availableAppModes.remove(am); + } + ApplicationMode.changeProfileStatus(am, 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); + } + startActivity(intent); + } + }; + } + adapter.setListener(listener); getBaseProfileListener();