Merge branch 'gedoor:master' into master

pull/1155/head
bushixuanqi 4 years ago committed by GitHub
commit 1d3a8ba04d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 15
      app/src/main/java/io/legado/app/base/BaseViewModel.kt
  2. 6
      app/src/main/java/io/legado/app/ui/association/ImportBookSourceActivity.kt
  3. 17
      app/src/main/java/io/legado/app/ui/association/ImportReplaceRuleActivity.kt
  4. 67
      app/src/main/java/io/legado/app/ui/association/ImportReplaceRuleDialog.kt
  5. 49
      app/src/main/java/io/legado/app/ui/association/ImportReplaceRuleViewModel.kt
  6. 6
      app/src/main/java/io/legado/app/ui/association/ImportRssSourceActivity.kt
  7. 42
      app/src/main/java/io/legado/app/ui/association/OnLineImportActivity.kt
  8. 6
      app/src/main/java/io/legado/app/ui/association/OnLineImportViewModel.kt
  9. 5
      app/src/main/java/io/legado/app/ui/book/audio/AudioPlayViewModel.kt
  10. 17
      app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt
  11. 3
      app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt
  12. 8
      app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineViewModel.kt
  13. 9
      app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditViewModel.kt
  14. 2
      app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt
  15. 5
      app/src/main/java/io/legado/app/ui/dict/DictViewModel.kt
  16. 10
      app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfViewModel.kt
  17. 3
      app/src/main/java/io/legado/app/ui/rss/article/RssArticlesViewModel.kt
  18. 12
      app/src/main/java/io/legado/app/ui/rss/read/ReadRssViewModel.kt
  19. 13
      app/src/main/java/io/legado/app/ui/rss/source/edit/RssSourceEditViewModel.kt
  20. 2
      app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceViewModel.kt

@ -2,12 +2,10 @@ package io.legado.app.base
import android.app.Application import android.app.Application
import android.content.Context import android.content.Context
import androidx.annotation.CallSuper
import androidx.lifecycle.AndroidViewModel import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import io.legado.app.App import io.legado.app.App
import io.legado.app.help.coroutine.Coroutine import io.legado.app.help.coroutine.Coroutine
import io.legado.app.utils.toastOnUi
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Deferred import kotlinx.coroutines.Deferred
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
@ -34,17 +32,4 @@ open class BaseViewModel(application: Application) : AndroidViewModel(applicatio
return Coroutine.async(scope, context) { block().await() } return Coroutine.async(scope, context) { block().await() }
} }
@CallSuper
override fun onCleared() {
super.onCleared()
}
open fun toastOnUi(message: Int) {
context.toastOnUi(message)
}
open fun toastOnUi(message: CharSequence?) {
context.toastOnUi(message ?: toString())
}
} }

@ -30,7 +30,7 @@ class ImportBookSourceActivity :
viewModel.successLiveData.observe(this, { viewModel.successLiveData.observe(this, {
binding.rotateLoading.hide() binding.rotateLoading.hide()
if (it > 0) { if (it > 0) {
successDialog() ImportBookSourceDialog().show(supportFragmentManager, "SourceDialog")
} else { } else {
errorDialog(getString(R.string.wrong_format)) errorDialog(getString(R.string.wrong_format))
} }
@ -72,8 +72,4 @@ class ImportBookSourceActivity :
}.show() }.show()
} }
private fun successDialog() {
ImportBookSourceDialog().show(supportFragmentManager, "SourceDialog")
}
} }

@ -4,8 +4,6 @@ import android.os.Bundle
import androidx.activity.viewModels import androidx.activity.viewModels
import io.legado.app.base.VMBaseActivity import io.legado.app.base.VMBaseActivity
import io.legado.app.constant.Theme import io.legado.app.constant.Theme
import io.legado.app.data.appDb
import io.legado.app.data.entities.ReplaceRule
import io.legado.app.databinding.ActivityTranslucenceBinding import io.legado.app.databinding.ActivityTranslucenceBinding
import io.legado.app.help.IntentDataHelp import io.legado.app.help.IntentDataHelp
import io.legado.app.lib.dialogs.alert import io.legado.app.lib.dialogs.alert
@ -28,8 +26,8 @@ class ImportReplaceRuleActivity :
}) })
viewModel.successLiveData.observe(this, { viewModel.successLiveData.observe(this, {
binding.rotateLoading.hide() binding.rotateLoading.hide()
if (it.size > 0) { if (it > 0) {
successDialog(it) ImportReplaceRuleDialog().show(supportFragmentManager, "importReplaceRule")
} else { } else {
errorDialog("格式不对") errorDialog("格式不对")
} }
@ -71,15 +69,4 @@ class ImportReplaceRuleActivity :
}.show() }.show()
} }
private fun successDialog(allSource: ArrayList<ReplaceRule>) {
alert("解析结果", "${allSource.size}个替换规则,是否确认导入?") {
okButton {
appDb.replaceRuleDao.insert(*allSource.toTypedArray())
}
noButton()
onDismiss {
finish()
}
}.show()
}
} }

