cloudroam
2025-04-11 cb99bd7dad1b305a434c5c6c99ca65e782eb0f34
app/src/main/java/com/example/firstapp/activity/ReminderListActivity.kt
@@ -11,11 +11,19 @@
import com.example.firstapp.databinding.ActivityReminderListBinding
import com.example.firstapp.ui.dashboard.ReminderRecordViewModel
import kotlinx.coroutines.launch
import android.widget.Toast
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.TextView
import android.graphics.Color
import android.view.Gravity
import android.util.TypedValue
class ReminderListActivity : AppCompatActivity() {
    private lateinit var binding: ActivityReminderListBinding
    private lateinit var viewModel: ReminderRecordViewModel
    private lateinit var adapter: ReminderRecordAdapter
    private var unreadBadge: TextView? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
@@ -26,7 +34,40 @@
        setupRecyclerView()
        setupClickListeners()
//        setupUnreadBadge()  消息列表页面不使用小红点
        observeRecords()
        // 检查未读消息数量
        checkUnreadMessages()
    }
    private fun setupUnreadBadge() {
        // 创建未读消息数量的小红点
        unreadBadge = TextView(this).apply {
            setBackgroundResource(android.R.color.holo_red_light)
            setTextColor(Color.WHITE)
            gravity = Gravity.CENTER
            textSize = 10f
            setPadding(8, 2, 8, 2)
            // 设置圆角背景
            val outValue = TypedValue()
            context.theme.resolveAttribute(
                android.R.attr.selectableItemBackgroundBorderless, outValue, true
            )
            setBackgroundResource(outValue.resourceId)
            setBackgroundColor(Color.RED)
        }
        // 添加到布局中
        (binding.clearReadMessagesButton.parent as ViewGroup).addView(unreadBadge)
        // 调整位置到已读按钮右上角
        (unreadBadge?.layoutParams as? FrameLayout.LayoutParams)?.apply {
            gravity = Gravity.TOP or Gravity.END
            topMargin = 8
            rightMargin = 8
        }
    }
    private fun setupRecyclerView() {
@@ -34,6 +75,7 @@
            // 点击提醒记录时,标记为已读
            lifecycleScope.launch {
                viewModel.updateRecordStatus(record.id, ReminderRecord.STATUS_READ)
                checkUnreadMessages()
            }
        }
        binding.reminderRecyclerView.apply {
@@ -46,11 +88,56 @@
        binding.addReminderButton.setOnClickListener {
            startActivity(Intent(this, ReminderSettingsActivity::class.java))
        }
    }
        // 添加已读按钮点击事件
        binding.clearReadMessagesButton.setOnClickListener {
            lifecycleScope.launch {
                // 将所有未读消息标记为已读
                adapter.currentList.forEach { record ->
                    if (record.status == ReminderRecord.STATUS_UNREAD) {
                        viewModel.updateRecordStatus(record.id, ReminderRecord.STATUS_READ)
                    }
                }
                Toast.makeText(
                    this@ReminderListActivity, "所有消息已标记为已读", Toast.LENGTH_SHORT
                ).show()
                checkUnreadMessages()
            }
        }
    }
    // 观察提醒记录列表
    private fun observeRecords() {
        viewModel.reminderRecords.observe(this) { records ->
            adapter.submitList(records)
            checkUnreadMessages()
        }
    }
    private fun checkUnreadMessages() {
        lifecycleScope.launch {
            val unreadCount = viewModel.getUnreadCount()
            // 更新未读消息数量徽章
            if (unreadCount > 0) {
                unreadBadge?.apply {
                    text = if (unreadCount > 99) "99+" else unreadCount.toString()
                    visibility = android.view.View.VISIBLE
                }
            } else {
                unreadBadge?.visibility = android.view.View.GONE
            }
        }
    }
    override fun onResume() {
        super.onResume()
        // 每次恢复活动时检查未读消息
        checkUnreadMessages()
    }
    override fun onPause() {
        super.onPause()
        // 发送广播通知HomeFragment更新未读提醒数量
        sendBroadcast(Intent("com.example.firstapp.REMINDER_UPDATED"))
    }