Fix custom color editing

This commit is contained in:
Vitaliy 2020-09-01 14:11:00 +03:00
parent ee6e283d03
commit 6029bb4fea
3 changed files with 18 additions and 50 deletions

View file

@ -11,6 +11,7 @@ import android.widget.EditText;
import androidx.annotation.ColorInt; import androidx.annotation.ColorInt;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentManager;
@ -46,7 +47,7 @@ public class CustomColorBottomSheet extends MenuBottomSheetDialogFragment implem
private boolean fromEditText; private boolean fromEditText;
@ColorInt @ColorInt
private int prevColor; private Integer prevColor;
@ColorInt @ColorInt
private int newColor; private int newColor;
@ -60,10 +61,10 @@ public class CustomColorBottomSheet extends MenuBottomSheetDialogFragment implem
if (args != null) { if (args != null) {
if (args.containsKey(PREV_SELECTED_COLOR)) { if (args.containsKey(PREV_SELECTED_COLOR)) {
prevColor = args.getInt(PREV_SELECTED_COLOR); prevColor = args.getInt(PREV_SELECTED_COLOR);
newColor = prevColor;
} else { } else {
prevColor = Color.RED; newColor = Color.RED;
} }
newColor = prevColor;
} }
} }
@ -78,7 +79,9 @@ public class CustomColorBottomSheet extends MenuBottomSheetDialogFragment implem
@Override @Override
public void onSaveInstanceState(Bundle outState) { public void onSaveInstanceState(Bundle outState) {
outState.putInt(NEW_SELECTED_COLOR, newColor); outState.putInt(NEW_SELECTED_COLOR, newColor);
outState.putInt(PREV_SELECTED_COLOR, prevColor); if (prevColor != null) {
outState.putInt(PREV_SELECTED_COLOR, prevColor);
}
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
} }
@ -161,11 +164,13 @@ public class CustomColorBottomSheet extends MenuBottomSheetDialogFragment implem
dismiss(); dismiss();
} }
public static void showInstance(@NonNull FragmentManager fragmentManager, Fragment target, int prevColor) { public static void showInstance(@NonNull FragmentManager fragmentManager, Fragment target, @Nullable Integer prevColor) {
try { try {
if (!fragmentManager.isStateSaved() && fragmentManager.findFragmentByTag(CustomColorBottomSheet.TAG) == null) { if (!fragmentManager.isStateSaved() && fragmentManager.findFragmentByTag(CustomColorBottomSheet.TAG) == null) {
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putInt(PREV_SELECTED_COLOR, prevColor); if (prevColor != null) {
args.putInt(PREV_SELECTED_COLOR, prevColor);
}
CustomColorBottomSheet customColorBottomSheet = new CustomColorBottomSheet(); CustomColorBottomSheet customColorBottomSheet = new CustomColorBottomSheet();
customColorBottomSheet.setArguments(args); customColorBottomSheet.setArguments(args);
@ -179,7 +184,7 @@ public class CustomColorBottomSheet extends MenuBottomSheetDialogFragment implem
public interface ColorPickerListener { public interface ColorPickerListener {
void onColorSelected(@ColorInt int prevColor, @ColorInt int newColor); void onColorSelected(@ColorInt Integer prevColor, @ColorInt int newColor);
} }
} }

View file

@ -180,13 +180,6 @@ public class TrackAppearanceFragment extends ContextMenuScrollFragment implement
trackIcon = view.findViewById(R.id.track_icon); trackIcon = view.findViewById(R.id.track_icon);
buttonsShadow = view.findViewById(R.id.buttons_shadow); buttonsShadow = view.findViewById(R.id.buttons_shadow);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openMenuHeaderOnly();
}
});
if (isPortrait()) { if (isPortrait()) {
updateCardsLayout(); updateCardsLayout();
} }
@ -341,7 +334,7 @@ public class TrackAppearanceFragment extends ContextMenuScrollFragment implement
} }
@Override @Override
public void onColorSelected(int prevColor, int newColor) { public void onColorSelected(Integer prevColor, int newColor) {
trackColoringCard.onColorSelected(prevColor, newColor); trackColoringCard.onColorSelected(prevColor, newColor);
updateColorItems(); updateColorItems();
} }

