pref: 优化一键签到
This commit is contained in:
parent
16ea42f136
commit
1d455ed452
|
|
@ -35,6 +35,7 @@ class OKSignService : IntentService(TAG), CoroutineScope, ProgressListener {
|
|||
private val manager: NotificationManager by lazy { getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager }
|
||||
private var position = 0
|
||||
|
||||
@Deprecated("Deprecated in Java")
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
updateNotification(
|
||||
|
|
@ -92,6 +93,7 @@ class OKSignService : IntentService(TAG), CoroutineScope, ProgressListener {
|
|||
manager.notify(1, notification)
|
||||
}
|
||||
|
||||
@Deprecated("Deprecated in Java")
|
||||
override fun onHandleIntent(intent: Intent?) {
|
||||
if (ACTION_START_SIGN == intent?.action) {
|
||||
val loginInfo = AccountUtil.getLoginInfo(this@OKSignService)
|
||||
|
|
@ -120,6 +122,7 @@ class OKSignService : IntentService(TAG), CoroutineScope, ProgressListener {
|
|||
}
|
||||
}
|
||||
|
||||
@Deprecated("Deprecated in Java")
|
||||
override fun onDestroy() {
|
||||
super.onDestroy()
|
||||
coroutineContext.cancel()
|
||||
|
|
@ -127,7 +130,11 @@ class OKSignService : IntentService(TAG), CoroutineScope, ProgressListener {
|
|||
|
||||
override fun onStart(total: Int) {
|
||||
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) {
|
||||
|
|
@ -183,12 +190,16 @@ class OKSignService : IntentService(TAG), CoroutineScope, ProgressListener {
|
|||
}
|
||||
|
||||
override fun onFailure(current: Int, total: Int, errorCode: Int, errorMsg: String) {
|
||||
if (total == 0) {
|
||||
updateNotification(
|
||||
getString(R.string.title_oksign_fail),
|
||||
errorMsg,
|
||||
Intent(this, LoginActivity::class.java)
|
||||
)
|
||||
stopForeground(true)
|
||||
} else {
|
||||
updateNotification(getString(R.string.title_oksign_fail), errorMsg)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
|
|
|||
|
|
@ -3,13 +3,17 @@ package com.huanchengfly.tieba.post.utils
|
|||
import android.content.Context
|
||||
import com.huanchengfly.tieba.post.api.TiebaApi
|
||||
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.getErrorMessage
|
||||
import com.huanchengfly.tieba.post.models.SignDataBean
|
||||
import com.huanchengfly.tieba.post.models.database.Account
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.FlowPreview
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.*
|
||||
import java.lang.ref.WeakReference
|
||||
import java.util.concurrent.ThreadLocalRandom
|
||||
import kotlin.properties.Delegates
|
||||
|
|
@ -25,8 +29,6 @@ abstract class IOKSigner(
|
|||
|
||||
abstract suspend fun start(): Boolean
|
||||
|
||||
abstract suspend fun startSync(): Boolean
|
||||
|
||||
suspend fun sign(signDataBean: SignDataBean): ApiResult<SignResultBean> {
|
||||
return TiebaApi.getInstance()
|
||||
.signAsync(signDataBean.forumName, signDataBean.tbs)
|
||||
|
|
@ -108,102 +110,70 @@ class SingleAccountSigner(
|
|||
mProgressListener = listener
|
||||
}
|
||||
|
||||
override suspend fun startSync(): Boolean {
|
||||
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
|
||||
}
|
||||
|
||||
@OptIn(FlowPreview::class)
|
||||
override suspend fun start(): Boolean {
|
||||
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()
|
||||
AccountUtil.updateUserInfoFlow(account.bduss)
|
||||
.flatMapConcat { myInfoBean ->
|
||||
userName = myInfoBean.data.name
|
||||
tbs = myInfoBean.data.itbTbs
|
||||
TiebaApi.getInstance().forumRecommendFlow()
|
||||
}
|
||||
.doIfSuccess { forumRecommend ->
|
||||
.flatMapConcat { 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 {
|
||||
signData
|
||||
.asFlow()
|
||||
.onEach {
|
||||
position = signData.indexOf(it)
|
||||
mProgressListener?.onProgressStart(it, position, signData.size)
|
||||
}
|
||||
.onEmpty {
|
||||
mProgressListener?.onFinish(true, 0, 0)
|
||||
}
|
||||
.onStart {
|
||||
mProgressListener?.onStart(totalCount)
|
||||
}
|
||||
.doIfFailure {
|
||||
lastFailure = it
|
||||
mProgressListener?.onFailure(
|
||||
0,
|
||||
0,
|
||||
it.getErrorCode(),
|
||||
it.getErrorMessage()
|
||||
.map { data -> sign(data) }
|
||||
}
|
||||
.catch { e -> emit(ApiResult.Failure(e)) }
|
||||
.onCompletion {
|
||||
mProgressListener?.onFinish(
|
||||
successCount == totalCount,
|
||||
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
|
||||
}
|
||||
|
||||
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 {
|
||||
|
|
|
|||
Loading…
Reference in New Issue