Created
November 18, 2014 14:03
-
-
Save bflorian/77c4b33db3a58ce6fb46 to your computer and use it in GitHub Desktop.
SmartApp: List Edit Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* List Edit Example | |
* | |
* Copyright 2014 Bob Florian | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | |
* in compliance with the License. You may obtain a copy of the License at: | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed | |
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License | |
* for the specific language governing permissions and limitations under the License. | |
* | |
*/ | |
definition( | |
name: "List Edit Example", | |
namespace: "bflorian", | |
author: "Bob Florian", | |
description: "SmartApp that allows editing of a list", | |
category: "", | |
iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png", | |
iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png", | |
iconX3Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png") | |
preferences { | |
page name: "mainPage", title: "A list of things", install: true, uninstall: true | |
page name: "editPage", title: "Edit a thing" | |
page name: "deletePage", title: "Delete a thing" | |
page name: "doDelete", title: "Actually do the delete" | |
} | |
def installed() { | |
log.debug "Installed with settings: ${settings}" | |
state.things = [] | |
} | |
def updated() { | |
log.debug "Updated with settings: ${settings}" | |
} | |
def initialize() { | |
// TODO: subscribe to attributes, devices, locations, etc. | |
} | |
def mainPage() { | |
dynamicPage(name: "mainPage") { | |
section { | |
settings.sort{it.value}.each { | |
def key = it.key | |
def value = it.value | |
if (value) { | |
href "editPage", title: "$value", params: [key: key], description: "", state: "complete" | |
} | |
} | |
} | |
section { | |
href "editPage", title: "New Thing", params: [key: nextKey()], description: "" | |
} | |
} | |
} | |
def editPage(params) { | |
dynamicPage(name: "editPage", title: "Edit ${settings[params.key]}") { | |
section { | |
input params.key, "text", title: "Name" | |
} | |
if (settings[params.key]) { | |
section { | |
href "deletePage", title: "Delete", params: [key: params.key], description: "" | |
} | |
} | |
} | |
} | |
def deletePage(params) { | |
dynamicPage(name: "deletePage", title: "Delete ${settings[params.key]}?") { | |
section { | |
href "doDelete", title: "Yes", params: [key: params.key], description: "" | |
} | |
section { | |
href "editPage", title: "No", params: [key: params.key], description: "" | |
} | |
} | |
} | |
def doDelete(params) { | |
app.updateSetting(params.key, "") | |
mainPage() | |
} | |
private nextKey() { | |
def n = -1 | |
settings.each {k,v -> | |
n = Math.max(n, (k - "name").toInteger()) + 1 | |
} | |
def key = "name$n" | |
log.trace key | |
key | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment