pull/32/head
Administrator 5 years ago
parent 870f08bada
commit 5de24ec41a
  1. 7
      app/src/main/java/io/legado/app/model/WebBook.kt
  2. 154
      app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt
  3. 100
      app/src/main/java/io/legado/app/ui/widget/anima/RotateLoading.kt

@ -18,9 +18,8 @@ class WebBook(private val bookSource: BookSource) {
bookSource.getSearchRule().searchUrl?.let { searchUrl -> bookSource.getSearchRule().searchUrl?.let { searchUrl ->
val analyzeUrl = AnalyzeUrl(searchUrl, key, page, baseUrl = bookSource.bookSourceUrl) val analyzeUrl = AnalyzeUrl(searchUrl, key, page, baseUrl = bookSource.bookSourceUrl)
val response = analyzeUrl.getResponseAsync().await() val response = analyzeUrl.getResponseAsync().await()
return@let BookList.analyzeBookList(response, bookSource, analyzeUrl, isSearch) BookList.analyzeBookList(response, bookSource, analyzeUrl, isSearch)
} } ?: arrayListOf()
return@async arrayListOf<SearchBook>()
} }
} }
@ -29,7 +28,7 @@ class WebBook(private val bookSource: BookSource) {
val analyzeUrl = AnalyzeUrl(book = book, ruleUrl = book.bookUrl) val analyzeUrl = AnalyzeUrl(book = book, ruleUrl = book.bookUrl)
val response = analyzeUrl.getResponseAsync().await() val response = analyzeUrl.getResponseAsync().await()
BookInfo.analyzeBookInfo(book, response.body(), bookSource, analyzeUrl) BookInfo.analyzeBookInfo(book, response.body(), bookSource, analyzeUrl)
return@async book book
} }
} }

