11package com.tencent.iot.explorer.link.demo.core.activity
22
33import android.content.Intent
4- import android.os.Bundle
54import android.view.LayoutInflater
65import android.view.View
76import android.view.ViewGroup
87import android.widget.ImageView
8+ import android.widget.PopupMenu
99import android.widget.TextView
1010import android.widget.Toast
1111import androidx.recyclerview.widget.LinearLayoutManager
1212import androidx.recyclerview.widget.RecyclerView
13- import com.alibaba.fastjson.JSON
1413import com.tencent.iot.explorer.link.core.auth.IoTAuth
1514import com.tencent.iot.explorer.link.core.auth.callback.MyCallback
1615import com.tencent.iot.explorer.link.core.auth.consts.RequestCode
16+ import com.tencent.iot.explorer.link.core.auth.entity.DeviceEntity
1717import com.tencent.iot.explorer.link.core.auth.response.BaseResponse
1818import com.tencent.iot.explorer.link.core.auth.response.DeviceListResponse
1919import com.tencent.iot.explorer.link.core.auth.response.FamilyListResponse
@@ -23,15 +23,14 @@ import com.tencent.iot.explorer.link.demo.App
2323import com.tencent.iot.explorer.link.demo.BaseActivity
2424import com.tencent.iot.explorer.link.demo.R
2525import com.tencent.iot.explorer.link.demo.common.log.L
26- import com.tencent.iot.explorer.link.demo.core.entity.DeviceInfo
2726import com.tencent.iot.explorer.link.demo.databinding.ActivityEnhancedDeviceListBinding
2827
2928/* *
3029 * 增强版设备列表页面
3130 */
3231class EnhancedDeviceListActivity : BaseActivity <ActivityEnhancedDeviceListBinding >(), MyCallback {
3332
34- private val deviceList = mutableListOf<DeviceItem >()
33+ private val deviceList = mutableListOf<DeviceEntity >()
3534 private lateinit var adapter: DeviceAdapter
3635
3736 override fun getViewBinding (): ActivityEnhancedDeviceListBinding =
@@ -103,8 +102,8 @@ class EnhancedDeviceListActivity : BaseActivity<ActivityEnhancedDeviceListBindin
103102 }
104103
105104 // 设置菜单点击事件
106- adapter.setOnMenuClickListener { position, device ->
107- showDeviceMenu(position, device)
105+ adapter.setOnMenuClickListener { position, device, rootView ->
106+ showDeviceMenu(position, device, rootView )
108107 }
109108 }
110109
@@ -122,7 +121,7 @@ class EnhancedDeviceListActivity : BaseActivity<ActivityEnhancedDeviceListBindin
122121 */
123122 private fun showAddDevicePage () {
124123 Toast .makeText(this , " 跳转到添加设备页面" , Toast .LENGTH_SHORT ).show()
125- startActivityForResult(Intent (this , AddDeviceActivity ::class .java),0 )
124+ startActivityForResult(Intent (this , AddDeviceActivity ::class .java), 0 )
126125 }
127126
128127 /* *
@@ -137,18 +136,29 @@ class EnhancedDeviceListActivity : BaseActivity<ActivityEnhancedDeviceListBindin
137136 /* *
138137 * 打开设备详情
139138 */
140- private fun openDeviceDetail (device : DeviceItem ) {
139+ private fun openDeviceDetail (device : DeviceEntity ) {
141140 // TODO: 跳转到设备详情页面
142- Toast .makeText(this , " 打开设备: ${device.name } " , Toast .LENGTH_SHORT ).show()
141+ Toast .makeText(this , " 打开设备: ${device.DeviceName } " , Toast .LENGTH_SHORT ).show()
143142// jumpActivity(CameraDetailActivity::class.java)
144143 }
145144
146145 /* *
147146 * 显示设备菜单
148147 */
149- private fun showDeviceMenu (position : Int , device : DeviceItem ) {
150- // TODO: 显示设备操作菜单(设置、分享、删除等)
151- Toast .makeText(this , " 显示设备菜单: ${device.name} " , Toast .LENGTH_SHORT ).show()
148+ private fun showDeviceMenu (position : Int , device : DeviceEntity , rootView : View ) {
149+ val popupMenu = PopupMenu (this , rootView)
150+ popupMenu.menuInflater.inflate(R .menu.device_menu, popupMenu.menu)
151+ popupMenu.setOnMenuItemClickListener { menuItem ->
152+ when (menuItem.itemId) {
153+ R .id.action_delete -> {
154+ IoTAuth .deviceImpl.deleteDevice(App .data.getCurrentFamily().FamilyId , device.ProductId ,device.DeviceName ,this @EnhancedDeviceListActivity)
155+ true
156+ }
157+
158+ else -> false
159+ }
160+ }
161+ popupMenu.show()
152162 }
153163
154164 /* *
@@ -242,21 +252,19 @@ class EnhancedDeviceListActivity : BaseActivity<ActivityEnhancedDeviceListBindin
242252 L .e(" 设备列表:${JsonManager .toJson(it.DeviceList )} " )
243253 if (it.DeviceList .isNotEmpty()) {
244254 deviceList.clear()
245- it.DeviceList .forEach {
246- deviceList.add(
247- DeviceItem (
248- it.DeviceName ,
249- " 卧室" ,
250- true ,
251- R .drawable.ic_camera_device
252- )
253- )
254- }
255+ deviceList.addAll(it.DeviceList )
255256 refreshDeviceList()
256257 }
257258 }
258259 }
259260 }
261+
262+ RequestCode .delete_device -> {
263+ if (response.isSuccess()) {
264+ Toast .makeText(this , " 设备删除成功" , Toast .LENGTH_SHORT ).show()
265+ initDeviceList()
266+ }
267+ }
260268 }
261269 }
262270
@@ -273,11 +281,11 @@ class EnhancedDeviceListActivity : BaseActivity<ActivityEnhancedDeviceListBindin
273281 /* *
274282 * 设备列表适配器
275283 */
276- class DeviceAdapter (private val deviceList : List <DeviceItem >) :
284+ class DeviceAdapter (private val deviceList : List <DeviceEntity >) :
277285 RecyclerView .Adapter <DeviceAdapter .DeviceViewHolder >() {
278286
279- private var itemClickListener: ((Int , DeviceItem ) -> Unit )? = null
280- private var menuClickListener: ((Int , DeviceItem ) -> Unit )? = null
287+ private var itemClickListener: ((Int , DeviceEntity ) -> Unit )? = null
288+ private var menuClickListener: ((Int , DeviceEntity , View ) -> Unit )? = null
281289
282290 class DeviceViewHolder (itemView : View ) : RecyclerView.ViewHolder(itemView) {
283291 val ivDeviceIcon: ImageView = itemView.findViewById(R .id.iv_device_icon)
@@ -297,12 +305,12 @@ class EnhancedDeviceListActivity : BaseActivity<ActivityEnhancedDeviceListBindin
297305 override fun onBindViewHolder (holder : DeviceViewHolder , position : Int ) {
298306 val device = deviceList[position]
299307
300- holder.tvDeviceName.text = device.name
301- holder.tvDeviceLocation.text = device.location
302- holder.ivDeviceIcon.setImageResource(device.iconRes )
308+ holder.tvDeviceName.text = device.DeviceName
309+ holder.tvDeviceLocation.text = device.RoomId
310+ // holder.ivDeviceIcon.setImageResource(device.IconUrl )
303311
304312 // 设置设备状态
305- if (device.isOnline ) {
313+ if (true ) {
306314 holder.tvDeviceStatus.text = " 在线"
307315 holder.indicatorStatus.setBackgroundResource(R .drawable.bg_status_online)
308316 } else {
@@ -317,17 +325,17 @@ class EnhancedDeviceListActivity : BaseActivity<ActivityEnhancedDeviceListBindin
317325
318326 // 菜单按钮点击事件
319327 holder.btnDeviceMenu.setOnClickListener {
320- menuClickListener?.invoke(position, device)
328+ menuClickListener?.invoke(position, device, holder.btnDeviceMenu )
321329 }
322330 }
323331
324332 override fun getItemCount (): Int = deviceList.size
325333
326- fun setOnItemClickListener (listener : (Int , DeviceItem ) -> Unit ) {
334+ fun setOnItemClickListener (listener : (Int , DeviceEntity ) -> Unit ) {
327335 itemClickListener = listener
328336 }
329337
330- fun setOnMenuClickListener (listener : (Int , DeviceItem ) -> Unit ) {
338+ fun setOnMenuClickListener (listener : (Int , DeviceEntity , View ) -> Unit ) {
331339 menuClickListener = listener
332340 }
333341 }
0 commit comments