This repository has been archived by the owner on Oct 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 116
/
page-params-by-href.groovy
144 lines (122 loc) · 4.5 KB
/
page-params-by-href.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/**
* Example of passing params via href element to a dynamic page
*
* Copyright 2015 SmartThings
*
* 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: "href params example",
namespace: "smartthings",
author: "SmartThings",
description: "passing params via href element to a dynamic page",
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")
// ========================================================
// PAGES
// ========================================================
preferences {
page(name: "firstPage")
page(name: "secondPage")
page(name: "thirdPage")
}
def firstPage() {
def hrefParams = [
foo: "bar",
thisIs: "totally working",
install: checkSomeCustomLogic(),
nextPage: "thirdPage"
]
dynamicPage(name: "firstPage", uninstall: true) {
/*
* The href element accepts a key of `params` and expects a `Map` as the value.
* Anything passed in `params` will be sent along with the request but will not be persisted in any way.
* The params will be used going to the next page but will not be available when backing out of that page.
* This, combined with the fact that pages refresh when navigating back to them, means that your params will not be
* available if your user hits the back button.
*/
section {
href(
name: "toSecondPage",
page: "secondPage",
params: hrefParams,
description: "includes params: ${hrefParams}",
state: hrefState()
)
}
}
}
def secondPage(params) {
/*
* firstPage included `params` in the href element that navigated to here.
* You must specify some variable name in the method declaration. (I used 'params' here, but it can be any variable name you want).
* If you do not specify a variable name, there is no way to get the params that you specified in your `href` element.
*/
log.debug "params: ${params}"
dynamicPage(name: "secondPage", uninstall: true, install: params?.install) {
if (params.nextPage) {
section {
href(
name: "toNextPage",
page: params.nextPage,
description: hrefState() ? "You've already been to the third page": "go checkout the third page",
state: hrefState()
)
}
} else {
section {
paragraph "There were no params included when fetching this page."
}
}
}
}
def thirdPage() {
state.reachedThirdPage = true
dynamicPage(name: "thirdPage") {
section {
image "https://placekitten.com/g/600/500"
}
}
}
// ========================================================
// HELPERS
// ========================================================
def checkSomeCustomLogic() {
// ...
false
}
def hrefState() {
/*
* `state: "complete"` makes the right side of the href green.
* It's a great way to show the user that they've already set up some stuff on that page.
* In other words, it's a great way to show state ;)
* If you're using hrefs, it's a good idea to set `state` when appropriate.
*/
state.reachedThirdPage ? "complete": ""
}
// ========================================================
// HANDLERS
// ========================================================
def installed() {
log.debug "Installed with settings: ${settings}"
initialize()
}
def updated() {
log.debug "Updated with settings: ${settings}"
unsubscribe()
initialize()
}
def initialize() {
// TODO: subscribe to attributes, devices, locations, etc.
}
// TODO: implement event handlers