pref: 优化 API 调用
This commit is contained in:
parent
257dd0ee38
commit
b487b1686c
|
|
@ -31,6 +31,7 @@ import androidx.core.content.pm.ShortcutInfoCompat
|
|||
import androidx.core.content.pm.ShortcutManagerCompat
|
||||
import androidx.core.graphics.drawable.IconCompat
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.lifecycle.flowWithLifecycle
|
||||
import butterknife.BindView
|
||||
import cn.jzvd.Jzvd
|
||||
import com.bumptech.glide.Glide
|
||||
|
|
@ -51,8 +52,7 @@ import com.huanchengfly.tieba.post.api.TiebaApi
|
|||
import com.huanchengfly.tieba.post.api.models.CommonResponse
|
||||
import com.huanchengfly.tieba.post.api.models.ForumPageBean
|
||||
import com.huanchengfly.tieba.post.api.models.LikeForumResultBean
|
||||
import com.huanchengfly.tieba.post.api.retrofit.doIfFailure
|
||||
import com.huanchengfly.tieba.post.api.retrofit.doIfSuccess
|
||||
import com.huanchengfly.tieba.post.api.retrofit.exception.getErrorMessage
|
||||
import com.huanchengfly.tieba.post.fragments.ForumFragment
|
||||
import com.huanchengfly.tieba.post.fragments.ForumFragment.OnRefreshedListener
|
||||
import com.huanchengfly.tieba.post.interfaces.Refreshable
|
||||
|
|
@ -72,6 +72,9 @@ import com.huanchengfly.tieba.post.utils.StringUtil.getShortNumString
|
|||
import com.huanchengfly.tieba.post.utils.anim.animSet
|
||||
import com.huanchengfly.tieba.post.utils.preload.PreloadUtil
|
||||
import kotlinx.coroutines.Dispatchers.IO
|
||||
import kotlinx.coroutines.flow.catch
|
||||
import kotlinx.coroutines.flow.flowOn
|
||||
import kotlinx.coroutines.flow.mapNotNull
|
||||
import kotlinx.coroutines.launch
|
||||
import retrofit2.Call
|
||||
import retrofit2.Callback
|
||||
|
|
@ -539,36 +542,40 @@ class ForumActivity : BaseActivity(), View.OnClickListener, OnRefreshedListener,
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
R.id.toolbar -> scrollToTop()
|
||||
R.id.forum_header_button, R.id.toolbar_btn_right -> if (mDataBean != null) {
|
||||
if ("1" == mDataBean!!.forum?.isLike) {
|
||||
if ("0" == mDataBean!!.forum?.signInInfo?.userInfo?.isSignIn) {
|
||||
launch(IO + job) {
|
||||
R.id.forum_header_button, R.id.toolbar_btn_right -> {
|
||||
val forum = mDataBean?.forum
|
||||
val tbs = mDataBean?.anti?.tbs ?: AccountUtil.getLoginInfo()?.tbs
|
||||
if ("1" == forum?.isLike) {
|
||||
if (tbs != null && forum.id != null && forum.name != null && "0" == forum.signInInfo?.userInfo?.isSignIn) {
|
||||
launch {
|
||||
TiebaApi.getInstance()
|
||||
.signAsync(mDataBean!!.forum?.name!!, mDataBean!!.anti?.tbs!!)
|
||||
.doIfSuccess {
|
||||
if (it.userInfo != null) {
|
||||
mDataBean!!.forum?.signInInfo?.userInfo?.isSignIn = "1"
|
||||
Util.createSnackbar(
|
||||
myViewPager,
|
||||
getString(
|
||||
R.string.toast_sign_success,
|
||||
it.userInfo.signBonusPoint,
|
||||
it.userInfo.userSignRank
|
||||
),
|
||||
Snackbar.LENGTH_SHORT
|
||||
).show()
|
||||
refreshHeaderView()
|
||||
refreshForumInfo()
|
||||
}
|
||||
}
|
||||
.doIfFailure {
|
||||
.signFlow(forum.id, forum.name, tbs)
|
||||
.catch {
|
||||
Util.createSnackbar(
|
||||
myViewPager,
|
||||
getString(R.string.toast_sign_failed, it.message),
|
||||
getString(R.string.toast_sign_failed, it.getErrorMessage()),
|
||||
Snackbar.LENGTH_SHORT
|
||||
).show()
|
||||
}
|
||||
.flowOn(IO)
|
||||
.flowWithLifecycle(lifecycle)
|
||||
.mapNotNull { it.userInfo }
|
||||
.collect {
|
||||
mDataBean!!.forum?.signInInfo?.userInfo?.isSignIn = "1"
|
||||
Util.createSnackbar(
|
||||
myViewPager,
|
||||
getString(
|
||||
R.string.toast_sign_success,
|
||||
it.signBonusPoint,
|
||||
it.userSignRank
|
||||
),
|
||||
Snackbar.LENGTH_SHORT
|
||||
).show()
|
||||
refreshHeaderView()
|
||||
refreshForumInfo()
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
|
@ -597,7 +604,7 @@ class ForumActivity : BaseActivity(), View.OnClickListener, OnRefreshedListener,
|
|||
this@ForumActivity,
|
||||
getString(
|
||||
R.string.toast_like_success,
|
||||
data.info?.memberSum
|
||||
data.info.memberSum
|
||||
),
|
||||
Toast.LENGTH_SHORT
|
||||
).show()
|
||||
|
|
|
|||
|
|
@ -89,29 +89,30 @@ public class ForumBeanCaster extends ICaster<ForumBean, ForumPageBean> {
|
|||
private static class FrsForumBeanImpl extends ICaster<ForumBean.FrsForumBean, ForumPageBean.ForumBean> {
|
||||
@Override
|
||||
public ForumPageBean.ForumBean cast(ForumBean.FrsForumBean frsForumBean) {
|
||||
ForumPageBean.ForumBean forumBean = new ForumPageBean.ForumBean();
|
||||
forumBean.setAvatar(frsForumBean.getAvatar());
|
||||
forumBean.setCurScore(frsForumBean.getCurScore());
|
||||
forumBean.setGoodClassify(frsForumBean.getGoodClassify());
|
||||
forumBean.setId(frsForumBean.getId());
|
||||
forumBean.setIsExists(frsForumBean.isExists() ? "1" : "0");
|
||||
forumBean.setIsLike(frsForumBean.getIsLike());
|
||||
forumBean.setLevelId(frsForumBean.getLevelId());
|
||||
forumBean.setLevelName(frsForumBean.getLevelName());
|
||||
forumBean.setLevelUpScore(frsForumBean.getLevelUpScore());
|
||||
forumBean.setName(frsForumBean.getName());
|
||||
forumBean.setSignInInfo(frsForumBean.getSignInInfo());
|
||||
forumBean.setTids(frsForumBean.getTids());
|
||||
forumBean.setSlogan(frsForumBean.getSlogan());
|
||||
forumBean.setUserLevel(frsForumBean.getUserLevel());
|
||||
forumBean.setMemberNum(frsForumBean.getMemberNum());
|
||||
forumBean.setPostNum(frsForumBean.getPostNum());
|
||||
forumBean.setThreadNum(frsForumBean.getThreadNum());
|
||||
forumBean.setManagers(frsForumBean.getManagers());
|
||||
forumBean.setZyqTitle(frsForumBean.getAttrs().getZyqTitle());
|
||||
forumBean.setZyqDefine(frsForumBean.getAttrs().getZyqDefine());
|
||||
forumBean.setZyqFriend(frsForumBean.getAttrs().getZyqFriend());
|
||||
return forumBean;
|
||||
return new ForumPageBean.ForumBean(
|
||||
frsForumBean.getId(),
|
||||
frsForumBean.getName(),
|
||||
frsForumBean.getIsLike(),
|
||||
frsForumBean.getUserLevel(),
|
||||
frsForumBean.getLevelId(),
|
||||
frsForumBean.getLevelName(),
|
||||
frsForumBean.isExists() ? "1" : "0",
|
||||
frsForumBean.getCurScore(),
|
||||
frsForumBean.getLevelUpScore(),
|
||||
frsForumBean.getMemberNum(),
|
||||
frsForumBean.getThreadNum(),
|
||||
null,
|
||||
frsForumBean.getPostNum(),
|
||||
frsForumBean.getManagers(),
|
||||
frsForumBean.getAttrs().getZyqTitle(),
|
||||
frsForumBean.getAttrs().getZyqDefine(),
|
||||
frsForumBean.getAttrs().getZyqFriend(),
|
||||
frsForumBean.getGoodClassify(),
|
||||
frsForumBean.getSlogan(),
|
||||
frsForumBean.getAvatar(),
|
||||
frsForumBean.getTids(),
|
||||
frsForumBean.getSignInInfo()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -58,90 +58,72 @@ class ForumPageBean : ErrorBean() {
|
|||
)
|
||||
}
|
||||
|
||||
class ForumBean : BaseBean() {
|
||||
var id: String? = null
|
||||
var name: String? = null
|
||||
data class ForumBean(
|
||||
val id: String? = null,
|
||||
val name: String? = null,
|
||||
|
||||
@SerializedName("is_like")
|
||||
var isLike: String? = null
|
||||
var isLike: String? = null,
|
||||
|
||||
@SerializedName("user_level")
|
||||
var userLevel: String? = null
|
||||
var userLevel: String? = null,
|
||||
|
||||
@SerializedName("level_id")
|
||||
var levelId: String? = null
|
||||
var levelId: String? = null,
|
||||
|
||||
@SerializedName("level_name")
|
||||
var levelName: String? = null
|
||||
var levelName: String? = null,
|
||||
|
||||
@SerializedName("is_exists")
|
||||
var isExists: String? = null
|
||||
var isExists: String? = null,
|
||||
|
||||
@SerializedName("cur_score")
|
||||
var curScore: String? = null
|
||||
var curScore: String? = null,
|
||||
|
||||
@SerializedName("levelup_score")
|
||||
var levelUpScore: String? = null
|
||||
var levelUpScore: String? = null,
|
||||
|
||||
@SerializedName("member_num")
|
||||
var memberNum: String? = null
|
||||
var memberNum: String? = null,
|
||||
|
||||
@SerializedName("thread_num")
|
||||
var threadNum: String? = null
|
||||
var threadNum: String? = null,
|
||||
|
||||
@SerializedName("theme_color")
|
||||
var themeColor: ThemeColors? = null
|
||||
var themeColor: ThemeColors? = null,
|
||||
|
||||
@SerializedName("post_num")
|
||||
var postNum: String? = null
|
||||
var managers: List<ManagerBean>? = null
|
||||
var zyqTitle: String? = null
|
||||
var zyqDefine: List<ZyqDefineBean>? = null
|
||||
var zyqFriend: List<String>? = null
|
||||
var postNum: String? = null,
|
||||
var managers: List<ManagerBean>? = null,
|
||||
var zyqTitle: String? = null,
|
||||
var zyqDefine: List<ZyqDefineBean>? = null,
|
||||
var zyqFriend: List<String>? = null,
|
||||
|
||||
@SerializedName("good_classify")
|
||||
var goodClassify: List<GoodClassifyBean>? = null
|
||||
var slogan: String? = null
|
||||
var avatar: String? = null
|
||||
var tids: String? = null
|
||||
|
||||
var goodClassify: List<GoodClassifyBean>? = null,
|
||||
var slogan: String? = null,
|
||||
var avatar: String? = null,
|
||||
var tids: String? = null,
|
||||
@SerializedName("sign_in_info")
|
||||
var signInInfo: SignInInfo? = null
|
||||
|
||||
fun setIsLike(isLike: String?): ForumBean {
|
||||
this.isLike = isLike
|
||||
return this
|
||||
}
|
||||
|
||||
fun setIsExists(isExists: String?): ForumBean {
|
||||
this.isExists = isExists
|
||||
return this
|
||||
}
|
||||
|
||||
class SignInInfo {
|
||||
) : BaseBean() {
|
||||
data class SignInInfo(
|
||||
@SerializedName("user_info")
|
||||
val userInfo: UserInfo? = null
|
||||
|
||||
class UserInfo {
|
||||
) {
|
||||
data class UserInfo(
|
||||
@SerializedName("is_sign_in")
|
||||
var isSignIn: String? = null
|
||||
|
||||
fun setIsSignIn(isSignIn: String?): UserInfo {
|
||||
this.isSignIn = isSignIn
|
||||
return this
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
class GoodClassifyBean : BaseBean() {
|
||||
data class GoodClassifyBean(
|
||||
@SerializedName("class_id")
|
||||
val classId: String? = null
|
||||
|
||||
val classId: String? = null,
|
||||
@SerializedName("class_name")
|
||||
val className: String? = null
|
||||
|
||||
}
|
||||
) : BaseBean()
|
||||
|
||||
class AntiBean : BaseBean() {
|
||||
val tbs: String? = null
|
||||
|
|
|
|||
|
|
@ -5,9 +5,6 @@ import android.util.Log
|
|||
import com.huanchengfly.tieba.post.api.TiebaApi
|
||||
import com.huanchengfly.tieba.post.api.models.MSignBean
|
||||
import com.huanchengfly.tieba.post.api.models.SignResultBean
|
||||
import com.huanchengfly.tieba.post.api.retrofit.ApiResult
|
||||
import com.huanchengfly.tieba.post.api.retrofit.doIfFailure
|
||||
import com.huanchengfly.tieba.post.api.retrofit.doIfSuccess
|
||||
import com.huanchengfly.tieba.post.api.retrofit.exception.getErrorCode
|
||||
import com.huanchengfly.tieba.post.api.retrofit.exception.getErrorMessage
|
||||
import com.huanchengfly.tieba.post.models.SignDataBean
|
||||
|
|
@ -31,10 +28,9 @@ abstract class IOKSigner(
|
|||
|
||||
abstract suspend fun start(): Boolean
|
||||
|
||||
suspend fun sign(signDataBean: SignDataBean): ApiResult<SignResultBean> {
|
||||
fun signFlow(signDataBean: SignDataBean): Flow<SignResultBean> {
|
||||
return TiebaApi.getInstance()
|
||||
.signAsync(signDataBean.forumName, signDataBean.tbs)
|
||||
.await()
|
||||
.signFlow(signDataBean.forumId, signDataBean.forumName, signDataBean.tbs)
|
||||
}
|
||||
|
||||
fun getSignDelay(): Long {
|
||||
|
|
@ -153,11 +149,12 @@ class SingleAccountSigner(
|
|||
.map { it.info }
|
||||
} else {
|
||||
flow { emit(emptyList()) }
|
||||
}).onStart {
|
||||
withContext(Dispatchers.Main) {
|
||||
mProgressListener?.onStart(totalCount)
|
||||
})
|
||||
.onStart {
|
||||
withContext(Dispatchers.Main) {
|
||||
mProgressListener?.onStart(totalCount)
|
||||
}
|
||||
}
|
||||
}
|
||||
.catch { emit(emptyList()) }
|
||||
}
|
||||
.flattenConcat()
|
||||
|
|
@ -197,29 +194,19 @@ class SingleAccountSigner(
|
|||
}
|
||||
result = true
|
||||
}
|
||||
.map { data -> sign(data) }
|
||||
.flatMapConcat { signFlow(it) }
|
||||
}
|
||||
/*
|
||||
.flatMapConcat { forumRecommend ->
|
||||
signData.addAll(forumRecommend.likeForum.filter { it.isSign != "1" }
|
||||
.map { SignDataBean(it.forumName, userName, tbs) })
|
||||
totalCount = signData.size
|
||||
signData
|
||||
.asFlow()
|
||||
.onEach {
|
||||
position = signData.indexOf(it)
|
||||
mProgressListener?.onProgressStart(it, position, signData.size)
|
||||
}
|
||||
.onEmpty {
|
||||
mProgressListener?.onFinish(true, 0, 0)
|
||||
}
|
||||
.onStart {
|
||||
mProgressListener?.onStart(totalCount)
|
||||
}
|
||||
.map { data -> sign(data) }
|
||||
.catch { e ->
|
||||
result = false
|
||||
lastFailure = e
|
||||
mProgressListener?.onFailure(
|
||||
position,
|
||||
totalCount,
|
||||
e.getErrorCode(),
|
||||
e.getErrorMessage()
|
||||
)
|
||||
delay(getSignDelay())
|
||||
}
|
||||
*/
|
||||
.catch { e -> emit(ApiResult.Failure(e)) }
|
||||
.onCompletion {
|
||||
withContext(Dispatchers.Main) {
|
||||
mProgressListener?.onFinish(
|
||||
|
|
@ -230,25 +217,14 @@ class SingleAccountSigner(
|
|||
}
|
||||
}
|
||||
.collect {
|
||||
it.doIfSuccess { res ->
|
||||
result = true
|
||||
successCount += 1
|
||||
mProgressListener?.onProgressFinish(
|
||||
signData[position],
|
||||
res,
|
||||
position,
|
||||
totalCount
|
||||
)
|
||||
}.doIfFailure { e ->
|
||||
result = false
|
||||
lastFailure = e
|
||||
mProgressListener?.onFailure(
|
||||
position,
|
||||
totalCount,
|
||||
e.getErrorCode(),
|
||||
e.getErrorMessage()
|
||||
)
|
||||
}
|
||||
result = true
|
||||
successCount += 1
|
||||
mProgressListener?.onProgressFinish(
|
||||
signData[position],
|
||||
it,
|
||||
position,
|
||||
totalCount
|
||||
)
|
||||
delay(getSignDelay())
|
||||
}
|
||||
return result
|
||||
|
|
|
|||
Loading…
Reference in New Issue