From 5de24ec41a9b43eda601258bbee73e2790e6c398 Mon Sep 17 00:00:00 2001 From: Administrator <1760316362@qq.com> Date: Mon, 29 Jul 2019 18:27:25 +0800 Subject: [PATCH] update --- .../main/java/io/legado/app/model/WebBook.kt | 7 +- .../legado/app/ui/search/SearchViewModel.kt | 158 +++++++++--------- .../app/ui/widget/anima/RotateLoading.kt | 108 ++++++------ 3 files changed, 138 insertions(+), 135 deletions(-) diff --git a/app/src/main/java/io/legado/app/model/WebBook.kt b/app/src/main/java/io/legado/app/model/WebBook.kt index 4f5f07e89..242510ee9 100644 --- a/app/src/main/java/io/legado/app/model/WebBook.kt +++ b/app/src/main/java/io/legado/app/model/WebBook.kt @@ -18,9 +18,8 @@ class WebBook(private val bookSource: BookSource) { bookSource.getSearchRule().searchUrl?.let { searchUrl -> val analyzeUrl = AnalyzeUrl(searchUrl, key, page, baseUrl = bookSource.bookSourceUrl) val response = analyzeUrl.getResponseAsync().await() - return@let BookList.analyzeBookList(response, bookSource, analyzeUrl, isSearch) - } - return@async arrayListOf() + BookList.analyzeBookList(response, bookSource, analyzeUrl, isSearch) + } ?: arrayListOf() } } @@ -29,7 +28,7 @@ class WebBook(private val bookSource: BookSource) { val analyzeUrl = AnalyzeUrl(book = book, ruleUrl = book.bookUrl) val response = analyzeUrl.getResponseAsync().await() BookInfo.analyzeBookInfo(book, response.body(), bookSource, analyzeUrl) - return@async book + book } } diff --git a/app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt b/app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt index a849dd80b..3252e5112 100644 --- a/app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt @@ -8,10 +8,8 @@ import io.legado.app.base.BaseViewModel import io.legado.app.data.api.CommonHttpApi import io.legado.app.data.entities.SearchBook import io.legado.app.help.http.HttpHelper -import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.channels.Channel -import kotlinx.coroutines.delay -import kotlinx.coroutines.launch import kotlinx.coroutines.withContext class SearchViewModel(application: Application) : BaseViewModel(application) { @@ -21,87 +19,93 @@ class SearchViewModel(application: Application) : BaseViewModel(application) { private val channel = Channel()//协程之间通信 fun search(start: (() -> Unit)? = null, finally: (() -> Unit)? = null) { - launch { - repeat(1000) { - channel.send(it) - } - } - - - val c = execute { - - Log.e("TAG1", "start") - - val response: String = HttpHelper.getApiService( - "http://www.baidu.com" - ).get("http://www.baidu.com").await() - - Log.e("TAG1", "result: $response") - - delay(2000L) - - response - } - .onStart { - Log.e("TAG!", "start") - start?.let { it() } - } - .onSuccess { - Log.e("TAG!", "success: $it") - } - .onError { - Log.e("TAG!", "error: $it") - } - .onFinally { - Log.e("TAG!", "finally") - if (finally != null) { - finally() - } - } - - val c2 = plus(c) -// .timeout { 100L } -// .onErrorReturn { "error return2" } - .onStart { - //会拦截掉c的onStart - Log.e("TAG!", "start2") - start?.let { it() } - } +// launch { +// repeat(1000) { +// channel.send(it) +// } +// } +// +// +// val c = execute { +// +// Log.e("TAG1", "start") +// +// val response: String = HttpHelper.getApiService( +// "http://www.baidu.com" +// ).get("http://www.baidu.com").await() +// +// Log.e("TAG1", "result: $response") +// +// delay(2000L) +// +// response +// } +// .onStart { +// Log.e("TAG!", "start") +// start?.let { it() } +// } // .onSuccess { -// Log.e("TAG!", "success2: $it") +// Log.e("TAG!", "success: $it") // } - .onError { - Log.e("TAG!", "error2: $it") - } - .onFinally { - Log.e("TAG!", "finally2") - if (finally != null) { - finally() - } - - Log.e("TAG!", "rec2: " + channel.receive()) - } - - launch { - delay(1500L) -// c2.cancel() - -// c.cancel() - } - - - launch { - val list = test() - println("size: ${list.size} $list") +// .onError { +// Log.e("TAG!", "error: $it") +// } +// .onFinally { +// Log.e("TAG!", "finally") +// if (finally != null) { +// finally() +// } +// } +// +// val c2 = plus(c) +//// .timeout { 100L } +//// .onErrorReturn { "error return2" } +// .onStart { +// //会拦截掉c的onStart +// Log.e("TAG!", "start2") +// start?.let { it() } +// } +//// .onSuccess { +//// Log.e("TAG!", "success2: $it") +//// } +// .onError { +// Log.e("TAG!", "error2: $it") +// } +// .onFinally { +// Log.e("TAG!", "finally2") +// if (finally != null) { +// finally() +// } +// +// Log.e("TAG!", "rec2: " + channel.receive()) +// } +// +// launch { +// delay(1500L) +//// c2.cancel() +// +//// c.cancel() +// } +// +// +// launch { +// val list = test() +// println("size: ${list.size} $list") +// } + + + execute { + test(this) + }.onSuccess { + println("size: ${it?.size} $it") } - - } - suspend fun test(): MutableList { + suspend fun test(scope: CoroutineScope): MutableList { val list = mutableListOf() repeat(10) { - withContext(Dispatchers.IO) { + withContext(scope.coroutineContext) { + Log.e("TAG3", Thread.currentThread().name) val response: String = HttpHelper.getApiService( "http://www.baidu.com" ).get("http://www.baidu.com").await() diff --git a/app/src/main/java/io/legado/app/ui/widget/anima/RotateLoading.kt b/app/src/main/java/io/legado/app/ui/widget/anima/RotateLoading.kt index d2bd8c0a8..9aac789c2 100644 --- a/app/src/main/java/io/legado/app/ui/widget/anima/RotateLoading.kt +++ b/app/src/main/java/io/legado/app/ui/widget/anima/RotateLoading.kt @@ -1,8 +1,7 @@ package io.legado.app.ui.widget.anima import android.animation.Animator -import android.animation.AnimatorSet -import android.animation.ObjectAnimator +import android.animation.AnimatorListenerAdapter import android.content.Context import android.graphics.Canvas import android.graphics.Color @@ -10,8 +9,6 @@ import android.graphics.Paint import android.graphics.RectF import android.util.AttributeSet import android.view.View -import android.view.animation.LinearInterpolator -import androidx.core.view.isVisible import io.legado.app.R import io.legado.app.utils.dp @@ -37,15 +34,23 @@ class RotateLoading : View { private var shadowPosition: Int = 0 - var isStart = false + var isStarted = false private set var loadingColor: Int = 0 + set(value) { + field = value + invalidate() + } private var speedOfDegree: Int = 0 private var speedOfArc: Float = 0.toFloat() + private val shown = Runnable { this.startInternal() } + + private val hidden = Runnable { this.stopInternal() } + constructor(context: Context) : super(context) { initView(context, null) } @@ -108,7 +113,7 @@ class RotateLoading : View { override fun onDraw(canvas: Canvas) { super.onDraw(canvas) - if (!isStart) { + if (!isStarted) { return } @@ -152,74 +157,69 @@ class RotateLoading : View { override fun onAttachedToWindow() { super.onAttachedToWindow() - if (isVisible) { - start() + if (visibility == VISIBLE) { + startInternal() } } - override fun onVisibilityChanged(changedView: View, visibility: Int) { - super.onVisibilityChanged(changedView, visibility) - if (visibility == VISIBLE) { - start() - } else { - stop() - } + override fun onDetachedFromWindow() { + super.onDetachedFromWindow() + isStarted = false + animate().cancel() + removeCallbacks(shown) + removeCallbacks(hidden) + } + + fun show() { + removeCallbacks(shown) + removeCallbacks(hidden) + post(shown) + } + + fun hide() { + removeCallbacks(shown) + removeCallbacks(hidden) + post(hidden) } - private fun start() { + private fun startInternal() { startAnimator() - isStart = true + + isStarted = true invalidate() } - private fun stop() { + private fun stopInternal() { stopAnimator() invalidate() } private fun startAnimator() { - val scaleXAnimator = ObjectAnimator.ofFloat(this, "scaleX", 0.0f, 1f) - val scaleYAnimator = ObjectAnimator.ofFloat(this, "scaleY", 0.0f, 1f) - scaleXAnimator.duration = 300 - scaleXAnimator.interpolator = LinearInterpolator() - scaleYAnimator.duration = 300 - scaleYAnimator.interpolator = LinearInterpolator() - val animatorSet = AnimatorSet() - animatorSet.playTogether(scaleXAnimator, scaleYAnimator) - animatorSet.start() + animate().cancel() + animate().scaleX(1.0f) + .scaleY(1.0f) + .setListener(object : AnimatorListenerAdapter() { + override fun onAnimationStart(animation: Animator) { + visibility = VISIBLE + } + }) + .start() } private fun stopAnimator() { - val scaleXAnimator = ObjectAnimator.ofFloat(this, "scaleX", 1f, 0f) - val scaleYAnimator = ObjectAnimator.ofFloat(this, "scaleY", 1f, 0f) - scaleXAnimator.duration = 300 - scaleXAnimator.interpolator = LinearInterpolator() - scaleYAnimator.duration = 300 - scaleYAnimator.interpolator = LinearInterpolator() - val animatorSet = AnimatorSet() - animatorSet.playTogether(scaleXAnimator, scaleYAnimator) - animatorSet.addListener(object : Animator.AnimatorListener { - override fun onAnimationStart(animation: Animator) { - - } - - override fun onAnimationEnd(animation: Animator) { - isStart = false - } - - override fun onAnimationCancel(animation: Animator) { - - } - - override fun onAnimationRepeat(animation: Animator) { - - } - }) - animatorSet.start() + animate().cancel() + animate().scaleX(0.0f) + .scaleY(0.0f) + .setListener(object : AnimatorListenerAdapter() { + override fun onAnimationEnd(animation: Animator) { + isStarted = false + visibility = GONE + } + }) + .start() } companion object { - private const val DEFAULT_WIDTH = 6 private const val DEFAULT_SHADOW_POSITION = 2 private const val DEFAULT_SPEED_OF_DEGREE = 10