feat: 全局返回顶部事件

This commit is contained in:
HuanCheng65 2023-07-15 22:54:25 +08:00
parent 0ae5dde3df
commit 2ce5dbb315
No known key found for this signature in database
GPG Key ID: 5EC9DD60A32C7360
2 changed files with 14 additions and 0 deletions

View File

@ -5,6 +5,7 @@ import android.os.Bundle
import android.os.Parcelable import android.os.Parcelable
import android.widget.Toast import android.widget.Toast
import androidx.activity.compose.setContent import androidx.activity.compose.setContent
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.LaunchedEffect
@ -106,6 +107,8 @@ abstract class BaseComposeActivity : BaseActivity() {
is CommonUiEvent.Toast -> { is CommonUiEvent.Toast -> {
Toast.makeText(this, event.message, event.length).show() Toast.makeText(this, event.message, event.length).show()
} }
else -> {}
} }
} }
@ -118,8 +121,17 @@ abstract class BaseComposeActivity : BaseActivity() {
sealed interface CommonUiEvent : UiEvent { sealed interface CommonUiEvent : UiEvent {
object ScrollToTop : CommonUiEvent
data class Toast( data class Toast(
val message: CharSequence, val message: CharSequence,
val length: Int = android.widget.Toast.LENGTH_SHORT val length: Int = android.widget.Toast.LENGTH_SHORT
) : CommonUiEvent ) : CommonUiEvent
@Composable
fun BaseViewModel<*, *, *, *>.bindScrollToTopEvent(lazyListState: LazyListState) {
onEvent<ScrollToTop> {
lazyListState.scrollToItem(0, 0)
}
}
} }

View File

@ -49,6 +49,7 @@ import com.huanchengfly.tieba.post.api.models.protos.ThreadInfo
import com.huanchengfly.tieba.post.api.models.protos.personalized.DislikeReason import com.huanchengfly.tieba.post.api.models.protos.personalized.DislikeReason
import com.huanchengfly.tieba.post.api.models.protos.personalized.ThreadPersonalized import com.huanchengfly.tieba.post.api.models.protos.personalized.ThreadPersonalized
import com.huanchengfly.tieba.post.arch.BaseComposeActivity import com.huanchengfly.tieba.post.arch.BaseComposeActivity
import com.huanchengfly.tieba.post.arch.CommonUiEvent.ScrollToTop.bindScrollToTopEvent
import com.huanchengfly.tieba.post.arch.ImmutableHolder import com.huanchengfly.tieba.post.arch.ImmutableHolder
import com.huanchengfly.tieba.post.arch.collectPartialAsState import com.huanchengfly.tieba.post.arch.collectPartialAsState
import com.huanchengfly.tieba.post.arch.onEvent import com.huanchengfly.tieba.post.arch.onEvent
@ -111,6 +112,7 @@ fun PersonalizedPage(
onRefresh = { viewModel.send(PersonalizedUiIntent.Refresh) } onRefresh = { viewModel.send(PersonalizedUiIntent.Refresh) }
) )
val lazyListState = rememberLazyListState() val lazyListState = rememberLazyListState()
viewModel.bindScrollToTopEvent(lazyListState = lazyListState)
var refreshCount by remember { var refreshCount by remember {
mutableStateOf(0) mutableStateOf(0)
} }