@ -1,11 +1,12 @@
package io.legado.app.ui.association package io.legado.app.ui.association
import android.content.Context import android.content.Context
import android.content.DialogInterface
import android.os.Bundle import android.os.Bundle
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.fragment.app.viewModels import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.BaseDialogFragment import io.legado.app.base.BaseDialogFragment
@ -14,12 +15,14 @@ import io.legado.app.base.adapter.RecyclerAdapter
import io.legado.app.data.entities.ReplaceRule import io.legado.app.data.entities.ReplaceRule
import io.legado.app.databinding.DialogRecyclerViewBinding import io.legado.app.databinding.DialogRecyclerViewBinding
import io.legado.app.databinding.ItemSourceImportBinding import io.legado.app.databinding.ItemSourceImportBinding
import io.legado.app.ui.widget.dialog.WaitDialog
import io.legado.app.utils.viewbindingdelegate.viewBinding import io.legado.app.utils.viewbindingdelegate.viewBinding
import io.legado.app.utils.visible
class ImportReplaceRuleDialog : BaseDialogFragment() { class ImportReplaceRuleDialog : BaseDialogFragment() {
private val binding by viewBinding(DialogRecyclerViewBinding::bind) private val binding by viewBinding(DialogRecyclerViewBinding::bind)
private val viewModel by viewModels<ImportReplaceRuleViewModel>() private val viewModel by activityViewModels<ImportReplaceRuleViewModel>()
lateinit var adapter: SourcesAdapter lateinit var adapter: SourcesAdapter
override fun onStart() { override fun onStart() {
@ -44,6 +47,52 @@ class ImportReplaceRuleDialog : BaseDialogFragment() {
binding.recyclerView.layoutManager = LinearLayoutManager(requireContext()) binding.recyclerView.layoutManager = LinearLayoutManager(requireContext())
binding.recyclerView.adapter = adapter binding.recyclerView.adapter = adapter
adapter.setItems(viewModel.allRules) adapter.setItems(viewModel.allRules)
binding.tvCancel.visible()
binding.tvCancel.setOnClickListener {
dismissAllowingStateLoss()
}
binding.tvOk.visible()
binding.tvOk.setOnClickListener {
val waitDialog = WaitDialog(requireContext())
waitDialog.show()
viewModel.importSelect {
waitDialog.dismiss()
dismissAllowingStateLoss()
}
}
upSelectText()
binding.tvFooterLeft.visible()
binding.tvFooterLeft.setOnClickListener {
val selectAll = viewModel.isSelectAll()
viewModel.selectStatus.forEachIndexed { index, b ->
if (b != !selectAll) {
viewModel.selectStatus[index] = !selectAll
}
}
adapter.notifyDataSetChanged()
upSelectText()
}
}
private fun upSelectText() {
if (viewModel.isSelectAll()) {
binding.tvFooterLeft.text = getString(
R.string.select_cancel_count,
viewModel.selectCount(),
viewModel.allRules.size
)
} else {
binding.tvFooterLeft.text = getString(
R.string.select_all_count,
viewModel.selectCount(),
viewModel.allRules.size
)
}
}
override fun onDismiss(dialog: DialogInterface) {
super.onDismiss(dialog)
activity?.finish()
} }
inner class SourcesAdapter(context: Context) : inner class SourcesAdapter(context: Context) :
@ -59,11 +108,21 @@ class ImportReplaceRuleDialog : BaseDialogFragment() {
item: ReplaceRule, item: ReplaceRule,
payloads: MutableList<Any> payloads: MutableList<Any>
) { ) {
TODO("Not yet implemented") binding.run {
cbSourceName.isChecked = viewModel.selectStatus[holder.layoutPosition]
cbSourceName.text = item.name
}
} }
override fun registerListener(holder: ItemViewHolder, binding: ItemSourceImportBinding) { override fun registerListener(holder: ItemViewHolder, binding: ItemSourceImportBinding) {
TODO("Not yet implemented") binding.run {
cbSourceName.setOnCheckedChangeListener { buttonView, isChecked ->
if (buttonView.isPressed) {
viewModel.selectStatus[holder.layoutPosition] = isChecked
upSelectText()
}
}
}
} }
} }

