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