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. 12
      app/src/main/java/io/legado/app/model/Debug.kt
  3. 2
      app/src/main/java/io/legado/app/model/SearchBookModel.kt
  4. 4
      app/src/main/java/io/legado/app/service/AudioPlayService.kt
  5. 2
      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. 8
      app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceViewModel.kt
  10. 2
      app/src/main/java/io/legado/app/ui/book/explore/ExploreShowViewModel.kt
  11. 4
      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. 8
      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. 2
      app/src/main/java/io/legado/app/ui/main/MainViewModel.kt
  16. 4
      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 val job: Job
private var start: VoidCallback? = null 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 error: Callback<Throwable>? = null
private var finally: VoidCallback? = null private var finally: VoidCallback? = null
private var cancel: VoidCallback? = null private var cancel: VoidCallback? = null
@ -79,7 +79,7 @@ class Coroutine<T>(
fun onSuccess( fun onSuccess(
context: CoroutineContext? = null, context: CoroutineContext? = null,
block: suspend CoroutineScope.(T?) -> Unit block: suspend CoroutineScope.(T) -> Unit
): Coroutine<T> { ): Coroutine<T> {
this.success = Callback(context, block) this.success = Callback(context, block)
return this@Coroutine return this@Coroutine
@ -192,7 +192,7 @@ class Coroutine<T>(
context: CoroutineContext, context: CoroutineContext,
timeMillis: Long, timeMillis: Long,
noinline block: suspend CoroutineScope.() -> T noinline block: suspend CoroutineScope.() -> T
): T? { ): T {
return withContext(scope.coroutineContext.plus(context)) { return withContext(scope.coroutineContext.plus(context)) {
if (timeMillis > 0L) withTimeout(timeMillis) { if (timeMillis > 0L) withTimeout(timeMillis) {
block() block()

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Loading…
Cancel
Save