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

499 lines
20 KiB
Kotlin
Raw Normal View History

2022-04-04 14:03:10 +00:00
package com.coldmint.rust.pro
import android.annotation.SuppressLint
import android.app.ActivityOptions
import android.content.Context
import android.content.Intent
import android.graphics.Color
2022-07-26 14:35:13 +00:00
import android.graphics.drawable.Drawable
2022-05-10 10:53:24 +00:00
import android.os.Bundle
2022-04-04 14:03:10 +00:00
import android.view.Gravity
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.view.inputmethod.EditorInfo
import android.widget.EditText
2022-09-04 07:49:29 +00:00
import androidx.coordinatorlayout.widget.CoordinatorLayout
2022-04-04 14:03:10 +00:00
import androidx.core.view.isVisible
import com.bumptech.glide.Glide
2022-09-04 07:49:29 +00:00
import com.coldmint.dialog.CoreDialog
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.web.*
import com.coldmint.rust.pro.adapters.UserHomeStateAdapter
2022-05-10 10:53:24 +00:00
import com.coldmint.rust.pro.base.BaseActivity
import com.coldmint.rust.pro.databinding.ActivityUserHomePageBinding
2022-07-17 13:51:14 +00:00
import com.coldmint.rust.pro.dialog.CommentDialog
2022-04-04 14:03:10 +00:00
import com.coldmint.rust.pro.tool.AppSettings
import com.coldmint.rust.pro.tool.GlobalMethod
import com.google.android.material.appbar.AppBarLayout
import com.google.android.material.appbar.CollapsingToolbarLayout
import com.google.android.material.snackbar.Snackbar
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
2022-09-04 07:49:29 +00:00
import com.gyf.immersionbar.ImmersionBar
2022-04-04 14:03:10 +00:00
import com.gyf.immersionbar.ktx.immersionBar
class UserHomePageActivity : BaseActivity<ActivityUserHomePageBinding>() {
lateinit var userId: String
var account: String? = null
2022-04-04 14:03:10 +00:00
var userName: String? = null
var fans: Int = 0
val userHomeStateAdapter by lazy {
UserHomeStateAdapter(this, userId)
}
//旧的备份数据
var oldSpaceInfoData: SpaceInfoData? = null
@SuppressLint("CheckResult")
private fun initView() {
2022-09-04 07:49:29 +00:00
if (ImmersionBar.hasNavigationBar(this)) {
val layoutParams =
viewBinding.fab.layoutParams as CoordinatorLayout.LayoutParams
layoutParams.setMargins(
GlobalMethod.dp2px(16),
GlobalMethod.dp2px(16),
GlobalMethod.dp2px(16),
ImmersionBar.getNavigationBarHeight(this) + GlobalMethod.dp2px(16)
)
DebugHelper.printLog("导航适配", "已调整fab按钮的位置。")
}
val s = ImmersionBar.getStatusBarHeight(this)
val layoutParams =
viewBinding.toolbar.layoutParams as CollapsingToolbarLayout.LayoutParams
layoutParams.setMargins(
0,
s,
0,
0
)
2022-04-04 14:03:10 +00:00
val thisIntent = intent
val temUserId = thisIntent.getStringExtra("userId")
if (temUserId == null) {
showError("请传入用户id")
return
} else {
userId = temUserId
}
2022-08-14 02:43:53 +00:00
val temAccount = AppSettings.getValue(AppSettings.Setting.Account, "")
if (temAccount.isNotBlank()) {
2022-04-04 14:03:10 +00:00
account = temAccount
}
viewBinding.toolbar.title = ""
setReturnButton()
viewBinding.followLayout.setOnClickListener {
openUserList(userId, true)
}
viewBinding.fansLayout.setOnClickListener {
openUserList(userId, false)
}
initButton()
2022-05-10 10:53:24 +00:00
}
2022-04-04 14:03:10 +00:00
2022-05-10 10:53:24 +00:00
2022-04-04 14:03:10 +00:00
/**
* 打开用户列表
* @param account String 账号
* @param isFollowMode Boolean 是否加载偶像
*/
fun openUserList(account: String, isFollowMode: Boolean) {
val bundle = Bundle()
bundle.putString("account", account)
bundle.putBoolean("isFollowMode", isFollowMode)
val intent = Intent(this, UserListActivity::class.java)
intent.putExtra("data", bundle)
startActivity(intent)
}
/**
* 初始化按钮
*/
private fun initButton() {
if (account == null) {
viewBinding.button.text = getString(R.string.please_login_first)
viewBinding.button.isEnabled = false
}else{
if (account == userId) {
viewBinding.button.text = getString(R.string.editData)
} else {
Community.getFollowState(account!!, userId, object : ApiCallBack<ApiResponse> {
override fun onResponse(t: ApiResponse) {
if (t.code == ServerConfiguration.Success_Code) {
val data = t.data
if (data != null && ServerConfiguration.isEvent(data)) {
when (data) {
"@event:已互粉" -> {
viewBinding.button.text = getString(R.string.each_other_follow)
}
"@event:已关注" -> {
viewBinding.button.text = getString(R.string.followed)
}
"@event:关注" -> {
viewBinding.button.text = getString(R.string.follow)
}
"@event:拒绝关注" -> {
viewBinding.button.text = getString(R.string.reject_follow)
viewBinding.button.isEnabled = false
}
2022-04-04 14:03:10 +00:00
}
}
} else {
2022-04-04 14:03:10 +00:00
}
2022-04-04 14:03:10 +00:00
}
override fun onFailure(e: Exception) {
2022-04-04 14:03:10 +00:00
}
2022-04-04 14:03:10 +00:00
})
}
2022-04-04 14:03:10 +00:00
}
}
/**
* 展示用户数据到视图
* @param spaceInfoData SpaceInfoData
*/
fun showUserdataIfNeed(spaceInfoData: SpaceInfoData) {
var isFirst = false
if (oldSpaceInfoData == null) {
isFirst = true
oldSpaceInfoData = spaceInfoData
} else {
if (oldSpaceInfoData == spaceInfoData) {
return
}
}
userName = spaceInfoData.data.userName
viewBinding.appBar.addOnOffsetChangedListener(AppBarLayout.OnOffsetChangedListener { appBarLayout, verticalOffset ->
viewBinding.toolbar.title =
if ((Math.abs(verticalOffset) >= appBarLayout.totalScrollRange)) {
spaceInfoData.data.userName
} else {
""
}
})
viewBinding.nameView.text = spaceInfoData.data.userName
viewBinding.describeView.text =
spaceInfoData.data.introduce ?: getString(R.string.defaultIntroduced)
viewBinding.loginTimeView.text = String.format(
getString(R.string.user_info),
spaceInfoData.data.loginTime,
2022-08-17 01:25:20 +00:00
spaceInfoData.data.account,
spaceInfoData.data.location
2022-04-04 14:03:10 +00:00
)
viewBinding.fab.isVisible = true
val gender = spaceInfoData.data.gender
if (gender > 0) {
Glide.with(application).load(R.drawable.boy).apply(GlobalMethod.getRequestOptions())
2022-05-10 10:53:24 +00:00
.into(viewBinding.genderView)
2022-04-04 14:03:10 +00:00
} else {
Glide.with(application).load(R.drawable.girl).apply(GlobalMethod.getRequestOptions())
2022-05-10 10:53:24 +00:00
.into(viewBinding.genderView)
2022-04-04 14:03:10 +00:00
}
val permission = spaceInfoData.data.permission
when (permission) {
1 -> {
viewBinding.cardView.isVisible = true
viewBinding.cardView.setCardBackgroundColor(Color.parseColor("#f47920"))
viewBinding.positionView.setText(R.string.super_admin)
}
2 -> {
viewBinding.cardView.isVisible = true
viewBinding.cardView.setCardBackgroundColor(Color.parseColor("#ffd400"))
viewBinding.positionView.setText(R.string.admin)
}
else -> {
if (spaceInfoData.data.expirationTime == ServerConfiguration.ForeverTime) {
viewBinding.cardView.isVisible = true
viewBinding.cardView.setCardBackgroundColor(Color.parseColor("#33a3dc"))
viewBinding.positionView.setText(R.string.forever_time)
}
}
}
val icon = spaceInfoData.data.headIcon
if (icon != null) {
val iconLink = ServerConfiguration.getRealLink(icon)
Glide.with(this@UserHomePageActivity).load(iconLink)
.apply(GlobalMethod.getRequestOptions(true))
.into(viewBinding.headIconView)
}
val cover = spaceInfoData.data.cover
if (cover != null) {
2022-05-10 10:53:24 +00:00
Glide.with(this).load(ServerConfiguration.getRealLink(cover))
2022-04-04 14:03:10 +00:00
.into(viewBinding.coverView)
}
viewBinding.viewPager.adapter = userHomeStateAdapter
viewBinding.coverView.setOnClickListener {
if (cover != null) {
val intent = Intent(this@UserHomePageActivity, FullScreenCoverActivity::class.java)
val name = getString(R.string.transition_cover)
val options =
ActivityOptions.makeSceneTransitionAnimation(this, viewBinding.coverView, name)
intent.putExtra("iconLink", cover)
this@UserHomePageActivity.startActivity(intent, options.toBundle())
}
}
fans = spaceInfoData.data.fans
viewBinding.fansNumView.text = ServerConfiguration.numberToString(spaceInfoData.data.fans)
viewBinding.followNumView.text =
ServerConfiguration.numberToString(spaceInfoData.data.follower)
viewBinding.praiseNumView.text =
ServerConfiguration.numberToString(spaceInfoData.data.praise)
if (isFirst) {
TabLayoutMediator(viewBinding.tabLayout, viewBinding.viewPager) { tab, position ->
when (position) {
0 ->
tab.text = getString(R.string.homepage)
else -> {
tab.text = getString(R.string.dynamic)
}
}
}.attach()
viewBinding.fab.hide()
viewBinding.tabLayout.addOnTabSelectedListener(object :
TabLayout.OnTabSelectedListener {
override fun onTabSelected(tab: TabLayout.Tab?) {
val finalTab = tab
if (finalTab != null) {
val tiltle = finalTab.text
val dynamic = getString(R.string.dynamic)
if (dynamic == tiltle && userId == account) {
viewBinding.fab.show()
} else {
viewBinding.fab.hide()
}
}
}
override fun onTabUnselected(tab: TabLayout.Tab?) {
}
override fun onTabReselected(tab: TabLayout.Tab?) {
}
})
}
}
override fun onResume() {
super.onResume()
User.getSpaceInfo(userId, object : ApiCallBack<SpaceInfoData> {
override fun onResponse(t: SpaceInfoData) {
if (t.code == ServerConfiguration.Success_Code) {
showUserdataIfNeed(t)
} else {
showError(t.message)
}
}
override fun onFailure(e: Exception) {
showInternetError(null, e)
}
})
}
2022-07-17 13:51:14 +00:00
// /**
// * 获取状态栏高度
// * @param context Context
// * @return Int
// */
// fun statusBarHeight(context: Context): Int {
// var height = 0
// val res = context.resources
// val resId = res.getIdentifier("status_bar_height", "dimen", "android")
// if (resId > 0) {
// height = res.getDimensionPixelSize(resId)
// }
// return height
// }
2022-04-04 14:03:10 +00:00
private fun initAction() {
viewBinding.fab.setOnClickListener {
2022-07-17 13:51:14 +00:00
CommentDialog(this).setTitle(R.string.send_dynamic).setCancelable(false)
.setSubmitFun { button, textInputLayout, s, alertDialog ->
val token =
2022-08-14 02:43:53 +00:00
AppSettings.getValue(AppSettings.Setting.Token, "")
2022-07-17 13:51:14 +00:00
if (!s.isBlank()) {
Dynamic.instance.send(
token,
s,
object : ApiCallBack<ApiResponse> {
override fun onResponse(t: ApiResponse) {
if (t.code == ServerConfiguration.Success_Code) {
alertDialog.dismiss()
Snackbar.make(
viewBinding.button,
R.string.release_ok,
Snackbar.LENGTH_SHORT
).show()
userHomeStateAdapter.updataDynamicList()
} else {
Snackbar.make(
viewBinding.button,
t.message,
Snackbar.LENGTH_SHORT
).show()
2022-04-04 14:03:10 +00:00
}
2022-07-17 13:51:14 +00:00
}
2022-04-04 14:03:10 +00:00
2022-07-17 13:51:14 +00:00
override fun onFailure(e: Exception) {
showInternetError(viewBinding.button, e)
}
2022-04-04 14:03:10 +00:00
2022-07-17 13:51:14 +00:00
})
}
}.show()
// MaterialDialog(this, BottomSheet(LayoutMode.WRAP_CONTENT)).show {
// input(maxLength = 255).title()
// .positiveButton(R.string.dialog_ok)
// .positiveButton {
// val inputField: EditText = it.getInputField()
// val text = inputField.text.toString()
//
// }
// }.negativeButton(R.string.dialog_cancel)
//
// val editText = this.getInputField()
// editText.inputType =
// EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE
// editText.minLines = 3
// editText.gravity = Gravity.TOP
// editText.isSingleLine = false
// }
2022-04-04 14:03:10 +00:00
}
viewBinding.button.setOnClickListener {
when (val type = viewBinding.button.text.toString()) {
getString(R.string.follow) -> {
viewBinding.button.setBackgroundColor(
GlobalMethod.getThemeColor(
this,
R.attr.colorPrimaryVariant
)
)
viewBinding.button.setText(R.string.request_data)
Community.follow(account!!, userId, object : ApiCallBack<ApiResponse> {
2022-04-04 14:03:10 +00:00
override fun onResponse(t: ApiResponse) {
viewBinding.button.setBackgroundColor(
GlobalMethod.getColorPrimary(
this@UserHomePageActivity
)
)
if (t.code == ServerConfiguration.Success_Code) {
fans++
viewBinding.fansNumView.text =
ServerConfiguration.numberToString(fans)
viewBinding.button.text = getString(R.string.followed)
} else {
viewBinding.button.text = type
2022-07-17 13:51:14 +00:00
Snackbar.make(
viewBinding.button,
t.message,
Snackbar.LENGTH_SHORT
)
2022-04-04 14:03:10 +00:00
.show()
}
}
override fun onFailure(e: Exception) {
showInternetError(viewBinding.button, e)
viewBinding.button.setBackgroundColor(
GlobalMethod.getColorPrimary(
this@UserHomePageActivity
)
)
viewBinding.button.text = type
}
})
}
getString(R.string.followed), getString(R.string.each_other_follow) -> {
val de = String.format(getString(R.string.defollow_tip), userName ?: userId)
2022-09-04 07:49:29 +00:00
CoreDialog(this).setTitle(R.string.defollow).setMessage(de)
.setPositiveButton(R.string.dialog_ok) {
viewBinding.button.setBackgroundColor(
GlobalMethod.getThemeColor(
this@UserHomePageActivity,
R.attr.colorPrimaryVariant
2022-04-04 14:03:10 +00:00
)
2022-09-04 07:49:29 +00:00
)
viewBinding.button.setText(R.string.request_data)
Community.deFollow(
account!!,
2022-09-04 07:49:29 +00:00
userId,
object : ApiCallBack<ApiResponse> {
override fun onResponse(t: ApiResponse) {
viewBinding.button.setBackgroundColor(
GlobalMethod.getColorPrimary(
this@UserHomePageActivity
2022-04-04 14:03:10 +00:00
)
2022-09-04 07:49:29 +00:00
)
if (t.code == ServerConfiguration.Success_Code) {
fans--
viewBinding.fansNumView.text =
ServerConfiguration.numberToString(fans)
viewBinding.button.text =
getString(R.string.follow)
} else {
Snackbar.make(
viewBinding.button,
t.message,
Snackbar.LENGTH_SHORT
).show()
viewBinding.button.text = type
2022-04-04 14:03:10 +00:00
}
2022-09-04 07:49:29 +00:00
}
2022-04-04 14:03:10 +00:00
2022-09-04 07:49:29 +00:00
override fun onFailure(e: Exception) {
showInternetError(viewBinding.button, e)
}
2022-04-04 14:03:10 +00:00
2022-09-04 07:49:29 +00:00
})
}.setNegativeButton(R.string.dialog_cancel) {
2022-04-04 14:03:10 +00:00
}.show()
2022-04-04 14:03:10 +00:00
}
getString(R.string.editData) -> {
val intent = Intent(this, EditUserInfoActivity::class.java)
intent.putExtra("userId", userId)
startActivity(intent)
}
}
}
}
2022-08-14 02:43:53 +00:00
override fun getViewBindingObject(layoutInflater: LayoutInflater): ActivityUserHomePageBinding {
2022-04-04 14:03:10 +00:00
return ActivityUserHomePageBinding.inflate(layoutInflater)
}
override fun whenCreateActivity(savedInstanceState: Bundle?, canUseView: Boolean) {
if (canUseView) {
initView()
initAction()
}
}
}