fix: Can load more content
This commit is contained in:
@@ -85,7 +85,7 @@ class MainActivity : AppCompatActivity() {
|
||||
viewModel.adapter.apply {
|
||||
setLoadMoreListener(object :PostAdapter.OnScrollToBottom{
|
||||
override fun event(position: Int) {
|
||||
viewModel.appendPost()
|
||||
viewModel.fetchMore()
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
@@ -38,7 +38,7 @@ class PostAdapter(val context: Context):
|
||||
fun appendDate(list: List<YandPost>){
|
||||
val pos = postList.size
|
||||
postList.addAll(list)
|
||||
notifyItemRangeInserted(pos, list.size)
|
||||
notifyItemRangeInserted(pos-1, list.size)
|
||||
}
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
|
||||
|
||||
@@ -72,21 +72,19 @@ class MainViewModel(private val repository: PostRepository,context: Context):Vie
|
||||
|
||||
}
|
||||
|
||||
private fun fetchNewPost(){
|
||||
private fun fetchNewPost(append:Boolean = false){
|
||||
fetchJob?.cancel()
|
||||
fetchJob = viewModelScope.launch(Dispatchers.IO) {
|
||||
|
||||
_uiState.value.isRefreshing.postValue(true)
|
||||
|
||||
try {
|
||||
val postList = repository.fetchPostData(
|
||||
val postList = repository.fetchPostData(
|
||||
_uiState.value.nowSearchText.value,
|
||||
_uiState.value.isSafe,
|
||||
_uiState.value.nowPage
|
||||
)
|
||||
|
||||
launch(Dispatchers.Main) {
|
||||
if (_uiState.value.isAppend){
|
||||
if (append){
|
||||
adapter.appendDate(postList)
|
||||
}else{
|
||||
adapter.pushNewData(postList)
|
||||
@@ -101,8 +99,6 @@ class MainViewModel(private val repository: PostRepository,context: Context):Vie
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -124,18 +120,9 @@ class MainViewModel(private val repository: PostRepository,context: Context):Vie
|
||||
fetchNewPost()
|
||||
}
|
||||
|
||||
fun appendPost(){
|
||||
|
||||
_uiState.update {
|
||||
it.copy(isAppend = true)
|
||||
}
|
||||
|
||||
fetchNewPost()
|
||||
|
||||
_uiState.update {
|
||||
it.copy(isAppend = false)
|
||||
}
|
||||
|
||||
fun fetchMore(){
|
||||
_uiState.value.nowPage++
|
||||
fetchNewPost(true)
|
||||
}
|
||||
|
||||
fun updateSafeMode(mode:Boolean){
|
||||
|
||||
@@ -8,8 +8,7 @@ data class UiState(
|
||||
var nowSearchText:MutableLiveData<String> = MutableLiveData(""),
|
||||
val nowSourceName: MutableLiveData<String> = MutableLiveData(),
|
||||
val isSafe: Boolean = true, //安全模式
|
||||
val nowPage:Int = 1,
|
||||
val isAppend:Boolean = false
|
||||
var nowPage:Int = 1,
|
||||
){
|
||||
fun switchShowSearchBar(){
|
||||
showSearchBar = !showSearchBar
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.lsp.view.repository.exception
|
||||
|
||||
import android.util.Log
|
||||
|
||||
open class LoggerException(override val message: String?):Exception() {
|
||||
init {
|
||||
logMessage()
|
||||
}
|
||||
|
||||
private fun logMessage(){
|
||||
Log.e(this::class.java.simpleName,message.toString())
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -2,5 +2,5 @@ package com.lsp.view.repository.exception
|
||||
|
||||
import okhttp3.ResponseBody
|
||||
|
||||
class NetworkErrorException(override val message: String?) : Exception() {
|
||||
class NetworkErrorException(override val message: String?) : LoggerException(message) {
|
||||
}
|
||||
+1
-1
@@ -2,5 +2,5 @@ package com.lsp.view.repository.exception
|
||||
|
||||
import java.lang.Exception
|
||||
|
||||
class UnableConstructObjectException(override val message: String?):Exception(message) {
|
||||
class UnableConstructObjectException(override val message: String?):LoggerException(message) {
|
||||
}
|
||||
Reference in New Issue
Block a user