fix: search

This commit is contained in:
AnranYus
2023-10-20 19:14:34 +08:00
parent 54a8ece487
commit 9ef7dda7dc
3 changed files with 23 additions and 21 deletions
@@ -55,6 +55,26 @@ class MainActivity : AppCompatActivity() {
Snackbar.make(refresh,it,Snackbar.LENGTH_SHORT).show() Snackbar.make(refresh,it,Snackbar.LENGTH_SHORT).show()
} }
search = findViewById(R.id.search)
shortAnnotationDuration = resources.getInteger(android.R.integer.config_shortAnimTime)
search.setOnEditorActionListener { content, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
viewModel.uiState.value.nowSearchText.value = content.text.toString()
viewModel.fetchPostByRefresh()
}
return@setOnEditorActionListener false
}
//接收来自PicActivity的快捷搜索Tag
val stringExtra = intent.getStringExtra("searchTag")
if (stringExtra!=null){
viewModel.uiState.value.nowSearchText.value = stringExtra
}
viewModel.uiState.value.nowSearchText.observe(this){
search.setText(it)
}
// val appbar = findViewById<AppBarLayout>(R.id.appbar) // val appbar = findViewById<AppBarLayout>(R.id.appbar)
// val nowHeight = appbar.layoutParams.height // val nowHeight = appbar.layoutParams.height
// appbar.layoutParams.height = (application as YandViewApplication).statusBarHeight()+nowHeight // appbar.layoutParams.height = (application as YandViewApplication).statusBarHeight()+nowHeight
@@ -85,17 +105,6 @@ class MainActivity : AppCompatActivity() {
it.setHomeAsUpIndicator(R.drawable.ic_baseline_menu_24) it.setHomeAsUpIndicator(R.drawable.ic_baseline_menu_24)
} }
search = findViewById(R.id.search)
shortAnnotationDuration = resources.getInteger(android.R.integer.config_shortAnimTime)
search.setOnEditorActionListener { _, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
viewModel.fetchNewPostBySearch(search.text.toString())
}
return@setOnEditorActionListener false
}
//侧边栏 //侧边栏
val sp = getSharedPreferences("username", 0) val sp = getSharedPreferences("username", 0)
@@ -196,7 +205,7 @@ class MainActivity : AppCompatActivity() {
} }
R.id.search_nav -> { R.id.search_nav -> {
if (viewModel.uiState.value.showSearchBar) { if (viewModel.uiState.value.showSearchBar) {
viewModel.fetchNewPostBySearch(search.text.toString()) viewModel.fetchPostByRefresh()
hideIm() hideIm()
} }
@@ -75,7 +75,7 @@ class MainViewModel(private val repository: PostRepository,context: Context):Vie
try { try {
val postList = repository.fetchPostData( val postList = repository.fetchPostData(
_uiState.value.nowSearchText, _uiState.value.nowSearchText.value,
_uiState.value.isSafe, _uiState.value.isSafe,
_uiState.value.nowPage _uiState.value.nowPage
) )
@@ -100,13 +100,6 @@ class MainViewModel(private val repository: PostRepository,context: Context):Vie
} }
fun fetchNewPostBySearch(target:String){
_uiState.update {
it.copy(nowSearchText = target)
}
fetchNewPost()
}
fun fetchPostByRefresh(){ fun fetchPostByRefresh(){
_uiState.update { _uiState.update {
@@ -5,7 +5,7 @@ import androidx.lifecycle.MutableLiveData
data class UiState( data class UiState(
var showSearchBar:Boolean = false, var showSearchBar:Boolean = false,
val isRefreshing:MutableLiveData<Boolean> = MutableLiveData(), val isRefreshing:MutableLiveData<Boolean> = MutableLiveData(),
val nowSearchText:String = "", var nowSearchText:MutableLiveData<String> = MutableLiveData(""),
val nowSourceName: String? = null, val nowSourceName: String? = null,
val isSafe: Boolean = true, //安全模式 val isSafe: Boolean = true, //安全模式
val nowPage:Int = 1, val nowPage:Int = 1,