From 61a7a2dc3d0f786260125f9051c55f99299b27f0 Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 12 Aug 2019 16:32:18 +0800 Subject: [PATCH] up --- .../io/legado/app/data/dao/SearchBookDao.kt | 6 ++++++ .../io/legado/app/data/entities/SearchBook.kt | 16 ++++++++++++++++ .../main/java/io/legado/app/help/BookHelp.kt | 18 ------------------ .../io/legado/app/model/webbook/SourceDebug.kt | 3 +-- .../app/ui/bookinfo/BookInfoViewModel.kt | 4 ++++ .../io/legado/app/ui/search/SearchActivity.kt | 13 +++++++++++-- .../io/legado/app/ui/search/SearchAdapter.kt | 6 +++--- .../io/legado/app/ui/search/SearchViewModel.kt | 15 +++++++-------- app/src/main/res/layout/item_search.xml | 1 - app/src/main/res/values/strings.xml | 1 + 10 files changed, 49 insertions(+), 34 deletions(-) diff --git a/app/src/main/java/io/legado/app/data/dao/SearchBookDao.kt b/app/src/main/java/io/legado/app/data/dao/SearchBookDao.kt index 0336d7ef7..f6f3d8f07 100644 --- a/app/src/main/java/io/legado/app/data/dao/SearchBookDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/SearchBookDao.kt @@ -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") fun observeShow(time: Long): DataSource.Factory + @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) fun insert(vararg searchBook: SearchBook): List diff --git a/app/src/main/java/io/legado/app/data/entities/SearchBook.kt b/app/src/main/java/io/legado/app/data/entities/SearchBook.kt index 9ac3e181c..ea8e1aa84 100644 --- a/app/src/main/java/io/legado/app/data/entities/SearchBook.kt +++ b/app/src/main/java/io/legado/app/data/entities/SearchBook.kt @@ -67,4 +67,20 @@ data class SearchBook( } 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 + } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/help/BookHelp.kt b/app/src/main/java/io/legado/app/help/BookHelp.kt index 1cdb6e570..db0b27222 100644 --- a/app/src/main/java/io/legado/app/help/BookHelp.kt +++ b/app/src/main/java/io/legado/app/help/BookHelp.kt @@ -1,25 +1,7 @@ package io.legado.app.help -import io.legado.app.data.entities.Book -import io.legado.app.data.entities.SearchBook - 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 { return author ?.replace("作\\s*者[\\s::]*".toRegex(), "") diff --git a/app/src/main/java/io/legado/app/model/webbook/SourceDebug.kt b/app/src/main/java/io/legado/app/model/webbook/SourceDebug.kt index c79b078f9..cd599f54b 100644 --- a/app/src/main/java/io/legado/app/model/webbook/SourceDebug.kt +++ b/app/src/main/java/io/legado/app/model/webbook/SourceDebug.kt @@ -3,7 +3,6 @@ package io.legado.app.model.webbook import android.annotation.SuppressLint import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookChapter -import io.legado.app.help.BookHelp import io.legado.app.help.coroutine.CompositeCoroutine import io.legado.app.model.WebBook import io.legado.app.utils.htmlFormat @@ -82,7 +81,7 @@ class SourceDebug(private val webBook: WebBook, callback: Callback) { if (searchBooks.isNotEmpty()) { printLog(debugSource, 1, "搜索完成") printLog(debugSource, 1, "", showTime = false) - infoDebug(BookHelp.toBook(searchBooks[0])) + infoDebug(searchBooks[0].toBook()) } else { printLog(debugSource, -1, "未获取到书籍") } diff --git a/app/src/main/java/io/legado/app/ui/bookinfo/BookInfoViewModel.kt b/app/src/main/java/io/legado/app/ui/bookinfo/BookInfoViewModel.kt index 296565424..ea88b145c 100644 --- a/app/src/main/java/io/legado/app/ui/bookinfo/BookInfoViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/bookinfo/BookInfoViewModel.kt @@ -17,6 +17,10 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) { App.db.bookDao().getBook(it)?.let { book -> bookData.postValue(book) } + } ?: intent.getStringExtra("searchBookUrl")?.let { + App.db.searchBookDao().getSearchBook(it)?.let { searchBook -> + bookData.postValue(searchBook.toBook()) + } } } } diff --git a/app/src/main/java/io/legado/app/ui/search/SearchActivity.kt b/app/src/main/java/io/legado/app/ui/search/SearchActivity.kt index 1b19f5b1d..2493916b3 100644 --- a/app/src/main/java/io/legado/app/ui/search/SearchActivity.kt +++ b/app/src/main/java/io/legado/app/ui/search/SearchActivity.kt @@ -1,7 +1,6 @@ package io.legado.app.ui.search import android.os.Bundle -import android.view.View import androidx.appcompat.widget.SearchView import androidx.lifecycle.LiveData 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.lib.theme.ATH import io.legado.app.lib.theme.primaryTextColor +import io.legado.app.ui.bookinfo.BookInfoActivity import io.legado.app.utils.getViewModel import kotlinx.android.synthetic.main.activity_search.* import kotlinx.android.synthetic.main.view_search.* +import org.jetbrains.anko.startActivity -class SearchActivity : VMBaseActivity(R.layout.activity_search) { +class SearchActivity : VMBaseActivity(R.layout.activity_search), SearchAdapter.CallBack { override val viewModel: SearchViewModel get() = getViewModel(SearchViewModel::class.java) @@ -66,6 +67,7 @@ class SearchActivity : VMBaseActivity(R.layout.activity_search) private fun initRecyclerView() { ATH.applyEdgeEffectColor(rv_search_list) adapter = SearchAdapter() + adapter.callBack = this rv_search_list.layoutManager = LinearLayoutManager(this) rv_search_list.adapter = adapter } @@ -76,4 +78,11 @@ class SearchActivity : VMBaseActivity(R.layout.activity_search) searchBookData?.observe(this, Observer { adapter.submitList(it) }) } + override fun showBookInfo(name: String, author: String?) { + viewModel.getSearchBook(name, author) { searchBook -> + searchBook?.let { + startActivity(Pair("searchBookUrl", it.bookUrl)) + } + } + } } diff --git a/app/src/main/java/io/legado/app/ui/search/SearchAdapter.kt b/app/src/main/java/io/legado/app/ui/search/SearchAdapter.kt index 66b55595d..c25e3f323 100644 --- a/app/src/main/java/io/legado/app/ui/search/SearchAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/search/SearchAdapter.kt @@ -50,7 +50,7 @@ class SearchAdapter : PagedListAdapter(D fun bind(searchBook: SearchShow, callBack: CallBack?) = with(itemView) { tv_name.text = searchBook.name 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()) { tv_lasted.gone() } else { @@ -96,12 +96,12 @@ class SearchAdapter : PagedListAdapter(D .setAsDrawable(iv_cover) } onClick { - callBack?.showBookInfo(searchBook.name, searchBook.author) + callBack?.showBookInfo(searchBook.name!!, searchBook.author) } } } interface CallBack { - fun showBookInfo(name: String?, author: String?) + fun showBookInfo(name: String, author: String?) } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt b/app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt index 1defc8695..870e64d05 100644 --- a/app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt @@ -1,17 +1,12 @@ package io.legado.app.ui.search import android.app.Application -import android.util.Log import io.legado.app.App import io.legado.app.base.BaseViewModel +import io.legado.app.data.entities.SearchBook import io.legado.app.help.coroutine.Coroutine import io.legado.app.model.WebBook 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) { private var task: Coroutine<*>? = null @@ -47,12 +42,16 @@ class SearchViewModel(application: Application) : BaseViewModel(application) { task?.invokeOnCompletion { finally?.invoke() } - - } fun stop() { task?.cancel() } + fun getSearchBook(name: String, author: String?, success: ((searchBook: SearchBook?) -> Unit)?) { + execute { + val searchBook = App.db.searchBookDao().getByNameAuthor(name, author) + success?.invoke(searchBook) + } + } } diff --git a/app/src/main/res/layout/item_search.xml b/app/src/main/res/layout/item_search.xml index 4a71b3049..6b849210a 100644 --- a/app/src/main/res/layout/item_search.xml +++ b/app/src/main/res/layout/item_search.xml @@ -54,7 +54,6 @@ android:layout_height="wrap_content" android:ellipsize="end" android:lines="1" - android:drawableStart="@drawable/ic_author" android:text="@string/author" android:textColor="@color/tv_text_default" android:textSize="12sp" /> diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 976375c91..d1b9122c2 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -141,6 +141,7 @@ 添加书籍网址 背景 作者 + 作者:%s 站点暂时不支持解析,请反馈 朗读停止 清除缓存