@ -3,7 +3,9 @@ package io.legado.app.ui.association
import android.app.Application import android.app.Application
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import io.legado.app.base.BaseViewModel import io.legado.app.base.BaseViewModel
import io.legado.app.data.appDb
import io.legado.app.data.entities.ReplaceRule import io.legado.app.data.entities.ReplaceRule
import io.legado.app.help.AppConfig
import io.legado.app.help.http.newCall import io.legado.app.help.http.newCall
import io.legado.app.help.http.okHttpClient import io.legado.app.help.http.okHttpClient
import io.legado.app.help.http.text import io.legado.app.help.http.text
@ -12,11 +14,30 @@ import io.legado.app.utils.isAbsUrl
class ImportReplaceRuleViewModel(app: Application) : BaseViewModel(app) { class ImportReplaceRuleViewModel(app: Application) : BaseViewModel(app) {
val errorLiveData = MutableLiveData<String>() val errorLiveData = MutableLiveData<String>()
val successLiveData = MutableLiveData<ArrayList<ReplaceRule>>() val successLiveData = MutableLiveData<Int>()
val allRules = arrayListOf<ReplaceRule>() val allRules = arrayListOf<ReplaceRule>()
val checkRules = arrayListOf<ReplaceRule>() val checkRules = arrayListOf<ReplaceRule?>()
val selectStatus = arrayListOf<ReplaceRule>() val selectStatus = arrayListOf<Boolean>()
fun isSelectAll(): Boolean {
selectStatus.forEach {
if (!it) {
return false
}
}
return true
}
fun selectCount(): Int {
var count = 0
selectStatus.forEach {
if (it) {
count++
}
}
return count
}
fun import(text: String) { fun import(text: String) {
execute { execute {
@ -35,15 +56,33 @@ class ImportReplaceRuleViewModel(app: Application) : BaseViewModel(app) {
errorLiveData.postValue(it.localizedMessage ?: "ERROR") errorLiveData.postValue(it.localizedMessage ?: "ERROR")
}.onSuccess { }.onSuccess {
comparisonSource() comparisonSource()
successLiveData.postValue(allRules) }
}
fun importSelect(finally: () -> Unit) {
execute {
val keepName = AppConfig.importKeepName
val selectRules = arrayListOf<ReplaceRule>()
selectStatus.forEachIndexed { index, b ->
if (b) {
val rule = allRules[index]
selectRules.add(rule)
}
}
appDb.replaceRuleDao.insert(*selectRules.toTypedArray())
}.onFinally {
finally.invoke()
} }
} }
private fun comparisonSource() { private fun comparisonSource() {
execute { execute {
allRules.forEach { allRules.forEach {
checkRules.add(null)
selectStatus.add(false)
} }
}.onSuccess {
successLiveData.postValue(allRules.size)
} }
} }
} }

@ -29,7 +29,7 @@ class ImportRssSourceActivity :
viewModel.successLiveData.observe(this, { viewModel.successLiveData.observe(this, {
binding.rotateLoading.hide() binding.rotateLoading.hide()
if (it > 0) { if (it > 0) {
successDialog() ImportRssSourceDialog().show(supportFragmentManager, "SourceDialog")
} else { } else {
errorDialog(getString(R.string.wrong_format)) errorDialog(getString(R.string.wrong_format))
} }
@ -71,8 +71,4 @@ class ImportRssSourceActivity :
}.show() }.show()
} }
private fun successDialog() {
ImportRssSourceDialog().show(supportFragmentManager, "SourceDialog")
}
} }

