feat: 优化代码

pull/141/head
kunfei 5 years ago
parent fb881c5951
commit bfc581092f
  1. 10
      app/src/main/java/io/legado/app/help/http/HttpHelper.kt
  2. 16
      app/src/main/java/io/legado/app/ui/rss/read/ReadRssViewModel.kt

@ -57,6 +57,16 @@ object HttpHelper {
return null return null
} }
suspend fun simpleGetByteAsync(url: String): ByteArray? {
NetworkUtils.getBaseUrl(url)?.let { baseUrl ->
return getByteRetrofit(baseUrl)
.create(HttpGetApi::class.java)
.getMapByteAsync(url, mapOf(), mapOf())
.body()
}
return null
}
inline fun <reified T> getApiService(baseUrl: String, encode: String? = null): T { inline fun <reified T> getApiService(baseUrl: String, encode: String? = null): T {
return getRetrofit(baseUrl, encode).create(T::class.java) return getRetrofit(baseUrl, encode).create(T::class.java)
} }

@ -6,6 +6,7 @@ import android.net.Uri
import android.speech.tts.TextToSpeech import android.speech.tts.TextToSpeech
import android.speech.tts.UtteranceProgressListener import android.speech.tts.UtteranceProgressListener
import android.util.Base64 import android.util.Base64
import android.webkit.URLUtil
import androidx.documentfile.provider.DocumentFile import androidx.documentfile.provider.DocumentFile
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import io.legado.app.App import io.legado.app.App
@ -14,6 +15,7 @@ import io.legado.app.base.BaseViewModel
import io.legado.app.constant.AppConst import io.legado.app.constant.AppConst
import io.legado.app.data.entities.RssArticle import io.legado.app.data.entities.RssArticle
import io.legado.app.data.entities.RssSource import io.legado.app.data.entities.RssSource
import io.legado.app.help.http.HttpHelper
import io.legado.app.model.Rss import io.legado.app.model.Rss
import io.legado.app.model.analyzeRule.AnalyzeUrl import io.legado.app.model.analyzeRule.AnalyzeUrl
import io.legado.app.utils.DocumentUtils import io.legado.app.utils.DocumentUtils
@ -99,7 +101,7 @@ class ReadRssViewModel(application: Application) : BaseViewModel(application),
webPic ?: return webPic ?: return
execute { execute {
val fileName = "${AppConst.fileNameFormat.format(Date(System.currentTimeMillis()))}.jpg" val fileName = "${AppConst.fileNameFormat.format(Date(System.currentTimeMillis()))}.jpg"
webData2bitmap(webPic).let { biteArray -> webData2bitmap(webPic)?.let { biteArray ->
if (path.isContentPath()) { if (path.isContentPath()) {
val uri = Uri.parse(path) val uri = Uri.parse(path)
DocumentFile.fromTreeUri(context, uri)?.let { doc -> DocumentFile.fromTreeUri(context, uri)?.let { doc ->
@ -110,14 +112,20 @@ class ReadRssViewModel(application: Application) : BaseViewModel(application),
val file = FileUtils.createFileIfNotExist(File(path), fileName) val file = FileUtils.createFileIfNotExist(File(path), fileName)
file.writeBytes(biteArray) file.writeBytes(biteArray)
} }
} } ?: throw Throwable("NULL")
}.onError { }.onError {
toast("保存图片失败:${it.localizedMessage}") toast("保存图片失败:${it.localizedMessage}")
}.onSuccess {
toast("保存成功")
} }
} }
private fun webData2bitmap(data: String): ByteArray { private suspend fun webData2bitmap(data: String): ByteArray? {
return Base64.decode(data.split(",").toTypedArray()[1], Base64.DEFAULT) return if (URLUtil.isValidUrl(data)) {
HttpHelper.simpleGetByteAsync(data)
} else {
Base64.decode(data.split(",").toTypedArray()[1], Base64.DEFAULT)
}
} }
fun clHtml(content: String): String { fun clHtml(content: String): String {

Loading…
Cancel
Save