fix: 修复用户主页头像模糊

This commit is contained in:
HuanCheng65 2023-02-15 21:38:25 +08:00
parent d917070d98
commit af407efdb9
No known key found for this signature in database
GPG Key ID: E9031EF91A805148
6 changed files with 110 additions and 97 deletions

View File

@ -22,7 +22,7 @@ import com.google.android.material.tabs.TabLayout
import com.huanchengfly.tieba.post.R
import com.huanchengfly.tieba.post.adapters.FragmentTabViewPagerAdapter
import com.huanchengfly.tieba.post.api.TiebaApi
import com.huanchengfly.tieba.post.api.models.ProfileBean
import com.huanchengfly.tieba.post.api.models.protos.profile.ProfileResponseData
import com.huanchengfly.tieba.post.api.retrofit.exception.getErrorMessage
import com.huanchengfly.tieba.post.fragments.UserLikeForumFragment
import com.huanchengfly.tieba.post.fragments.UserPostFragment
@ -35,14 +35,8 @@ import com.huanchengfly.tieba.post.ui.page.editprofile.view.EditProfileActivity
import com.huanchengfly.tieba.post.ui.widgets.theme.TintMaterialButton
import com.huanchengfly.tieba.post.ui.widgets.theme.TintToolbar
import com.huanchengfly.tieba.post.utils.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.launch
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import kotlin.math.abs
@ -92,7 +86,7 @@ class UserActivity : BaseActivity() {
@BindView(R.id.user_info_chips)
lateinit var infoChips: View
private var profileBean: ProfileBean? = null
private var profileBean: ProfileResponseData? = null
private var uid: String? = null
private var tab = 0
@ -113,7 +107,8 @@ class UserActivity : BaseActivity() {
uid = intent.getStringExtra(EXTRA_UID)
tab = intent.getIntExtra(EXTRA_TAB, TAB_THREAD)
val avatar = intent.getStringExtra(EXTRA_AVATAR)
if (uid == null) {
val uidLong = uid?.toLongOrNull()
if (uidLong == null) {
finish()
return
}
@ -124,14 +119,17 @@ class UserActivity : BaseActivity() {
if (!TextUtils.isEmpty(avatar)) {
loadingView.visibility = View.GONE
ImageUtil.load(avatarView, ImageUtil.LOAD_TYPE_AVATAR, StringUtil.getAvatarUrl(avatar))
ImageUtil.initImageView(avatarView, PhotoViewBean(StringUtil.getAvatarUrl(avatar), null))
ImageUtil.initImageView(
avatarView,
PhotoViewBean(StringUtil.getAvatarUrl(avatar), null)
)
}
appbar.addOnOffsetChangedListener { appBarLayout: AppBarLayout, verticalOffset: Int ->
val percent = abs(verticalOffset * 1.0f) / appBarLayout.totalScrollRange
headerView.alpha = 1f - percent
headerMaskView.alpha = percent
if (profileBean != null && profileBean!!.user != null && abs(verticalOffset) >= appBarLayout.totalScrollRange) {
toolbar.title = profileBean!!.user!!.nameShow
if (profileBean != null && abs(verticalOffset) >= appBarLayout.totalScrollRange) {
toolbar.title = profileBean?.user?.nameShow
} else {
toolbar.title = null
}
@ -142,36 +140,34 @@ class UserActivity : BaseActivity() {
setSupportActionBar(toolbar)
val actionBar = supportActionBar
actionBar?.setDisplayHomeAsUpEnabled(true)
launch {
TiebaApi.getInstance()
.profile(uid!!)
.enqueue(object : Callback<ProfileBean?> {
override fun onResponse(
call: Call<ProfileBean?>,
response: Response<ProfileBean?>
) {
val data = response.body()
.userProfileFlow(uidLong)
.collect {
actionBtn.visibility = View.VISIBLE
loadingView.visibility = View.GONE
val data = it.data_
if (data?.user == null) {
return@collect
}
profileBean = data
refreshHeader()
adapter.clear()
adapter.addFragment(
UserPostFragment.newInstance(uid, true),
"贴子 " + data!!.user!!.threadNum
"贴子 " + data.user.thread_num
)
adapter.addFragment(
UserPostFragment.newInstance(uid, false),
"回复 " + data.user!!.repostNum
"回复 " + data.user.post_num
)
adapter.addFragment(
UserLikeForumFragment.newInstance(uid),
"关注吧 " + data.user.myLikeNum
"关注吧 " + data.user.my_like_num
)
viewPager.setCurrentItem(tab, false)
}
override fun onFailure(call: Call<ProfileBean?>, t: Throwable) {}
})
}
listOf(
followStatTv,
fansStatTv
@ -180,39 +176,36 @@ class UserActivity : BaseActivity() {
}
}
fun refreshHeader() {
profileBean?.let {
if (it.user == null) {
return
}
titleView.text = it.user.nameShow
sloganView.text = it.user.intro
followStatTv.text = "${it.user.concernNum}"
fansStatTv.text = "${it.user.fansNum}"
private fun refreshHeader() {
profileBean?.user?.let {
titleView.text = it.nameShow
sloganView.text = it.intro
followStatTv.text = "${it.concern_num}"
fansStatTv.text = "${it.fans_num}"
//getString(R.string.tip_stat, profileBean!!.user!!.concernNum, profileBean!!.user!!.fansNum)
if (avatarView.tag == null) {
ImageUtil.load(
avatarView,
ImageUtil.LOAD_TYPE_AVATAR,
StringUtil.getAvatarUrl(it.user.portrait)
StringUtil.getAvatarUrl(it.portrait)
)
ImageUtil.initImageView(
avatarView,
PhotoViewBean(StringUtil.getAvatarUrl(it.user.portrait), null)
PhotoViewBean(StringUtil.getBigAvatarUrl(it.portraith), null)
)
}
if (TextUtils.equals(AccountUtil.getUid(), it.user.id)) {
if (TextUtils.equals(AccountUtil.getUid(), "${it.id}")) {
actionBtn.setText(R.string.menu_edit_info)
} else {
if ("1" == it.user.hasConcerned) {
if (1 == it.has_concerned) {
actionBtn.setText(R.string.button_unfollow)
} else {
actionBtn.setText(R.string.button_follow)
}
}
sexTv.text =
if (it.user.sex == "1") "" else if (it.user.sex == "2") "" else "?"
tbAgeTv.text = getString(R.string.tb_age, it.user.tbAge)
if (it.sex == 1) "" else if (it.sex == 2) "" else "?"
tbAgeTv.text = getString(R.string.tb_age, it.tb_age)
infoChips.visibility = View.VISIBLE
}
}
@ -234,16 +227,14 @@ class UserActivity : BaseActivity() {
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.menu_block_black, R.id.menu_block_white -> {
if (profileBean == null || profileBean!!.user == null) {
return true
}
profileBean?.user?.let {
val category =
if (item.itemId == R.id.menu_block_black) Block.CATEGORY_BLACK_LIST else Block.CATEGORY_WHITE_LIST
Block(
category = category,
type = Block.TYPE_USER,
username = profileBean!!.user!!.name,
uid = profileBean!!.user!!.id
username = it.name,
uid = it.id.toString()
).saveAsync()
.listen { success: Boolean ->
if (success) {
@ -251,8 +242,10 @@ class UserActivity : BaseActivity() {
.show()
}
}
}
return true
}
R.id.menu_edit_info -> {
startActivity(WebViewActivity.newIntent(this, getString(R.string.url_edit_info)))
return true
@ -272,35 +265,37 @@ class UserActivity : BaseActivity() {
@OnClick(R.id.user_center_action_btn)
fun onActionBtnClick(view: View?) {
if (TextUtils.equals(profileBean!!.user!!.id, AccountUtil.getUid())) {
profileBean?.user?.let { user ->
if (TextUtils.equals(user.id.toString(), AccountUtil.getUid())) {
goToActivity<EditProfileActivity>()
return
}
if ("1" == profileBean!!.user!!.hasConcerned) {
MainScope().launch {
if (1 == user.has_concerned) {
launch {
TiebaApi.getInstance()
.unfollowFlow(
profileBean!!.user!!.portrait!!,
user.portrait,
AccountUtil.getLoginInfo()!!.tbs
)
.flowOn(Dispatchers.IO)
.catch { e ->
toastShort(e.getErrorMessage())
}
.collect {
//toastShort(R.string.toast_success)
profileBean!!.user!!.setHasConcerned("0")
profileBean = profileBean!!.copy(
user = profileBean?.user!!.copy(
has_concerned = 0
)
)
refreshHeader()
}
}
} else {
MainScope().launch {
launch {
TiebaApi.getInstance()
.followFlow(
profileBean!!.user!!.portrait!!,
user.portrait,
AccountUtil.getLoginInfo()!!.tbs
)
.flowOn(Dispatchers.IO)
.catch { e ->
toastShort(e.getErrorMessage())
}
@ -308,12 +303,17 @@ class UserActivity : BaseActivity() {
if ("1" == it.info.isToast) {
toastShort(it.info.toastText)
}
profileBean!!.user!!.setHasConcerned("1")
profileBean = profileBean!!.copy(
user = profileBean?.user!!.copy(
has_concerned = 1
)
)
refreshHeader()
}
}
}
}
}
companion object {
const val TAG = "UserActivity"

View File

@ -155,7 +155,7 @@ interface ITiebaApi {
*
* @param forumName 吧名
* @param page 分页页码 1 开始
* @param sortType 排序类型 [com.huanchengfly.tieba.api.ForumSortType]
* @param sortType 排序类型 [com.huanchengfly.tieba.post.api.ForumSortType]
* @param goodClassifyId 精品贴分类 ID
*/
fun forumPage(
@ -1222,7 +1222,7 @@ interface ITiebaApi {
*
* @param uid 用户 ID
*/
fun profileFlow(
fun userProfileFlow(
uid: Long
): Flow<ProfileResponse>
}

View File

@ -975,7 +975,7 @@ object MixedTiebaApiImpl : ITiebaApi {
)
}
override fun profileFlow(uid: Long): Flow<ProfileResponse> {
override fun userProfileFlow(uid: Long): Flow<ProfileResponse> {
val selfUid = AccountUtil.getUid()?.toLongOrNull()
val isSelf = selfUid == uid
return RetrofitTiebaApi.OFFICIAL_PROTOBUF_TIEBA_API.profileFlow(
@ -987,6 +987,7 @@ object MixedTiebaApiImpl : ITiebaApi {
friend_uid_portrait = "",
has_plist = 1,
is_from_usercenter = 1,
is_guest = if (isSelf) 0 else 1,
need_post_count = 1,
page = 2,
pn = 1,

View File

@ -119,6 +119,16 @@ object StringUtil {
} else "http://tb.himg.baidu.com/sys/portrait/item/$portrait"
}
@JvmStatic
fun getBigAvatarUrl(portrait: String?): String {
if (portrait.isNullOrEmpty()) {
return ""
}
return if (portrait.startsWith("http://") || portrait.startsWith("https://")) {
portrait
} else "http://tb.himg.baidu.com/sys/portraith/item/$portrait"
}
fun String.getShortNumString(): String {
val long = toLongOrNull() ?: return ""
return long.getShortNumString()

View File

@ -16,6 +16,7 @@ message ProfileRequestData {
optional string friend_uid_portrait = 16;
uint32 has_plist = 8;
int32 is_from_usercenter = 14;
uint32 is_guest = 4;
uint32 need_post_count = 2;
int32 page = 15;
uint32 pn = 6;

View File

@ -17,6 +17,7 @@ message User {
int32 is_manager = 11;
int32 is_bawu = 25;
string bawu_type = 26;
string portraith = 27;
int32 fans_num = 30;
int32 concern_num = 31;
int32 sex = 32;