|
|
|
@ -8,20 +8,47 @@ import io.legado.app.data.appDb |
|
|
|
|
import io.legado.app.data.entities.SearchBook |
|
|
|
|
import io.legado.app.data.entities.SearchKeyword |
|
|
|
|
import io.legado.app.model.webBook.SearchModel |
|
|
|
|
import kotlinx.coroutines.Job |
|
|
|
|
import kotlinx.coroutines.delay |
|
|
|
|
import kotlinx.coroutines.launch |
|
|
|
|
import kotlinx.coroutines.channels.awaitClose |
|
|
|
|
import kotlinx.coroutines.flow.callbackFlow |
|
|
|
|
import kotlinx.coroutines.flow.conflate |
|
|
|
|
|
|
|
|
|
class SearchViewModel(application: Application) : BaseViewModel(application), SearchModel.CallBack { |
|
|
|
|
private val searchModel = SearchModel(viewModelScope, this) |
|
|
|
|
private var upAdapterJob: Job? = null |
|
|
|
|
class SearchViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
|
private val searchModel = SearchModel(viewModelScope) |
|
|
|
|
var isSearchLiveData = MutableLiveData<Boolean>() |
|
|
|
|
var searchBookLiveData = MutableLiveData<List<SearchBook>>() |
|
|
|
|
var searchKey: String = "" |
|
|
|
|
var isLoading = false |
|
|
|
|
private var searchBooks = arrayListOf<SearchBook>() |
|
|
|
|
private var searchID = 0L |
|
|
|
|
private var postTime = 0L |
|
|
|
|
|
|
|
|
|
val searchDataFlow = callbackFlow { |
|
|
|
|
|
|
|
|
|
val callback = object : SearchModel.CallBack { |
|
|
|
|
override fun onSearchStart() { |
|
|
|
|
isSearchLiveData.postValue(true) |
|
|
|
|
isLoading = true |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Synchronized |
|
|
|
|
override fun onSearchSuccess(searchBooks: ArrayList<SearchBook>) { |
|
|
|
|
trySend(ArrayList(searchBooks)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
override fun onSearchFinish() { |
|
|
|
|
isSearchLiveData.postValue(false) |
|
|
|
|
isLoading = false |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
override fun onSearchCancel() { |
|
|
|
|
isSearchLiveData.postValue(false) |
|
|
|
|
isLoading = false |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
searchModel.registerCallback(callback) |
|
|
|
|
|
|
|
|
|
awaitClose { |
|
|
|
|
searchModel.unRegisterCallback() |
|
|
|
|
} |
|
|
|
|
}.conflate() |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 开始搜索 |
|
|
|
@ -29,8 +56,6 @@ class SearchViewModel(application: Application) : BaseViewModel(application), Se |
|
|
|
|
fun search(key: String) { |
|
|
|
|
if ((searchKey == key) || key.isNotEmpty()) { |
|
|
|
|
searchModel.cancelSearch() |
|
|
|
|
searchBooks.clear() |
|
|
|
|
searchBookLiveData.postValue(searchBooks) |
|
|
|
|
searchID = System.currentTimeMillis() |
|
|
|
|
searchKey = key |
|
|
|
|
} |
|
|
|
@ -40,42 +65,6 @@ class SearchViewModel(application: Application) : BaseViewModel(application), Se |
|
|
|
|
searchModel.search(searchID, searchKey) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Synchronized |
|
|
|
|
private fun upAdapter() { |
|
|
|
|
upAdapterJob?.cancel() |
|
|
|
|
if (System.currentTimeMillis() >= postTime + 1000) { |
|
|
|
|
postTime = System.currentTimeMillis() |
|
|
|
|
searchBookLiveData.postValue(searchBooks) |
|
|
|
|
} else { |
|
|
|
|
upAdapterJob = viewModelScope.launch { |
|
|
|
|
delay(1000) |
|
|
|
|
upAdapter() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
override fun onSearchStart() { |
|
|
|
|
isSearchLiveData.postValue(true) |
|
|
|
|
isLoading = true |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Synchronized |
|
|
|
|
override fun onSearchSuccess(searchBooks: ArrayList<SearchBook>) { |
|
|
|
|
this.searchBooks = searchBooks |
|
|
|
|
upAdapter() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
override fun onSearchFinish() { |
|
|
|
|
isSearchLiveData.postValue(false) |
|
|
|
|
isLoading = false |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
override fun onSearchCancel() { |
|
|
|
|
isSearchLiveData.postValue(false) |
|
|
|
|
isLoading = false |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 停止搜索 |
|
|
|
|
*/ |
|
|
|
|