@@ -6,6 +6,8 @@ import android.view.LayoutInflater
6
6
import android.view.View
7
7
import android.view.ViewGroup
8
8
import android.widget.ArrayAdapter
9
+ import android.widget.EditText
10
+ import android.widget.TextView
9
11
import android.widget.Toast
10
12
import androidx.fragment.app.Fragment
11
13
import androidx.lifecycle.lifecycleScope
@@ -16,6 +18,7 @@ import chip.devicecontroller.ClusterIDMapping.ModeSelect
16
18
import chip.devicecontroller.ReportCallback
17
19
import chip.devicecontroller.WriteAttributesCallback
18
20
import chip.devicecontroller.cluster.structs.ModeSelectClusterModeOptionStruct
21
+ import chip.devicecontroller.model.AttributeState
19
22
import chip.devicecontroller.model.AttributeWriteRequest
20
23
import chip.devicecontroller.model.ChipAttributePath
21
24
import chip.devicecontroller.model.ChipEventPath
@@ -125,60 +128,24 @@ class ModeSelectClientFragment : Fragment() {
125
128
val currentMode = attributeStates[ClusterIDMapping .ModeSelect .Attribute .CurrentMode .id]
126
129
binding.currentModeEd.setText(currentMode?.value.toString())
127
130
128
- val startUpMode = attributeStates[ClusterIDMapping .ModeSelect .Attribute .StartUpMode .id]
129
- val startUpModeVisibility =
130
- if (startUpMode != null ) {
131
- binding.startUpModeEd.setText(startUpMode.value.toString())
132
- View .VISIBLE
133
- } else {
134
- View .GONE
135
- }
136
- binding.startUpModeEd.visibility = startUpModeVisibility
137
- binding.startUpModeTv.visibility = startUpModeVisibility
138
- binding.startUpModeWriteBtn.visibility = startUpModeVisibility
139
-
140
- val onMode = attributeStates[ClusterIDMapping .ModeSelect .Attribute .OnMode .id]
141
- val onModeVisibility =
142
- if (onMode != null ) {
143
- binding.onModeEd.setText(onMode.value.toString())
144
- View .VISIBLE
145
- } else {
146
- View .GONE
147
- }
148
- binding.onModeEd.visibility = onModeVisibility
149
- binding.onModeTv.visibility = onModeVisibility
150
- binding.onModeWriteBtn.visibility = onModeVisibility
131
+ setVisibility(
132
+ attributeStates[ClusterIDMapping .ModeSelect .Attribute .StartUpMode .id],
133
+ binding.startUpModeEd,
134
+ binding.startUpModeTv,
135
+ binding.startUpModeWriteBtn
136
+ )
137
+ setVisibility(
138
+ attributeStates[ClusterIDMapping .ModeSelect .Attribute .OnMode .id],
139
+ binding.onModeEd,
140
+ binding.onModeTv,
141
+ binding.onModeWriteBtn
142
+ )
151
143
152
144
val supportedModesTlv =
153
145
attributeStates[ClusterIDMapping .ModeSelect .Attribute .SupportedModes .id]?.tlv
154
- if (supportedModesTlv != null ) {
155
- var pos = 0
156
- var currentItemId = 0
157
- val modeOptionStructList: List <ModeSelectClusterModeOptionStruct >
158
- TlvReader (supportedModesTlv).also {
159
- modeOptionStructList = buildList {
160
- it.enterArray(AnonymousTag )
161
- while (! it.isEndOfContainer()) {
162
- val struct = ModeSelectClusterModeOptionStruct .fromTlv(AnonymousTag , it)
163
- add(struct)
164
- if (
165
- currentMode != null && struct.mode == currentMode.value.toString().toUInt()
166
- ) {
167
- currentItemId = pos
168
- }
169
- pos++
170
- }
171
- it.exitContainer()
172
- }
173
- binding.supportedModesSp.adapter =
174
- ArrayAdapter (
175
- requireContext(),
176
- android.R .layout.simple_spinner_dropdown_item,
177
- modeOptionStructList.map { it.show() }
178
- )
179
- binding.supportedModesSp.setSelection(currentItemId)
180
- binding.currentModeEd.setText(binding.supportedModesSp.selectedItem.toString())
181
- }
146
+
147
+ supportedModesTlv?.let {
148
+ setSupportedModeSpinner(it, currentMode?.value.toString().toUInt())
182
149
}
183
150
}
184
151
}
@@ -189,6 +156,52 @@ class ModeSelectClientFragment : Fragment() {
189
156
)
190
157
}
191
158
159
+ private fun setVisibility (
160
+ attribute : AttributeState ? ,
161
+ modeEd : EditText ,
162
+ modeTv : TextView ,
163
+ writeBtn : TextView
164
+ ) {
165
+ val modeVisibility =
166
+ if (attribute != null ) {
167
+ modeEd.setText(attribute.value.toString())
168
+ View .VISIBLE
169
+ } else {
170
+ View .GONE
171
+ }
172
+ modeEd.visibility = modeVisibility
173
+ modeTv.visibility = modeVisibility
174
+ writeBtn.visibility = modeVisibility
175
+ }
176
+
177
+ private fun setSupportedModeSpinner (supportedModesTlv : ByteArray , currentModeValue : UInt? ) {
178
+ var pos = 0
179
+ var currentItemId = 0
180
+ val modeOptionStructList: List <ModeSelectClusterModeOptionStruct >
181
+ TlvReader (supportedModesTlv).also {
182
+ modeOptionStructList = buildList {
183
+ it.enterArray(AnonymousTag )
184
+ while (! it.isEndOfContainer()) {
185
+ val struct = ModeSelectClusterModeOptionStruct .fromTlv(AnonymousTag , it)
186
+ add(struct)
187
+ if (currentModeValue != null && struct.mode == currentModeValue) {
188
+ currentItemId = pos
189
+ }
190
+ pos++
191
+ }
192
+ it.exitContainer()
193
+ }
194
+ binding.supportedModesSp.adapter =
195
+ ArrayAdapter (
196
+ requireContext(),
197
+ android.R .layout.simple_spinner_dropdown_item,
198
+ modeOptionStructList.map { it.show() }
199
+ )
200
+ binding.supportedModesSp.setSelection(currentItemId)
201
+ binding.currentModeEd.setText(binding.supportedModesSp.selectedItem.toString())
202
+ }
203
+ }
204
+
192
205
private suspend fun changeToModeBtnClick () {
193
206
val devicePtr =
194
207
try {
0 commit comments