refactor(offlineCmd): 重构离线命令服务监听器管理
- 将单个监听器改为监听器列表支持多实例注册 - 添加 addOnLineListener 和 removeOnLineListener 方法 - 在 TaskListActivity 中实现监听器的生命周期管理 - 移除无用的导入和变量声明 - 优化页面跳转和翻页逻辑的代码结构 - 在布局文件中添加必要的控件ID用于点击事件处理
这个提交包含在:
父节点
a574d23f07
当前提交
2c5878ab90
@ -1,17 +1,14 @@
|
|||||||
package com.nova.brain.glass.helper
|
package com.nova.brain.glass.helper
|
||||||
|
|
||||||
import com.luck.picture.lib.config.PictureSelectionConfig.listener
|
|
||||||
import com.rokid.security.glass3.open.sdk.GlassSdk
|
import com.rokid.security.glass3.open.sdk.GlassSdk
|
||||||
import com.rokid.security.glass3.sdk.base.data.offlineCmd.bean.VoiceAction
|
import com.rokid.security.glass3.sdk.base.data.offlineCmd.bean.VoiceAction
|
||||||
import com.rokid.security.glass3.sdk.base.data.offlineCmd.listener.IVoiceCallback
|
import com.rokid.security.glass3.sdk.base.data.offlineCmd.listener.IVoiceCallback
|
||||||
import com.xuqm.base.common.LogHelper
|
import com.xuqm.base.common.LogHelper
|
||||||
import com.xuqm.base.common.ToolsHelper
|
|
||||||
import com.xuqm.sdhbwfu.core.extensions.isNull
|
|
||||||
|
|
||||||
data class OfflineCmdBean(val text: String, val pinyin: String)
|
data class OfflineCmdBean(val text: String, val pinyin: String)
|
||||||
|
|
||||||
object OfflineCmdServiceHelper {
|
object OfflineCmdServiceHelper {
|
||||||
private var listener: OfflineCmdListener? = null
|
private var listenerList = mutableListOf<OfflineCmdListener>()
|
||||||
|
|
||||||
private val list = mutableListOf<OfflineCmdBean>().apply {
|
private val list = mutableListOf<OfflineCmdBean>().apply {
|
||||||
add(OfflineCmdBean("任务列表", "ren wu lie biao"))
|
add(OfflineCmdBean("任务列表", "ren wu lie biao"))
|
||||||
@ -37,13 +34,18 @@ object OfflineCmdServiceHelper {
|
|||||||
service.add(VoiceAction(bean.text, bean.pinyin, object : IVoiceCallback.Stub() {
|
service.add(VoiceAction(bean.text, bean.pinyin, object : IVoiceCallback.Stub() {
|
||||||
override fun onVoiceTriggered() {
|
override fun onVoiceTriggered() {
|
||||||
LogHelper.d("onOfflineCmd: ${bean.text}")
|
LogHelper.d("onOfflineCmd: ${bean.text}")
|
||||||
listener?.onOfflineCmd(bean.text)
|
for (l in listenerList) {
|
||||||
|
l.onOfflineCmd(bean.text)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fun setOnLineListener(listener: OfflineCmdListener){
|
fun addOnLineListener(listener: OfflineCmdListener){
|
||||||
this.listener = listener
|
this.listenerList.add(listener)
|
||||||
|
}
|
||||||
|
fun removeOnLineListener(listener: OfflineCmdListener){
|
||||||
|
this.listenerList.remove(listener)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
package com.nova.brain.glass.ui
|
package com.nova.brain.glass.ui
|
||||||
|
|
||||||
import android.content.Intent
|
import android.os.Bundle
|
||||||
|
import androidx.recyclerview.widget.LinearLayoutManager
|
||||||
import com.nova.brain.glass.R
|
import com.nova.brain.glass.R
|
||||||
import com.nova.brain.glass.databinding.ActivityTaskListBinding
|
import com.nova.brain.glass.databinding.ActivityTaskListBinding
|
||||||
import com.nova.brain.glass.helper.OfflineCmdListener
|
import com.nova.brain.glass.helper.OfflineCmdListener
|
||||||
@ -11,8 +12,6 @@ import com.xuqm.base.adapter.BasePagedAdapter
|
|||||||
import com.xuqm.base.adapter.CommonPagedAdapter
|
import com.xuqm.base.adapter.CommonPagedAdapter
|
||||||
import com.xuqm.base.adapter.ViewHolder
|
import com.xuqm.base.adapter.ViewHolder
|
||||||
import com.xuqm.base.common.ToolsHelper
|
import com.xuqm.base.common.ToolsHelper
|
||||||
import androidx.recyclerview.widget.LinearLayoutManager
|
|
||||||
import com.xuqm.base.common.LogHelper
|
|
||||||
import com.xuqm.base.ui.BaseListFormLayoutActivity
|
import com.xuqm.base.ui.BaseListFormLayoutActivity
|
||||||
|
|
||||||
class TaskListActivity :
|
class TaskListActivity :
|
||||||
@ -20,24 +19,32 @@ class TaskListActivity :
|
|||||||
override fun getLayoutId(): Int = R.layout.activity_task_list
|
override fun getLayoutId(): Int = R.layout.activity_task_list
|
||||||
override fun fullscreen(): Boolean = true
|
override fun fullscreen(): Boolean = true
|
||||||
|
|
||||||
override fun initData() {
|
override fun initView(savedInstanceState: Bundle?) {
|
||||||
super.initData()
|
super.initView(savedInstanceState)
|
||||||
OfflineCmdServiceHelper.setOnLineListener(object : OfflineCmdListener {
|
binding.xia.setOnClickListener {
|
||||||
|
toNext()
|
||||||
|
}
|
||||||
|
binding.shang.setOnClickListener {
|
||||||
|
toPre()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private val offlineCmdListener = object : OfflineCmdListener {
|
||||||
override fun onOfflineCmd(cmd: String) {
|
override fun onOfflineCmd(cmd: String) {
|
||||||
when (cmd) {
|
when (cmd) {
|
||||||
"下一页", "翻页", "查看下一页", "继续翻页", "继续下一页" -> {
|
"下一页", "翻页", "查看下一页", "继续翻页", "继续下一页" -> {
|
||||||
val layoutManager = binding.baseRecyclerView.layoutManager as? LinearLayoutManager
|
toNext()
|
||||||
if (layoutManager != null) {
|
|
||||||
val lastVisible = layoutManager.findLastVisibleItemPosition()
|
|
||||||
val nextPosition = lastVisible + 1
|
|
||||||
if (nextPosition < adapter.itemCount) {
|
|
||||||
layoutManager.scrollToPositionWithOffset(nextPosition, 0)
|
|
||||||
} else {
|
|
||||||
ToolsHelper.showMessage("没有下一页了")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
"上一页", "查看上一页", "继续上一页" -> {
|
"上一页", "查看上一页", "继续上一页" -> {
|
||||||
|
toPre()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun toPre() {
|
||||||
val layoutManager = binding.baseRecyclerView.layoutManager as? LinearLayoutManager
|
val layoutManager = binding.baseRecyclerView.layoutManager as? LinearLayoutManager
|
||||||
if (layoutManager != null) {
|
if (layoutManager != null) {
|
||||||
val firstVisible = layoutManager.findFirstVisibleItemPosition()
|
val firstVisible = layoutManager.findFirstVisibleItemPosition()
|
||||||
@ -51,10 +58,28 @@ class TaskListActivity :
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
private fun toNext() {
|
||||||
|
val layoutManager = binding.baseRecyclerView.layoutManager as? LinearLayoutManager
|
||||||
|
if (layoutManager != null) {
|
||||||
|
val lastVisible = layoutManager.findLastVisibleItemPosition()
|
||||||
|
if (lastVisible < adapter.itemCount) {
|
||||||
|
layoutManager.scrollToPositionWithOffset(lastVisible, 0)
|
||||||
|
} else {
|
||||||
|
ToolsHelper.showMessage("没有下一页了")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun onResume() {
|
||||||
|
super.onResume()
|
||||||
|
OfflineCmdServiceHelper.addOnLineListener(offlineCmdListener)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onPause() {
|
||||||
|
super.onPause()
|
||||||
|
OfflineCmdServiceHelper.removeOnLineListener(offlineCmdListener)
|
||||||
|
}
|
||||||
|
|
||||||
private val adapter = object : CommonPagedAdapter<TaskItem>(R.layout.item_task_list) {
|
private val adapter = object : CommonPagedAdapter<TaskItem>(R.layout.item_task_list) {
|
||||||
override fun convert(holder: ViewHolder, item: TaskItem, position: Int) {
|
override fun convert(holder: ViewHolder, item: TaskItem, position: Int) {
|
||||||
holder
|
holder
|
||||||
|
|||||||
@ -2,14 +2,11 @@ package com.nova.brain.glass.ui
|
|||||||
|
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.util.Log
|
|
||||||
import com.nova.brain.glass.R
|
import com.nova.brain.glass.R
|
||||||
import com.nova.brain.glass.databinding.ActivityWelcomeBinding
|
import com.nova.brain.glass.databinding.ActivityWelcomeBinding
|
||||||
import com.nova.brain.glass.helper.OfflineCmdListener
|
import com.nova.brain.glass.helper.OfflineCmdListener
|
||||||
import com.nova.brain.glass.helper.OfflineCmdServiceHelper
|
import com.nova.brain.glass.helper.OfflineCmdServiceHelper
|
||||||
import com.rokid.security.glass3.open.sdk.GlassSdk
|
import com.xuqm.base.common.LogHelper
|
||||||
import com.rokid.security.glass3.sdk.base.data.offlineCmd.bean.VoiceAction
|
|
||||||
import com.rokid.security.glass3.sdk.base.data.offlineCmd.listener.IVoiceCallback
|
|
||||||
import com.xuqm.base.ui.BaseActivity
|
import com.xuqm.base.ui.BaseActivity
|
||||||
import java.util.Timer
|
import java.util.Timer
|
||||||
import kotlin.concurrent.schedule
|
import kotlin.concurrent.schedule
|
||||||
@ -21,7 +18,12 @@ class WelcomeActivity : BaseActivity<ActivityWelcomeBinding>() {
|
|||||||
|
|
||||||
override fun initView(savedInstanceState: Bundle?) {
|
override fun initView(savedInstanceState: Bundle?) {
|
||||||
super.initView(savedInstanceState)
|
super.initView(savedInstanceState)
|
||||||
OfflineCmdServiceHelper.setOnLineListener(object : OfflineCmdListener {
|
binding.tv.setOnClickListener {
|
||||||
|
startActivity(Intent(this@WelcomeActivity, TaskListActivity::class.java))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private val offlineCmdListener = object : OfflineCmdListener {
|
||||||
override fun onOfflineCmd(cmd: String) {
|
override fun onOfflineCmd(cmd: String) {
|
||||||
when (cmd) {
|
when (cmd) {
|
||||||
"任务列表", "查看任务", "查看任务列表" -> {
|
"任务列表", "查看任务", "查看任务列表" -> {
|
||||||
@ -29,12 +31,19 @@ class WelcomeActivity : BaseActivity<ActivityWelcomeBinding>() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
|
||||||
Timer().schedule(3000) {
|
|
||||||
startActivity(Intent(this@WelcomeActivity, MainActivity::class.java))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun onResume() {
|
||||||
|
super.onResume()
|
||||||
|
LogHelper.d(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
|
||||||
|
OfflineCmdServiceHelper.addOnLineListener(offlineCmdListener)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onPause() {
|
||||||
|
super.onPause()
|
||||||
|
LogHelper.d("---------------------------")
|
||||||
|
OfflineCmdServiceHelper.removeOnLineListener(offlineCmdListener)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -42,6 +42,7 @@
|
|||||||
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
|
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
|
android:id="@+id/shang"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_marginTop="20dp"
|
android:layout_marginTop="20dp"
|
||||||
@ -54,6 +55,7 @@
|
|||||||
app:layout_constraintStart_toStartOf="parent"
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
app:layout_constraintTop_toBottomOf="@+id/baseRefreshLayout" />
|
app:layout_constraintTop_toBottomOf="@+id/baseRefreshLayout" />
|
||||||
<TextView
|
<TextView
|
||||||
|
android:id="@+id/xia"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_marginTop="20dp"
|
android:layout_marginTop="20dp"
|
||||||
|
|||||||
@ -5,6 +5,7 @@
|
|||||||
android:background="@color/app_color_black"
|
android:background="@color/app_color_black"
|
||||||
android:layout_height="match_parent">
|
android:layout_height="match_parent">
|
||||||
<TextView
|
<TextView
|
||||||
|
android:id="@+id/tv"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:background="@color/app_color_black"
|
android:background="@color/app_color_black"
|
||||||
|
|||||||
正在加载...
在新工单中引用
屏蔽一个用户