From e1a7211fbfe2bcdd1113811a5003f4811ac0863e Mon Sep 17 00:00:00 2001 From: Akash Yadav Date: Wed, 29 Dec 2021 12:02:44 +0530 Subject: [PATCH] Apply new color value when picked from color picker --- .../itsaky/androidide/DesignerActivity.java | 1 + .../fragments/sheets/AttrEditorSheet.java | 25 +++++++++++-------- .../androidide/utils/AttributeDialogs.java | 12 ++++++--- .../colorpicker/ColorPickerView.java | 2 +- 4 files changed, 26 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/com/itsaky/androidide/DesignerActivity.java b/app/src/main/java/com/itsaky/androidide/DesignerActivity.java index dcc863656..b3430ac0f 100755 --- a/app/src/main/java/com/itsaky/androidide/DesignerActivity.java +++ b/app/src/main/java/com/itsaky/androidide/DesignerActivity.java @@ -76,6 +76,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Stack; +// TODO Generate and save the source code once the user leaves this activity public class DesignerActivity extends StudioActivity implements WidgetItemAdapter.OnDragStartListener { private ActivityDesignerBinding mBinding; diff --git a/app/src/main/java/com/itsaky/androidide/fragments/sheets/AttrEditorSheet.java b/app/src/main/java/com/itsaky/androidide/fragments/sheets/AttrEditorSheet.java index 0e53e7674..61293332d 100644 --- a/app/src/main/java/com/itsaky/androidide/fragments/sheets/AttrEditorSheet.java +++ b/app/src/main/java/com/itsaky/androidide/fragments/sheets/AttrEditorSheet.java @@ -43,6 +43,7 @@ import com.itsaky.androidide.R; import com.itsaky.androidide.adapters.AttributeListAdapter; import com.itsaky.androidide.adapters.SimpleIconTextAdapter; import com.itsaky.androidide.app.StudioApp; +import com.itsaky.androidide.colorpicker.ColorPickerView; import com.itsaky.androidide.databinding.LayoutAttrEditorSheetBinding; import com.itsaky.androidide.databinding.LayoutAttrEditorSheetItemBinding; import com.itsaky.androidide.models.IconTextListItem; @@ -144,15 +145,9 @@ public class AttrEditorSheet extends BottomSheetDialogFragment implements Simple private void showEditorDialog (@NonNull XMLAttribute attribute) { final var attr = attribute.getAttr (); final var values = attr.possibleValues.toArray (new String[0]); - final AttributeDialogs.OnClickListener onDone = (dialog, which, newValue) -> { - if (!this.selectedView.updateAttribute (attribute.getNamespace (), attribute.getAttributeName (), newValue)) { - StudioApp.getInstance ().toast ("Unable to update this attribute", Toaster.Type.ERROR); - } else { - // Update the view data - // This will make sure that the attributes list has been updated - setupViewData (); - } - }; + final AttributeDialogs.OnClickListener onDone = (dialog, which, newValue) -> applyNewValue (attribute, newValue); + + final ColorPickerView.OnPickListener mPickListener = (color, hexCode) -> applyNewValue (attribute, hexCode); AlertDialog dialog = null; if (attribute.hasFormat (DIMENSION)) { @@ -168,7 +163,7 @@ public class AttrEditorSheet extends BottomSheetDialogFragment implements Simple } else if (attribute.hasFormat (COLOR)) { // TODO Implement something to allow values from colors.xml or ColorStateLists // Make this color picker optional - dialog = colorPicker (); + dialog = colorPicker (mPickListener); } if (dialog != null) { @@ -176,6 +171,16 @@ public class AttrEditorSheet extends BottomSheetDialogFragment implements Simple } } + private void applyNewValue (@NonNull XMLAttribute attribute, String newValue) { + if (!this.selectedView.updateAttribute (attribute.getNamespace (), attribute.getAttributeName (), newValue)) { + StudioApp.getInstance ().toast ("Unable to update this attribute", Toaster.Type.ERROR); + } else { + // Update the view data + // This will make sure that the attributes list has been updated + setupViewData (); + } + } + @Override public void postBind (IconTextListItem item, @NonNull SimpleIconTextAdapter.VH holder, int position) { final var binding = holder.binding; diff --git a/app/src/main/java/com/itsaky/androidide/utils/AttributeDialogs.java b/app/src/main/java/com/itsaky/androidide/utils/AttributeDialogs.java index 5cf0f024c..79132530f 100644 --- a/app/src/main/java/com/itsaky/androidide/utils/AttributeDialogs.java +++ b/app/src/main/java/com/itsaky/androidide/utils/AttributeDialogs.java @@ -269,15 +269,21 @@ public class AttributeDialogs { } @NonNull - public static AlertDialog colorPicker () { + public static AlertDialog colorPicker (final ColorPickerView.OnPickListener listener) { final var view = new ColorPickerView (mContext); - view.setOnPickListener ((color, hexCode) -> LOG.debug ("Picked color:", color, hexCode)); - final var builder = newMaterialDialogBuilder (mContext); builder.setView (view); final var dialog = builder.create (); dialog.getWindow ().setSoftInputMode (WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); + + view.setOnPickListener ((color, hexCode) -> { + dialog.dismiss (); + if (listener != null) { + listener.onPick (color, hexCode); + } + }); + return dialog; } diff --git a/common/src/main/java/com/itsaky/androidide/colorpicker/ColorPickerView.java b/common/src/main/java/com/itsaky/androidide/colorpicker/ColorPickerView.java index 4340e7793..ffb2816b7 100644 --- a/common/src/main/java/com/itsaky/androidide/colorpicker/ColorPickerView.java +++ b/common/src/main/java/com/itsaky/androidide/colorpicker/ColorPickerView.java @@ -136,7 +136,7 @@ public class ColorPickerView extends LinearLayout { */ @NonNull public String getHexColor () { - return Integer.toHexString (getColor ()); + return "#" + Integer.toHexString (getColor ()); } /**