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

Loading…
Cancel
Save