Skip to content

Commit

Permalink
add back class description
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonLiuZhuoCheng committed Nov 3, 2021
1 parent fe92941 commit d1d2091
Showing 1 changed file with 31 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel

/**
* ClusterDetailFragment allows user to pick cluster, command, specify parameters and see
* the callback result.
*/
class ClusterDetailFragment : Fragment() {
private val deviceController: ChipDeviceController
get() = ChipClient.getDeviceController(requireContext())
Expand All @@ -55,7 +59,8 @@ class ClusterDetailFragment : Fragment() {
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
clusterMap = checkNotNull(requireArguments().getSerializable(CLUSTER_MAP_INFO )) as HashMap<String, ClusterInfo>
clusterMap =
checkNotNull(requireArguments().getSerializable(CLUSTER_MAP_INFO)) as HashMap<String, ClusterInfo>
devicePtr = checkNotNull(requireArguments().getLong(DEVICE_PTR))
return inflater.inflate(R.layout.cluster_detail_fragment, container, false).apply {
deviceController.setCompletionListener(ChipControllerCallback())
Expand All @@ -69,14 +74,15 @@ class ClusterDetailFragment : Fragment() {
commandArguments.put(it.parameterName.text.toString(), data)
}

selectedCommandInfo.getCommandFunction().invokeCommand(selectedCluster, selectedCommandCallback, commandArguments)
selectedCommandInfo.getCommandFunction()
.invokeCommand(selectedCluster, selectedCommandCallback, commandArguments)
}
}
}

private fun castCorrectType(type: Class<*>, data: String): Any? {

return when (type){
return when (type) {
Int::class.java -> data.toInt()
String::class.java -> data
Boolean::class.java -> data.toBoolean()
Expand All @@ -96,7 +102,8 @@ class ClusterDetailFragment : Fragment() {
commandAutoComplete: AutoCompleteTextView
) {
val clusterNameList = constructHint(clusterMap)
val clusterAdapter = ArrayAdapter(requireContext(), android.R.layout.simple_list_item_1, clusterNameList)
val clusterAdapter =
ArrayAdapter(requireContext(), android.R.layout.simple_list_item_1, clusterNameList)
clusterAutoComplete.setAdapter(clusterAdapter)
clusterAutoComplete.setOnItemClickListener { parent, view, position, id ->

Expand All @@ -117,13 +124,19 @@ class ClusterDetailFragment : Fragment() {
selectedCluster = selectedClusterInfo.createClusterFunction.create(devicePtr, endpointId)
val selectedCommand: String = commandAutoComplete.adapter.getItem(position).toString()
selectedCommandInfo = selectedClusterInfo.commands[selectedCommand]!!
selectedCommandCallback = selectedCommandInfo.commandCallbackSupplier.get()
selectedCommandCallback = selectedCommandInfo.commandCallbackSupplier.get()
populateCommandParameter(inflater, parameterList)
selectedCommandCallback?.setCallbackDelegate(object : ClusterCommandCallback {
override fun onSuccess(responseValues: Map<String, Any>) {
showMessage("Command success")
// Populate UI based on response values. We know the types from CommandInfo.getCommandResponses().
requireActivity().runOnUiThread { populateCallbackResult(responseValues, inflater, callbackList) }
requireActivity().runOnUiThread {
populateCallbackResult(
responseValues,
inflater,
callbackList
)
}
responseValues.forEach { Log.d(TAG, it.toString()) }
}

Expand All @@ -145,7 +158,11 @@ class ClusterDetailFragment : Fragment() {
}
}

private fun populateCallbackResult(responseValues: Map<String, Any>, inflater: LayoutInflater, callbackList: LinearLayout) {
private fun populateCallbackResult(
responseValues: Map<String, Any>,
inflater: LayoutInflater,
callbackList: LinearLayout
) {
responseValues.forEach { (variableNameType, response) ->
val callback = inflater.inflate(R.layout.callback_item, null, false) as LinearLayout
callback.callbackName.text = variableNameType.split(',')[0]
Expand All @@ -156,7 +173,8 @@ class ClusterDetailFragment : Fragment() {
}

private fun getCommand(
clusterName: String): ArrayAdapter<String> {
clusterName: String
): ArrayAdapter<String> {
selectedClusterInfo = clusterMap[clusterName]!!
val commandNameList = constructHint(selectedClusterInfo.commands as HashMap<String, Any>)
return ArrayAdapter(requireContext(), android.R.layout.simple_list_item_1, commandNameList)
Expand Down Expand Up @@ -199,15 +217,17 @@ class ClusterDetailFragment : Fragment() {
private const val CLUSTER_MAP_INFO = "cluster_map_info"
private const val DEVICE_PTR = "device_ptr"
private const val ENDPOINT_ID = "endpoint_id"
fun newInstance(clusterMap: HashMap<String, ClusterInfo>, deviceId: Long): ClusterDetailFragment {
fun newInstance(
clusterMap: HashMap<String, ClusterInfo>,
deviceId: Long
): ClusterDetailFragment {
return ClusterDetailFragment().apply {
arguments = Bundle(1).apply {
putSerializable(CLUSTER_MAP_INFO, clusterMap)
putLong(DEVICE_PTR, deviceId)
}
}
}


}

}

0 comments on commit d1d2091

Please sign in to comment.