@ -7,7 +7,6 @@ import io.legado.app.base.BaseActivity
import io.legado.app.constant.Theme import io.legado.app.constant.Theme
import io.legado.app.databinding.ActivityTranslucenceBinding import io.legado.app.databinding.ActivityTranslucenceBinding
import io.legado.app.lib.dialogs.alert import io.legado.app.lib.dialogs.alert
import io.legado.app.utils.startActivity
import io.legado.app.utils.viewbindingdelegate.viewBinding import io.legado.app.utils.viewbindingdelegate.viewBinding
class OnLineImportActivity : BaseActivity<ActivityTranslucenceBinding>(theme = Theme.Transparent) { class OnLineImportActivity : BaseActivity<ActivityTranslucenceBinding>(theme = Theme.Transparent) {
@ -16,18 +15,15 @@ class OnLineImportActivity : BaseActivity<ActivityTranslucenceBinding>(theme = T
override fun onActivityCreated(savedInstanceState: Bundle?) { override fun onActivityCreated(savedInstanceState: Bundle?) {
intent.data?.let { intent.data?.let {
val url = it.getQueryParameter("src")
if (url.isNullOrBlank()) {
finish()
return
}
when (it.path) { when (it.path) {
"bookSource" -> it.getQueryParameter("src")?.let { url -> "bookSource" -> importBookSource(url)
importBookSource(url) "rssSource" -> importRssSource(url)
} "replaceRule" -> importReplaceRule(url)
"rssSource" -> it.getQueryParameter("src")?.let { url ->
importRssSource(url)
}
"replaceRule" -> it.getQueryParameter("src")?.let { url ->
startActivity<ImportReplaceRuleActivity> {
putExtra("source", url)
}
}
else -> { else -> {
} }
} }
@ -44,7 +40,7 @@ class OnLineImportActivity : BaseActivity<ActivityTranslucenceBinding>(theme = T
viewModel.successLiveData.observe(this, { viewModel.successLiveData.observe(this, {
binding.rotateLoading.hide() binding.rotateLoading.hide()
if (it > 0) { if (it > 0) {
ImportBookSourceDialog().show(supportFragmentManager, "SourceDialog") ImportBookSourceDialog().show(supportFragmentManager, "bookSource")
} else { } else {
errorDialog(getString(R.string.wrong_format)) errorDialog(getString(R.string.wrong_format))
} }
@ -62,7 +58,7 @@ class OnLineImportActivity : BaseActivity<ActivityTranslucenceBinding>(theme = T
viewModel.successLiveData.observe(this, { viewModel.successLiveData.observe(this, {
binding.rotateLoading.hide() binding.rotateLoading.hide()
if (it > 0) { if (it > 0) {
ImportRssSourceDialog().show(supportFragmentManager, "SourceDialog") ImportRssSourceDialog().show(supportFragmentManager, "rssSource")
} else { } else {
errorDialog(getString(R.string.wrong_format)) errorDialog(getString(R.string.wrong_format))
} }
@ -70,6 +66,24 @@ class OnLineImportActivity : BaseActivity<ActivityTranslucenceBinding>(theme = T
viewModel.importSource(url) viewModel.importSource(url)
} }
private fun importReplaceRule(url: String) {
val viewModel by viewModels<ImportReplaceRuleViewModel>()
binding.rotateLoading.show()
viewModel.errorLiveData.observe(this, {
binding.rotateLoading.hide()
errorDialog(it)
})
viewModel.successLiveData.observe(this, {
binding.rotateLoading.hide()
if (it > 0) {
ImportReplaceRuleDialog().show(supportFragmentManager, "replaceRule")
} else {
errorDialog(getString(R.string.wrong_format))
}
})
viewModel.import(url)
}
private fun errorDialog(msg: String) { private fun errorDialog(msg: String) {
alert(getString(R.string.error), msg) { alert(getString(R.string.error), msg) {
okButton { } okButton { }

@ -0,0 +1,6 @@
package io.legado.app.ui.association
import io.legado.app.App
import io.legado.app.base.BaseViewModel
class OnLineImportViewModel(app: App) : BaseViewModel(app)

@ -10,6 +10,7 @@ import io.legado.app.data.entities.BookChapter
import io.legado.app.help.BookHelp import io.legado.app.help.BookHelp
import io.legado.app.model.webBook.WebBook import io.legado.app.model.webBook.WebBook
import io.legado.app.service.help.AudioPlay import io.legado.app.service.help.AudioPlay
import io.legado.app.utils.toastOnUi
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
class AudioPlayViewModel(application: Application) : BaseViewModel(application) { class AudioPlayViewModel(application: Application) : BaseViewModel(application) {
@ -71,10 +72,10 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application)
} }
AudioPlay.upDurChapter(book) AudioPlay.upDurChapter(book)
} else { } else {
toastOnUi(R.string.error_load_toc) context.toastOnUi(R.string.error_load_toc)
} }
}?.onError { }?.onError {
toastOnUi(R.string.error_load_toc) context.toastOnUi(R.string.error_load_toc)
} }
} }
} }

@ -13,6 +13,7 @@ import io.legado.app.help.BookHelp
import io.legado.app.model.localBook.LocalBook import io.legado.app.model.localBook.LocalBook
import io.legado.app.model.webBook.WebBook import io.legado.app.model.webBook.WebBook
import io.legado.app.service.help.ReadBook import io.legado.app.service.help.ReadBook
import io.legado.app.utils.toastOnUi
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
class BookInfoViewModel(application: Application) : BaseViewModel(application) { class BookInfoViewModel(application: Application) : BaseViewModel(application) {
@ -86,11 +87,11 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
} }
loadChapter(it, changeDruChapterIndex) loadChapter(it, changeDruChapterIndex)
}.onError { }.onError {
toastOnUi(R.string.error_get_book_info) context.toastOnUi(R.string.error_get_book_info)
} }
} ?: let { } ?: let {
chapterListData.postValue(emptyList()) chapterListData.postValue(emptyList())
toastOnUi(R.string.error_no_source) context.toastOnUi(R.string.error_no_source)
} }
} }
} }
@ -122,19 +123,19 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
changeDruChapterIndex(it) changeDruChapterIndex(it)
} }
} else { } else {
toastOnUi(R.string.chapter_list_empty) context.toastOnUi(R.string.chapter_list_empty)
} }
}.onError { }.onError {
chapterListData.postValue(emptyList()) chapterListData.postValue(emptyList())
toastOnUi(R.string.error_get_chapter_list) context.toastOnUi(R.string.error_get_chapter_list)
} }
} ?: let { } ?: let {
chapterListData.postValue(emptyList()) chapterListData.postValue(emptyList())
toastOnUi(R.string.error_no_source) context.toastOnUi(R.string.error_no_source)
} }
} }
}.onError { }.onError {
toastOnUi("LoadTocError:${it.localizedMessage}") context.toastOnUi("LoadTocError:${it.localizedMessage}")
} }
} }
@ -260,9 +261,9 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
execute { execute {
BookHelp.clearCache(bookData.value!!) BookHelp.clearCache(bookData.value!!)
}.onSuccess { }.onSuccess {
toastOnUi(R.string.clear_cache_success) context.toastOnUi(R.string.clear_cache_success)
}.onError { }.onError {
toastOnUi(it.stackTraceToString()) context.toastOnUi(it.stackTraceToString())
} }
} }

