feat: 优化代码

pull/159/head
kunfei 5 years ago
parent a767619320
commit 9588c1a622
  1. 5
      app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt
  2. 42
      app/src/main/java/io/legado/app/model/WebBook.kt
  3. 15
      app/src/main/java/io/legado/app/service/help/ReadBook.kt
  4. 31
      app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt
  5. 9
      app/src/main/java/io/legado/app/ui/book/read/page/TextPageFactory.kt

@ -50,9 +50,12 @@ interface BookSourceDao {
@get:Query("select * from book_sources order by customOrder asc")
val all: List<BookSource>
@get:Query("select * from book_sources where enabled = 1 order by customOrder asc")
@get:Query("select * from book_sources where enabled = 1 order by customOrder")
val allEnabled: List<BookSource>
@get:Query("select * from book_sources where enabled = 1 and bookSourceType = 0 order by customOrder")
val allTextEnabled: List<BookSource>
@Query("select * from book_sources where bookSourceUrl = :key")
fun getBookSource(key: String): BookSource?

@ -29,26 +29,34 @@ class WebBook(val bookSource: BookSource) {
context: CoroutineContext = Dispatchers.IO
): Coroutine<List<SearchBook>> {
return Coroutine.async(scope, context) {
bookSource.searchUrl?.let { searchUrl ->
val analyzeUrl = AnalyzeUrl(
ruleUrl = searchUrl,
key = key,
page = page,
baseUrl = sourceUrl,
headerMapF = bookSource.getHeaderMap()
)
val res = analyzeUrl.getResponseAwait(bookSource.bookSourceUrl)
BookList.analyzeBookList(
res.body,
bookSource,
analyzeUrl,
res.url,
true
)
} ?: arrayListOf()
searchBookSuspend(key, page)
}
}
suspend fun searchBookSuspend(
key: String,
page: Int? = 1
): ArrayList<SearchBook> {
bookSource.searchUrl?.let { searchUrl ->
val analyzeUrl = AnalyzeUrl(
ruleUrl = searchUrl,
key = key,
page = page,
baseUrl = sourceUrl,
headerMapF = bookSource.getHeaderMap()
)
val res = analyzeUrl.getResponseAwait(bookSource.bookSourceUrl)
return BookList.analyzeBookList(
res.body,
bookSource,
analyzeUrl,
res.url,
true
)
}
return arrayListOf()
}
/**
* 发现
*/

@ -21,7 +21,6 @@ import org.jetbrains.anko.toast
object ReadBook {
var titleDate = MutableLiveData<String>()
var book: Book? = null
var inBookshelf = false
@ -34,9 +33,10 @@ object ReadBook {
var curTextChapter: TextChapter? = null
var nextTextChapter: TextChapter? = null
var webBook: WebBook? = null
var msg: String? = null
private val loadingChapters = arrayListOf<Int>()
fun resetData(book: Book, noSource: () -> Unit) {
fun resetData(book: Book, noSource: (name: String, author: String) -> Unit) {
this.book = book
titleDate.postValue(book.name)
durChapterIndex = book.durChapterIndex
@ -46,19 +46,20 @@ object ReadBook {
prevTextChapter = null
curTextChapter = null
nextTextChapter = null
upWebBook(book.origin, noSource)
upWebBook(book, noSource)
}
fun upWebBook(origin: String, noSource: () -> Unit) {
if (origin == BookType.local) {
fun upWebBook(book: Book?, noSource: (name: String, author: String) -> Unit) {
book ?: return
if (book.origin == BookType.local) {
webBook = null
} else {
val bookSource = App.db.bookSourceDao().getBookSource(origin)
val bookSource = App.db.bookSourceDao().getBookSource(book.origin)
if (bookSource != null) {
webBook = WebBook(bookSource)
} else {
webBook = null
noSource.invoke()
noSource.invoke(book.name, book.author)
}
}
}

@ -39,8 +39,8 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
private fun initBook(book: Book) {
if (ReadBook.book?.bookUrl != book.bookUrl) {
ReadBook.resetData(book) {
autoChangeSource()
ReadBook.resetData(book) { name, author ->
autoChangeSource(name, author)
}
isInitFinish = true
ReadBook.chapterSize = App.db.bookChapterDao().getChapterCount(book.bookUrl)
@ -62,8 +62,8 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
} else {
isInitFinish = true
ReadBook.titleDate.postValue(book.name)
ReadBook.upWebBook(book.origin) {
autoChangeSource()
ReadBook.upWebBook(book) { name, author ->
autoChangeSource(name, author)
}
ReadBook.chapterSize = App.db.bookChapterDao().getChapterCount(book.bookUrl)
if (ReadBook.chapterSize == 0) {
@ -126,7 +126,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
}
}?.onError {
toast(R.string.error_load_toc)
} ?: autoChangeSource()
} ?: autoChangeSource(book.name, book.author)
}
}
}
@ -155,8 +155,23 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
}
}
private fun autoChangeSource() {
private fun autoChangeSource(name: String, author: String) {
execute {
App.db.bookSourceDao().allTextEnabled.forEach {
try {
val searchBooks = WebBook(it).searchBookSuspend(name)
} catch (e: Exception) {
}
}
}.onStart {
ReadBook.msg = "正在自动换源"
ReadBook.callBack?.upContent()
}.onFinally {
ReadBook.msg = null
ReadBook.callBack?.upContent()
}
}
private fun upChangeDurChapterIndex(book: Book, chapters: List<BookChapter>) {
@ -212,9 +227,9 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
execute {
App.db.bookChapterDao().getChapter(book.bookUrl, ReadBook.durChapterIndex)
?.let { chapter ->
BookHelp.delContent(book, chapter)
BookHelp.delContent(book, chapter)
ReadBook.loadContent(ReadBook.durChapterIndex)
}
}
}
}

@ -59,6 +59,9 @@ class TextPageFactory(dataSource: DataSource) : PageFactory<TextPage>(dataSource
override val currentPage: TextPage
get() = with(dataSource) {
ReadBook.msg?.let {
return@with TextPage(text = it).format()
}
currentChapter?.let {
return@with it.page(pageIndex)
?: TextPage(title = it.title).format()
@ -68,6 +71,9 @@ class TextPageFactory(dataSource: DataSource) : PageFactory<TextPage>(dataSource
override val nextPage: TextPage
get() = with(dataSource) {
ReadBook.msg?.let {
return@with TextPage(text = it).format()
}
currentChapter?.let {
if (pageIndex < it.pageSize() - 1) {
return@with it.page(pageIndex + 1)?.removePageAloudSpan()
@ -86,6 +92,9 @@ class TextPageFactory(dataSource: DataSource) : PageFactory<TextPage>(dataSource
override val prevPage: TextPage
get() = with(dataSource) {
ReadBook.msg?.let {
return@with TextPage(text = it).format()
}
if (pageIndex > 0) {
currentChapter?.let {
return@with it.page(pageIndex - 1)?.removePageAloudSpan()

Loading…
Cancel
Save