feat: 优化代码

pull/105/head
kunfei 5 years ago
parent d04ded3910
commit 2f04af482d
  1. 102
      app/src/main/java/io/legado/app/help/http/CoroutinesCallAdapterFactory.kt
  2. 4
      app/src/main/java/io/legado/app/help/http/HttpHelper.kt
  3. 9
      app/src/main/java/io/legado/app/help/http/api/HttpGetApi.kt
  4. 13
      app/src/main/java/io/legado/app/help/http/api/HttpPostApi.kt
  5. 4
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt
  6. 3
      app/src/main/java/io/legado/app/service/HttpReadAloudService.kt

@ -1,102 +0,0 @@
package io.legado.app.help.http
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.Deferred
import retrofit2.*
import java.lang.reflect.ParameterizedType
import java.lang.reflect.Type
class CoroutinesCallAdapterFactory private constructor() : CallAdapter.Factory() {
companion object {
fun create(): CoroutinesCallAdapterFactory {
return CoroutinesCallAdapterFactory()
}
}
override fun get(
returnType: Type,
annotations: Array<out Annotation>,
retrofit: Retrofit
): CallAdapter<*, *>? {
if (Deferred::class.java != getRawType(returnType)) {
return null
}
check(returnType is ParameterizedType) { "Deferred return type must be parameterized as Deferred<Foo> or Deferred<out Foo>" }
val responseType = getParameterUpperBound(0, returnType)
val rawDeferredType = getRawType(responseType)
return if (rawDeferredType == Response::class.java) {
check(responseType is ParameterizedType) { "Response must be parameterized as Response<Foo> or Response<out Foo>" }
ResponseCallAdapter<Any>(
getParameterUpperBound(
0,
responseType
)
)
} else {
BodyCallAdapter<Any>(responseType)
}
}
private class BodyCallAdapter<T>(
private val responseType: Type
) : CallAdapter<T, Deferred<T>> {
override fun responseType() = responseType
override fun adapt(call: Call<T>): Deferred<T> {
val deferred = CompletableDeferred<T>()
deferred.invokeOnCompletion {
if (deferred.isCancelled) {
call.cancel()
}
}
call.enqueue(object : Callback<T> {
override fun onFailure(call: Call<T>, t: Throwable) {
deferred.completeExceptionally(t)
}
override fun onResponse(call: Call<T>, response: Response<T>) {
if (response.isSuccessful) {
deferred.complete(response.body()!!)
} else {
deferred.completeExceptionally(HttpException(response))
}
}
})
return deferred
}
}
private class ResponseCallAdapter<T>(
private val responseType: Type
) : CallAdapter<T, Deferred<Response<T>>> {
override fun responseType() = responseType
override fun adapt(call: Call<T>): Deferred<Response<T>> {
val deferred = CompletableDeferred<Response<T>>()
deferred.invokeOnCompletion {
if (deferred.isCancelled) {
call.cancel()
}
}
call.enqueue(object : Callback<T> {
override fun onFailure(call: Call<T>, t: Throwable) {
deferred.completeExceptionally(t)
}
override fun onResponse(call: Call<T>, response: Response<T>) {
deferred.complete(response)
}
})
return deferred
}
}
}

@ -55,8 +55,6 @@ object HttpHelper {
return Retrofit.Builder().baseUrl(baseUrl)
//增加返回值为字符串的支持(以实体类返回)
.addConverterFactory(EncodeConverter(encode))
//增加返回值为Observable<T>的支持
.addCallAdapterFactory(CoroutinesCallAdapterFactory.create())
.client(client)
.build()
}
@ -64,8 +62,6 @@ object HttpHelper {
fun getByteRetrofit(baseUrl: String): Retrofit {
return Retrofit.Builder().baseUrl(baseUrl)
.addConverterFactory(ByteConverter())
//增加返回值为Observable<T>的支持
.addCallAdapterFactory(CoroutinesCallAdapterFactory.create())
.client(client)
.build()
}

@ -1,6 +1,5 @@
package io.legado.app.help.http.api
import kotlinx.coroutines.Deferred
import retrofit2.Call
import retrofit2.Response
import retrofit2.http.GET
@ -15,17 +14,17 @@ import retrofit2.http.Url
interface HttpGetApi {
@GET
fun getAsync(
suspend fun getAsync(
@Url url: String,
@HeaderMap headers: Map<String, String>
): Deferred<Response<String>>
): Response<String>
@GET
fun getMapAsync(
suspend fun getMapAsync(
@Url url: String,
@QueryMap(encoded = true) queryMap: Map<String, String>,
@HeaderMap headers: Map<String, String>
): Deferred<Response<String>>
): Response<String>
@GET
fun get(

@ -1,6 +1,5 @@
package io.legado.app.help.http.api
import kotlinx.coroutines.Deferred
import okhttp3.RequestBody
import retrofit2.Call
import retrofit2.Response
@ -15,18 +14,18 @@ interface HttpPostApi {
@FormUrlEncoded
@POST
fun postMapAsync(
suspend fun postMapAsync(
@Url url: String,
@FieldMap(encoded = true) fieldMap: Map<String, String>,
@HeaderMap headers: Map<String, String>
): Deferred<Response<String>>
): Response<String>
@POST
fun postBodyAsync(
suspend fun postBodyAsync(
@Url url: String,
@Body body: RequestBody,
@HeaderMap headers: Map<String, String>
): Deferred<Response<String>>
): Response<String>
@FormUrlEncoded
@POST
@ -45,9 +44,9 @@ interface HttpPostApi {
@FormUrlEncoded
@POST
fun postMapByteAsync(
suspend fun postMapByteAsync(
@Url url: String,
@FieldMap(encoded = true) fieldMap: Map<String, String>,
@HeaderMap headers: Map<String, String>
): Deferred<Response<ByteArray>>
): Response<ByteArray>
}

@ -286,22 +286,18 @@ class AnalyzeUrl(
HttpHelper
.getApiService<HttpPostApi>(baseUrl, charset)
.postMapAsync(url, fieldMap, headerMap)
.await()
} else {
HttpHelper
.getApiService<HttpPostApi>(baseUrl, charset)
.postBodyAsync(url, body!!, headerMap)
.await()
}
}
fieldMap.isEmpty() -> HttpHelper
.getApiService<HttpGetApi>(baseUrl, charset)
.getAsync(url, headerMap)
.await()
else -> HttpHelper
.getApiService<HttpGetApi>(baseUrl, charset)
.getMapAsync(url, fieldMap, headerMap)
.await()
}
return Res(NetworkUtils.getUrl(res), res.body())
}

@ -73,8 +73,7 @@ class HttpReadAloudService : BaseReadAloudService(),
.postMapByteAsync(
"http://tts.baidu.com/text2audio",
getAudioBody(contentList[index]), mapOf()
).await()
.body()
).body()
if (bytes != null && isActive) {
val file = getSpeakFile(index)
file.writeBytes(bytes)

Loading…
Cancel
Save