From 743f6da9b5f2b6156c62e2298109d1c53a58facc Mon Sep 17 00:00:00 2001 From: kunfei Date: Sun, 4 Aug 2019 15:01:17 +0800 Subject: [PATCH] up --- .../io/legado/app/data/dao/SearchBookDao.kt | 6 ++- .../io/legado/app/model/webbook/BookList.kt | 2 + .../io/legado/app/ui/search/SearchActivity.kt | 7 +++- .../legado/app/ui/search/SearchViewModel.kt | 42 ++++++------------- 4 files changed, 26 insertions(+), 31 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 ff36e8afb..dfa6794e1 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 @@ -2,6 +2,8 @@ package io.legado.app.data.dao import androidx.paging.DataSource import androidx.room.Dao +import androidx.room.Insert +import androidx.room.OnConflictStrategy import androidx.room.Query import io.legado.app.data.entities.SearchBook @@ -11,8 +13,10 @@ interface SearchBookDao { @Query("SELECT * FROM searchBooks") fun observeAll(): DataSource.Factory - @Query("SELECT * FROM searchBooks where time >= :time") + @Query("SELECT name, author, '' bookUrl, '' origin, '' originName, min(time) time, max(intro) intro, max(kind) kind, max(coverUrl) coverUrl, max(latestChapterTitle) latestChapterTitle FROM searchBooks where time >= :time group by name and author") fun observeNew(time: Long): DataSource.Factory + @Insert(onConflict = OnConflictStrategy.REPLACE) + fun insert(vararg searchBook: SearchBook) } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/model/webbook/BookList.kt b/app/src/main/java/io/legado/app/model/webbook/BookList.kt index 06a8ea92d..cea3c9f49 100644 --- a/app/src/main/java/io/legado/app/model/webbook/BookList.kt +++ b/app/src/main/java/io/legado/app/model/webbook/BookList.kt @@ -131,6 +131,7 @@ object BookList { SourceDebug.printLog(bookSource.bookSourceUrl, 1, "获取最新章节") searchBook.latestChapterTitle = analyzeRule.getString(lastChapter ?: "") SourceDebug.printLog(bookSource.bookSourceUrl, 1, searchBook.latestChapterTitle ?: "") + searchBook.time = System.currentTimeMillis() return searchBook } } @@ -182,6 +183,7 @@ object BookList { SourceDebug.printLog(bookSource.bookSourceUrl, 1, "获取最新章节", printLog) searchBook.latestChapterTitle = analyzeRule.getString(ruleLastChapter) SourceDebug.printLog(bookSource.bookSourceUrl, 1, searchBook.latestChapterTitle ?: "", printLog) + searchBook.time = System.currentTimeMillis() return searchBook } return null 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 bba1721af..64978fe69 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 @@ -32,8 +32,9 @@ class SearchActivity : VMBaseActivity(R.layout.activity_search) search_view.setOnQueryTextListener(object : SearchView.OnQueryTextListener { override fun onQueryTextSubmit(query: String?): Boolean { query?.let { - viewModel.search(it, { + viewModel.search(it, { startTime -> content_view.showProgressView() + initData(startTime) }, { }) @@ -58,4 +59,8 @@ class SearchActivity : VMBaseActivity(R.layout.activity_search) rv_search_list.adapter = adapter } + private fun initData(startTime: Long) { + + } + } 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 3ca042ad0..8ea099c5f 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,36 +1,30 @@ package io.legado.app.ui.search import android.app.Application -import android.util.Log -import androidx.lifecycle.LiveData -import androidx.lifecycle.MutableLiveData import io.legado.app.App import io.legado.app.base.BaseViewModel -import io.legado.app.data.api.CommonHttpApi -import io.legado.app.data.entities.SearchBook import io.legado.app.help.coroutine.CompositeCoroutine -import io.legado.app.help.http.HttpHelper import io.legado.app.model.WebBook -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.channels.Channel -import kotlinx.coroutines.withContext class SearchViewModel(application: Application) : BaseViewModel(application) { - val tasks: CompositeCoroutine = CompositeCoroutine() - val searchBookList = arrayListOf() - val searchBooksData: LiveData> = MutableLiveData() + private val tasks: CompositeCoroutine = CompositeCoroutine() var searchPage = 0 - private val channel = Channel()//协程之间通信 - fun search(key: String, start: (() -> Unit)? = null, finally: (() -> Unit)? = null) { + fun search(key: String, start: ((startTime: Long) -> Unit)? = null, finally: (() -> Unit)? = null) { if (key.isEmpty()) return - start?.invoke() + tasks.clear() + start?.invoke(System.currentTimeMillis()) execute { val bookSourceList = App.db.bookSourceDao().allEnabled for (item in bookSourceList) { val search = WebBook(item).searchBook(key, searchPage) .onSuccess { searchBookS -> - searchBookS?.let { searchBookList.addAll(it) } + searchBookS?.let { + for (searchBook in searchBookS) { + + } + App.db.searchBookDao().insert(*it.toTypedArray()) + } } tasks.add(search) } @@ -38,18 +32,8 @@ class SearchViewModel(application: Application) : BaseViewModel(application) { } } - suspend fun test(scope: CoroutineScope): MutableList { - val list = mutableListOf() - repeat(10) { - withContext(scope.coroutineContext) { - Log.e("TAG3", Thread.currentThread().name) - val response: String = HttpHelper.getApiService( - "http://www.baidu.com" - ).get("http://www.baidu.com").await() - list.add(response) - } - } - return list + override fun onCleared() { + super.onCleared() + tasks.clear() } - }