feat: 优化代码

pull/169/head
kunfei 5 years ago
parent 5b4e4d4e26
commit 83e5a5d91f
  1. 6
      app/src/main/java/io/legado/app/help/coroutine/Coroutine.kt
  2. 50
      app/src/main/java/io/legado/app/model/Debug.kt
  3. 6
      app/src/main/java/io/legado/app/model/SearchBookModel.kt
  4. 4
      app/src/main/java/io/legado/app/service/AudioPlayService.kt
  5. 6
      app/src/main/java/io/legado/app/service/DownloadService.kt
  6. 2
      app/src/main/java/io/legado/app/service/help/ReadBook.kt
  7. 2
      app/src/main/java/io/legado/app/ui/audio/AudioPlayViewModel.kt
  8. 2
      app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverViewModel.kt
  9. 16
      app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceViewModel.kt
  10. 8
      app/src/main/java/io/legado/app/ui/book/explore/ExploreShowViewModel.kt
  11. 32
      app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt
  12. 2
      app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt
  13. 12
      app/src/main/java/io/legado/app/ui/book/search/SearchViewModel.kt
  14. 2
      app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt
  15. 8
      app/src/main/java/io/legado/app/ui/main/MainViewModel.kt
  16. 6
      app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfViewModel.kt
  17. 8
      app/src/main/java/io/legado/app/ui/rss/article/RssArticlesViewModel.kt
  18. 2
      app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceViewModel.kt

