pref: 优化一键签到

This commit is contained in:
HuanCheng65 2022-08-01 15:47:58 +08:00
parent 16ea42f136
commit 1d455ed452
No known key found for this signature in database
GPG Key ID: E9031EF91A805148
2 changed files with 72 additions and 91 deletions

View File

@ -35,6 +35,7 @@ class OKSignService : IntentService(TAG), CoroutineScope, ProgressListener {
private val manager: NotificationManager by lazy { getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager } private val manager: NotificationManager by lazy { getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager }
private var position = 0 private var position = 0
@Deprecated("Deprecated in Java")
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
updateNotification( updateNotification(
@ -92,6 +93,7 @@ class OKSignService : IntentService(TAG), CoroutineScope, ProgressListener {
manager.notify(1, notification) manager.notify(1, notification)
} }
@Deprecated("Deprecated in Java")
override fun onHandleIntent(intent: Intent?) { override fun onHandleIntent(intent: Intent?) {
if (ACTION_START_SIGN == intent?.action) { if (ACTION_START_SIGN == intent?.action) {
val loginInfo = AccountUtil.getLoginInfo(this@OKSignService) val loginInfo = AccountUtil.getLoginInfo(this@OKSignService)
@ -120,6 +122,7 @@ class OKSignService : IntentService(TAG), CoroutineScope, ProgressListener {
} }
} }
@Deprecated("Deprecated in Java")
override fun onDestroy() { override fun onDestroy() {
super.onDestroy() super.onDestroy()
coroutineContext.cancel() coroutineContext.cancel()
@ -127,7 +130,11 @@ class OKSignService : IntentService(TAG), CoroutineScope, ProgressListener {
override fun onStart(total: Int) { override fun onStart(total: Int) {
updateNotification(getString(R.string.title_start_sign), null) updateNotification(getString(R.string.title_start_sign), null)
Toast.makeText(this@OKSignService, R.string.toast_oksign_start, Toast.LENGTH_SHORT).show() if (total > 0) Toast.makeText(
this@OKSignService,
R.string.toast_oksign_start,
Toast.LENGTH_SHORT
).show()
} }
override fun onProgressStart(signDataBean: SignDataBean, current: Int, total: Int) { override fun onProgressStart(signDataBean: SignDataBean, current: Int, total: Int) {
@ -183,12 +190,16 @@ class OKSignService : IntentService(TAG), CoroutineScope, ProgressListener {
} }
override fun onFailure(current: Int, total: Int, errorCode: Int, errorMsg: String) { override fun onFailure(current: Int, total: Int, errorCode: Int, errorMsg: String) {
if (total == 0) {
updateNotification( updateNotification(
getString(R.string.title_oksign_fail), getString(R.string.title_oksign_fail),
errorMsg, errorMsg,
Intent(this, LoginActivity::class.java) Intent(this, LoginActivity::class.java)
) )
stopForeground(true) stopForeground(true)
} else {
updateNotification(getString(R.string.title_oksign_fail), errorMsg)
}
} }
companion object { companion object {

View File

@ -3,13 +3,17 @@ package com.huanchengfly.tieba.post.utils
import android.content.Context import android.content.Context
import com.huanchengfly.tieba.post.api.TiebaApi import com.huanchengfly.tieba.post.api.TiebaApi
import com.huanchengfly.tieba.post.api.models.SignResultBean import com.huanchengfly.tieba.post.api.models.SignResultBean
import com.huanchengfly.tieba.post.api.retrofit.* 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
import com.huanchengfly.tieba.post.models.database.Account import com.huanchengfly.tieba.post.models.database.Account
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.*
import java.lang.ref.WeakReference import java.lang.ref.WeakReference
import java.util.concurrent.ThreadLocalRandom import java.util.concurrent.ThreadLocalRandom
import kotlin.properties.Delegates import kotlin.properties.Delegates
@ -25,8 +29,6 @@ abstract class IOKSigner(
abstract suspend fun start(): Boolean abstract suspend fun start(): Boolean
abstract suspend fun startSync(): Boolean
suspend fun sign(signDataBean: SignDataBean): ApiResult<SignResultBean> { suspend fun sign(signDataBean: SignDataBean): ApiResult<SignResultBean> {
return TiebaApi.getInstance() return TiebaApi.getInstance()
.signAsync(signDataBean.forumName, signDataBean.tbs) .signAsync(signDataBean.forumName, signDataBean.tbs)
@ -108,102 +110,70 @@ class SingleAccountSigner(
mProgressListener = listener mProgressListener = listener
} }
override suspend fun startSync(): Boolean { @OptIn(FlowPreview::class)
var result = false
signData.clear()
var userName: String by Delegates.notNull()
var tbs: String by Delegates.notNull()
AccountUtil.updateUserInfoAsync(coroutineScope, account.bduss)
.await()
.fetchIfSuccess {
userName = it.data.name
tbs = it.data.itbTbs
TiebaApi.getInstance().forumRecommendAsync().getData()
}
.doIfSuccess { forumRecommend ->
signData.addAll(forumRecommend.likeForum.filter { it.isSign != "1" }
.map { SignDataBean(it.forumName, userName, tbs) })
totalCount = signData.size
mProgressListener?.onStart(totalCount)
if (signData.isNotEmpty()) {
result = sign(0)
} else {
mProgressListener?.onFinish(true, 0, 0)
}
}
.doIfFailure {
lastFailure = it
mProgressListener?.onFailure(
0,
0,
it.getErrorCode(),
it.getErrorMessage()
)
throw it
}
return result
}
override suspend fun start(): Boolean { override suspend fun start(): Boolean {
var result = false var result = false
signData.clear() signData.clear()
var userName: String by Delegates.notNull() var userName: String by Delegates.notNull()
var tbs: String by Delegates.notNull() var tbs: String by Delegates.notNull()
AccountUtil.updateUserInfoAsync(coroutineScope, account.bduss) AccountUtil.updateUserInfoFlow(account.bduss)
.await() .flatMapConcat { myInfoBean ->
.fetchIfSuccess { userName = myInfoBean.data.name
userName = it.data.name tbs = myInfoBean.data.itbTbs
tbs = it.data.itbTbs TiebaApi.getInstance().forumRecommendFlow()
TiebaApi.getInstance().forumRecommendAsync().getData()
} }
.doIfSuccess { forumRecommend -> .flatMapConcat { forumRecommend ->
signData.addAll(forumRecommend.likeForum.filter { it.isSign != "1" } signData.addAll(forumRecommend.likeForum.filter { it.isSign != "1" }
.map { SignDataBean(it.forumName, userName, tbs) }) .map { SignDataBean(it.forumName, userName, tbs) })
totalCount = signData.size totalCount = signData.size
mProgressListener?.onStart(totalCount) signData
if (signData.isNotEmpty()) { .asFlow()
result = sign(0) .onEach {
} else { position = signData.indexOf(it)
mProgressListener?.onProgressStart(it, position, signData.size)
}
.onEmpty {
mProgressListener?.onFinish(true, 0, 0) mProgressListener?.onFinish(true, 0, 0)
} }
.onStart {
mProgressListener?.onStart(totalCount)
} }
.doIfFailure { .map { data -> sign(data) }
lastFailure = it }
mProgressListener?.onFailure( .catch { e -> emit(ApiResult.Failure(e)) }
0, .onCompletion {
0, mProgressListener?.onFinish(
it.getErrorCode(), successCount == totalCount,
it.getErrorMessage() successCount,
totalCount
) )
} }
.collect {
it.doIfSuccess { res ->
result = true
successCount += 1
mProgressListener?.onProgressFinish(
signData[position],
res,
position,
totalCount
)
}
it.doIfFailure { e ->
result = false
lastFailure = e
mProgressListener?.onFailure(
position,
totalCount,
e.getErrorCode(),
e.getErrorMessage()
)
}
delay(getSignDelay())
}
return result return result
} }
private suspend fun sign(position: Int): Boolean {
this.position = position
val data = signData[position]
mProgressListener?.onProgressStart(data, position, signData.size)
val result = sign(data)
.doIfSuccess {
successCount += 1
mProgressListener?.onProgressFinish(data, it, position, totalCount)
}
.doIfFailure {
mProgressListener?.onFailure(
position,
totalCount,
it.getErrorCode(),
it.getErrorMessage()
)
}
return if (position < signData.size - 1) {
delay(getSignDelay())
sign(position + 1)
} else {
mProgressListener?.onFinish(successCount == totalCount, successCount, totalCount)
result.isSuccessful
}
}
} }
interface ProgressListener { interface ProgressListener {