1.2
tj
2025-04-11 27bbd0435881e408f267c99e6a253d2e17873bcc
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package com.example.firstapp.activity
 
import androidx.appcompat.app.AppCompatActivity
import android.annotation.SuppressLint
import android.app.AlertDialog
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.MenuItem
import android.widget.Button
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import com.example.firstapp.databinding.ActivitySettingBinding
import com.example.firstapp.R
import com.example.firstapp.core.Core
import com.example.firstapp.database.service.RetrofitClient
import com.example.firstapp.ui.home.HomeViewModel
import com.example.firstapp.utils.Log
import kotlinx.coroutines.launch
 
/**
 * An example full-screen activity that shows and hides the system UI (i.e.
 * status bar and navigation/system bar) with user interaction.
 */
class SettingActivity : AppCompatActivity() {
 
    private lateinit var binding: ActivitySettingBinding
    private val homeViewModel: HomeViewModel by lazy {
        ViewModelProvider(this).get(HomeViewModel::class.java)
    }
    private var isFullscreen: Boolean = false
 
    @SuppressLint("ClickableViewAccessibility")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
 
        binding = ActivitySettingBinding.inflate(layoutInflater)
        setContentView(binding.root)
 
        var toolbar=binding.settingToolbar
 
        // 获取 Toolbar 并设置为 ActionBar
        setSupportActionBar(toolbar)
 
        // 显示返回按钮
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        supportActionBar?.setDisplayShowHomeEnabled(true)
 
        // 设置标题(如果没有在 XML 中设置标题,可以在代码中动态设置)
        supportActionBar?.title = ""
 
 
 
 
 
//        binding.settingExit.setOnClickListener(View.OnClickListener {
//
//            homeViewModel.logout()
//            // 跳转到vipActivity
//            val intent = Intent(this, LoginActivity::class.java)
//            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
//            startActivity(intent)
//
//        })
 
 
        // 退出登录
        logout()
 
        // 账号注销
        accountClose()
 
 
 
    }
 
    private fun accountClose(){
 
 
        binding.accountClose.setOnClickListener {
 
            val dialogView = LayoutInflater.from(this).inflate(R.layout.account_close_dialog_custom, null)
 
            val dialog = AlertDialog.Builder(this)
                .setView(dialogView)
                .create()
 
            dialog.window?.setBackgroundDrawableResource(R.drawable.dialog_background)
 
            val btnConfirm = dialogView.findViewById<Button>(R.id.btnConfirm)
            btnConfirm.setOnClickListener {
                dialog.dismiss()
            }
 
            val btnCancel = dialogView.findViewById<Button>(R.id.btnCancel)
            btnCancel.setOnClickListener {
                // 关闭账户
                lifecycleScope.launch {
                    try {
                        // 清除本地的数据库
                        RetrofitClient.apiService.closeAccount()
 
                        Core.code.deleteAll()
                        Core.msg.deleteAll()
                        Core.keyword.deleteAll()
                        Core.reminder.deleteAll()
 
                        dialog.dismiss()
                        // 跳转到 LoginActivity 并清除之前的任务栈
                        var intent = Intent(this@SettingActivity, LoginActivity::class.java)
                        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
                        startActivity(intent)
                        // 关闭当前页面
                        finish()
 
                    } catch (ex: Exception) {
                        Log.e("关闭账户", ex.message ?: "关闭账户报错")
                    }
 
                }
            }
 
            dialog.show()
 
        }
    }
 
    private fun logout(){
 
        binding.settingExit.setOnClickListener {
            // 弹出确认退出的对话框
            val alertDialog = AlertDialog.Builder(this)
                .setTitle("确认退出")
                .setMessage("您确定要退出吗?")
                .setPositiveButton("确认") { dialog, _ ->
                    // 确认退出
                    homeViewModel.logout()
 
                    // 跳转到 LoginActivity 并清除之前的任务栈
                    val intent = Intent(this, LoginActivity::class.java)
                    intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
                    startActivity(intent)
                    // 关闭当前页面
                    finish()
                }
                .setNegativeButton("取消") { dialog, _ ->
                    // 取消退出操作
                    dialog.dismiss()
                }
                .create()
 
            // 显示对话框
            alertDialog.show()
        }
    }
 
    // 点击返回按钮时调用
    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        if (item.itemId == android.R.id.home) {
            onBackPressed()
            return true
        }
        return super.onOptionsItemSelected(item)
    }
 
    override fun onPostCreate(savedInstanceState: Bundle?) {
        super.onPostCreate(savedInstanceState)
 
        // Trigger the initial hide() shortly after the activity has been
        // created, to briefly hint to the user that UI controls
        // are available.
        delayedHide(100)
    }
 
    private fun toggle() {
        if (isFullscreen) {
            hide()
        } else {
            show()
        }
    }
 
    private fun hide() {
        // Hide UI first
        supportActionBar?.hide()
 
    }
 
    private fun show() {
 
 
    }
 
    private fun delayedHide(delayMillis: Int) {
 
    }
 
}