@ -28,7 +28,7 @@ class Coroutine<T>(
private val job: Job
private var start: VoidCallback? = null
private var success: Callback<T?>? = null
private var success: Callback<T>? = null
private var error: Callback<Throwable>? = null
private var finally: VoidCallback? = null
private var cancel: VoidCallback? = null
@ -79,7 +79,7 @@ class Coroutine<T>(
fun onSuccess(
context: CoroutineContext? = null,
block: suspend CoroutineScope.(T?) -> Unit
block: suspend CoroutineScope.(T) -> Unit
): Coroutine<T> {
this.success = Callback(context, block)
return this@Coroutine
@ -192,7 +192,7 @@ class Coroutine<T>(
context: CoroutineContext,
timeMillis: Long,
noinline block: suspend CoroutineScope.() -> T
): T? {
): T {
return withContext(scope.coroutineContext.plus(context)) {
if (timeMillis > 0L) withTimeout(timeMillis) {
block()

@ -56,9 +56,7 @@ object Debug {
log(debugSource, "︾开始解析")
Rss.getArticles(rssSource, null)
.onSuccess {
if (it == null) {
log(debugSource, "︽解析失败", state = -1)
} else if(it.articles.isEmpty()) {
if (it.articles.isEmpty()) {
log(debugSource, "⇒列表页解析成功,为空")
log(debugSource, "︽解析完成", state = 1000)
} else {
@ -140,14 +138,12 @@ object Debug {
log(debugSource, "︾开始解析发现页")
val explore = webBook.exploreBook(url, 1)
.onSuccess { exploreBooks ->
exploreBooks?.let {
if (exploreBooks.isNotEmpty()) {
log(debugSource, "︽发现页解析完成")
log(debugSource, showTime = false)
infoDebug(webBook, exploreBooks[0].toBook())
} else {
log(debugSource, "︽未获取到书籍", state = -1)
}
if (exploreBooks.isNotEmpty()) {
log(debugSource, "︽发现页解析完成")
log(debugSource, showTime = false)
infoDebug(webBook, exploreBooks[0].toBook())
} else {
log(debugSource, "︽未获取到书籍", state = -1)
}
}
.onError {
@ -164,14 +160,12 @@ object Debug {
log(debugSource, "︾开始解析搜索页")
val search = webBook.searchBook(key, 1)
.onSuccess { searchBooks ->
searchBooks?.let {
if (searchBooks.isNotEmpty()) {
log(debugSource, "︽搜索页解析完成")
log(debugSource, showTime = false)
infoDebug(webBook, searchBooks[0].toBook())
} else {
log(debugSource, "︽未获取到书籍", state = -1)
}
if (searchBooks.isNotEmpty()) {
log(debugSource, "︽搜索页解析完成")
log(debugSource, showTime = false)
infoDebug(webBook, searchBooks[0].toBook())
} else {
log(debugSource, "︽未获取到书籍", state = -1)
}
}
.onError {
@ -197,16 +191,14 @@ object Debug {
private fun tocDebug(webBook: WebBook, book: Book) {
log(debugSource, "︾开始解析目录页")
val chapterList = webBook.getChapterList(book)
.onSuccess { chapterList ->
chapterList?.let {
if (it.isNotEmpty()) {
log(debugSource, "︽目录页解析完成")
log(debugSource, showTime = false)
val nextChapterUrl = if (it.size > 1) it[1].url else null
contentDebug(webBook, book, it[0], nextChapterUrl)
} else {
log(debugSource, "︽目录列表为空", state = -1)
}
.onSuccess {
if (it.isNotEmpty()) {
log(debugSource, "︽目录页解析完成")
log(debugSource, showTime = false)
val nextChapterUrl = if (it.size > 1) it[1].url else null
contentDebug(webBook, book, it[0], nextChapterUrl)
} else {
log(debugSource, "︽目录列表为空", state = -1)
}
}
.onError {

@ -49,10 +49,8 @@ class SearchBookModel(private val scope: CoroutineScope, private val callBack: C
)
.timeout(30000L)
.onSuccess(IO) {
it?.let {
if (searchId == mSearchId) {
callBack.onSearchSuccess(it)
}
if (searchId == mSearchId) {
callBack.onSearchSuccess(it)
}
}
}

@ -18,9 +18,9 @@ import androidx.core.app.NotificationCompat
import io.legado.app.App
import io.legado.app.R
import io.legado.app.base.BaseService
import io.legado.app.constant.IntentAction
import io.legado.app.constant.AppConst
import io.legado.app.constant.EventBus
import io.legado.app.constant.IntentAction
import io.legado.app.constant.Status
import io.legado.app.data.entities.BookChapter
import io.legado.app.help.BookHelp
@ -275,7 +275,7 @@ class AudioPlayService : BaseService(),
AudioPlay.book?.let { book ->
AudioPlay.webBook?.getContent(book, chapter, scope = this)
?.onSuccess(IO) { content ->
if (content.isNullOrEmpty()) {
if (content.isEmpty()) {
withContext(Main) {
toast("未获取到资源链接")
}

@ -130,10 +130,8 @@ class DownloadService : BaseService() {
// notificationContent = "启动:" + chapter.title
//}
.onSuccess(IO) { content ->
content?.let {
downloadCount[entry.key]?.increaseSuccess()
BookHelp.saveContent(book, chapter, content)
}
downloadCount[entry.key]?.increaseSuccess()
BookHelp.saveContent(book, chapter, content)
}
.onFinally(IO) {
synchronized(this@DownloadService) {

@ -245,7 +245,7 @@ object ReadBook {
book?.let { book ->
webBook?.getContent(book, chapter)
?.onSuccess(Dispatchers.IO) { content ->
if (content.isNullOrEmpty()) {
if (content.isEmpty()) {
contentLoadFinish(
chapter,
App.INSTANCE.getString(R.string.content_empty),

@ -72,7 +72,7 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application)
execute {
AudioPlay.webBook?.getChapterList(book, this)
?.onSuccess(Dispatchers.IO) { cList ->
if (!cList.isNullOrEmpty()) {
if (cList.isNotEmpty()) {
if (changeDruChapterIndex == null) {
App.db.bookChapterDao().insert(*cList.toTypedArray())
AudioPlay.chapterSize = cList.size

@ -57,7 +57,7 @@ class ChangeCoverViewModel(application: Application) : BaseViewModel(application
WebBook(item).searchBook(name, scope = this@execute, context = searchPool)
.timeout(30000L)
.onSuccess(Dispatchers.IO) {
if (it != null && it.isNotEmpty()) {
if (it.isNotEmpty()) {
val searchBook = it[0]
if (searchBook.name == name && searchBook.author == author
&& !searchBook.coverUrl.isNullOrEmpty()

@ -78,7 +78,7 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio
WebBook(item).searchBook(name, scope = this@execute, context = searchPool)
.timeout(30000L)
.onSuccess(IO) {
it?.forEach { searchBook ->
it.forEach { searchBook ->
if (searchBook.name == name && searchBook.author == author) {
if (context.getPrefBoolean(PreferKey.changeSourceLoadToc)) {
if (searchBook.tocUrl.isEmpty()) {
@ -110,7 +110,7 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio
App.db.bookSourceDao().getBookSource(book.origin)?.let { bookSource ->
WebBook(bookSource).getBookInfo(book, this)
.onSuccess {
it?.let { loadChapter(it) }
loadChapter(it)
}.onError {
debug { context.getString(R.string.error_get_book_info) }
}
@ -122,13 +122,11 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio
execute {
App.db.bookSourceDao().getBookSource(book.origin)?.let { bookSource ->
WebBook(bookSource).getChapterList(book, this)
.onSuccess(IO) {
it?.let { chapters ->
if (chapters.isNotEmpty()) {
book.latestChapterTitle = chapters.last().title
val searchBook: SearchBook = book.toSearchBook()
searchFinish(searchBook)
}
.onSuccess(IO) { chapters ->
if (chapters.isNotEmpty()) {
book.latestChapterTitle = chapters.last().title
val searchBook: SearchBook = book.toSearchBook()
searchFinish(searchBook)
}
}.onError {
debug { context.getString(R.string.error_get_chapter_list) }

@ -35,11 +35,9 @@ class ExploreShowViewModel(application: Application) : BaseViewModel(application
WebBook(source).exploreBook(url, page, this)
.timeout(30000L)
.onSuccess(IO) { searchBooks ->
searchBooks?.let {
booksData.postValue(searchBooks)
App.db.searchBookDao().insert(*searchBooks.toTypedArray())
page++
}
booksData.postValue(searchBooks)
App.db.searchBookDao().insert(*searchBooks.toTypedArray())
page++
}
}
}

@ -59,13 +59,11 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
App.db.bookSourceDao().getBookSource(book.origin)?.let { bookSource ->
WebBook(bookSource).getBookInfo(book, this)
.onSuccess(IO) {
it?.let {
bookData.postValue(book)
if (inBookshelf) {
App.db.bookDao().update(book)
}
loadChapter(it, changeDruChapterIndex)
bookData.postValue(book)
if (inBookshelf) {
App.db.bookDao().update(book)
}
loadChapter(it, changeDruChapterIndex)
}.onError {
toast(R.string.error_get_book_info)
}
@ -92,20 +90,18 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
App.db.bookSourceDao().getBookSource(book.origin)?.let { bookSource ->
WebBook(bookSource).getChapterList(book, this)
.onSuccess(IO) {
it?.let {
if (it.isNotEmpty()) {
if (inBookshelf) {
App.db.bookDao().update(book)
App.db.bookChapterDao().insert(*it.toTypedArray())
}
if (changeDruChapterIndex == null) {
chapterListData.postValue(it)
} else {
changeDruChapterIndex(it)
}
if (it.isNotEmpty()) {
if (inBookshelf) {
App.db.bookDao().update(book)
App.db.bookChapterDao().insert(*it.toTypedArray())
}
if (changeDruChapterIndex == null) {
chapterListData.postValue(it)
} else {
toast(R.string.chapter_list_empty)
changeDruChapterIndex(it)
}
} else {
toast(R.string.chapter_list_empty)
}
}.onError {
chapterListData.postValue(null)

@ -112,7 +112,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
} else {
ReadBook.webBook?.getChapterList(book, this)
?.onSuccess(IO) { cList ->
if (!cList.isNullOrEmpty()) {
if (cList.isNotEmpty()) {
if (changeDruChapterIndex == null) {
App.db.bookChapterDao().insert(*cList.toTypedArray())
App.db.bookDao().update(book)

@ -63,13 +63,11 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
.timeout(30000L)
.onSuccess(IO) {
if (isActive) {
it?.let { list ->
if (context.getPrefBoolean(PreferKey.precisionSearch)) {
precisionSearch(this, list)
} else {
App.db.searchBookDao().insert(*list.toTypedArray())
mergeItems(this, list)
}
if (context.getPrefBoolean(PreferKey.precisionSearch)) {
precisionSearch(this, it)
} else {
App.db.searchBookDao().insert(*it.toTypedArray())
mergeItems(this, it)
}
}
}

@ -211,7 +211,7 @@ class BookSourceViewModel(application: Application) : BaseViewModel(application)
}.onError {
finally(it.localizedMessage ?: "")
}.onSuccess {
finally(it ?: "导入完成")
finally(it)
}
}

@ -33,11 +33,9 @@ class MainViewModel(application: Application) : BaseViewModel(application) {
updateList.remove(book.bookUrl)
postEvent(EventBus.UP_BOOK, book.bookUrl)
}
it?.let {
App.db.bookDao().update(book)
App.db.bookChapterDao().delByBook(book.bookUrl)
App.db.bookChapterDao().insert(*it.toTypedArray())
}
App.db.bookDao().update(book)
App.db.bookChapterDao().delByBook(book.bookUrl)
App.db.bookChapterDao().insert(*it.toTypedArray())
}
.onError {
synchronized(this) {

@ -43,10 +43,8 @@ class BookshelfViewModel(application: Application) : BaseViewModel(application)
)
WebBook(bookSource).getBookInfo(book, this)
.onSuccess(IO) {
it?.let { book ->
App.db.bookDao().insert(book)
successCount++
}
App.db.bookDao().insert(it)
successCount++
}.onError {
throw Exception(it.localizedMessage)
}

@ -44,8 +44,8 @@ class RssArticlesViewModel(application: Application) : BaseViewModel(application
rssSource?.let { rssSource ->
Rss.getArticles(rssSource, null)
.onSuccess(IO) {
nextPageUrl = it?.nextPageUrl
it?.articles?.let { list ->
nextPageUrl = it.nextPageUrl
it.articles.let { list ->
list.forEach { rssArticle ->
rssArticle.order = order--
}
@ -76,8 +76,8 @@ class RssArticlesViewModel(application: Application) : BaseViewModel(application
if (source != null && !pageUrl.isNullOrEmpty()) {
Rss.getArticles(source, pageUrl)
.onSuccess(IO) {
nextPageUrl = it?.nextPageUrl
it?.articles?.let { list ->
nextPageUrl = it.nextPageUrl
it.articles.let { list ->
if (list.isEmpty()) {
callBack?.loadFinally(false)
return@let

@ -187,7 +187,7 @@ class RssSourceViewModel(application: Application) : BaseViewModel(application)
}.onError {
finally(it.localizedMessage ?: "")
}.onSuccess {
finally(it ?: "导入完成")
finally(it)
}
}

Loading…
Cancel
Save