cloudroam
2025-04-15 3466799c94227c5ebba9fb201621e745058867ee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package com.example.firstapp.activity
 
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.firstapp.adapter.ReminderRecordAdapter
import com.example.firstapp.database.entity.ReminderRecord
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)
        binding = ActivityReminderListBinding.inflate(layoutInflater)
        setContentView(binding.root)
 
        viewModel = ViewModelProvider(this)[ReminderRecordViewModel::class.java]
 
        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() {
        adapter = ReminderRecordAdapter { record ->
            // 点击提醒记录时,标记为已读
            lifecycleScope.launch {
                viewModel.updateRecordStatus(record.id, ReminderRecord.STATUS_READ)
                checkUnreadMessages()
            }
        }
        binding.reminderRecyclerView.apply {
            layoutManager = LinearLayoutManager(this@ReminderListActivity)
            adapter = this@ReminderListActivity.adapter
        }
    }
 
    private fun setupClickListeners() {
        binding.addReminderButton.setOnClickListener {
            startActivity(Intent(this, ReminderSettingsActivity::class.java))
        }
 
        // 添加已读按钮点击事件
        binding.clearReadContainer.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"))
    }