@ -20,6 +20,7 @@ import io.legado.app.service.BaseReadAloudService
import io.legado.app.service.help.ReadAloud import io.legado.app.service.help.ReadAloud
import io.legado.app.service.help.ReadBook import io.legado.app.service.help.ReadBook
import io.legado.app.utils.msg import io.legado.app.utils.msg
import io.legado.app.utils.toastOnUi
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Dispatchers.Main import kotlinx.coroutines.Dispatchers.Main
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
@ -221,7 +222,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
}.onStart { }.onStart {
ReadBook.upMsg(context.getString(R.string.source_auto_changing)) ReadBook.upMsg(context.getString(R.string.source_auto_changing))
}.onError { }.onError {
toastOnUi(it.msg) context.toastOnUi(it.msg)
}.onFinally { }.onFinally {
ReadBook.upMsg(null) ReadBook.upMsg(null)
} }

@ -27,9 +27,9 @@ class SpeakEngineViewModel(application: Application) : BaseViewModel(application
import(json) import(json)
} }
}.onSuccess { }.onSuccess {
toastOnUi("导入成功") context.toastOnUi("导入成功")
}.onError { }.onError {
toastOnUi("导入失败") context.toastOnUi("导入失败")
} }
} }
@ -39,9 +39,9 @@ class SpeakEngineViewModel(application: Application) : BaseViewModel(application
import(it) import(it)
} }
}.onSuccess { }.onSuccess {
toastOnUi("导入成功") context.toastOnUi("导入成功")
}.onError { }.onError {
toastOnUi("导入失败") context.toastOnUi("导入失败")
} }
} }

