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.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
@ -46,7 +47,7 @@ public class CustomColorBottomSheet extends MenuBottomSheetDialogFragment implem
private boolean fromEditText;
@ColorInt
private int prevColor;
private Integer prevColor;
@ColorInt
private int newColor;
@ -60,10 +61,10 @@ public class CustomColorBottomSheet extends MenuBottomSheetDialogFragment implem
if (args != null) {
if (args.containsKey(PREV_SELECTED_COLOR)) {
prevColor = args.getInt(PREV_SELECTED_COLOR);
} else {
prevColor = Color.RED;
}
newColor = prevColor;
} else {
newColor = Color.RED;
}
}
}
@ -78,7 +79,9 @@ public class CustomColorBottomSheet extends MenuBottomSheetDialogFragment implem
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putInt(NEW_SELECTED_COLOR, newColor);
if (prevColor != null) {
outState.putInt(PREV_SELECTED_COLOR, prevColor);
}
super.onSaveInstanceState(outState);
}
@ -161,11 +164,13 @@ public class CustomColorBottomSheet extends MenuBottomSheetDialogFragment implem
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 {
if (!fragmentManager.isStateSaved() && fragmentManager.findFragmentByTag(CustomColorBottomSheet.TAG) == null) {
Bundle args = new Bundle();
if (prevColor != null) {
args.putInt(PREV_SELECTED_COLOR, prevColor);
}
CustomColorBottomSheet customColorBottomSheet = new CustomColorBottomSheet();
customColorBottomSheet.setArguments(args);
@ -179,7 +184,7 @@ public class CustomColorBottomSheet extends MenuBottomSheetDialogFragment implem
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);
buttonsShadow = view.findViewById(R.id.buttons_shadow);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openMenuHeaderOnly();
}
});
if (isPortrait()) {
updateCardsLayout();
}
@ -341,7 +334,7 @@ public class TrackAppearanceFragment extends ContextMenuScrollFragment implement
}
@Override
public void onColorSelected(int prevColor, int newColor) {
public void onColorSelected(Integer prevColor, int newColor) {
trackColoringCard.onColorSelected(prevColor, newColor);
updateColorItems();
}

View file

@ -23,9 +23,6 @@ import com.google.android.material.internal.FlowLayout;
import net.osmand.AndroidUtils;
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.R;
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 static final int INVALID_VALUE = -1;
private final static String SOLID_COLOR = "solid_color";
private static final int INVALID_VALUE = -1;
private static final String SOLID_COLOR = "solid_color";
private static final Log log = PlatformUtil.getLog(TrackColoringCard.class);
private TrackDrawInfo trackDrawInfo;
@ -202,7 +198,7 @@ public class TrackColoringCard extends BaseCard implements ColorPickerListener {
public void onClick(View v) {
MapActivity mapActivity = getMapActivity();
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
public void onColorSelected(int prevColor, int newColor) {
if (prevColor == INVALID_VALUE && customColors.size() < 6) {
public void onColorSelected(Integer prevColor, int newColor) {
if (prevColor == null && customColors.size() < 6) {
customColors.add(newColor);
trackDrawInfo.setColor(newColor);
} else if (!Algorithms.isEmpty(customColors)) {
@ -299,35 +295,9 @@ public class TrackColoringCard extends BaseCard implements ColorPickerListener {
customColors.set(index, newColor);
}
}
saveCustomColors();
saveCustomColorsToTracks(prevColor, newColor);
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 List<TrackAppearanceItem> items;