From af407efdb9a47ab067cf34c0d68981bfa0183986 Mon Sep 17 00:00:00 2001 From: HuanCheng65 <22636177+HuanCheng65@users.noreply.github.com> Date: Wed, 15 Feb 2023 21:38:25 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E4=B8=BB=E9=A1=B5=E5=A4=B4=E5=83=8F=E6=A8=A1=E7=B3=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tieba/post/activities/UserActivity.kt | 188 +++++++++--------- .../tieba/post/api/interfaces/ITiebaApi.kt | 4 +- .../api/interfaces/impls/MixedTiebaApiImpl.kt | 3 +- .../tieba/post/utils/StringUtil.kt | 10 + app/src/main/protos/Profile/Profile.proto | 1 + app/src/main/protos/User.proto | 1 + 6 files changed, 110 insertions(+), 97 deletions(-) diff --git a/app/src/main/java/com/huanchengfly/tieba/post/activities/UserActivity.kt b/app/src/main/java/com/huanchengfly/tieba/post/activities/UserActivity.kt index f3bfed78..40ebcbb8 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/activities/UserActivity.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/activities/UserActivity.kt @@ -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) - TiebaApi.getInstance() - .profile(uid!!) - .enqueue(object : Callback { - override fun onResponse( - call: Call, - response: Response - ) { - val data = response.body() + launch { + TiebaApi.getInstance() + .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, 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,25 +227,25 @@ 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 - } - 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 - ).saveAsync() - .listen { success: Boolean -> - if (success) { - Toast.makeText(this, R.string.toast_add_success, Toast.LENGTH_SHORT) - .show() + 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 = it.name, + uid = it.id.toString() + ).saveAsync() + .listen { success: Boolean -> + if (success) { + Toast.makeText(this, R.string.toast_add_success, Toast.LENGTH_SHORT) + .show() + } } - } + } return true } + R.id.menu_edit_info -> { startActivity(WebViewActivity.newIntent(this, getString(R.string.url_edit_info))) return true @@ -272,45 +265,52 @@ class UserActivity : BaseActivity() { @OnClick(R.id.user_center_action_btn) fun onActionBtnClick(view: View?) { - if (TextUtils.equals(profileBean!!.user!!.id, AccountUtil.getUid())) { - goToActivity() - return - } - if ("1" == profileBean!!.user!!.hasConcerned) { - MainScope().launch { - TiebaApi.getInstance() - .unfollowFlow( - profileBean!!.user!!.portrait!!, - AccountUtil.getLoginInfo()!!.tbs - ) - .flowOn(Dispatchers.IO) - .catch { e -> - toastShort(e.getErrorMessage()) - } - .collect { - //toastShort(R.string.toast_success) - profileBean!!.user!!.setHasConcerned("0") - refreshHeader() - } + profileBean?.user?.let { user -> + if (TextUtils.equals(user.id.toString(), AccountUtil.getUid())) { + goToActivity() + return } - } else { - MainScope().launch { - TiebaApi.getInstance() - .followFlow( - profileBean!!.user!!.portrait!!, - AccountUtil.getLoginInfo()!!.tbs - ) - .flowOn(Dispatchers.IO) - .catch { e -> - toastShort(e.getErrorMessage()) - } - .collect { - if ("1" == it.info.isToast) { - toastShort(it.info.toastText) + if (1 == user.has_concerned) { + launch { + TiebaApi.getInstance() + .unfollowFlow( + user.portrait, + AccountUtil.getLoginInfo()!!.tbs + ) + .catch { e -> + toastShort(e.getErrorMessage()) } - profileBean!!.user!!.setHasConcerned("1") - refreshHeader() - } + .collect { + profileBean = profileBean!!.copy( + user = profileBean?.user!!.copy( + has_concerned = 0 + ) + ) + refreshHeader() + } + } + } else { + launch { + TiebaApi.getInstance() + .followFlow( + user.portrait, + AccountUtil.getLoginInfo()!!.tbs + ) + .catch { e -> + toastShort(e.getErrorMessage()) + } + .collect { + if ("1" == it.info.isToast) { + toastShort(it.info.toastText) + } + profileBean = profileBean!!.copy( + user = profileBean?.user!!.copy( + has_concerned = 1 + ) + ) + refreshHeader() + } + } } } } diff --git a/app/src/main/java/com/huanchengfly/tieba/post/api/interfaces/ITiebaApi.kt b/app/src/main/java/com/huanchengfly/tieba/post/api/interfaces/ITiebaApi.kt index 608e4598..64d8f308 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/api/interfaces/ITiebaApi.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/api/interfaces/ITiebaApi.kt @@ -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 } \ No newline at end of file diff --git a/app/src/main/java/com/huanchengfly/tieba/post/api/interfaces/impls/MixedTiebaApiImpl.kt b/app/src/main/java/com/huanchengfly/tieba/post/api/interfaces/impls/MixedTiebaApiImpl.kt index 2e0d50af..d09a6487 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/api/interfaces/impls/MixedTiebaApiImpl.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/api/interfaces/impls/MixedTiebaApiImpl.kt @@ -975,7 +975,7 @@ object MixedTiebaApiImpl : ITiebaApi { ) } - override fun profileFlow(uid: Long): Flow { + override fun userProfileFlow(uid: Long): Flow { 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, diff --git a/app/src/main/java/com/huanchengfly/tieba/post/utils/StringUtil.kt b/app/src/main/java/com/huanchengfly/tieba/post/utils/StringUtil.kt index 588386dd..0957d0aa 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/utils/StringUtil.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/utils/StringUtil.kt @@ -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() diff --git a/app/src/main/protos/Profile/Profile.proto b/app/src/main/protos/Profile/Profile.proto index 0655c84b..09946346 100644 --- a/app/src/main/protos/Profile/Profile.proto +++ b/app/src/main/protos/Profile/Profile.proto @@ -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; diff --git a/app/src/main/protos/User.proto b/app/src/main/protos/User.proto index 41c1e4df..99c468ac 100644 --- a/app/src/main/protos/User.proto +++ b/app/src/main/protos/User.proto @@ -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;