@ -9,6 +9,7 @@ import io.legado.app.help.storage.OldRule
import io.legado.app.utils.GSON import io.legado.app.utils.GSON
import io.legado.app.utils.fromJsonObject import io.legado.app.utils.fromJsonObject
import io.legado.app.utils.getClipText import io.legado.app.utils.getClipText
import io.legado.app.utils.toastOnUi
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
class BookSourceEditViewModel(application: Application) : BaseViewModel(application) { class BookSourceEditViewModel(application: Application) : BaseViewModel(application) {
@ -46,7 +47,7 @@ class BookSourceEditViewModel(application: Application) : BaseViewModel(applicat
}.onSuccess { }.onSuccess {
success?.invoke() success?.invoke()
}.onError { }.onError {
toastOnUi(it.localizedMessage) context.toastOnUi(it.localizedMessage)
it.printStackTrace() it.printStackTrace()
} }
} }
@ -59,13 +60,13 @@ class BookSourceEditViewModel(application: Application) : BaseViewModel(applicat
} }
source source
}.onError { }.onError {
toastOnUi(it.localizedMessage) context.toastOnUi(it.localizedMessage)
it.printStackTrace() it.printStackTrace()
}.onSuccess { }.onSuccess {
if (it != null) { if (it != null) {
onSuccess(it) onSuccess(it)
} else { } else {
toastOnUi("格式不对") context.toastOnUi("格式不对")
} }
} }
} }
@ -77,7 +78,7 @@ class BookSourceEditViewModel(application: Application) : BaseViewModel(applicat
finally.invoke(it) finally.invoke(it)
} }
}.onError { }.onError {
toastOnUi(it.localizedMessage ?: "Error") context.toastOnUi(it.localizedMessage ?: "Error")
} }
} }
} }

@ -178,7 +178,7 @@ class BookSourceViewModel(application: Application) : BaseViewModel(application)
}.onSuccess { }.onSuccess {
success.invoke(it) success.invoke(it)
}.onError { }.onError {
toastOnUi(it.msg) context.toastOnUi(it.msg)
} }
} }

@ -6,6 +6,7 @@ import io.legado.app.base.BaseViewModel
import io.legado.app.help.http.get import io.legado.app.help.http.get
import io.legado.app.help.http.newCallStrResponse import io.legado.app.help.http.newCallStrResponse
import io.legado.app.help.http.okHttpClient import io.legado.app.help.http.okHttpClient
import io.legado.app.utils.toastOnUi
import org.jsoup.Jsoup import org.jsoup.Jsoup
class DictViewModel(application: Application) : BaseViewModel(application) { class DictViewModel(application: Application) : BaseViewModel(application) {
@ -17,12 +18,12 @@ class DictViewModel(application: Application) : BaseViewModel(application) {
val body = okHttpClient.newCallStrResponse { val body = okHttpClient.newCallStrResponse {
get("http://apii.dict.cn/mini.php", mapOf(Pair("q", word))) get("http://apii.dict.cn/mini.php", mapOf(Pair("q", word)))
}.body }.body
val jsoup = Jsoup.parse(body) val jsoup = Jsoup.parse(body!!)
jsoup.body() jsoup.body()
}.onSuccess { }.onSuccess {
dictHtmlData.postValue(it.html()) dictHtmlData.postValue(it.html())
}.onError { }.onError {
toastOnUi(it.localizedMessage) context.toastOnUi(it.localizedMessage)
} }
} }

@ -58,12 +58,12 @@ class BookshelfViewModel(application: Application) : BaseViewModel(application)
} }
}.onSuccess { }.onSuccess {
if (successCount > 0) { if (successCount > 0) {
toastOnUi(R.string.success) context.toastOnUi(R.string.success)
} else { } else {
toastOnUi("ERROR") context.toastOnUi("ERROR")
} }
}.onError { }.onError {
toastOnUi(it.localizedMessage ?: "ERROR") context.toastOnUi(it.localizedMessage ?: "ERROR")
} }
} }
@ -102,7 +102,7 @@ class BookshelfViewModel(application: Application) : BaseViewModel(application)
} }
} }
}.onError { }.onError {
toastOnUi(it.localizedMessage ?: "ERROR") context.toastOnUi(it.localizedMessage ?: "ERROR")
} }
} }
@ -125,7 +125,7 @@ class BookshelfViewModel(application: Application) : BaseViewModel(application)
} }
} }
}.onFinally { }.onFinally {
toastOnUi(R.string.success) context.toastOnUi(R.string.success)
} }
} }

