Skip to content

Commit

Permalink
Show more items in attribute value completion
Browse files Browse the repository at this point in the history
Auto-fill already set value in input fields in value editor
  • Loading branch information
itsaky committed Mar 16, 2022
1 parent 9d8124c commit d71e736
Show file tree
Hide file tree
Showing 17 changed files with 761 additions and 479 deletions.
1 change: 1 addition & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified app/src/main/assets/data/common/gradle-plugin.zip
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* This file is part of AndroidIDE.
*
* AndroidIDE is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* AndroidIDE is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with AndroidIDE. If not, see <https://www.gnu.org/licenses/>.
*/
package com.itsaky.androidide.fragments.attr;

import static com.itsaky.androidide.utils.Logger.instance;

import android.text.Editable;
import android.text.TextUtils;
import android.widget.AutoCompleteTextView;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ThreadUtils;
import com.google.android.material.textfield.MaterialAutoCompleteTextView;
import com.itsaky.androidide.adapters.AttrValueCompletionAdapter;
import com.itsaky.androidide.utils.Logger;
import com.itsaky.androidide.utils.TextWatcherAdapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;

/**
* @author Akash Yadav
*/
public abstract class AbstractReferenceEditor extends BaseValueEditorFragment {

private static final Logger LOG = instance ("ReferenceEditor");
public TextWatcherAdapter resInputWatcher;

@Override
protected void notifyValueChanged (@NonNull String newValue) {
try {
super.notifyValueChanged (newValue);
} catch (Throwable e) {
LOG.error ("Unable to update resource value to '" + newValue + "'", e);
}
}

protected void setupReferenceInput (MaterialAutoCompleteTextView referenceInput) {
Objects.requireNonNull (referenceInput);

referenceInput.setText (attribute.getValue ());

if (resInputWatcher != null) {
referenceInput.removeTextChangedListener (resInputWatcher);
}

resInputWatcher =
new TextWatcherAdapter () {
@Override
public void afterTextChanged (@NonNull Editable s) {
final var text = s.toString ().trim ();
if (TextUtils.isEmpty (text)) {
return;
}

notifyValueChanged (text);
}
};

referenceInput.addTextChangedListener (resInputWatcher);

CompletableFuture.supplyAsync (this::computeReferenceItems)
.whenComplete (
(list, error) -> handleAutoCompleteResult (list, error, referenceInput));
}

protected void handleAutoCompleteResult (
List<String> list, Throwable error, AutoCompleteTextView referenceInput) {
if (list == null || list.isEmpty ()) {
LOG.error ("No completion items found");
LOG.error ("Error was:", error);
return;
}

LOG.debug ("Found", list.size (), "resource items for completion");

ThreadUtils.runOnUiThread (
() -> {
final var adapter = new AttrValueCompletionAdapter (requireContext (), list);
referenceInput.setThreshold (1);
referenceInput.setAdapter (adapter);
adapter.notifyDataSetChanged ();
});
}

protected List<String> computeReferenceItems () {
return new ArrayList<> ();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,51 +35,61 @@
import java.util.List;
import java.util.stream.Collectors;

/** @author Akash Yadav */
public class BooleanEditor extends ReferenceEditor {

/**
* @author Akash Yadav
*/
public class BooleanEditor extends AbstractReferenceEditor {

private LayoutBooleanAttrEditorBinding binding;

@Nullable
@Override
public View onCreateView(
public View onCreateView (
@NonNull LayoutInflater inflater,
@Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
this.binding = LayoutBooleanAttrEditorBinding.inflate(inflater, container, false);
return this.binding.getRoot();
this.binding = LayoutBooleanAttrEditorBinding.inflate (inflater, container, false);
return this.binding.getRoot ();
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

this.binding.choiceGroup.setOnCheckedChangeListener(
public void onViewCreated (@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated (view, savedInstanceState);
this.binding.choiceGroup.setOnCheckedChangeListener (
(group, checkedId) -> {
final var checked = (RadioButton) group.findViewById(checkedId);
notifyValueChanged(checked.getText().toString().trim());
final var checked = (RadioButton) group.findViewById (checkedId);
notifyValueChanged (checked.getText ().toString ().trim ());
});

setupReferenceInput(
(MaterialAutoCompleteTextView) this.binding.booleanResInput.getEditText());

switch (attribute.getValue ()) {
case "true":
this.binding.boolTrue.setChecked (true);
break;
case "false":
this.binding.boolFalse.setChecked (true);
}

setupReferenceInput (
(MaterialAutoCompleteTextView) this.binding.booleanResInput.getEditText ());
}

@Override
protected List<String> computeReferenceItems() {
final var list = new ArrayList<String>();
final var tables = ValuesTableFactory.getAllTables();
for (var entry : tables.entrySet()) {
final var dimens = entry.getValue().getTable("bool");
protected List<String> computeReferenceItems () {
final var list = new ArrayList<String> ();
final var tables = ValuesTableFactory.getAllTables ();
for (var entry : tables.entrySet ()) {
final var dimens = entry.getValue ().getTable ("bool");
if (dimens != null) {
list.addAll(
dimens.keySet().stream().map("@bool/"::concat).collect(Collectors.toSet()));
list.addAll (
dimens.keySet ().stream ().map ("@bool/"::concat).collect (Collectors.toSet ()));
}
}

list.addAll(
FrameworkValues.listBools().stream()
.map("@android:bool/"::concat)
.collect(Collectors.toList()));
list.addAll (
FrameworkValues.listBools ().stream ()
.map ("@android:bool/"::concat)
.collect (Collectors.toList ()));
return list;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package com.itsaky.androidide.fragments.attr;

import android.app.Dialog;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
Expand All @@ -27,6 +28,7 @@
import androidx.annotation.Nullable;

import com.google.android.material.textfield.MaterialAutoCompleteTextView;
import com.itsaky.androidide.app.StudioApp;
import com.itsaky.androidide.colorpicker.ColorPickerView;
import com.itsaky.androidide.databinding.LayoutColorAttrEditorBinding;
import com.itsaky.androidide.utils.DialogUtils;
Expand All @@ -40,7 +42,7 @@
/**
* @author Akash Yadav
*/
public class ColorEditor extends ReferenceEditor {
public class ColorEditor extends AbstractReferenceEditor {

private LayoutColorAttrEditorBinding binding;
private Dialog colorPickerDialog;
Expand All @@ -58,6 +60,13 @@ public void onViewCreated (@NonNull View view, @Nullable Bundle savedInstanceSta

this.binding.pickColor.setOnClickListener (v -> getColorPickerDialog ().show ());
setupReferenceInput ((MaterialAutoCompleteTextView) this.binding.colorResInput.getEditText ());

try {
final var col = Color.parseColor (attribute.getValue ());
this.binding.colorPreview.setCardBackgroundColor (col);
} catch (Throwable th) {
// ignored
}
}

@Override
Expand All @@ -78,6 +87,10 @@ protected List<String> computeReferenceItems () {
FrameworkValues.listColors ().stream ()
.map ("@android:color/"::concat)
.collect (Collectors.toList ()));

final var resTable = StudioApp.getInstance ().getResourceTable ();
list.addAll (resTable.listResourceNames ("color"));

return list;
}

Expand Down
Loading

0 comments on commit d71e736

Please sign in to comment.