From 33a552af84d911f785c18e55b0925f168f02c8aa Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Fri, 27 Dec 2019 17:36:23 +0200 Subject: [PATCH] Add copy profile preferences dialog --- .../profiles/ConfigureProfileMenuAdapter.java | 8 +- .../profiles/ProfileAbstractViewHolder.java | 6 +- .../SelectCopyAppModeBottomSheet.java | 131 ++++++++++++++++++ .../SelectCopyProfilesMenuAdapter.java | 101 ++++++++++++++ .../profiles/SelectProfileMenuAdapter.java | 4 +- .../settings/ConfigureProfileFragment.java | 13 +- 6 files changed, 244 insertions(+), 19 deletions(-) create mode 100644 OsmAnd/src/net/osmand/plus/profiles/SelectCopyAppModeBottomSheet.java create mode 100644 OsmAnd/src/net/osmand/plus/profiles/SelectCopyProfilesMenuAdapter.java diff --git a/OsmAnd/src/net/osmand/plus/profiles/ConfigureProfileMenuAdapter.java b/OsmAnd/src/net/osmand/plus/profiles/ConfigureProfileMenuAdapter.java index e9ce8cf877..a8870d7b86 100644 --- a/OsmAnd/src/net/osmand/plus/profiles/ConfigureProfileMenuAdapter.java +++ b/OsmAnd/src/net/osmand/plus/profiles/ConfigureProfileMenuAdapter.java @@ -100,14 +100,14 @@ public class ConfigureProfileMenuAdapter extends AbstractProfileMenuAdapter { + + public static final String TAG = "SelectCopyAppModeBottomSheet"; + + private static final String SELECTED_APP_MODE_KEY = "selected_app_mode_key"; + private static final String CURRENT_APP_MODE_KEY = "current_app_mode_key"; + + private static final Log LOG = PlatformUtil.getLog(SelectCopyAppModeBottomSheet.class); + + private List appModes = new ArrayList<>(); + + private ApplicationMode selectedAppMode; + private ApplicationMode currentAppMode; + + @Override + public void onCreate(Bundle savedInstanceState) { + Bundle args = getArguments(); + if (args != null && args.containsKey(CURRENT_APP_MODE_KEY)) { + currentAppMode = ApplicationMode.valueOfStringKey(args.getString(CURRENT_APP_MODE_KEY), null); + } else { + OsmandApplication app = requiredMyApplication(); + if (currentAppMode == null) { + currentAppMode = app.getSettings().getApplicationMode(); + } + } + super.onCreate(savedInstanceState); + if (savedInstanceState != null) { + selectedAppMode = ApplicationMode.valueOfStringKey(savedInstanceState.getString(SELECTED_APP_MODE_KEY), null); + } + } + + public ApplicationMode getSelectedAppMode() { + return selectedAppMode; + } + + @Override + protected void getData() { + appModes = new ArrayList<>(); + for (ApplicationMode mode : ApplicationMode.allPossibleValues()) { + if (mode != currentAppMode) { + appModes.add(mode); + } + } + } + + @Override + protected SelectCopyProfilesMenuAdapter getMenuAdapter() { + return new SelectCopyProfilesMenuAdapter(appModes, requiredMyApplication(), nightMode, selectedAppMode); + } + + @Override + protected String getTitle() { + return getString(R.string.copy_from_other_profile); + } + + @Override + public void onSaveInstanceState(Bundle outState) { + super.onSaveInstanceState(outState); + if (selectedAppMode != null) { + outState.putString(SELECTED_APP_MODE_KEY, selectedAppMode.getStringKey()); + } + } + + @Override + protected boolean isNightMode(@NonNull OsmandApplication app) { + if (usedOnMap) { + return app.getDaynightHelper().isNightModeForMapControlsForProfile(currentAppMode); + } else { + return !app.getSettings().isLightContentForMode(currentAppMode); + } + } + + @Override + public void onProfilePressed(ApplicationMode item) { + selectedAppMode = item; + } + + @Override + protected int getDismissButtonTextId() { + return R.string.shared_string_cancel; + } + + @Override + protected int getRightBottomButtonTextId() { + return R.string.shared_string_copy; + } + + @Override + protected void onRightBottomButtonClick() { + OsmandApplication app = getMyApplication(); + if (app != null && selectedAppMode != null) { + getMyApplication().getSettings().copyPreferencesFromProfile(selectedAppMode, currentAppMode); + } + dismiss(); + } + + public static void showInstance(@NonNull FragmentManager fm, Fragment target, boolean usedOnMap, + @NonNull ApplicationMode currentMode) { + try { + if (fm.findFragmentByTag(SelectCopyAppModeBottomSheet.TAG) == null) { + Bundle args = new Bundle(); + args.putString(CURRENT_APP_MODE_KEY, currentMode.getStringKey()); + + SelectCopyAppModeBottomSheet fragment = new SelectCopyAppModeBottomSheet(); + fragment.setTargetFragment(target, 0); + fragment.setUsedOnMap(usedOnMap); + fragment.setArguments(args); + fragment.show(fm, SelectCopyAppModeBottomSheet.TAG); + } + } catch (RuntimeException e) { + LOG.error("showInstance", e); + } + } +} \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/profiles/SelectCopyProfilesMenuAdapter.java b/OsmAnd/src/net/osmand/plus/profiles/SelectCopyProfilesMenuAdapter.java new file mode 100644 index 0000000000..2770eb2e4e --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/profiles/SelectCopyProfilesMenuAdapter.java @@ -0,0 +1,101 @@ +package net.osmand.plus.profiles; + +import android.graphics.drawable.ColorDrawable; +import android.graphics.drawable.Drawable; +import android.graphics.drawable.LayerDrawable; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.support.v4.content.ContextCompat; +import android.support.v7.widget.RecyclerView; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +import net.osmand.AndroidUtils; +import net.osmand.plus.ApplicationMode; +import net.osmand.plus.OsmandApplication; +import net.osmand.plus.R; +import net.osmand.plus.UiUtilities; + +import java.util.ArrayList; +import java.util.List; + +public class SelectCopyProfilesMenuAdapter extends AbstractProfileMenuAdapter { + + private OsmandApplication app; + + private ApplicationMode selectedAppMode; + private List items = new ArrayList<>(); + + private boolean nightMode; + + public SelectCopyProfilesMenuAdapter(List items, @NonNull OsmandApplication app, + boolean nightMode, @Nullable ApplicationMode selectedAppMode) { + this.items.addAll(items); + this.app = app; + this.selectedAppMode = selectedAppMode; + this.nightMode = nightMode; + } + + @NonNull + @Override + public SelectProfileViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + LayoutInflater inflater = UiUtilities.getInflater(parent.getContext(), nightMode); + View itemView = inflater.inflate(R.layout.bottom_sheet_item_with_radio_btn, parent, false); + return new SelectProfileViewHolder(itemView); + } + + @Override + public void onBindViewHolder(@NonNull final SelectProfileViewHolder holder, int position) { + ApplicationMode appMode = items.get(position); + boolean selected = appMode == selectedAppMode; + + holder.title.setText(appMode.toHumanString(app)); + holder.compoundButton.setChecked(selected); + + updateViewHolder(holder, appMode, selected); + } + + @Override + public int getItemCount() { + return items.size(); + } + + private void updateViewHolder(SelectProfileViewHolder holder, ApplicationMode appMode, boolean selected) { + int iconRes = appMode.getIconRes(); + if (iconRes == 0 || iconRes == -1) { + iconRes = R.drawable.ic_action_world_globe; + } + int iconColor = appMode.getIconColorInfo().getColor(nightMode); + holder.icon.setImageDrawable(app.getUIUtilities().getIcon(iconRes, iconColor)); + + int colorNoAlpha = ContextCompat.getColor(app, iconColor); + Drawable drawable = UiUtilities.getColoredSelectableDrawable(app, colorNoAlpha, 0.3f); + + if (selected) { + Drawable[] layers = {new ColorDrawable(UiUtilities.getColorWithAlpha(colorNoAlpha, 0.15f)), drawable}; + drawable = new LayerDrawable(layers); + } + AndroidUtils.setBackground(holder.itemView, drawable); + } + + class SelectProfileViewHolder extends ProfileAbstractViewHolder { + + SelectProfileViewHolder(View itemView) { + super(itemView); + itemView.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + int pos = getAdapterPosition(); + if (pos != RecyclerView.NO_POSITION) { + selectedAppMode = items.get(pos); + if (profilePressedListener != null) { + profilePressedListener.onProfilePressed(selectedAppMode); + notifyDataSetChanged(); + } + } + } + }); + } + } +} \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/profiles/SelectProfileMenuAdapter.java b/OsmAnd/src/net/osmand/plus/profiles/SelectProfileMenuAdapter.java index c5ed1cfc2b..b7a26fb080 100644 --- a/OsmAnd/src/net/osmand/plus/profiles/SelectProfileMenuAdapter.java +++ b/OsmAnd/src/net/osmand/plus/profiles/SelectProfileMenuAdapter.java @@ -94,7 +94,7 @@ public class SelectProfileMenuAdapter extends AbstractProfileMenuAdapter