@ -8,10 +8,8 @@ import io.legado.app.base.BaseViewModel
import io.legado.app.data.api.CommonHttpApi import io.legado.app.data.api.CommonHttpApi
import io.legado.app.data.entities.SearchBook import io.legado.app.data.entities.SearchBook
import io.legado.app.help.http.HttpHelper import io.legado.app.help.http.HttpHelper
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
class SearchViewModel(application: Application) : BaseViewModel(application) { class SearchViewModel(application: Application) : BaseViewModel(application) {
@ -21,87 +19,93 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
private val channel = Channel<Int>()//协程之间通信 private val channel = Channel<Int>()//协程之间通信
fun search(start: (() -> Unit)? = null, finally: (() -> Unit)? = null) { fun search(start: (() -> Unit)? = null, finally: (() -> Unit)? = null) {
launch { // launch {
repeat(1000) { // repeat(1000) {
channel.send(it) // channel.send(it)
} // }
} // }
//
//
val c = execute { // val c = execute {
//
Log.e("TAG1", "start") // Log.e("TAG1", "start")
//
val response: String = HttpHelper.getApiService<CommonHttpApi>( // val response: String = HttpHelper.getApiService<CommonHttpApi>(
"http://www.baidu.com" // "http://www.baidu.com"
).get("http://www.baidu.com").await() // ).get("http://www.baidu.com").await()
//
Log.e("TAG1", "result: $response") // Log.e("TAG1", "result: $response")
//
delay(2000L) // delay(2000L)
//
response // response
} // }
.onStart { // .onStart {
Log.e("TAG!", "start") // Log.e("TAG!", "start")
start?.let { it() } // 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() }
}
// .onSuccess { // .onSuccess {
// Log.e("TAG!", "success2: $it") // 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() }
// }
//// .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")
// } // }
.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 { execute {
val list = test() test(this)
println("size: ${list.size} $list") }.onSuccess {
println("size: ${it?.size} $it")
} }
} }
suspend fun test(): MutableList<String> { suspend fun test(scope: CoroutineScope): MutableList<String> {
val list = mutableListOf<String>() val list = mutableListOf<String>()
repeat(10) { repeat(10) {
withContext(Dispatchers.IO) { withContext(scope.coroutineContext) {
Log.e("TAG3", Thread.currentThread().name)
val response: String = HttpHelper.getApiService<CommonHttpApi>( val response: String = HttpHelper.getApiService<CommonHttpApi>(
"http://www.baidu.com" "http://www.baidu.com"
).get("http://www.baidu.com").await() ).get("http://www.baidu.com").await()

@ -1,8 +1,7 @@
package io.legado.app.ui.widget.anima package io.legado.app.ui.widget.anima
import android.animation.Animator import android.animation.Animator
import android.animation.AnimatorSet import android.animation.AnimatorListenerAdapter
import android.animation.ObjectAnimator
import android.content.Context import android.content.Context
import android.graphics.Canvas import android.graphics.Canvas
import android.graphics.Color import android.graphics.Color
@ -10,8 +9,6 @@ import android.graphics.Paint
import android.graphics.RectF import android.graphics.RectF
import android.util.AttributeSet import android.util.AttributeSet
import android.view.View import android.view.View
import android.view.animation.LinearInterpolator
import androidx.core.view.isVisible
import io.legado.app.R import io.legado.app.R
import io.legado.app.utils.dp import io.legado.app.utils.dp
@ -37,15 +34,23 @@ class RotateLoading : View {
private var shadowPosition: Int = 0 private var shadowPosition: Int = 0
var isStart = false var isStarted = false
private set private set
var loadingColor: Int = 0 var loadingColor: Int = 0
set(value) {
field = value
invalidate()
}
private var speedOfDegree: Int = 0 private var speedOfDegree: Int = 0
private var speedOfArc: Float = 0.toFloat() private var speedOfArc: Float = 0.toFloat()
private val shown = Runnable { this.startInternal() }
private val hidden = Runnable { this.stopInternal() }
constructor(context: Context) : super(context) { constructor(context: Context) : super(context) {
initView(context, null) initView(context, null)
} }
@ -108,7 +113,7 @@ class RotateLoading : View {
override fun onDraw(canvas: Canvas) { override fun onDraw(canvas: Canvas) {
super.onDraw(canvas) super.onDraw(canvas)
if (!isStart) { if (!isStarted) {
return return
} }
@ -152,74 +157,69 @@ class RotateLoading : View {
override fun onAttachedToWindow() { override fun onAttachedToWindow() {
super.onAttachedToWindow() super.onAttachedToWindow()
if (isVisible) { if (visibility == VISIBLE) {
start() startInternal()
} }
} }
override fun onVisibilityChanged(changedView: View, visibility: Int) { override fun onDetachedFromWindow() {
super.onVisibilityChanged(changedView, visibility) super.onDetachedFromWindow()
if (visibility == VISIBLE) { isStarted = false
start() animate().cancel()
} else { removeCallbacks(shown)
stop() 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() startAnimator()
isStart = true
isStarted = true
invalidate() invalidate()
} }
private fun stop() { private fun stopInternal() {
stopAnimator() stopAnimator()
invalidate() invalidate()
} }
private fun startAnimator() { private fun startAnimator() {
val scaleXAnimator = ObjectAnimator.ofFloat(this, "scaleX", 0.0f, 1f) animate().cancel()
val scaleYAnimator = ObjectAnimator.ofFloat(this, "scaleY", 0.0f, 1f) animate().scaleX(1.0f)
scaleXAnimator.duration = 300 .scaleY(1.0f)
scaleXAnimator.interpolator = LinearInterpolator() .setListener(object : AnimatorListenerAdapter() {
scaleYAnimator.duration = 300
scaleYAnimator.interpolator = LinearInterpolator()
val animatorSet = AnimatorSet()
animatorSet.playTogether(scaleXAnimator, scaleYAnimator)
animatorSet.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 onAnimationStart(animation: Animator) {
visibility = VISIBLE
}
override fun onAnimationEnd(animation: Animator) {
isStart = false
} }
})
override fun onAnimationCancel(animation: Animator) { .start()
} }
override fun onAnimationRepeat(animation: Animator) { private fun stopAnimator() {
animate().cancel()
animate().scaleX(0.0f)
.scaleY(0.0f)
.setListener(object : AnimatorListenerAdapter() {
override fun onAnimationEnd(animation: Animator) {
isStarted = false
visibility = GONE
} }
}) })
animatorSet.start() .start()
} }
companion object { companion object {
private const val DEFAULT_WIDTH = 6 private const val DEFAULT_WIDTH = 6
private const val DEFAULT_SHADOW_POSITION = 2 private const val DEFAULT_SHADOW_POSITION = 2
private const val DEFAULT_SPEED_OF_DEGREE = 10 private const val DEFAULT_SPEED_OF_DEGREE = 10

Loading…
Cancel
Save