Skip to content

Commit

Permalink
Add support for populating selection widget from item options.
Browse files Browse the repository at this point in the history
Fixes openhab#912

Signed-off-by: Danny Baumann <[email protected]>
  • Loading branch information
maniac103 committed Jun 18, 2018
1 parent 6659946 commit 8a61c53
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
17 changes: 17 additions & 0 deletions mobile/src/main/java/org/openhab/habdroid/model/OpenHABItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public enum Type {
public abstract boolean readOnly();
public abstract List<OpenHABItem> members();
@Nullable
public abstract List<OpenHABLabeledValue> options();
@Nullable
public abstract String state();
public abstract boolean stateAsBoolean();
public abstract float stateAsFloat();
Expand All @@ -80,6 +82,7 @@ abstract static class Builder {
public abstract Builder link(@Nullable String link);
public abstract Builder readOnly(boolean readOnly);
public abstract Builder members(List<OpenHABItem> members);
public abstract Builder options(@Nullable List<OpenHABLabeledValue> options);

public OpenHABItem build() {
String state = state();
Expand Down Expand Up @@ -234,6 +237,19 @@ public static OpenHABItem fromJson(JSONObject jsonObject) throws JSONException {
? stateDescription.optBoolean("readOnly", false)
: false;

List<OpenHABLabeledValue> options = null;
if (stateDescription != null && stateDescription.has("options")) {
JSONArray optionsJson = stateDescription.getJSONArray("options");
options = new ArrayList<>();
for (int i = 0; i < optionsJson.length(); i++) {
JSONObject optionJson = optionsJson.getJSONObject(i);
options.add(OpenHABLabeledValue.newBuilder()
.value(optionJson.getString("value"))
.label(optionJson.getString("label"))
.build());
}
}

List<OpenHABItem> members = new ArrayList<>();
JSONArray membersJson = jsonObject.optJSONArray("members");
if (membersJson != null) {
Expand All @@ -249,6 +265,7 @@ public static OpenHABItem fromJson(JSONObject jsonObject) throws JSONException {
.label(jsonObject.optString("label", name))
.link(jsonObject.optString("link", null))
.members(members)
.options(options)
.state(state)
.readOnly(readOnly)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,17 @@ public enum Type {
public abstract int height();

public boolean hasMappings() {
return !mappings().isEmpty();
}

public boolean hasMappingsOrItemOptions() {
return !getMappingsOrItemOptions().isEmpty();
}

public List<OpenHABLabeledValue> getMappingsOrItemOptions() {
List<OpenHABLabeledValue> mappings = mappings();
return mappings != null && !mappings.isEmpty();
List<OpenHABLabeledValue> options = item() != null ? item().options() : null;
return mappings.isEmpty() && options != null ? options : mappings;
}

abstract Builder toBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,7 @@ public void stop() {
public static class SelectionViewHolder extends LabeledItemBaseViewHolder
implements AdapterView.OnItemClickListener {
private final Spinner mSpinner;
private List<OpenHABLabeledValue> mBoundMappings;

SelectionViewHolder(LayoutInflater inflater, ViewGroup parent,
Connection conn, ColorMapper colorMapper) {
Expand All @@ -633,11 +634,14 @@ public static class SelectionViewHolder extends LabeledItemBaseViewHolder
public void bind(OpenHABWidget widget) {
super.bind(widget);

OpenHABItem item = widget.item();
mBoundMappings = widget.getMappingsOrItemOptions();

int spinnerSelectedIndex = -1;
ArrayList<String> spinnerArray = new ArrayList<>();
String state = widget.item() != null ? widget.item().state() : null;
String state = item != null ? item.state() : null;

for (OpenHABLabeledValue mapping : widget.mappings()) {
for (OpenHABLabeledValue mapping : mBoundMappings) {
String command = mapping.value();
spinnerArray.add(mapping.label());
if (command != null && command.equals(state)) {
Expand Down Expand Up @@ -670,9 +674,9 @@ public void onItemClick(AdapterView<?> parent, View view, int index, long id) {
Log.d(TAG, "Spinner onItemSelected selected label = " + selectedLabel);

OpenHABWidget widget = (OpenHABWidget) parent.getTag();
if (index < widget.mappings().size()) {
Log.d(TAG, "Label selected = " + widget.mappings().get(index).label());
for (OpenHABLabeledValue mapping : widget.mappings()) {
if (index < mBoundMappings.size()) {
Log.d(TAG, "Label selected = " + mBoundMappings.get(index).label());
for (OpenHABLabeledValue mapping : mBoundMappings) {
if (mapping.label().equals(selectedLabel)) {
Log.d(TAG, "Spinner onItemSelected found match with " + mapping.value());
Util.sendItemCommand(mConnection.getAsyncHttpClient(), widget.item(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ public void onItemLongClicked(final OpenHABWidget widget) {
if (widget.item() != null) {
// If the widget has mappings, we will populate names and commands with
// values from those mappings
if (widget.hasMappings()) {
for (OpenHABLabeledValue mapping : widget.mappings()) {
if (widget.hasMappingsOrItemOptions()) {
for (OpenHABLabeledValue mapping : widget.getMappingsOrItemOptions()) {
labels.add(mapping.label());
commands.add(mapping.value());
}
Expand Down

0 comments on commit 8a61c53

Please sign in to comment.