What is *your* opinion about helper blocks in App Inventor? (GSoC Project - User Feedback)

The extension needs a boolean value so I thought it could be added with the helper block. the user could only select the appropriate value without dragging logical built-in blocks.

Hmm. I think this is something we could add, but at the moment only Integer and String are supported types.

Edit: Alternatively, you might be able to do the following:

public enum MyOption implements OptionList<Integer> {
  OptionA(false),
  OptionB(true);

  private final boolean value;

  MyOption(boolean value) {
    this.value = value;
  }

  public boolean toBoolean() {
    return value;
  }

  // implement the standard methods for Helper blocks here
}

And then in your extension:

public class MyExtension {
  @SimpleProperty
  public void MyProperty(MyOption input) {
    this.value = input.toBoolean();
  }
}
1 Like

I didn't realize there is a method that converts to a boolean value. Thanks.

You can also be "C-style" and interpret a 0 as false and 1 as true. Not ideal, but it would work.

So far I have worked with this and it worked fine.:

@Options(Animal.class) String animal

When I use it:

Animal animal

I can't work with a string in extension, e.g .:

if (animal == "elephant") {}

This causes a compile error:

error: incomparable types: Animal and String

1 Like

Assuming that Elephant is an item in the enumeration, you would probably want to do this instead:

if (animal == Animal.Elephant) {}
1 Like

Thanks. I am using a workaround:

if (animal + "" == "elephant") {}

but now I can do it right.

An important thing to note here is that the == operator checks identity, not equality, when it comes to objects, so this if statement using strings is likely to fail. If you want to compare strings, use the .equals method instead:

if ("elephant".equals(animal.toString())) {}
1 Like

It works. I would still need a method that returns an integer:

int value = (Integer) animal;
int value = ((Integer) animal).intValue();

Tried this, unfortunately it doesn't work.

I did for the integer something similar to what you suggested for the boolean. It works, but maybe there is something better.

I'd have to see more code in order to understand what you're trying to accomplish here.

Is it posible right now (Shadow blocks)?
If possible, so how to do it for extension development?

No, it is not possible.

1 Like