pull/1155/head
gedoor 3 years ago
parent 86e59b46d9
commit a3f278551a
  1. 15
      app/src/main/java/io/legado/app/base/BaseViewModel.kt
  2. 6
      app/src/main/java/io/legado/app/ui/association/OnLineImportViewModel.kt
  3. 5
      app/src/main/java/io/legado/app/ui/book/audio/AudioPlayViewModel.kt
  4. 15
      app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt
  5. 6
      app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfViewModel.kt
  6. 12
      app/src/main/java/io/legado/app/ui/rss/read/ReadRssViewModel.kt

@ -2,12 +2,10 @@ package io.legado.app.base
import android.app.Application
import android.content.Context
import androidx.annotation.CallSuper
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import io.legado.app.App
import io.legado.app.help.coroutine.Coroutine
import io.legado.app.utils.toastOnUi
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.Dispatchers
@ -34,17 +32,4 @@ open class BaseViewModel(application: Application) : AndroidViewModel(applicatio
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())
}
}

@ -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.model.webBook.WebBook
import io.legado.app.service.help.AudioPlay
import io.legado.app.utils.toastOnUi
import kotlinx.coroutines.Dispatchers
class AudioPlayViewModel(application: Application) : BaseViewModel(application) {
@ -71,10 +72,10 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application)
}
AudioPlay.upDurChapter(book)
} else {
toastOnUi(R.string.error_load_toc)
context.toastOnUi(R.string.error_load_toc)
}
}?.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.webBook.WebBook
import io.legado.app.service.help.ReadBook
import io.legado.app.utils.toastOnUi
import kotlinx.coroutines.Dispatchers.IO
class BookInfoViewModel(application: Application) : BaseViewModel(application) {
@ -86,11 +87,11 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
}
loadChapter(it, changeDruChapterIndex)
}.onError {
toastOnUi(R.string.error_get_book_info)
context.toastOnUi(R.string.error_get_book_info)
}
} ?: let {
chapterListData.postValue(emptyList())
toastOnUi(R.string.error_no_source)
context.toastOnUi(R.string.error_no_source)
}
}
}
@ -122,15 +123,15 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
changeDruChapterIndex(it)
}
} else {
toastOnUi(R.string.chapter_list_empty)
context.toastOnUi(R.string.chapter_list_empty)
}
}.onError {
chapterListData.postValue(emptyList())
toastOnUi(R.string.error_get_chapter_list)
context.toastOnUi(R.string.error_get_chapter_list)
}
} ?: let {
chapterListData.postValue(emptyList())
toastOnUi(R.string.error_no_source)
context.toastOnUi(R.string.error_no_source)
}
}
}.onError {
@ -260,9 +261,9 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
execute {
BookHelp.clearCache(bookData.value!!)
}.onSuccess {
toastOnUi(R.string.clear_cache_success)
context.toastOnUi(R.string.clear_cache_success)
}.onError {
toastOnUi(it.stackTraceToString())
context.toastOnUi(it.stackTraceToString())
}
}

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

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

Loading…
Cancel
Save