fix: 解绑不跳转+绑定偶尔失败
1. 事件监听提前到onViewCreated,不等API返回,避免解绑消息丢失 2. 解绑导航加防护:子页面时先popBack再跳转,防止导航异常 3. 绑定加防重复标记,防止MQTT重发导致多次处理 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -148,7 +148,14 @@ class BindFragment : BaseFragment<FragmentBindBinding>() {
|
|||||||
* 3. 调用 bindWatchConfirm API 确认
|
* 3. 调用 bindWatchConfirm API 确认
|
||||||
* 4. 导航到首页
|
* 4. 导航到首页
|
||||||
*/
|
*/
|
||||||
|
/** 防重复处理标记 */
|
||||||
|
private var bindHandled = false
|
||||||
|
|
||||||
private fun handleBindSuccess(rawJson: String) {
|
private fun handleBindSuccess(rawJson: String) {
|
||||||
|
// 防止重复处理(MQTT 可能重发)
|
||||||
|
if (bindHandled) return
|
||||||
|
bindHandled = true
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Timber.d("绑定: 收到绑定消息 $rawJson")
|
Timber.d("绑定: 收到绑定消息 $rawJson")
|
||||||
|
|
||||||
@@ -185,6 +192,7 @@ class BindFragment : BaseFragment<FragmentBindBinding>() {
|
|||||||
|
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Timber.e(e, "绑定: 处理绑定消息异常")
|
Timber.e(e, "绑定: 处理绑定消息异常")
|
||||||
|
bindHandled = false // 异常时允许重试
|
||||||
// 异常时恢复二维码显示
|
// 异常时恢复二维码显示
|
||||||
showQrCode()
|
showQrCode()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -136,7 +136,10 @@ class HomeFragment : BaseFragment<FragmentHomeBinding>() {
|
|||||||
updateCardNum(tvPunchNum, cachedPunch)
|
updateCardNum(tvPunchNum, cachedPunch)
|
||||||
updateCardNum(tvCompleteNum, cachedComplete)
|
updateCardNum(tvCompleteNum, cachedComplete)
|
||||||
|
|
||||||
// 再异步请求最新数据,静默更新 + 持久化
|
// 立即开始监听事件(不等 API 返回,避免解绑等关键消息丢失)
|
||||||
|
observeEvents()
|
||||||
|
|
||||||
|
// 异步请求最新统计数据
|
||||||
viewLifecycleOwner.lifecycleScope.launch {
|
viewLifecycleOwner.lifecycleScope.launch {
|
||||||
val result = safeApiCall { taskApi.getStatistics() }
|
val result = safeApiCall { taskApi.getStatistics() }
|
||||||
if (result is ApiResult.Success && result.data != null) {
|
if (result is ApiResult.Success && result.data != null) {
|
||||||
@@ -152,9 +155,6 @@ class HomeFragment : BaseFragment<FragmentHomeBinding>() {
|
|||||||
.putInt("complete", data.incompleteTask)
|
.putInt("complete", data.incompleteTask)
|
||||||
.apply()
|
.apply()
|
||||||
}
|
}
|
||||||
|
|
||||||
// 基准设好后再开始监听事件
|
|
||||||
observeEvents()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 监听打卡状态
|
// 监听打卡状态
|
||||||
@@ -631,7 +631,19 @@ class HomeFragment : BaseFragment<FragmentHomeBinding>() {
|
|||||||
Timber.d("首页: 收到解绑消息")
|
Timber.d("首页: 收到解绑消息")
|
||||||
bluetoothScanManager.stop()
|
bluetoothScanManager.stop()
|
||||||
userPrefs.clear()
|
userPrefs.clear()
|
||||||
findNavController().navigate(R.id.action_home_to_bind)
|
try {
|
||||||
|
// 确保当前在 homeFragment 才能导航
|
||||||
|
val navController = findNavController()
|
||||||
|
if (navController.currentDestination?.id == R.id.homeFragment) {
|
||||||
|
navController.navigate(R.id.action_home_to_bind)
|
||||||
|
} else {
|
||||||
|
// 在子页面(任务列表/详情),先弹回首页再跳绑定
|
||||||
|
navController.popBackStack(R.id.homeFragment, false)
|
||||||
|
navController.navigate(R.id.action_home_to_bind)
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Timber.w(e, "首页: 解绑导航异常")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
4 -> {
|
4 -> {
|
||||||
// 系统参数变更(蓝牙/NFC 配置)
|
// 系统参数变更(蓝牙/NFC 配置)
|
||||||
|
|||||||
Reference in New Issue
Block a user