zhujie
2025-03-26 a895ef58396939abd792d1cc2fe6594efa4ffda7
app/src/main/java/com/example/firstapp/ui/notifications/NotificationsFragment.kt
@@ -1,13 +1,28 @@
package com.example.firstapp.ui.notifications
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.EditText
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.example.firstapp.R
import com.example.firstapp.activity.ContentDetailActivity
import com.example.firstapp.database.service.RetrofitClient
import com.example.firstapp.databinding.FragmentNotificationsBinding
import com.example.firstapp.ui.invitation.InvitationActivity
import com.example.firstapp.ui.reminderOther.ReminderOtherAddActivity2
import com.example.firstapp.ui.reminderOther.ReminderSettingsFragmentOther
import kotlinx.coroutines.launch
class NotificationsFragment : Fragment() {
@@ -17,22 +32,172 @@
    // onDestroyView.
    private val binding get() = _binding!!
    // 默认值
    private val DEFAULT_XIAOHONGSHU_URL = "https://www.xiaohongshu.com/user/profile/64978d5c000000001001ee26"
    private val DEFAULT_EMAIL = "support@example.com"
    private val DEFAULT_SHARE_TEXT = "推荐一个很棒的应用给你!\n下载地址:https://oia.xiaohongshu.com/oia"
    // 存储从接口获取的值
    private var xiaohongshuUrl = ""
    private var contactEmail = ""
    private var shareText = ""
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        val notificationsViewModel =
            ViewModelProvider(this).get(NotificationsViewModel::class.java)
        _binding = FragmentNotificationsBinding.inflate(inflater, container, false)
        val root: View = binding.root
        val textView: TextView = binding.textNotifications
        notificationsViewModel.text.observe(viewLifecycleOwner) {
            textView.text = it
        // 先加载配置,再设置点击事件
        lifecycleScope.launch {
            loadConfigurations()
            setupClickListeners()
        }
        return root
        return binding.root
    }
    private suspend fun loadConfigurations() {
        try {
            // 获取小红书链接
            val xhsResponse = RetrofitClient.apiService.getDictValue("setting", "xiaohongshu")
            if (xhsResponse.code == 200 && !xhsResponse.data?.itemValue.isNullOrEmpty()) {
                xiaohongshuUrl = xhsResponse.data?.itemValue ?: DEFAULT_XIAOHONGSHU_URL
            } else {
                xiaohongshuUrl = DEFAULT_XIAOHONGSHU_URL
            }
            // 获取邮箱
            val emailResponse = RetrofitClient.apiService.getDictValue("setting", "email")
            if (emailResponse.code == 200 && !emailResponse.data?.itemValue.isNullOrEmpty()) {
                contactEmail = emailResponse.data?.itemValue ?: DEFAULT_EMAIL
            } else {
                contactEmail = DEFAULT_EMAIL
            }
            // 获取分享文本
            val shareResponse = RetrofitClient.apiService.getDictValue("setting", "share_link")
            if (shareResponse.code == 200 && !shareResponse.data?.itemValue.isNullOrEmpty()) {
                shareText = shareResponse.data?.itemValue ?: DEFAULT_SHARE_TEXT
            } else {
                shareText = DEFAULT_SHARE_TEXT
            }
        } catch (e: Exception) {
            // 如果发生错误,使用默认值
            xiaohongshuUrl = DEFAULT_XIAOHONGSHU_URL
            contactEmail = DEFAULT_EMAIL
            shareText = DEFAULT_SHARE_TEXT
        }
    }
    private fun setupClickListeners() {
        // 设置提醒
        binding.settingsReminder.setOnClickListener {
            // 跳转到设置提醒页面
            findNavController().navigate(R.id.action_navigation_notifications_to_reminderSettingsFragment)
        }
//         设置其他提醒 暂时不需要
//        binding.settingsReminderOther.setOnClickListener {
//            // 跳转到设置提醒页面
//            findNavController().navigate(R.id.action_settings_to_reminderSettingsFragmentOther)
//        }
        // 关于小红书
        binding.aboutApp.setOnClickListener {
            // 跳转到小红书账号页面
            val intent = Intent(Intent.ACTION_VIEW, Uri.parse(xiaohongshuUrl))
            startActivity(intent)
        }
        // 邮件联系
        binding.emailContact.setOnClickListener {
            showEmailDialog()
        }
        // 意见与反馈
        binding.feedback.setOnClickListener {
            showFeedbackDialog()
        }
        // 分享给好友
        binding.shareToFriends.setOnClickListener {
            val intent = Intent(requireActivity(), InvitationActivity::class.java)
            startActivity(intent)
        }
        // 隐私协议
        binding.privacyPolicy.setOnClickListener {
            startContentActivity("privacy_policy", "隐私协议")
        }
        // 如何使用
        binding.howToUse.setOnClickListener {
            startContentActivity("user_guide", "使用说明")
        }
    }
    private fun showEmailDialog() {
        MaterialAlertDialogBuilder(requireContext())
            .setTitle("联系邮箱")
            .setMessage(contactEmail)
            .setPositiveButton("复制") { _, _ ->
                val clipboard = requireContext().getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
                val clip = ClipData.newPlainText("email", contactEmail)
                clipboard.setPrimaryClip(clip)
                Toast.makeText(context, "邮箱已复制", Toast.LENGTH_SHORT).show()
            }
            .setNegativeButton("取消", null)
            .show()
    }
    private fun showFeedbackDialog() {
        val dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_feedback, null)
        val editText = dialogView.findViewById<EditText>(R.id.feedback_edit_text)
        MaterialAlertDialogBuilder(requireContext())
            .setTitle("意见反馈")
            .setView(dialogView)
            .setPositiveButton("提交") { _, _ ->
                val feedback = editText.text.toString()
                if (feedback.isNotEmpty()) {
                    // TODO: 提交反馈到服务器
                    Toast.makeText(context, "感谢您的反馈", Toast.LENGTH_SHORT).show()
                }
            }
            .setNegativeButton("取消", null)
            .show()
    }
    private fun shareToWechat() {
        try {
            // 获取应用程序的包名
            //val packageName = requireContext().packageName
            // 创建分享意图
            val intent = Intent().apply {
                action = Intent.ACTION_SEND
                type = "text/plain"
                putExtra(Intent.EXTRA_TEXT, shareText)
                // 指定分享到微信
                setPackage("com.tencent.mm")
            }
            startActivity(Intent.createChooser(intent, "分享到微信"))
        } catch (e: Exception) {
            Toast.makeText(context, "请先安装微信", Toast.LENGTH_SHORT).show()
        }
    }
    private fun startContentActivity(type: String, title: String) {
        val intent = Intent(requireContext(), ContentDetailActivity::class.java).apply {
            putExtra(ContentDetailActivity.EXTRA_CONTENT_TYPE, type)
            putExtra(ContentDetailActivity.EXTRA_TITLE, title)
        }
        startActivity(intent)
    }
    override fun onDestroyView() {