Refactor: Add debounce for navigation and optimize comments loading

- Implemented debounced navigation to prevent multiple rapid navigations.
- Replaced Pager-based comment loading with a simpler list-based approach for improved performance and reduced complexity.
- Added loading and error states for comment fetching.
- Introduced `debouncedClickable` modifier for handling click events with debounce.
- Updated image viewer to use simple navigation arrows instead of HorizontalPager for better user experience.
- Added a new string resource for password length error.
This commit is contained in:
2025-09-03 18:07:44 +08:00
parent ae7254163a
commit d93373d8fa

View File

@@ -41,6 +41,8 @@ import com.aiosman.ravenow.ui.composables.CustomAsyncImage
import com.aiosman.ravenow.ui.composables.StatusBarMaskLayout import com.aiosman.ravenow.ui.composables.StatusBarMaskLayout
import com.aiosman.ravenow.ui.composables.StatusBarSpacer import com.aiosman.ravenow.ui.composables.StatusBarSpacer
import com.aiosman.ravenow.ui.composables.form.FormTextInput import com.aiosman.ravenow.ui.composables.form.FormTextInput
import com.aiosman.ravenow.ui.composables.debouncedClickable
import com.aiosman.ravenow.ui.composables.rememberDebouncedNavigation
import com.aiosman.ravenow.ui.modifiers.noRippleClickable import com.aiosman.ravenow.ui.modifiers.noRippleClickable
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@@ -56,6 +58,9 @@ fun AccountEditScreen2() {
val context = LocalContext.current val context = LocalContext.current
var usernameError by remember { mutableStateOf<String?>(null) } var usernameError by remember { mutableStateOf<String?>(null) }
var bioError by remember { mutableStateOf<String?>(null) } var bioError by remember { mutableStateOf<String?>(null) }
// 防抖导航器
val debouncedNavigation = rememberDebouncedNavigation()
fun onNicknameChange(value: String) { fun onNicknameChange(value: String) {
// 去除换行符,确保昵称不包含换行 // 去除换行符,确保昵称不包含换行
val cleanValue = value.replace("\n", "").replace("\r", "") val cleanValue = value.replace("\n", "").replace("\r", "")
@@ -87,8 +92,10 @@ fun AccountEditScreen2() {
// 检查是否为游客模式 // 检查是否为游客模式
if (AppStore.isGuest) { if (AppStore.isGuest) {
LaunchedEffect(Unit) { LaunchedEffect(Unit) {
// 游客模式不允许编辑资料,返回上一页 // 游客模式不允许编辑资料,返回上一页(防抖)
navController.navigateUp() debouncedNavigation {
navController.navigateUp()
}
} }
// 游客模式时不渲染任何内容 // 游客模式时不渲染任何内容
return return
@@ -122,19 +129,22 @@ fun AccountEditScreen2() {
Icon( Icon(
modifier = Modifier modifier = Modifier
.size(24.dp) .size(24.dp)
.noRippleClickable { .debouncedClickable(
enabled = validate() && !model.isUpdating,
debounceTime = 1000L
) {
if (validate() && !model.isUpdating) { if (validate() && !model.isUpdating) {
model.viewModelScope.launch { model.viewModelScope.launch {
model.isUpdating = true model.isUpdating = true
model.updateUserProfile(context) model.updateUserProfile(context)
model.viewModelScope.launch(Dispatchers.Main) { model.viewModelScope.launch(Dispatchers.Main) {
debouncedNavigation {
navController.navigateUp() navController.navigateUp()
}
model.isUpdating = false model.isUpdating = false
} }
} }
} }
}, },
imageVector = Icons.Default.Check, imageVector = Icons.Default.Check,
contentDescription = "保存", contentDescription = "保存",
@@ -172,8 +182,12 @@ fun AccountEditScreen2() {
.clip(CircleShape) .clip(CircleShape)
.background(appColors.main) .background(appColors.main)
.align(Alignment.BottomEnd) .align(Alignment.BottomEnd)
.noRippleClickable { .debouncedClickable(
navController.navigate(NavigationRoute.ImageCrop.route) debounceTime = 800L
) {
debouncedNavigation {
navController.navigate(NavigationRoute.ImageCrop.route)
}
}, },
contentAlignment = Alignment.Center contentAlignment = Alignment.Center
) { ) {