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

View File

@ -155,7 +155,7 @@ interface ITiebaApi {
* *
* @param forumName 吧名 * @param forumName 吧名
* @param page 分页页码 1 开始 * @param page 分页页码 1 开始
* @param sortType 排序类型 [com.huanchengfly.tieba.api.ForumSortType] * @param sortType 排序类型 [com.huanchengfly.tieba.post.api.ForumSortType]
* @param goodClassifyId 精品贴分类 ID * @param goodClassifyId 精品贴分类 ID
*/ */
fun forumPage( fun forumPage(
@ -1222,7 +1222,7 @@ interface ITiebaApi {
* *
* @param uid 用户 ID * @param uid 用户 ID
*/ */
fun profileFlow( fun userProfileFlow(
uid: Long uid: Long
): Flow<ProfileResponse> ): 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 selfUid = AccountUtil.getUid()?.toLongOrNull()
val isSelf = selfUid == uid val isSelf = selfUid == uid
return RetrofitTiebaApi.OFFICIAL_PROTOBUF_TIEBA_API.profileFlow( return RetrofitTiebaApi.OFFICIAL_PROTOBUF_TIEBA_API.profileFlow(
@ -987,6 +987,7 @@ object MixedTiebaApiImpl : ITiebaApi {
friend_uid_portrait = "", friend_uid_portrait = "",
has_plist = 1, has_plist = 1,
is_from_usercenter = 1, is_from_usercenter = 1,
is_guest = if (isSelf) 0 else 1,
need_post_count = 1, need_post_count = 1,
page = 2, page = 2,
pn = 1, pn = 1,

View File

@ -119,6 +119,16 @@ object StringUtil {
} else "http://tb.himg.baidu.com/sys/portrait/item/$portrait" } 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 { fun String.getShortNumString(): String {
val long = toLongOrNull() ?: return "" val long = toLongOrNull() ?: return ""
return long.getShortNumString() return long.getShortNumString()

View File

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

View File

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