RustAssistant/app/src/main/java/com/coldmint/rust/pro/EditUserInfoActivity.kt

467 lines
18 KiB
Kotlin
Raw Normal View History

2022-04-04 14:03:10 +00:00
package com.coldmint.rust.pro
import android.content.Intent
import android.graphics.BitmapFactory
2022-07-26 14:35:13 +00:00
import android.graphics.Color
2022-04-04 14:03:10 +00:00
import android.net.Uri
import android.os.Bundle
import android.os.Environment
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
2022-08-14 02:43:53 +00:00
import android.view.LayoutInflater
2022-04-04 14:03:10 +00:00
import android.view.View
import android.widget.PopupMenu
import android.widget.Toast
import androidx.core.view.isVisible
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
2022-09-04 07:49:29 +00:00
import com.coldmint.dialog.InputDialog
2022-04-04 14:03:10 +00:00
import com.coldmint.rust.core.dataBean.ApiResponse
import com.coldmint.rust.core.dataBean.user.SpaceInfoData
import com.coldmint.rust.core.interfaces.ApiCallBack
2022-09-04 07:49:29 +00:00
import com.coldmint.rust.core.tool.DebugHelper
2022-04-04 14:03:10 +00:00
import com.coldmint.rust.core.tool.FileOperator
import com.coldmint.rust.core.web.Community
import com.coldmint.rust.core.web.ServerConfiguration
import com.coldmint.rust.core.web.User
import com.coldmint.rust.pro.base.BaseActivity
import com.coldmint.rust.pro.databinding.ActivityEditModInfoBinding
import com.coldmint.rust.pro.databinding.ActivityEditUserInfoBinding
2022-07-17 13:51:14 +00:00
import com.coldmint.rust.pro.tool.AppSettings
2022-04-04 14:03:10 +00:00
import com.coldmint.rust.pro.tool.GlobalMethod
2022-07-26 14:35:13 +00:00
import com.flask.colorpicker.ColorPickerView
import com.flask.colorpicker.builder.ColorPickerDialogBuilder
2022-04-04 14:03:10 +00:00
import com.google.android.material.snackbar.Snackbar
import com.yalantis.ucrop.UCrop
import java.io.File
2022-07-26 14:35:13 +00:00
import java.lang.System.out
2022-04-04 14:03:10 +00:00
class EditUserInfoActivity : BaseActivity<ActivityEditUserInfoBinding>() {
lateinit var userId: String
var needIcon = false
var needCover = false
var iconLink: String? = null
var coverLink: String? = null
var needCleanCache = false
lateinit var iconCacheFile: File
private lateinit var item: Array<String>
override fun whenCreateActivity(savedInstanceState: Bundle?, canUseView: Boolean) {
if (canUseView) {
initView()
initAction()
}
}
private fun initView() {
2022-07-17 13:51:14 +00:00
title = getText(R.string.editData)
2022-04-04 14:03:10 +00:00
setReturnButton()
val thisIntent = intent
val temUserId = thisIntent.getStringExtra("userId")
if (temUserId == null) {
showError("用户id错误。")
return
}
item = resources.getStringArray(R.array.gender_entries)
userId = temUserId
User.getSpaceInfo(userId, object : ApiCallBack<SpaceInfoData> {
override fun onResponse(t: SpaceInfoData) {
if (t.code == ServerConfiguration.Success_Code) {
viewBinding.loadLayout.isVisible = false
viewBinding.nestedScrollView.isVisible = true
viewBinding.userNameView.setText(t.data.userName)
val head = t.data.headIcon
if (head != null) {
loadIcon(head)
}
val cover = t.data.cover
if (cover != null) {
loadCover(cover)
}
val introduce =
t.data.introduce ?: getString(R.string.defaultIntroduced)
viewBinding.signatureView.setText(introduce)
val gender = t.data.gender
if (gender < 0) {
2022-07-26 14:35:13 +00:00
viewBinding.sexView.setText(item[1])
} else {
viewBinding.sexView.setText(item[0])
2022-04-04 14:03:10 +00:00
}
2022-07-26 14:35:13 +00:00
viewBinding.sexView.setSimpleItems(R.array.gender_entries)
2022-04-04 14:03:10 +00:00
viewBinding.button.isVisible = true
} else {
viewBinding.progressBar.isVisible = false
viewBinding.tipView.isVisible = true
viewBinding.tipView.text = t.message
}
}
override fun onFailure(e: Exception) {
viewBinding.progressBar.isVisible = false
viewBinding.tipView.isVisible = true
viewBinding.tipView.text = getString(R.string.network_error)
}
})
}
private fun initAction() {
// viewBinding.changeColorButton.setOnClickListener {
// ColorPickerDialogBuilder
// .with(this)
// .setTitle(getString(R.string.choose_color))
// .initialColor(Color.WHITE)
// .wheelType(ColorPickerView.WHEEL_TYPE.FLOWER)
// .density(12)
// .setOnColorSelectedListener {
// //toast("onColorSelected: 0x" + Integer.toHexString(selectedColor));
// }
// .setPositiveButton(R.string.dialog_ok) { dialog, selectedColor, allColors ->
// GlobalMethod.temColor = selectedColor
// if (iconLink != null) {
// Glide.with(this@EditUserInfoActivity)
// .load(iconLink)
// .apply(GlobalMethod.getRequestOptions(true, !GlobalMethod.isActive))
// .into(viewBinding.iconView)
// }
// }
// .setNegativeButton(R.string.dialog_cancel) { dialog, which -> }
// .build()
// .show()
// }
2022-04-04 14:03:10 +00:00
viewBinding.userNameView.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
}
override fun afterTextChanged(s: Editable?) {
val userName = s.toString()
checkUserName(userName)
}
})
viewBinding.iconView.setOnClickListener {
val popupMenu = GlobalMethod.createPopMenu(it)
2022-04-04 14:03:10 +00:00
popupMenu.menu.add(R.string.from_url)
if (needIcon) {
popupMenu.menu.add(R.string.change_image)
popupMenu.menu.add(R.string.del_image)
} else {
popupMenu.menu.add(R.string.select_image)
}
popupMenu.setOnMenuItemClickListener { item ->
val title = item.title.toString()
if (title == getString(R.string.change_image) || title == getString(R.string.select_image)) {
//选择文件
val startIntent =
Intent(this, FileManagerActivity::class.java)
val fileBundle = Bundle()
fileBundle.putString("type", "selectFile")
startIntent.putExtra("data", fileBundle)
startActivityForResult(startIntent, 1)
} else if (title == getString(R.string.from_url)) {
InputDialog(this).setTitle(R.string.from_url).setMessage(R.string.from_url_tip)
.setErrorTip { s, textInputLayout ->
if (s.matches(Regex("^http://([\\w-]+\\.)+[\\w-]+(/[\\w-./?%&=]*)?$|^https://([\\w-]+\\.)+[\\w-]+(/[\\w-./?%&=]*)?$"))) {
textInputLayout.error = getString(R.string.from_url_tip)
} else {
textInputLayout.isErrorEnabled = false
}
}.setPositiveButton(R.string.dialog_ok) { input ->
2022-09-04 07:49:29 +00:00
needCleanCache = true
loadIcon(input)
true
}.setNegativeButton(R.string.dialog_close) {
2022-09-04 07:49:29 +00:00
}.show()
2022-04-04 14:03:10 +00:00
} else {
val link = iconLink
if (link != null) {
needIcon = false
}
viewBinding.iconView.setImageResource(R.drawable.image)
}
false
}
popupMenu.show()
}
viewBinding.coverView.setOnClickListener {
val popupMenu = GlobalMethod.createPopMenu(it)
2022-04-04 14:03:10 +00:00
popupMenu.menu.add(R.string.from_url)
if (needCover) {
popupMenu.menu.add(R.string.change_image)
popupMenu.menu.add(R.string.del_image)
} else {
popupMenu.menu.add(R.string.select_image)
}
popupMenu.setOnMenuItemClickListener { item ->
val title = item.title.toString()
if (title == getString(R.string.change_image) || title == getString(R.string.select_image)) {
//选择文件
val startIntent =
Intent(this, FileManagerActivity::class.java)
val fileBundle = Bundle()
fileBundle.putString("type", "selectFile")
startIntent.putExtra("data", fileBundle)
startActivityForResult(startIntent, 2)
} else if (title == getString(R.string.from_url)) {
InputDialog(this).setTitle(R.string.from_url).setMessage(R.string.from_url_tip)
.setErrorTip { s, textInputLayout ->
if (s.matches(Regex("^http://([\\w-]+\\.)+[\\w-]+(/[\\w-./?%&=]*)?$|^https://([\\w-]+\\.)+[\\w-]+(/[\\w-./?%&=]*)?$"))) {
textInputLayout.error = getString(R.string.from_url_tip)
} else {
textInputLayout.isErrorEnabled = false
}
}.setPositiveButton(R.string.dialog_ok) { input ->
2022-09-04 07:49:29 +00:00
needCleanCache = true
loadCover(input)
true
}.setNegativeButton(R.string.dialog_close) {
2022-09-04 07:49:29 +00:00
}.show()
2022-04-04 14:03:10 +00:00
} else {
val link = coverLink
if (link != null) {
needCover = false
}
viewBinding.coverView.setImageResource(R.drawable.image)
}
false
}
popupMenu.show()
}
viewBinding.signatureView.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
}
override fun afterTextChanged(s: Editable?) {
val introduce = s.toString()
checkIntroduce(introduce)
}
})
viewBinding.button.setOnClickListener {
2022-08-22 07:32:10 +00:00
val userName = viewBinding.userNameView.text.toString()
val introduce = viewBinding.signatureView.text.toString()
val genderValue = viewBinding.sexView.text.toString()
val boy = getString(R.string.boy)
if (!checkUserName(userName)) {
return@setOnClickListener
}
if (!checkIntroduce(introduce)) {
return@setOnClickListener
}
val gender = if (genderValue == boy) {
1
} else {
-1
}
viewBinding.button.hide()
val token =
AppSettings.getValue(AppSettings.Setting.Token, "")
User.updateSpaceInfo(
token,
userName,
introduce,
gender,
object : ApiCallBack<ApiResponse> {
override fun onResponse(t: ApiResponse) {
if (t.code == ServerConfiguration.Success_Code) {
if (needCleanCache) {
Thread {
Glide.get(this@EditUserInfoActivity).clearDiskCache()
runOnUiThread {
Glide.get(this@EditUserInfoActivity).clearMemory()
2022-04-04 14:03:10 +00:00
finish()
}
2022-08-22 07:32:10 +00:00
}.start()
} else {
finish()
}
} else {
viewBinding.button.show()
val data = t.data
if (data != null && ServerConfiguration.isEvent(data)) {
when (data) {
"@event:用户名占用" -> {
setErrorAndInput(
viewBinding.userNameView,
getString(R.string.user_name_error),
viewBinding.userNameInputLayout
)
2022-04-04 14:03:10 +00:00
}
}
2022-08-22 07:32:10 +00:00
} else {
Snackbar.make(
viewBinding.button,
t.message,
Snackbar.LENGTH_SHORT
).show()
2022-04-04 14:03:10 +00:00
}
2022-08-22 07:32:10 +00:00
}
}
2022-04-04 14:03:10 +00:00
2022-08-22 07:32:10 +00:00
override fun onFailure(e: Exception) {
viewBinding.button.show()
showInternetError(viewBinding.button, e)
}
2022-04-04 14:03:10 +00:00
2022-08-22 07:32:10 +00:00
},
ServerConfiguration.toRelativePath(iconLink ?: ""),
ServerConfiguration.toRelativePath(coverLink ?: "")
)
2022-04-04 14:03:10 +00:00
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == RESULT_OK && data != null) {
if (requestCode == 1) {
val filePath = data.getStringExtra("File") ?: return
val newIconFile = File(filePath)
val type = FileOperator.getFileType(newIconFile)
if (type == "png" || type == "jpg") {
val bitmap = BitmapFactory.decodeFile(newIconFile.absolutePath)
if (bitmap == null) {
Snackbar.make(
viewBinding.button,
getString(R.string.parsing_picture_error),
Snackbar.LENGTH_SHORT
).show()
return
}
2022-04-04 14:03:10 +00:00
if (bitmap.height == bitmap.width) {
needCleanCache = true
loadIcon(filePath)
} else {
val cacheFolder = File(cacheDir.absolutePath + "/System/Images")
if (!cacheFolder.exists()) {
cacheFolder.mkdirs()
}
iconCacheFile = File(cacheFolder.absolutePath + "/" + newIconFile.name)
UCrop.of(
Uri.parse(newIconFile.toURI().toString()),
Uri.parse(iconCacheFile.toURI().toString())
).withAspectRatio(1f, 1f).start(this)
}
} else {
Snackbar.make(
viewBinding.button,
getString(R.string.bad_file_type),
Snackbar.LENGTH_SHORT
).show()
}
} else if (requestCode == 2) {
val filePath = data.getStringExtra("File") ?: return
val newIconFile = File(filePath)
val type = FileOperator.getFileType(newIconFile)
if (type == "png" || type == "jpg") {
needCleanCache = true
loadCover(filePath)
} else {
Snackbar.make(
viewBinding.button,
getString(R.string.bad_file_type),
Snackbar.LENGTH_SHORT
).show()
}
} else if (requestCode == UCrop.REQUEST_CROP) {
//val resultUri = UCrop.getOutput(data)
needCleanCache = true
loadIcon(iconCacheFile.absolutePath)
}
}
}
/**
* 加载图像
* @param link String 链接
*/
fun loadIcon(link: String) {
needIcon = true
val temLink = ServerConfiguration.getRealLink(link)
iconLink = temLink
Glide.with(this@EditUserInfoActivity)
2022-07-26 14:35:13 +00:00
.load(temLink).apply(GlobalMethod.getRequestOptions(true, !GlobalMethod.isActive))
2022-04-04 14:03:10 +00:00
.into(viewBinding.iconView)
}
/**
* 加载封面
* @param link String 链接
*/
fun loadCover(link: String) {
needCover = true
val temLink = ServerConfiguration.getRealLink(link)
coverLink = temLink
Glide.with(this@EditUserInfoActivity)
2022-07-26 14:35:13 +00:00
.load(temLink).apply(GlobalMethod.getRequestOptions(grayscale = !GlobalMethod.isActive))
2022-04-04 14:03:10 +00:00
.into(viewBinding.coverView)
}
/**
* 检查用户名
* @param userName String
* @return Boolean
*/
fun checkUserName(userName: String): Boolean {
return if (userName.isBlank()) {
setErrorAndInput(
viewBinding.userNameView,
String.format(
getString(R.string.please_input_value),
viewBinding.userNameInputLayout.hint.toString()
),
viewBinding.userNameInputLayout
)
false
} else {
viewBinding.userNameInputLayout.isErrorEnabled = false
true
}
}
fun checkIntroduce(introduce: String): Boolean {
return if (introduce.isBlank()) {
setErrorAndInput(
viewBinding.signatureView,
String.format(
getString(R.string.please_input_value),
viewBinding.signatureLayout.hint.toString()
),
viewBinding.signatureLayout
)
false
} else {
viewBinding.signatureLayout.isErrorEnabled = false
true
}
}
2022-08-14 02:43:53 +00:00
override fun getViewBindingObject(layoutInflater: LayoutInflater): ActivityEditUserInfoBinding {
2022-04-04 14:03:10 +00:00
return ActivityEditUserInfoBinding.inflate(layoutInflater)
}
}