Fix ChangeGeneralProfilesPrefBottomSheet

This commit is contained in:
Chumva 2019-09-02 16:02:45 +03:00
parent aca30f998e
commit 221d6c8aac
6 changed files with 80 additions and 48 deletions

View file

@ -11,6 +11,7 @@
Thx - Hardy
-->
<string name="pref_selected_by_default_for_profiles">This setting is selected by default for profiles: %s.</string>
<string name="change_default_settings">Change default settings</string>
<string name="discard_changes">Discard changes</string>
<string name="apply_to_current_profile">Apply to current %1$s profile</string>

View file

@ -32,7 +32,6 @@ import android.support.v4.content.ContextCompat;
import android.support.v4.content.FileProvider;
import android.support.v4.text.TextUtilsCompat;
import android.support.v4.view.ViewCompat;
import android.text.ParcelableSpan;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
@ -594,7 +593,7 @@ public class AndroidUtils {
}
if(replaceStyle != null) {
ssb.setSpan(replaceStyle, indexOfPlaceholder,
stringToInsertAndStyle.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
indexOfPlaceholder + stringToInsertAndStyle.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return ssb;
} else {

View file

@ -218,8 +218,12 @@ public class OsmandSettings {
return registeredPreferences.get(key);
}
@SuppressWarnings("unchecked")
public boolean setPreference(String key, Object value) {
return setPreference(key, value, APPLICATION_MODE.get());
}
@SuppressWarnings("unchecked")
public boolean setPreference(String key, Object value, ApplicationMode mode) {
OsmandPreference<?> preference = registeredPreferences.get(key);
if (preference != null) {
if (preference == APPLICATION_MODE) {
@ -251,7 +255,7 @@ public class OsmandSettings {
} catch (IllegalArgumentException e) {
return false;
}
METRIC_SYSTEM.set(metricSystem);
METRIC_SYSTEM.setModeValue(mode, metricSystem);
return true;
} else if (preference == SPEED_SYSTEM && value instanceof String) {
String speedSystemName = (String) value;
@ -261,31 +265,31 @@ public class OsmandSettings {
} catch (IllegalArgumentException e) {
return false;
}
SPEED_SYSTEM.set(speedSystem);
SPEED_SYSTEM.setModeValue(mode, speedSystem);
return true;
} else if (preference instanceof BooleanPreference) {
if (value instanceof Boolean) {
((BooleanPreference) preference).set((Boolean) value);
((BooleanPreference) preference).setModeValue(mode, (Boolean) value);
return true;
}
} else if (preference instanceof StringPreference) {
if (value instanceof String) {
((StringPreference) preference).set((String) value);
((StringPreference) preference).setModeValue(mode, (String) value);
return true;
}
} else if (preference instanceof FloatPreference) {
if (value instanceof Float) {
((FloatPreference) preference).set((Float) value);
((FloatPreference) preference).setModeValue(mode, (Float) value);
return true;
}
} else if (preference instanceof IntPreference) {
if (value instanceof Integer) {
((IntPreference) preference).set((Integer) value);
((IntPreference) preference).setModeValue(mode, (Integer) value);
return true;
}
} else if (preference instanceof LongPreference) {
if (value instanceof Long) {
((LongPreference) preference).set((Long) value);
((LongPreference) preference).setModeValue(mode, (Long) value);
return true;
}
} else if (preference instanceof EnumIntPreference) {
@ -294,7 +298,7 @@ public class OsmandSettings {
int newVal = (Integer) value;
if (enumPref.values.length > newVal) {
Enum enumValue = enumPref.values[newVal];
return enumPref.set(enumValue);
return enumPref.setModeValue(mode, enumValue);
}
return false;
}
@ -3202,6 +3206,7 @@ public class OsmandSettings {
return defValue;
}
@Nullable
public Object getValue(String key, Object defValue) {
OsmandPreference preference = getPreference(key);
if (preference != null) {

View file

@ -1,49 +1,80 @@
package net.osmand.plus.settings;
import android.content.Context;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.View;
import net.osmand.AndroidUtils;
import net.osmand.PlatformUtil;
import net.osmand.plus.ApplicationMode;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings;
import net.osmand.plus.OsmandSettings.CommonPreference;
import net.osmand.plus.OsmandSettings.OsmandPreference;
import net.osmand.plus.R;
import net.osmand.plus.base.MenuBottomSheetDialogFragment;
import net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem;
import net.osmand.plus.base.bottomsheetmenu.SimpleBottomSheetItem;
import net.osmand.plus.base.bottomsheetmenu.simpleitems.LongDescriptionItem;
import net.osmand.plus.base.bottomsheetmenu.simpleitems.TitleItem;
import org.apache.commons.logging.Log;
import java.util.ArrayList;
import java.util.List;
public class ChangeProfilesPreferenceBottomSheet extends MenuBottomSheetDialogFragment {
public class ChangeGeneralProfilesPrefBottomSheet extends MenuBottomSheetDialogFragment {
public static final String TAG = "ChangeProfilesPreferenceBottomSheet";
public static final String TAG = "ChangeGeneralProfilesPrefBottomSheet";
private static final String PREFERENCE_ID = "preference_id";
private static final Log LOG = PlatformUtil.getLog(ChangeProfilesPreferenceBottomSheet.class);
private static final Log LOG = PlatformUtil.getLog(ChangeGeneralProfilesPrefBottomSheet.class);
private Object newValue;
@Override
public void createMenuItems(Bundle savedInstanceState) {
Context context = getContext();
if (context == null) {
return;
}
final OsmandApplication app = getMyApplication();
Bundle args = getArguments();
if (args == null || !args.containsKey(PREFERENCE_ID)) {
if (app == null || args == null || !args.containsKey(PREFERENCE_ID)) {
return;
}
final String prefId = args.getString(PREFERENCE_ID);
CommonPreference pref = getPreference(prefId);
if (pref == null) {
return;
}
items.add(new TitleItem(getString(R.string.change_default_settings)));
StringBuilder builder = new StringBuilder();
final List<ApplicationMode> values = ApplicationMode.values(app);
List<ApplicationMode> appModesSameValue = new ArrayList<>();
for (int i = 0; i < values.size(); i++) {
ApplicationMode mode = values.get(i);
Object modeValue = pref.getModeValue(mode);
if (modeValue.equals(newValue)) {
appModesSameValue.add(mode);
}
}
for (int i = 0; i < appModesSameValue.size(); i++) {
ApplicationMode mode = appModesSameValue.get(i);
builder.append(mode.toHumanString(app));
if (i < appModesSameValue.size() - 1) {
builder.append(", ");
}
}
if (builder.length() > 0) {
CharSequence description = AndroidUtils.getStyledString(app.getString(R.string.pref_selected_by_default_for_profiles), builder.toString(), Typeface.BOLD);
items.add(new LongDescriptionItem(description));
}
BaseBottomSheetItem applyToAllProfiles = new SimpleBottomSheetItem.Builder()
.setTitle(getString(R.string.apply_to_all_profiles))
.setIcon(getActiveIcon(R.drawable.ic_action_copy))
@ -51,17 +82,8 @@ public class ChangeProfilesPreferenceBottomSheet extends MenuBottomSheetDialogFr
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
OsmandApplication app = getMyApplication();
if (app != null) {
OsmandSettings settings = app.getSettings();
OsmandSettings.OsmandPreference pref = settings.getPreference(prefId);
if (pref instanceof OsmandSettings.CommonPreference) {
OsmandSettings.CommonPreference commonPref = (OsmandSettings.CommonPreference) pref;
final List<ApplicationMode> values = ApplicationMode.values(app);
for (ApplicationMode mode : values) {
commonPref.setModeValue(mode, newValue);
}
}
for (ApplicationMode mode : values) {
app.getSettings().setPreference(prefId, newValue, mode);
}
dismiss();
}
@ -72,21 +94,13 @@ public class ChangeProfilesPreferenceBottomSheet extends MenuBottomSheetDialogFr
ApplicationMode selectedAppMode = getMyApplication().getSettings().APPLICATION_MODE.get();
BaseBottomSheetItem applyToCurrentProfile = new SimpleBottomSheetItem.Builder()
.setTitle(getString(R.string.apply_to_current_profile, selectedAppMode.toHumanString(context)))
.setTitle(getString(R.string.apply_to_current_profile, selectedAppMode.toHumanString(app)))
.setIcon(getActiveIcon(selectedAppMode.getIconRes()))
.setLayoutId(R.layout.bottom_sheet_item_simple)
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
OsmandApplication app = getMyApplication();
if (app != null) {
OsmandSettings settings = app.getSettings();
OsmandSettings.OsmandPreference pref = settings.getPreference(prefId);
if (pref instanceof OsmandSettings.CommonPreference) {
OsmandSettings.CommonPreference commonPref = (OsmandSettings.CommonPreference) pref;
commonPref.setModeValue(settings.APPLICATION_MODE.get(), newValue);
}
}
app.getSettings().setPreference(prefId, newValue);
dismiss();
}
})
@ -112,16 +126,29 @@ public class ChangeProfilesPreferenceBottomSheet extends MenuBottomSheetDialogFr
return true;
}
public static void showInstance(@NonNull FragmentManager fm, String prefId, Object newValue) {
private CommonPreference getPreference(String prefId) {
OsmandApplication app = getMyApplication();
if (app != null) {
OsmandPreference pref = app.getSettings().getPreference(prefId);
if (pref instanceof CommonPreference) {
return (CommonPreference) pref;
}
}
return null;
}
public static void showInstance(@NonNull FragmentManager fm, String prefId, Object newValue, Fragment target) {
try {
if (fm.findFragmentByTag(ChangeProfilesPreferenceBottomSheet.TAG) == null) {
if (fm.findFragmentByTag(ChangeGeneralProfilesPrefBottomSheet.TAG) == null) {
Bundle args = new Bundle();
args.putString(PREFERENCE_ID, prefId);
ChangeProfilesPreferenceBottomSheet fragment = new ChangeProfilesPreferenceBottomSheet();
ChangeGeneralProfilesPrefBottomSheet fragment = new ChangeGeneralProfilesPrefBottomSheet();
fragment.setArguments(args);
fragment.setTargetFragment(target, 0);
fragment.newValue = newValue;
fragment.show(fm, ChangeProfilesPreferenceBottomSheet.TAG);
fragment.show(fm, ChangeGeneralProfilesPrefBottomSheet.TAG);
}
} catch (RuntimeException e) {
LOG.error("showInstance", e);

View file

@ -331,7 +331,7 @@ public class GeneralProfileSettingsFragment extends BaseSettingsFragment {
public boolean onPreferenceChange(Preference preference, Object newValue) {
FragmentManager fragmentManager = getFragmentManager();
if (fragmentManager != null) {
ChangeProfilesPreferenceBottomSheet.showInstance(fragmentManager, preference.getKey(), newValue);
ChangeGeneralProfilesPrefBottomSheet.showInstance(fragmentManager, preference.getKey(), newValue, this);
}
return false;
}

View file

@ -85,7 +85,7 @@ public class GlobalSettingsFragment extends BaseSettingsFragment {
}
private void setupDefaultAppModePref() {
ApplicationMode selectedMode = getSelectedAppMode();
ApplicationMode selectedMode = settings.DEFAULT_APPLICATION_MODE.get();
int iconRes = selectedMode.getIconRes();
String title = selectedMode.toHumanString(getContext());