feat: 消息通知模块(横幅+红点+跳转)
MQTT type=1 新任务推送 → 震动+亮屏+顶部蓝色横幅+卡片红点。 新增: - NotificationManager: 去抖1s+内存存储taskIds+统计对比红点 - NotificationBannerView: Activity层横幅(滑入/10s倒计时/点击) - AppEvent.NewTaskArrived: 携带taskIds和count 集成: - MainActivity: 监听MQTT type=1→NotificationManager→横幅 - HomeFragment: 监听NewTaskArrived→刷新统计+对比红点+横幅点击跳转 - page_main.xml: 3个卡片各加红点角标(FrameLayout包裹) - nav_main.xml: 新增action_home_to_taskDetail Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -7,8 +7,16 @@ import android.view.View
|
||||
import androidx.activity.OnBackPressedCallback
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import com.xiaoqu.watch.databinding.ActivityMainBinding
|
||||
import com.xiaoqu.watch.event.AppEvent
|
||||
import com.xiaoqu.watch.event.EventBus
|
||||
import com.xiaoqu.watch.service.manager.NotificationManager
|
||||
import com.xiaoqu.watch.service.manager.SystemStateMonitor
|
||||
import com.xiaoqu.watch.ui.widget.NotificationBannerView
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.SupervisorJob
|
||||
import kotlinx.coroutines.launch
|
||||
import timber.log.Timber
|
||||
import javax.inject.Inject
|
||||
import kotlin.math.abs
|
||||
@@ -24,6 +32,16 @@ class MainActivity : AppCompatActivity() {
|
||||
|
||||
/** 系统状态监听器(电量、蓝牙状态) */
|
||||
@Inject lateinit var systemStateMonitor: SystemStateMonitor
|
||||
/** 消息通知管理器 */
|
||||
@Inject lateinit var notificationManager: NotificationManager
|
||||
/** 事件总线 */
|
||||
@Inject lateinit var eventBus: EventBus
|
||||
|
||||
/** 通知横幅 */
|
||||
private lateinit var notificationBanner: NotificationBannerView
|
||||
|
||||
/** Activity 协程作用域 */
|
||||
private val activityScope = CoroutineScope(Dispatchers.Main + SupervisorJob())
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
@@ -47,13 +65,43 @@ class MainActivity : AppCompatActivity() {
|
||||
// 注册系统状态监听(电量、蓝牙)
|
||||
systemStateMonitor.register()
|
||||
|
||||
// 初始化通知横幅
|
||||
notificationBanner = binding.notificationBanner
|
||||
|
||||
// 监听 MQTT 新任务消息,显示横幅
|
||||
observeMqttMessages()
|
||||
|
||||
Timber.d("MainActivity created")
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
super.onDestroy()
|
||||
// 取消系统状态监听
|
||||
systemStateMonitor.unregister()
|
||||
notificationBanner.destroy()
|
||||
}
|
||||
|
||||
// ===== MQTT 新任务 → 通知横幅 =====
|
||||
|
||||
/** 监听 MQTT 消息,type=1 时通知横幅 */
|
||||
private fun observeMqttMessages() {
|
||||
activityScope.launch {
|
||||
eventBus.events.collect { event ->
|
||||
when (event) {
|
||||
is AppEvent.MqttMessageReceived -> {
|
||||
if (event.type == 1) {
|
||||
// 交给 NotificationManager 处理(去抖+震动+亮屏+事件)
|
||||
val handled = notificationManager.onNewTaskMessage(event.rawJson)
|
||||
if (handled) {
|
||||
// 显示横幅
|
||||
notificationBanner.show(notificationManager.pendingCount)
|
||||
}
|
||||
}
|
||||
}
|
||||
// 横幅点击由 HomeFragment 处理跳转
|
||||
else -> {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ===== 下拉手势检测(Activity 级别,不可能被子 View 拦截) =====
|
||||
|
||||
Reference in New Issue
Block a user