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()
|
}
|
}
|
}
|
}
|
}
|