View file

@ -23,9 +23,6 @@ import com.google.android.material.internal.FlowLayout;
import net.osmand.AndroidUtils; import net.osmand.AndroidUtils;
import net.osmand.PlatformUtil; import net.osmand.PlatformUtil;
import net.osmand.plus.GPXDatabase;
import net.osmand.plus.GPXDatabase.GpxDataItem;
import net.osmand.plus.GpxSelectionHelper.SelectedGpxFile;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.UiUtilities; import net.osmand.plus.UiUtilities;
@ -46,9 +43,8 @@ import static net.osmand.plus.dialogs.GpxAppearanceAdapter.getAppearanceItems;
public class TrackColoringCard extends BaseCard implements ColorPickerListener { public class TrackColoringCard extends BaseCard implements ColorPickerListener {
public static final int INVALID_VALUE = -1; private static final int INVALID_VALUE = -1;
private static final String SOLID_COLOR = "solid_color";
private final static String SOLID_COLOR = "solid_color";
private static final Log log = PlatformUtil.getLog(TrackColoringCard.class); private static final Log log = PlatformUtil.getLog(TrackColoringCard.class);
private TrackDrawInfo trackDrawInfo; private TrackDrawInfo trackDrawInfo;
@ -202,7 +198,7 @@ public class TrackColoringCard extends BaseCard implements ColorPickerListener {
public void onClick(View v) { public void onClick(View v) {
MapActivity mapActivity = getMapActivity(); MapActivity mapActivity = getMapActivity();
if (mapActivity != null) { if (mapActivity != null) {
CustomColorBottomSheet.showInstance(mapActivity.getSupportFragmentManager(), target, TrackColoringCard.INVALID_VALUE); CustomColorBottomSheet.showInstance(mapActivity.getSupportFragmentManager(), target, null);
} }
} }
}); });
@ -289,8 +285,8 @@ public class TrackColoringCard extends BaseCard implements ColorPickerListener {
} }
@Override @Override
public void onColorSelected(int prevColor, int newColor) { public void onColorSelected(Integer prevColor, int newColor) {
if (prevColor == INVALID_VALUE && customColors.size() < 6) { if (prevColor == null && customColors.size() < 6) {
customColors.add(newColor); customColors.add(newColor);
trackDrawInfo.setColor(newColor); trackDrawInfo.setColor(newColor);
} else if (!Algorithms.isEmpty(customColors)) { } else if (!Algorithms.isEmpty(customColors)) {
@ -299,35 +295,9 @@ public class TrackColoringCard extends BaseCard implements ColorPickerListener {
customColors.set(index, newColor); customColors.set(index, newColor);
} }
} }
saveCustomColors();
saveCustomColorsToTracks(prevColor, newColor);
updateContent(); updateContent();
} }
private void saveCustomColorsToTracks(int prevColor, int newColor) {
List<GpxDataItem> gpxDataItems = app.getGpxDbHelper().getItems();
for (GPXDatabase.GpxDataItem dataItem : gpxDataItems) {
if (prevColor == dataItem.getColor()) {
app.getGpxDbHelper().updateColor(dataItem, newColor);
}
}
List<SelectedGpxFile> files = app.getSelectedGpxHelper().getSelectedGPXFiles();
for (SelectedGpxFile selectedGpxFile : files) {
if (prevColor == selectedGpxFile.getGpxFile().getColor(0)) {
selectedGpxFile.getGpxFile().setColor(newColor);
}
}
}
private void saveCustomColors() {
List<String> colorNames = new ArrayList<>();
for (Integer color : customColors) {
String colorHex = Algorithms.colorToString(color);
colorNames.add(colorHex);
}
app.getSettings().CUSTOM_TRACK_COLORS.setStringsList(colorNames);
}
private class TrackColoringAdapter extends RecyclerView.Adapter<TrackAppearanceViewHolder> { private class TrackColoringAdapter extends RecyclerView.Adapter<TrackAppearanceViewHolder> {
private List<TrackAppearanceItem> items; private List<TrackAppearanceItem> items;