Add copy profile preferences dialog

This commit is contained in:
Vitaliy 2019-12-27 17:36:23 +02:00
parent 9ffa6c13c7
commit 33a552af84
6 changed files with 244 additions and 19 deletions

View file

@ -100,14 +100,14 @@ public class ConfigureProfileMenuAdapter extends AbstractProfileMenuAdapter<Conf
holder.dividerBottom.setVisibility(View.VISIBLE);
holder.icon.setVisibility(View.VISIBLE);
holder.descr.setVisibility(View.VISIBLE);
holder.switcher.setVisibility(View.VISIBLE);
holder.compoundButton.setVisibility(View.VISIBLE);
holder.menuIcon.setVisibility(View.VISIBLE);
final ApplicationMode item = (ApplicationMode) obj;
holder.title.setText(item.toHumanString(app));
holder.descr.setText(BaseSettingsFragment.getAppModeDescription(app, item));
holder.initSwitcher = true;
holder.switcher.setChecked(selectedItems.contains(item));
holder.compoundButton.setChecked(selectedItems.contains(item));
holder.initSwitcher = false;
updateViewHolder(holder, item);
} else {
@ -117,7 +117,7 @@ public class ConfigureProfileMenuAdapter extends AbstractProfileMenuAdapter<Conf
}
holder.icon.setVisibility(View.INVISIBLE);
holder.descr.setVisibility(View.GONE);
holder.switcher.setVisibility(View.GONE);
holder.compoundButton.setVisibility(View.GONE);
holder.menuIcon.setVisibility(View.GONE);
holder.title.setTextColor(app.getResources().getColor(
nightMode
@ -166,7 +166,7 @@ public class ConfigureProfileMenuAdapter extends AbstractProfileMenuAdapter<Conf
}
}
});
switcher.setOnCheckedChangeListener(new OnCheckedChangeListener() {
compoundButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int pos = getAdapterPosition();

View file

@ -1,8 +1,8 @@
package net.osmand.plus.profiles;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SwitchCompat;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
@ -11,7 +11,7 @@ import net.osmand.plus.R;
public abstract class ProfileAbstractViewHolder extends RecyclerView.ViewHolder {
TextView title, descr;
SwitchCompat switcher;
CompoundButton compoundButton;
ImageView icon, menuIcon;
LinearLayout profileOptions;
View dividerBottom;
@ -21,7 +21,7 @@ public abstract class ProfileAbstractViewHolder extends RecyclerView.ViewHolder
super(itemView);
title = itemView.findViewById(R.id.title);
descr = itemView.findViewById(R.id.description);
switcher = itemView.findViewById(R.id.compound_button);
compoundButton = itemView.findViewById(R.id.compound_button);
icon = itemView.findViewById(R.id.icon);
profileOptions = itemView.findViewById(R.id.profile_settings);
dividerBottom = itemView.findViewById(R.id.divider_bottom);

View file

@ -0,0 +1,131 @@
package net.osmand.plus.profiles;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import net.osmand.PlatformUtil;
import net.osmand.plus.ApplicationMode;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import org.apache.commons.logging.Log;
import java.util.ArrayList;
import java.util.List;
public class SelectCopyAppModeBottomSheet extends AppModesBottomSheetDialogFragment<SelectCopyProfilesMenuAdapter> {
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<ApplicationMode> 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);
}
}
}

View file

@ -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<SelectCopyProfilesMenuAdapter.SelectProfileViewHolder> {
private OsmandApplication app;
private ApplicationMode selectedAppMode;
private List<ApplicationMode> items = new ArrayList<>();
private boolean nightMode;
public SelectCopyProfilesMenuAdapter(List<ApplicationMode> 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();
}
}
}
});
}
}
}

View file

@ -94,7 +94,7 @@ public class SelectProfileMenuAdapter extends AbstractProfileMenuAdapter<SelectP
holder.dividerUp.setVisibility(View.INVISIBLE);
holder.icon.setVisibility(View.VISIBLE);
holder.descr.setVisibility(View.VISIBLE);
holder.switcher.setVisibility(View.GONE);
holder.compoundButton.setVisibility(View.GONE);
holder.menuIcon.setVisibility(View.GONE);
final ApplicationMode item = (ApplicationMode) obj;
holder.title.setText(item.toHumanString(app));
@ -123,7 +123,7 @@ public class SelectProfileMenuAdapter extends AbstractProfileMenuAdapter<SelectP
}
holder.icon.setVisibility(View.INVISIBLE);
holder.descr.setVisibility(View.GONE);
holder.switcher.setVisibility(View.GONE);
holder.compoundButton.setVisibility(View.GONE);
holder.menuIcon.setVisibility(View.GONE);
int color = ContextCompat.getColor(app, nightMode
? R.color.active_color_primary_dark

View file

@ -45,7 +45,7 @@ import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.activities.PluginActivity;
import net.osmand.plus.helpers.FontCache;
import net.osmand.plus.openseamapsplugin.NauticalMapsPlugin;
import net.osmand.plus.profiles.SelectAppModesBottomSheetDialogFragment;
import net.osmand.plus.profiles.SelectCopyAppModeBottomSheet;
import net.osmand.plus.settings.preferences.SwitchPreferenceEx;
import net.osmand.plus.skimapsplugin.SkiMapsPlugin;
@ -366,13 +366,6 @@ public class ConfigureProfileFragment extends BaseSettingsFragment {
return intent;
}
@Override
public void onAppModeChanged(final ApplicationMode appMode) {
long start = System.currentTimeMillis();
app.getSettings().copyPreferencesFromProfile(appMode, getSelectedAppMode());
LOG.debug("copyPrefs " + (System.currentTimeMillis() - start));
}
@Override
public boolean onPreferenceClick(Preference preference) {
String prefId = preference.getKey();
@ -400,8 +393,8 @@ public class ConfigureProfileFragment extends BaseSettingsFragment {
} else if (COPY_FROM_OTHER_PROFILE.equals(prefId)) {
FragmentManager fragmentManager = getFragmentManager();
if (fragmentManager != null) {
SelectAppModesBottomSheetDialogFragment.showInstance(fragmentManager,
ConfigureProfileFragment.this, false, getSelectedAppMode(), false);
SelectCopyAppModeBottomSheet.showInstance(fragmentManager,
ConfigureProfileFragment.this, false, getSelectedAppMode());
}
} else if (RESET_TO_DEFAULT.equals(prefId)) {
app.getSettings().resetPreferencesForProfile(getSelectedAppMode());