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