feat: SearchFragment 使用 SourceHandler 执行搜索
This commit is contained in:
@@ -25,7 +25,8 @@ import com.videoapp.tv.data.AppDatabase
|
||||
import com.videoapp.tv.data.PlayHistory
|
||||
import com.videoapp.tv.data.SearchHistory
|
||||
import com.videoapp.tv.data.SearchResult
|
||||
import com.videoapp.tv.engine.SearchCoordinator
|
||||
import com.videoapp.tv.data.ConfigRepository
|
||||
import com.videoapp.tv.engine.SourceRegistry
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
class SearchFragment : Fragment() {
|
||||
@@ -45,7 +46,7 @@ class SearchFragment : Fragment() {
|
||||
private lateinit var statusText: TextView
|
||||
private lateinit var fallbackWebView: WebView
|
||||
|
||||
private val searchCoordinator by lazy { SearchCoordinator(requireContext()) }
|
||||
private val configRepo by lazy { ConfigRepository(requireContext()) }
|
||||
private val historyDao by lazy { AppDatabase.getInstance(requireContext()).searchHistoryDao() }
|
||||
private val playHistoryDao by lazy { AppDatabase.getInstance(requireContext()).playHistoryDao() }
|
||||
private val adapter by lazy {
|
||||
@@ -137,6 +138,13 @@ class SearchFragment : Fragment() {
|
||||
private fun performSearch(keyword: String) {
|
||||
if (keyword.isEmpty()) return
|
||||
|
||||
val currentId = configRepo.getCurrentSourceId()
|
||||
val handler = SourceRegistry.getOrDefault(currentId)
|
||||
if (handler == null) {
|
||||
showStatus("未知源")
|
||||
return
|
||||
}
|
||||
|
||||
hideFallbackWebView()
|
||||
showLoading(true)
|
||||
statusText.visibility = View.GONE
|
||||
@@ -144,7 +152,6 @@ class SearchFragment : Fragment() {
|
||||
playHistoryContainer.visibility = View.GONE
|
||||
|
||||
lifecycleScope.launch {
|
||||
// Save to history
|
||||
val existing = historyDao.findByKeyword(keyword)
|
||||
if (existing != null) {
|
||||
historyDao.updateTime(existing.id, System.currentTimeMillis())
|
||||
@@ -154,7 +161,7 @@ class SearchFragment : Fragment() {
|
||||
}
|
||||
|
||||
lifecycleScope.launch {
|
||||
searchCoordinator.search(
|
||||
handler.search(
|
||||
keyword = keyword,
|
||||
onResult = { results ->
|
||||
showLoading(false)
|
||||
@@ -165,22 +172,15 @@ class SearchFragment : Fragment() {
|
||||
},
|
||||
onError = { error ->
|
||||
showLoading(false)
|
||||
if (error == "FALLBACK_WEBVIEW") {
|
||||
showFallbackWebView(keyword)
|
||||
} else {
|
||||
showStatus("搜索失败,请重试")
|
||||
Toast.makeText(requireContext(), error, Toast.LENGTH_LONG).show()
|
||||
}
|
||||
},
|
||||
onFallbackToWebView = {
|
||||
showFallbackWebView(keyword)
|
||||
showStatus("搜索失败,请重试")
|
||||
Toast.makeText(requireContext(), error, Toast.LENGTH_LONG).show()
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private fun showFallbackWebView(keyword: String) {
|
||||
val config = searchCoordinator.getConfig()
|
||||
val config = configRepo.getConfig()
|
||||
val base = config.baseUrl.trimEnd('/')
|
||||
val path = config.searchPath.trimStart('/')
|
||||
val params = "${config.keywordParam}=${java.net.URLEncoder.encode(keyword, "UTF-8")}"
|
||||
|
||||
Reference in New Issue
Block a user