pull/32/head
kunfei 5 years ago
parent 5ce713f8b4
commit 61a7a2dc3d
  1. 6
      app/src/main/java/io/legado/app/data/dao/SearchBookDao.kt
  2. 16
      app/src/main/java/io/legado/app/data/entities/SearchBook.kt
  3. 18
      app/src/main/java/io/legado/app/help/BookHelp.kt
  4. 3
      app/src/main/java/io/legado/app/model/webbook/SourceDebug.kt
  5. 4
      app/src/main/java/io/legado/app/ui/bookinfo/BookInfoViewModel.kt
  6. 13
      app/src/main/java/io/legado/app/ui/search/SearchActivity.kt
  7. 6
      app/src/main/java/io/legado/app/ui/search/SearchAdapter.kt
  8. 15
      app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt
  9. 1
      app/src/main/res/layout/item_search.xml
  10. 1
      app/src/main/res/values/strings.xml

@ -20,6 +20,12 @@ interface SearchBookDao {
@Query("SELECT name, author, min(time) time, max(kind) kind, max(coverUrl) coverUrl, max(intro) intro, max(wordCount) wordCount, max(latestChapterTitle) latestChapterTitle, count(origin) originCount FROM searchBooks where time >= :time group by name, author") @Query("SELECT name, author, min(time) time, max(kind) kind, max(coverUrl) coverUrl, max(intro) intro, max(wordCount) wordCount, max(latestChapterTitle) latestChapterTitle, count(origin) originCount FROM searchBooks where time >= :time group by name, author")
fun observeShow(time: Long): DataSource.Factory<Int, SearchShow> fun observeShow(time: Long): DataSource.Factory<Int, SearchShow>
@Query("select * from searchBooks where bookUrl = :bookUrl")
fun getSearchBook(bookUrl: String): SearchBook?
@Query("select * from searchBooks where name = :name and author = :author order by originOrder limit 1")
fun getByNameAuthor(name: String, author: String?): SearchBook?
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(vararg searchBook: SearchBook): List<Long> fun insert(vararg searchBook: SearchBook): List<Long>

@ -67,4 +67,20 @@ data class SearchBook(
} }
return kindList return kindList
} }
fun toBook(): Book {
val book = Book()
book.name = name
book.author = author
book.kind = kind
book.bookUrl = bookUrl
book.origin = origin
book.originName = originName
book.wordCount = wordCount
book.latestChapterTitle = latestChapterTitle
book.coverUrl = coverUrl
book.intro = intro
return book
}
} }

@ -1,25 +1,7 @@
package io.legado.app.help package io.legado.app.help
import io.legado.app.data.entities.Book
import io.legado.app.data.entities.SearchBook
object BookHelp { object BookHelp {
fun toBook(searchBook: SearchBook): Book {
val book = Book()
book.name = searchBook.name
book.author = searchBook.author
book.kind = searchBook.kind
book.bookUrl = searchBook.bookUrl
book.origin = searchBook.origin
book.wordCount = searchBook.wordCount
book.latestChapterTitle = searchBook.latestChapterTitle
book.coverUrl = searchBook.coverUrl
return book
}
fun formatAuthor(author: String?): String { fun formatAuthor(author: String?): String {
return author return author
?.replace("\\s*者[\\s::]*".toRegex(), "") ?.replace("\\s*者[\\s::]*".toRegex(), "")

@ -3,7 +3,6 @@ package io.legado.app.model.webbook
import android.annotation.SuppressLint import android.annotation.SuppressLint
import io.legado.app.data.entities.Book import io.legado.app.data.entities.Book
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.coroutine.CompositeCoroutine import io.legado.app.help.coroutine.CompositeCoroutine
import io.legado.app.model.WebBook import io.legado.app.model.WebBook
import io.legado.app.utils.htmlFormat import io.legado.app.utils.htmlFormat
@ -82,7 +81,7 @@ class SourceDebug(private val webBook: WebBook, callback: Callback) {
if (searchBooks.isNotEmpty()) { if (searchBooks.isNotEmpty()) {
printLog(debugSource, 1, "搜索完成") printLog(debugSource, 1, "搜索完成")
printLog(debugSource, 1, "", showTime = false) printLog(debugSource, 1, "", showTime = false)
infoDebug(BookHelp.toBook(searchBooks[0])) infoDebug(searchBooks[0].toBook())
} else { } else {
printLog(debugSource, -1, "未获取到书籍") printLog(debugSource, -1, "未获取到书籍")
} }

@ -17,6 +17,10 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
App.db.bookDao().getBook(it)?.let { book -> App.db.bookDao().getBook(it)?.let { book ->
bookData.postValue(book) bookData.postValue(book)
} }
} ?: intent.getStringExtra("searchBookUrl")?.let {
App.db.searchBookDao().getSearchBook(it)?.let { searchBook ->
bookData.postValue(searchBook.toBook())
}
} }
} }
} }

