From 95e522b1ee9f21b64d73ea24c2bf565baf200755 Mon Sep 17 00:00:00 2001 From: madwasp79 Date: Mon, 27 May 2019 13:50:28 +0300 Subject: [PATCH] add select app profile bottom sheet to rp menu --- .../bottom_sheet_select_type_fragment.xml | 27 +---- .../AppModesBottomSheetDialogFragment.java | 109 ++++++++++++++++++ .../plus/profiles/ProfileMenuAdapter.java | 4 + ...electProfileBottomSheetDialogFragment.java | 54 --------- .../MapRouteInfoMenu.java | 10 +- 5 files changed, 116 insertions(+), 88 deletions(-) create mode 100644 OsmAnd/src/net/osmand/plus/profiles/AppModesBottomSheetDialogFragment.java diff --git a/OsmAnd/res/layout/bottom_sheet_select_type_fragment.xml b/OsmAnd/res/layout/bottom_sheet_select_type_fragment.xml index b5b6b1de29..6ad12162d3 100644 --- a/OsmAnd/res/layout/bottom_sheet_select_type_fragment.xml +++ b/OsmAnd/res/layout/bottom_sheet_select_type_fragment.xml @@ -7,33 +7,8 @@ android:background="?attr/bg_color" android:orientation="vertical"> - - - - diff --git a/OsmAnd/src/net/osmand/plus/profiles/AppModesBottomSheetDialogFragment.java b/OsmAnd/src/net/osmand/plus/profiles/AppModesBottomSheetDialogFragment.java new file mode 100644 index 0000000000..4eab6c5472 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/profiles/AppModesBottomSheetDialogFragment.java @@ -0,0 +1,109 @@ +package net.osmand.plus.profiles; + +import static net.osmand.plus.profiles.SettingsProfileFragment.IS_USER_PROFILE; +import static net.osmand.plus.profiles.SettingsProfileFragment.PROFILE_STRING_KEY; + +import android.content.Intent; +import android.os.Bundle; +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; +import android.util.TypedValue; +import android.view.ContextThemeWrapper; +import android.view.View; +import android.widget.TextView; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import net.osmand.plus.ApplicationMode; +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.util.Algorithms; + +public class AppModesBottomSheetDialogFragment extends MenuBottomSheetDialogFragment { + + private List allModes = new ArrayList<>(); + private Set selectedModes = new HashSet<>(); + + protected boolean nightMode; + + private ProfileMenuAdapter adapter; + private RecyclerView recyclerView; + + private ProfileListener listener; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + allModes.addAll(ApplicationMode.allPossibleValues()); + allModes.remove(ApplicationMode.DEFAULT); + selectedModes.addAll(ApplicationMode.values(getMyApplication())); + selectedModes.remove(ApplicationMode.DEFAULT); + } + + @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); + } + 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); + } + startActivity(intent); + } + }; + + adapter.setListener(listener); + } + + + + @Override + public void createMenuItems(Bundle savedInstanceState) { + + adapter = new ProfileMenuAdapter(allModes, selectedModes, getMyApplication(), listener); + + recyclerView = new RecyclerView(getContext()); + final int themeRes = nightMode ? R.style.OsmandDarkTheme : R.style.OsmandLightTheme; + recyclerView = (RecyclerView) View + .inflate(new ContextThemeWrapper(getContext(), themeRes), R.layout.recyclerview, null); + recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); + recyclerView.setAdapter(adapter); + + final View textButtonView = View.inflate(new ContextThemeWrapper(getContext(), themeRes), + R.layout.bottom_sheet_item_simple, null); + TextView textView = (TextView) textButtonView.findViewById(R.id.title); + textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16.0f); + textView.setTextColor(nightMode + ? getResources().getColor(R.color.active_buttons_and_links_dark) + : getResources().getColor(R.color.active_buttons_and_links_light)); + textView.setText(R.string.shared_string_manage); + + items.add(new TitleItem(getString(R.string.application_profiles))); + items.add(new BaseBottomSheetItem.Builder().setCustomView(recyclerView).create()); + items.add(new BaseBottomSheetItem.Builder().setCustomView(textButtonView) + .setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + startActivity(new Intent(getContext(), SettingsProfileActivity.class)); + } + }) + .create()); + } +} diff --git a/OsmAnd/src/net/osmand/plus/profiles/ProfileMenuAdapter.java b/OsmAnd/src/net/osmand/plus/profiles/ProfileMenuAdapter.java index b408ce98b0..b7f451004b 100644 --- a/OsmAnd/src/net/osmand/plus/profiles/ProfileMenuAdapter.java +++ b/OsmAnd/src/net/osmand/plus/profiles/ProfileMenuAdapter.java @@ -47,6 +47,10 @@ public class ProfileMenuAdapter extends RecyclerView.Adapter notifyDataSetChanged(); } + public void setListener(ProfileListener listener) { + this.listener = listener; + } + public void updateItemsList(List newList, Set selectedItems) { items.clear(); this.selectedItems.clear(); diff --git a/OsmAnd/src/net/osmand/plus/profiles/SelectProfileBottomSheetDialogFragment.java b/OsmAnd/src/net/osmand/plus/profiles/SelectProfileBottomSheetDialogFragment.java index 178e2cd919..8d4197dd42 100644 --- a/OsmAnd/src/net/osmand/plus/profiles/SelectProfileBottomSheetDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/profiles/SelectProfileBottomSheetDialogFragment.java @@ -35,7 +35,6 @@ public class SelectProfileBottomSheetDialogFragment extends MenuBottomSheetDialo public final static String TYPE_BASE_APP_PROFILE = "base_profiles"; public final static String TYPE_NAV_PROFILE = "routing_profiles"; public final static String TYPE_ICON = "icon_type"; - public final static String TYPE_APP_PROFILES = "app_profiles"; public final static String SELECTED_KEY = "selected_base"; String type; @@ -66,12 +65,6 @@ public class SelectProfileBottomSheetDialogFragment extends MenuBottomSheetDialo } else if (type.equals(TYPE_ICON)) { selectedIconRes = args.getInt(SELECTED_ICON, -1); icons = getProfileIcons(); - } else if (type.equals(TYPE_APP_PROFILES)){ - appModes.addAll(ApplicationMode.allPossibleValues()); - appModes.remove(0); - activeAppModes.addAll(ApplicationMode.values(app)); - activeAppModes.remove(0); - bottomButtonText = R.string.shared_string_close; } else { LOG.error("Check intent data!"); dismiss(); @@ -179,53 +172,6 @@ public class SelectProfileBottomSheetDialogFragment extends MenuBottomSheetDialo }) .create()); } - } else if (type.equals(TYPE_APP_PROFILES)) { - items.add(new TitleItem(getString(R.string.application_profiles))); - for (int i = 0; i < appModes.size(); i++) { - final int pos = i; - final ApplicationMode mode = appModes.get(i); - final boolean[] isSelected = {activeAppModes.contains(mode)}; - final Drawable icon; - if (isSelected[0]) { - icon = getMyApplication().getUIUtilities().getIcon(mode.getSmallIconDark(), nightMode - ? R.color.active_buttons_and_links_dark - : R.color.active_buttons_and_links_light); - } else { - icon = getMyApplication().getUIUtilities().getIcon( - mode.getSmallIconDark(), R.color.icon_color); - } - items.add(new BottomSheetItemWithCompoundButton.Builder() - .setChecked(isSelected[0]) - .setDescription(mode.getParent() == null - ? getString(R.string.profile_type_base_string) - : String.format(getString(R.string.profile_type_descr_string), - getString(mode.getParent().getStringResource()))) - .setTitle(mode.getParent() == null - ? getString(mode.getStringResource()) - : mode.getUserProfileName()) - .setIcon(icon) - .setLayoutId(R.layout.profile_list_item) - .setOnClickListener(new OnClickListener() { - @Override - public void onClick(View v) { - if (getView() != null) { - ((SwitchCompat) getView().findViewById(R.id.compound_button)).toggle(); - } - if (!isSelected[0]) { - activeAppModes.add(mode); - } else { - activeAppModes.remove(mode); - } - ApplicationMode - .changeProfileStatus(mode, isSelected[0], getMyApplication()); - boolean status = !isSelected[0]; - isSelected[0] = status; - } - }) - .create() - - ); - } } else if (type.equals(TYPE_ICON)) { items.add(new TitleItem(getString(R.string.select_icon_profile_dialog_title))); for (final IconResWithDescr icon : icons) { diff --git a/OsmAnd/src/net/osmand/plus/routepreparationmenu/MapRouteInfoMenu.java b/OsmAnd/src/net/osmand/plus/routepreparationmenu/MapRouteInfoMenu.java index 50ef72b778..b2e0ec2f76 100644 --- a/OsmAnd/src/net/osmand/plus/routepreparationmenu/MapRouteInfoMenu.java +++ b/OsmAnd/src/net/osmand/plus/routepreparationmenu/MapRouteInfoMenu.java @@ -1,7 +1,5 @@ package net.osmand.plus.routepreparationmenu; -import static net.osmand.plus.profiles.SelectProfileBottomSheetDialogFragment.DIALOG_TYPE; -import static net.osmand.plus.profiles.SelectProfileBottomSheetDialogFragment.TYPE_APP_PROFILES; import android.app.Activity; import android.content.Context; @@ -10,7 +8,6 @@ import android.content.DialogInterface.OnDismissListener; import android.graphics.PointF; import android.graphics.drawable.Drawable; import android.os.Build; -import android.os.Bundle; import android.os.Handler; import android.support.annotation.DrawableRes; import android.support.annotation.NonNull; @@ -65,7 +62,7 @@ import net.osmand.plus.helpers.GpxUiHelper; import net.osmand.plus.helpers.WaypointHelper; import net.osmand.plus.mapmarkers.MapMarkerSelectionFragment; import net.osmand.plus.poi.PoiUIFilter; -import net.osmand.plus.profiles.SelectProfileBottomSheetDialogFragment; +import net.osmand.plus.profiles.AppModesBottomSheetDialogFragment; import net.osmand.plus.routepreparationmenu.RoutingOptionsHelper.AvoidPTTypesRoutingParameter; import net.osmand.plus.routepreparationmenu.RoutingOptionsHelper.AvoidRoadsRoutingParameter; import net.osmand.plus.routepreparationmenu.RoutingOptionsHelper.LocalRoutingParameter; @@ -751,10 +748,7 @@ public class MapRouteInfoMenu implements IRouteInformationListener, CardListener } private void showProfileBottomSheetDialog(Activity activity) { - final SelectProfileBottomSheetDialogFragment fragment = new SelectProfileBottomSheetDialogFragment(); - Bundle bundle = new Bundle(); - bundle.putString(DIALOG_TYPE, TYPE_APP_PROFILES); - fragment.setArguments(bundle); + final AppModesBottomSheetDialogFragment fragment = new AppModesBottomSheetDialogFragment(); getMapActivity().getSupportFragmentManager().beginTransaction() .add(fragment, "app_profile_settings").commitAllowingStateLoss();