245 lines
8.5 KiB
Kotlin
245 lines
8.5 KiB
Kotlin
package com.juhabi.birds.mine
|
||
|
||
import android.Manifest
|
||
import android.content.Intent
|
||
import android.os.Bundle
|
||
import android.view.LayoutInflater
|
||
import android.view.View
|
||
import androidx.compose.foundation.layout.Column
|
||
import androidx.compose.material3.Button
|
||
import androidx.compose.material3.Text
|
||
import androidx.compose.runtime.Composable
|
||
import androidx.compose.ui.tooling.preview.Preview
|
||
import com.juhabi.birds.R
|
||
import com.juhabi.birds.base.BaseActivity
|
||
import com.juhabi.birds.component.picker.DateTimePicker
|
||
import com.juhabi.birds.databinding.ActivityMineCenterBinding
|
||
import com.juhabi.birds.demo.constraint.ConstraintLayoutDemoActivity
|
||
import com.juhabi.birds.demo.recycler.RecyclerViewDemoActivity
|
||
import com.juhabi.birds.login.LoginActivity
|
||
import com.juhabi.birds.mine.model.MineUserInfo
|
||
import com.juhabi.birds.mine.mvp.MineContract
|
||
import com.juhabi.birds.mine.mvp.MineImpl
|
||
import com.juhabi.birds.mine.mvp.MinePresenter
|
||
import com.juhabi.birds.main.model.AppVersionInfo
|
||
import com.juhabi.birds.network.TokenManager
|
||
import com.juhabi.birds.utils.AppUpdateManager
|
||
import com.gyf.immersionbar.ImmersionBar
|
||
import java.util.Calendar
|
||
|
||
class MineCenterActivity : BaseActivity<ActivityMineCenterBinding, MinePresenter>(),
|
||
MineContract.MineView, View.OnClickListener {
|
||
|
||
private val appUpdateManager by lazy {
|
||
AppUpdateManager(this, ::showMessage)
|
||
}
|
||
|
||
override fun createBinding(inflater: LayoutInflater): ActivityMineCenterBinding {
|
||
return ActivityMineCenterBinding.inflate(inflater)
|
||
}
|
||
|
||
override fun createPresenter(): MinePresenter {
|
||
return MinePresenter(MineImpl())
|
||
}
|
||
|
||
override fun initView(savedInstanceState: Bundle?) {
|
||
TokenManager.get(this)
|
||
listOf(
|
||
binding.reloadButton,
|
||
binding.checkVersionButton,
|
||
binding.openLoginButton,
|
||
binding.multiplePermissionButton,
|
||
binding.locationPermissionButton,
|
||
binding.normalDialogButton,
|
||
binding.inputDialogButton,
|
||
binding.datePickerButton,
|
||
binding.timePickerButton,
|
||
binding.dateTimePickerButton,
|
||
binding.recyclerViewDemoButton,
|
||
binding.constraintLayoutDemoButton
|
||
).forEach { button ->
|
||
button.setOnClickListener(this)
|
||
}
|
||
}
|
||
|
||
override fun initWindows() {
|
||
ImmersionBar.with(this)
|
||
.statusBarView(binding.statusBarView)
|
||
.statusBarDarkFont(true)
|
||
.navigationBarColor(R.color.color_zhu)
|
||
.navigationBarDarkIcon(true)
|
||
.init()
|
||
}
|
||
|
||
override fun onClick(view: View) {
|
||
when (view.id) {
|
||
R.id.reloadButton -> {
|
||
presenter?.getUserInfo()
|
||
}
|
||
R.id.checkVersionButton -> checkVersion()
|
||
|
||
R.id.openLoginButton -> {
|
||
goTo(LoginActivity::class.java, null);
|
||
}
|
||
|
||
R.id.multiplePermissionButton -> {
|
||
getMorePermissionRequst(
|
||
arrayOf(
|
||
Manifest.permission.CAMERA,
|
||
Manifest.permission.RECORD_AUDIO
|
||
),
|
||
"相机和麦克风权限",
|
||
"拍照和录音功能需要使用相机及麦克风权限",
|
||
REQUEST_CAMERA_AND_AUDIO
|
||
)
|
||
}
|
||
|
||
R.id.locationPermissionButton -> {
|
||
getSinglePermissionRequst(
|
||
Manifest.permission.ACCESS_FINE_LOCATION,
|
||
"定位权限",
|
||
"定位功能需要获取您的位置信息",
|
||
REQUEST_LOCATION
|
||
)
|
||
}
|
||
|
||
R.id.normalDialogButton -> {
|
||
showDialogtext(
|
||
"操作确认",
|
||
"确定要执行这项操作吗?",
|
||
"确定",
|
||
"取消",
|
||
object : OnDialogClick {
|
||
override fun onCancelClick() {
|
||
showMessage("已取消")
|
||
}
|
||
|
||
override fun onConfirmClick() {
|
||
showMessage("已点击确定")
|
||
}
|
||
}
|
||
)
|
||
}
|
||
|
||
R.id.inputDialogButton -> {
|
||
showEditDialog(
|
||
"请输入内容",
|
||
"请输入测试内容",
|
||
click = object : OnDialogClick {
|
||
override fun onCancelClick() {
|
||
showMessage("已取消输入")
|
||
}
|
||
override fun onConfirmClick(content: String) {
|
||
showMessage("输入内容:$content")
|
||
}
|
||
}
|
||
)
|
||
}
|
||
|
||
R.id.datePickerButton -> showDateTimePicker(DateTimePicker.Mode.DATE)
|
||
|
||
R.id.timePickerButton -> showDateTimePicker(DateTimePicker.Mode.TIME)
|
||
|
||
R.id.dateTimePickerButton -> showDateTimePicker(DateTimePicker.Mode.DATE_TIME)
|
||
|
||
R.id.recyclerViewDemoButton -> {
|
||
startActivity(Intent(this, RecyclerViewDemoActivity::class.java))
|
||
}
|
||
|
||
R.id.constraintLayoutDemoButton -> {
|
||
startActivity(Intent(this, ConstraintLayoutDemoActivity::class.java))
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
|
||
private fun checkVersion() {
|
||
val currentVersion = packageManager
|
||
.getPackageInfo(packageName, 0)
|
||
.versionName
|
||
.orEmpty()
|
||
presenter?.checkVersion(currentVersion)
|
||
}
|
||
|
||
override fun onVersionChecked(versionInfo: AppVersionInfo) {
|
||
val currentVersion = packageManager.getPackageInfo(packageName, 0).versionName.orEmpty()
|
||
appUpdateManager.checkAndUpdate(currentVersion, versionInfo)
|
||
}
|
||
|
||
override fun onRequestPermissionsResult(
|
||
requestCode: Int,
|
||
permissions: Array<out String>,
|
||
grantResults: IntArray
|
||
) {
|
||
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
|
||
appUpdateManager.onRequestPermissionsResult(requestCode, grantResults)
|
||
}
|
||
|
||
/**
|
||
* 日期、时间和日期时间共用同一个组件调用入口。
|
||
* 后续页面需要限制可选范围时,可以继续调用 setTimeRange()。
|
||
*/
|
||
private fun showDateTimePicker(mode: DateTimePicker.Mode) {
|
||
DateTimePicker.Builder(this)
|
||
.setMode(mode)
|
||
.setSelectedTime(selectedDateTime)
|
||
.setOnSelectedListener { result ->
|
||
selectedDateTime = result.timestamp
|
||
binding.dateTimeResultTextView.text = result.formattedText
|
||
showMessage("已选择:${result.formattedText}")
|
||
}
|
||
.build()
|
||
.show()
|
||
}
|
||
|
||
override fun initData(savedInstanceState: Bundle?) {
|
||
presenter?.getUserInfo()
|
||
}
|
||
|
||
override fun getUserInfoSuccess(userInfo: MineUserInfo) {
|
||
val user = userInfo.user ?: return showError("个人信息为空")
|
||
binding.nicknameTextView.text = user.nickName.ifBlank { "未设置昵称" }
|
||
binding.phoneTextView.text = "手机号:${user.phonenumber.ifBlank { "未绑定" }}"
|
||
binding.emailTextView.text = "邮箱:${user.email.ifBlank { "未绑定" }}"
|
||
binding.userIdTextView.text = "用户 ID:${user.userId}"
|
||
binding.userTypeTextView.text = "用户类型:${user.userType}"
|
||
binding.loginDateTextView.text = "最近登录:${user.loginDate.ifBlank { "暂无" }}"
|
||
}
|
||
|
||
override fun showError(message: String) {
|
||
showMessage(message)
|
||
}
|
||
|
||
override fun onPermissionsGranted(permissions: Array<String>) {
|
||
showMessage("权限获取成功:${permissions.joinToString { it.substringAfterLast('.') }}")
|
||
}
|
||
|
||
override fun onPermissionsDenied(permissions: Array<String>, permanentlyDenied: Boolean) {
|
||
showMessage("权限被拒绝:${permissions.joinToString { it.substringAfterLast('.') }}")
|
||
super.onPermissionsDenied(permissions, permanentlyDenied)
|
||
}
|
||
|
||
companion object {
|
||
private const val REQUEST_CAMERA_AND_AUDIO = 1001
|
||
private const val REQUEST_LOCATION = 1002
|
||
}
|
||
|
||
/** 保存最近一次选择结果,再次打开选择器时用于回显。 */
|
||
private var selectedDateTime: Long = Calendar.getInstance().timeInMillis
|
||
|
||
}
|
||
//@Preview(showBackground = true, name = "Test")
|
||
//@Composable
|
||
//fun UserInfo() {
|
||
// Column {
|
||
// Text("张三")
|
||
// Text("手机号:13800138000")
|
||
//
|
||
// Button(
|
||
// onClick = {}
|
||
// ) {
|
||
// Text("修改")
|
||
// }
|
||
// }
|
||
//} |