@ -1,7 +1,6 @@
package io.legado.app.ui.search package io.legado.app.ui.search
import android.os.Bundle import android.os.Bundle
import android.view.View
import androidx.appcompat.widget.SearchView import androidx.appcompat.widget.SearchView
import androidx.lifecycle.LiveData import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer import androidx.lifecycle.Observer
@ -14,11 +13,13 @@ import io.legado.app.base.VMBaseActivity
import io.legado.app.data.entities.SearchShow import io.legado.app.data.entities.SearchShow
import io.legado.app.lib.theme.ATH import io.legado.app.lib.theme.ATH
import io.legado.app.lib.theme.primaryTextColor import io.legado.app.lib.theme.primaryTextColor
import io.legado.app.ui.bookinfo.BookInfoActivity
import io.legado.app.utils.getViewModel import io.legado.app.utils.getViewModel
import kotlinx.android.synthetic.main.activity_search.* import kotlinx.android.synthetic.main.activity_search.*
import kotlinx.android.synthetic.main.view_search.* import kotlinx.android.synthetic.main.view_search.*
import org.jetbrains.anko.startActivity
class SearchActivity : VMBaseActivity<SearchViewModel>(R.layout.activity_search) { class SearchActivity : VMBaseActivity<SearchViewModel>(R.layout.activity_search), SearchAdapter.CallBack {
override val viewModel: SearchViewModel override val viewModel: SearchViewModel
get() = getViewModel(SearchViewModel::class.java) get() = getViewModel(SearchViewModel::class.java)
@ -66,6 +67,7 @@ class SearchActivity : VMBaseActivity<SearchViewModel>(R.layout.activity_search)
private fun initRecyclerView() { private fun initRecyclerView() {
ATH.applyEdgeEffectColor(rv_search_list) ATH.applyEdgeEffectColor(rv_search_list)
adapter = SearchAdapter() adapter = SearchAdapter()
adapter.callBack = this
rv_search_list.layoutManager = LinearLayoutManager(this) rv_search_list.layoutManager = LinearLayoutManager(this)
rv_search_list.adapter = adapter rv_search_list.adapter = adapter
} }
@ -76,4 +78,11 @@ class SearchActivity : VMBaseActivity<SearchViewModel>(R.layout.activity_search)
searchBookData?.observe(this, Observer { adapter.submitList(it) }) searchBookData?.observe(this, Observer { adapter.submitList(it) })
} }
override fun showBookInfo(name: String, author: String?) {
viewModel.getSearchBook(name, author) { searchBook ->
searchBook?.let {
startActivity<BookInfoActivity>(Pair("searchBookUrl", it.bookUrl))
}
}
}
} }

@ -50,7 +50,7 @@ class SearchAdapter : PagedListAdapter<SearchShow, SearchAdapter.MyViewHolder>(D
fun bind(searchBook: SearchShow, callBack: CallBack?) = with(itemView) { fun bind(searchBook: SearchShow, callBack: CallBack?) = with(itemView) {
tv_name.text = searchBook.name tv_name.text = searchBook.name
bv_originCount.setBadgeCount(searchBook.originCount) bv_originCount.setBadgeCount(searchBook.originCount)
tv_author.text = searchBook.author tv_author.text = context.getString(R.string.author_show, searchBook.author)
if (searchBook.latestChapterTitle.isNullOrEmpty()) { if (searchBook.latestChapterTitle.isNullOrEmpty()) {
tv_lasted.gone() tv_lasted.gone()
} else { } else {
@ -96,12 +96,12 @@ class SearchAdapter : PagedListAdapter<SearchShow, SearchAdapter.MyViewHolder>(D
.setAsDrawable(iv_cover) .setAsDrawable(iv_cover)
} }
onClick { onClick {
callBack?.showBookInfo(searchBook.name, searchBook.author) callBack?.showBookInfo(searchBook.name!!, searchBook.author)
} }
} }
} }
interface CallBack { interface CallBack {
fun showBookInfo(name: String?, author: String?) fun showBookInfo(name: String, author: String?)
} }
} }

@ -1,17 +1,12 @@
package io.legado.app.ui.search package io.legado.app.ui.search
import android.app.Application import android.app.Application
import android.util.Log
import io.legado.app.App import io.legado.app.App
import io.legado.app.base.BaseViewModel import io.legado.app.base.BaseViewModel
import io.legado.app.data.entities.SearchBook
import io.legado.app.help.coroutine.Coroutine import io.legado.app.help.coroutine.Coroutine
import io.legado.app.model.WebBook import io.legado.app.model.WebBook
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.cancel
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import java.lang.Exception
import java.util.concurrent.CancellationException
class SearchViewModel(application: Application) : BaseViewModel(application) { class SearchViewModel(application: Application) : BaseViewModel(application) {
private var task: Coroutine<*>? = null private var task: Coroutine<*>? = null
@ -47,12 +42,16 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
task?.invokeOnCompletion { task?.invokeOnCompletion {
finally?.invoke() finally?.invoke()
} }
} }
fun stop() { fun stop() {
task?.cancel() task?.cancel()
} }
fun getSearchBook(name: String, author: String?, success: ((searchBook: SearchBook?) -> Unit)?) {
execute {
val searchBook = App.db.searchBookDao().getByNameAuthor(name, author)
success?.invoke(searchBook)
}
}
} }

@ -54,7 +54,6 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:ellipsize="end" android:ellipsize="end"
android:lines="1" android:lines="1"
android:drawableStart="@drawable/ic_author"
android:text="@string/author" android:text="@string/author"
android:textColor="@color/tv_text_default" android:textColor="@color/tv_text_default"
android:textSize="12sp" /> android:textSize="12sp" />

@ -141,6 +141,7 @@
<string name="add_book_url">添加书籍网址</string> <string name="add_book_url">添加书籍网址</string>
<string name="background">背景</string> <string name="background">背景</string>
<string name="author">作者</string> <string name="author">作者</string>
<string name="author_show">作者:%s</string>
<string name="analyze_error">站点暂时不支持解析,请反馈</string> <string name="analyze_error">站点暂时不支持解析,请反馈</string>
<string name="aloud_stop">朗读停止</string> <string name="aloud_stop">朗读停止</string>
<string name="clear_cache">清除缓存</string> <string name="clear_cache">清除缓存</string>

Loading…
Cancel
Save