Merge pull request #3 from XDH2022/dev

fix:reload
This commit is contained in:
AnranYus
2024-03-02 20:45:49 +08:00
committed by GitHub
@@ -96,28 +96,28 @@ class MainViewModel(private val repository: PostRepository, context: Context):Vi
} }
private fun fetchNewPost(){ private fun fetchNewPost() {
fetchJob?.cancel() fetchJob?.cancel()
fetchJob = viewModelScope.launch(Dispatchers.IO) { fetchJob = viewModelScope.launch(Dispatchers.IO) {
_uiState.value.isRefreshing.postValue(true) _uiState.value.isRefreshing.postValue(true)
try { try {
_postList.postValue(repository.fetchPostData( val newPosts = repository.fetchPostData(
_uiState.value.nowSearchText.value, _uiState.value.nowSearchText.value,
_uiState.value.isSafe, _uiState.value.isSafe,
_uiState.value.nowPage _uiState.value.nowPage
)) )
val currentPosts = _postList.value ?: arrayListOf()
}catch (e:NetworkErrorException){ currentPosts.addAll(0,newPosts)
_postList.postValue(currentPosts)
} catch (e: NetworkErrorException) {
postNewToast(e.message.toString()) postNewToast(e.message.toString())
} }
_uiState.value.isRefreshing.postValue(false) _uiState.value.isRefreshing.postValue(false)
} }
} }
fun fetchPostByRefresh(){ fun fetchPostByRefresh(){
_uiState.update { _uiState.update {
it.copy(nowPage = 1) it.copy(nowPage = 1)