cloudroam
2025-04-10 5fc9567cfa6b6beee4f52a9f835f304865d693e1
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package com.example.firstapp.activity
 
import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import com.example.firstapp.databinding.ActivityReminderSettingsBinding
import com.example.firstapp.model.CategoryConfig
import com.example.firstapp.database.entity.Reminder
import com.example.firstapp.utils.PreferencesManager
import com.example.firstapp.ui.dashboard.ReminderViewModel
import kotlinx.coroutines.launch
 
class ReminderSettingsActivity : AppCompatActivity() {
    private lateinit var binding: ActivityReminderSettingsBinding
    private lateinit var viewModel: ReminderViewModel
    private var isInitializing = true
 
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityReminderSettingsBinding.inflate(layoutInflater)
        setContentView(binding.root)
 
        viewModel = ViewModelProvider(this)[ReminderViewModel::class.java]
 
        setupCheckBoxes()
        setupSaveButton()
        loadExistingSettings()
    }
 
    private fun setupCheckBoxes() {
        // 全选分类按钮逻辑
        binding.tvSelectAll.setOnClickListener {
            val shouldCheck = !(binding.checkBoxExpress.isChecked && binding.checkBoxFinance.isChecked && 
                    binding.checkBoxIncome.isChecked && binding.checkBoxFlight.isChecked && binding.checkBoxTrain.isChecked)
            
            binding.checkBoxExpress.isChecked = shouldCheck
            binding.checkBoxFinance.isChecked = shouldCheck
            binding.checkBoxIncome.isChecked = shouldCheck
            binding.checkBoxFlight.isChecked = shouldCheck
            binding.checkBoxTrain.isChecked = shouldCheck
        }
 
        // 全选提醒方式按钮逻辑
        binding.tvSelectAllNotifications.setOnClickListener {
            val shouldCheck = !binding.checkBoxPhone.isChecked
            binding.checkBoxPhone.isChecked = shouldCheck
        }
 
        // 检查会员状态,非会员只能看到快递和还款
        val savedPhone = PreferencesManager.getPhone()
        if (savedPhone.isNullOrEmpty()) {
            disableVipCategories()
            return
        }
 
        lifecycleScope.launch {
            try {
                val response = viewModel.getUserInfo(savedPhone)
                if (response.code == "0" && response.data?.isMember == true) {
                    enableAllCategories()
                } else {
                    disableVipCategories()
                }
            } catch (e: Exception) {
                e.printStackTrace()
                disableVipCategories()
            }
        }
    }
 
    private fun disableVipCategories() {
        binding.apply {
            checkBoxIncome.isEnabled = false
            checkBoxFlight.isEnabled = false
            checkBoxTrain.isEnabled = false
        }
    }
 
    private fun enableAllCategories() {
        binding.apply {
            checkBoxIncome.isEnabled = true
            checkBoxFlight.isEnabled = true
            checkBoxTrain.isEnabled = true
        }
    }
 
    private fun loadExistingSettings() {
        isInitializing = true
        viewModel.reminders.observe(this) { reminders ->
            binding.apply {
                checkBoxExpress.isChecked = reminders.any { it.categoryName == "快递" }
                checkBoxFinance.isChecked = reminders.any { it.categoryName == "还款" }
                checkBoxIncome.isChecked = reminders.any { it.categoryName == "收入" }
                checkBoxFlight.isChecked = reminders.any { it.categoryName == "航班" }
                checkBoxTrain.isChecked = reminders.any { it.categoryName == "火车票" }
                
                // 检查是否有通知方法设置
                checkBoxPhone.isChecked = reminders.any { it.notificationMethod == "PHONE" }
            }
            isInitializing = false
        }
    }
 
    private fun setupSaveButton() {
        binding.saveButton.setOnClickListener {
            saveSettings()
        }
        
        // 更改监听器逻辑,不再每次选择都马上保存
        binding.checkBoxPhone.setOnCheckedChangeListener { _, _ -> 
            // 不做任何事情,只在保存按钮点击时保存
        }
 
        binding.checkBoxExpress.setOnCheckedChangeListener { _, _ -> }
        binding.checkBoxFinance.setOnCheckedChangeListener { _, _ -> }
        binding.checkBoxIncome.setOnCheckedChangeListener { _, _ -> }
        binding.checkBoxFlight.setOnCheckedChangeListener { _, _ -> }
        binding.checkBoxTrain.setOnCheckedChangeListener { _, _ -> }
    }
 
    private fun saveSettings() {
        if (isInitializing) return
 
        val selectedCategories = mutableListOf<CategoryConfig>()
        
        binding.apply {
            if (checkBoxExpress.isChecked) selectedCategories.add(CategoryConfig(1, "快递", 1, true))
            if (checkBoxFinance.isChecked) selectedCategories.add(CategoryConfig(2, "还款", 2, true))
            if (checkBoxIncome.isChecked) selectedCategories.add(CategoryConfig(3, "收入", 3, true))
            if (checkBoxFlight.isChecked) selectedCategories.add(CategoryConfig(4, "航班", 4, true))
            if (checkBoxTrain.isChecked) selectedCategories.add(CategoryConfig(5, "火车票", 5, true))
        }
 
        if (selectedCategories.isEmpty()) {
            Toast.makeText(this, "请选择至少一个分类", Toast.LENGTH_SHORT).show()
            return
        }
 
        if (!binding.checkBoxPhone.isChecked) {
            Toast.makeText(this, "请选择至少一种提醒方式", Toast.LENGTH_SHORT).show()
            return
        }
 
        lifecycleScope.launch {
            try {
                // 先删除所有现有提醒
                viewModel.deleteAllReminders()
                
                // 保存新的提醒设置
                selectedCategories.forEach { category ->
                    val reminder = Reminder(
                        categoryId = category.id,
                        categoryName = category.name,
                        notificationMethod = if (binding.checkBoxPhone.isChecked) "PHONE" else ""
                    )
                    viewModel.insertReminder(reminder)
                }
                
                // 保存成功,回到主线程处理UI相关操作
                runOnUiThread {
                    Toast.makeText(this@ReminderSettingsActivity, "设置已保存", Toast.LENGTH_SHORT).show()
                    // 关闭当前activity,返回到调用它的activity
                    finish()
                }
            } catch (e: Exception) {
                e.printStackTrace()
                runOnUiThread {
                    Toast.makeText(this@ReminderSettingsActivity, "保存失败: ${e.message}", Toast.LENGTH_SHORT).show()
                }
            }
        }
    }