diff --git a/app/src/main/java/io/legado/app/model/webbook/Debug.kt b/app/src/main/java/io/legado/app/model/webbook/SourceDebug.kt similarity index 56% rename from app/src/main/java/io/legado/app/model/webbook/Debug.kt rename to app/src/main/java/io/legado/app/model/webbook/SourceDebug.kt index a62fe1b2a..0e6dafa0b 100644 --- a/app/src/main/java/io/legado/app/model/webbook/Debug.kt +++ b/app/src/main/java/io/legado/app/model/webbook/SourceDebug.kt @@ -1,16 +1,15 @@ package io.legado.app.model.webbook import android.annotation.SuppressLint -import io.legado.app.App import io.legado.app.data.entities.Book -import io.legado.app.data.entities.BookSource +import io.legado.app.data.entities.BookChapter import io.legado.app.help.BookHelp import io.legado.app.model.WebBook import io.legado.app.utils.htmlFormat import java.text.SimpleDateFormat import java.util.* -class Debug(sourceUrl: String) { +class SourceDebug(val webBook: WebBook) { companion object { var debugSource: String? = null @@ -36,32 +35,41 @@ class Debug(sourceUrl: String) { fun printLog(state: Int, msg: String) } - private var bookSource: BookSource? = null - - init { - debugSource = sourceUrl - bookSource = App.db.bookSourceDao().findByKey(sourceUrl) - } - fun searchDebug(key: String) { - bookSource?.let { - WebBook(it).searchBook(key, 1) - .onSuccess { searchBooks -> - searchBooks?.let { - if (searchBooks.isNotEmpty()) { - infoDebug(BookHelp.toBook(searchBooks[0])) - } + webBook.searchBook(key, 1) + .onSuccess { searchBooks -> + searchBooks?.let { + if (searchBooks.isNotEmpty()) { + infoDebug(BookHelp.toBook(searchBooks[0])) } } - - } ?: printLog(debugSource, -1, "未找到书源") + } } fun infoDebug(book: Book) { - bookSource?.let { - WebBook(it).getBookInfo(book) - } ?: printLog(debugSource, -1, "未找到书源") + webBook.getBookInfo(book) + .onSuccess { + tocDebug(book) + } } + private fun tocDebug(book: Book) { + webBook.getChapterList(book) + .onSuccess { chapterList -> + chapterList?.let { + if (it.isNotEmpty()) { + contentDebug(book, it[0]) + } + } + } + } + private fun contentDebug(book: Book, bookChapter: BookChapter) { + webBook.getContent(book, bookChapter) + .onSuccess { content -> + content?.let { + printLog(debugSource, 1000, it) + } + } + } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/sourcedebug/SourceDebugActivity.kt b/app/src/main/java/io/legado/app/ui/sourcedebug/SourceDebugActivity.kt index de3a42833..5e980bbeb 100644 --- a/app/src/main/java/io/legado/app/ui/sourcedebug/SourceDebugActivity.kt +++ b/app/src/main/java/io/legado/app/ui/sourcedebug/SourceDebugActivity.kt @@ -5,11 +5,19 @@ import android.view.View import androidx.appcompat.widget.SearchView import androidx.lifecycle.AndroidViewModel import androidx.recyclerview.widget.LinearLayoutManager +import io.legado.app.App import io.legado.app.R import io.legado.app.base.BaseActivity +import io.legado.app.data.entities.Book +import io.legado.app.data.entities.BookSource +import io.legado.app.model.WebBook +import io.legado.app.model.webbook.SourceDebug import io.legado.app.utils.getViewModel +import io.legado.app.utils.isAbsUrl import kotlinx.android.synthetic.main.activity_source_debug.* import kotlinx.android.synthetic.main.view_title_bar.* +import kotlinx.coroutines.Dispatchers.IO +import kotlinx.coroutines.launch class SourceDebugActivity : BaseActivity() { override val viewModel: AndroidViewModel @@ -18,8 +26,14 @@ class SourceDebugActivity : BaseActivity() { get() = R.layout.activity_source_debug private lateinit var adapter: SourceDebugAdapter + private var bookSource: BookSource? = null override fun onViewModelCreated(viewModel: AndroidViewModel, savedInstanceState: Bundle?) { + launch(IO) { + intent.getStringExtra("key")?.let { + bookSource = App.db.bookSourceDao().findByKey(it) + } + } initRecyclerView() initSearchView() } @@ -51,7 +65,18 @@ class SourceDebugActivity : BaseActivity() { private fun startSearch(key: String) { adapter.logList.clear() adapter.notifyDataSetChanged() + bookSource?.let { + if (key.isAbsUrl()) { + val book = Book() + book.origin = it.bookSourceUrl + book.bookUrl = key + SourceDebug(WebBook(it)) + .infoDebug(book) + } else { + SourceDebug(WebBook(it)) + .searchDebug(key) + } + } } - } \ No newline at end of file