@ -9,6 +9,7 @@ import io.legado.app.data.appDb
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.model.rss.Rss import io.legado.app.model.rss.Rss
import io.legado.app.utils.toastOnUi
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
@ -52,7 +53,7 @@ class RssArticlesViewModel(application: Application) : BaseViewModel(application
}.onError { }.onError {
loadFinally.postValue(false) loadFinally.postValue(false)
it.printStackTrace() it.printStackTrace()
toastOnUi(it.localizedMessage) context.toastOnUi(it.localizedMessage)
} }
} }

@ -21,10 +21,7 @@ import io.legado.app.help.http.newCall
import io.legado.app.help.http.okHttpClient import io.legado.app.help.http.okHttpClient
import io.legado.app.model.analyzeRule.AnalyzeUrl import io.legado.app.model.analyzeRule.AnalyzeUrl
import io.legado.app.model.rss.Rss import io.legado.app.model.rss.Rss
import io.legado.app.utils.DocumentUtils import io.legado.app.utils.*
import io.legado.app.utils.FileUtils
import io.legado.app.utils.isContentScheme
import io.legado.app.utils.writeBytes
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import java.io.File import java.io.File
import java.util.* import java.util.*
@ -139,14 +136,15 @@ class ReadRssViewModel(application: Application) : BaseViewModel(application),
} }
} ?: throw Throwable("NULL") } ?: throw Throwable("NULL")
}.onError { }.onError {
toastOnUi("保存图片失败:${it.localizedMessage}") context.toastOnUi("保存图片失败:${it.localizedMessage}")
}.onSuccess { }.onSuccess {
toastOnUi("保存成功") context.toastOnUi("保存成功")
} }
} }
private suspend fun webData2bitmap(data: String): ByteArray? { private suspend fun webData2bitmap(data: String): ByteArray? {
return if (URLUtil.isValidUrl(data)) { return if (URLUtil.isValidUrl(data)) {
@Suppress("BlockingMethodInNonBlockingContext")
okHttpClient.newCall { okHttpClient.newCall {
url(data) url(data)
}.bytes() }.bytes()
@ -189,7 +187,7 @@ class ReadRssViewModel(application: Application) : BaseViewModel(application),
ttsInitFinish = true ttsInitFinish = true
play() play()
} else { } else {
toastOnUi(R.string.tts_init_failed) context.toastOnUi(R.string.tts_init_failed)
} }
} }

@ -5,10 +5,7 @@ import android.content.Intent
import io.legado.app.base.BaseViewModel import io.legado.app.base.BaseViewModel
import io.legado.app.data.appDb import io.legado.app.data.appDb
import io.legado.app.data.entities.RssSource import io.legado.app.data.entities.RssSource
import io.legado.app.utils.GSON import io.legado.app.utils.*
import io.legado.app.utils.fromJsonObject
import io.legado.app.utils.getClipText
import io.legado.app.utils.msg
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
class RssSourceEditViewModel(application: Application) : BaseViewModel(application) { class RssSourceEditViewModel(application: Application) : BaseViewModel(application) {
@ -40,7 +37,7 @@ class RssSourceEditViewModel(application: Application) : BaseViewModel(applicati
}.onSuccess { }.onSuccess {
success() success()
}.onError { }.onError {
toastOnUi(it.localizedMessage) context.toastOnUi(it.localizedMessage)
it.printStackTrace() it.printStackTrace()
} }
} }
@ -53,12 +50,12 @@ class RssSourceEditViewModel(application: Application) : BaseViewModel(applicati
} }
source source
}.onError { }.onError {
toastOnUi(it.localizedMessage) context.toastOnUi(it.localizedMessage)
}.onSuccess { }.onSuccess {
if (it != null) { if (it != null) {
onSuccess(it) onSuccess(it)
} else { } else {
toastOnUi("格式不对") context.toastOnUi("格式不对")
} }
} }
} }
@ -70,7 +67,7 @@ class RssSourceEditViewModel(application: Application) : BaseViewModel(applicati
finally.invoke(it) finally.invoke(it)
} }
}.onError { }.onError {
toastOnUi(it.msg) context.toastOnUi(it.msg)
} }
} }

@ -120,7 +120,7 @@ class RssSourceViewModel(application: Application) : BaseViewModel(application)
}.onSuccess { }.onSuccess {
success.invoke(it) success.invoke(it)
}.onError { }.onError {
toastOnUi(it.msg) context.toastOnUi(it.msg)
} }
} }

Loading…
Cancel
Save