diff --git a/mobile/src/main/java/org/openhab/habdroid/model/OpenHABItem.java b/mobile/src/main/java/org/openhab/habdroid/model/OpenHABItem.java index 919a132bec..a25f65e105 100644 --- a/mobile/src/main/java/org/openhab/habdroid/model/OpenHABItem.java +++ b/mobile/src/main/java/org/openhab/habdroid/model/OpenHABItem.java @@ -57,6 +57,8 @@ public enum Type { public abstract boolean readOnly(); public abstract List members(); @Nullable + public abstract List options(); + @Nullable public abstract String state(); public abstract boolean stateAsBoolean(); public abstract float stateAsFloat(); @@ -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 members); + public abstract Builder options(@Nullable List options); public OpenHABItem build() { String state = state(); @@ -234,6 +237,19 @@ public static OpenHABItem fromJson(JSONObject jsonObject) throws JSONException { ? stateDescription.optBoolean("readOnly", false) : false; + List 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 members = new ArrayList<>(); JSONArray membersJson = jsonObject.optJSONArray("members"); if (membersJson != null) { @@ -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(); diff --git a/mobile/src/main/java/org/openhab/habdroid/model/OpenHABWidget.java b/mobile/src/main/java/org/openhab/habdroid/model/OpenHABWidget.java index 392044037f..3a208e60f6 100644 --- a/mobile/src/main/java/org/openhab/habdroid/model/OpenHABWidget.java +++ b/mobile/src/main/java/org/openhab/habdroid/model/OpenHABWidget.java @@ -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 getMappingsOrItemOptions() { List mappings = mappings(); - return mappings != null && !mappings.isEmpty(); + List options = item() != null ? item().options() : null; + return mappings.isEmpty() && options != null ? options : mappings; } abstract Builder toBuilder(); diff --git a/mobile/src/main/java/org/openhab/habdroid/ui/OpenHABWidgetAdapter.java b/mobile/src/main/java/org/openhab/habdroid/ui/OpenHABWidgetAdapter.java index 032b757a43..80d756bc78 100644 --- a/mobile/src/main/java/org/openhab/habdroid/ui/OpenHABWidgetAdapter.java +++ b/mobile/src/main/java/org/openhab/habdroid/ui/OpenHABWidgetAdapter.java @@ -622,6 +622,7 @@ public void stop() { public static class SelectionViewHolder extends LabeledItemBaseViewHolder implements AdapterView.OnItemClickListener { private final Spinner mSpinner; + private List mBoundMappings; SelectionViewHolder(LayoutInflater inflater, ViewGroup parent, Connection conn, ColorMapper colorMapper) { @@ -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 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)) { @@ -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(), diff --git a/mobile/src/main/java/org/openhab/habdroid/ui/OpenHABWidgetListFragment.java b/mobile/src/main/java/org/openhab/habdroid/ui/OpenHABWidgetListFragment.java index dffe2b8189..e07c383d95 100644 --- a/mobile/src/main/java/org/openhab/habdroid/ui/OpenHABWidgetListFragment.java +++ b/mobile/src/main/java/org/openhab/habdroid/ui/OpenHABWidgetListFragment.java @@ -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()); }