Fix profile navigation

This commit is contained in:
世界
2026-03-16 19:31:25 +08:00
parent a7caf965a0
commit b3515329c2
4 changed files with 10 additions and 19 deletions

View File

@@ -92,12 +92,11 @@ fun EditProfileContentScreen(
profileId: Long, profileId: Long,
onNavigateBack: () -> Unit, onNavigateBack: () -> Unit,
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
profileName: String = "",
isReadOnly: Boolean = false, isReadOnly: Boolean = false,
) { ) {
val viewModel: EditProfileContentViewModel = val viewModel: EditProfileContentViewModel =
viewModel( viewModel(
factory = EditProfileContentViewModel.Factory(profileId, profileName, isReadOnly), factory = EditProfileContentViewModel.Factory(profileId, isReadOnly),
) )
val uiState by viewModel.uiState.collectAsState() val uiState by viewModel.uiState.collectAsState()
val context = LocalContext.current val context = LocalContext.current

View File

@@ -38,11 +38,10 @@ data class EditProfileContentUiState(
val profileName: String = "", // Add profile name val profileName: String = "", // Add profile name
) )
class EditProfileContentViewModel(private val profileId: Long, initialProfileName: String = "", initialIsReadOnly: Boolean = false) : ViewModel() { class EditProfileContentViewModel(private val profileId: Long, initialIsReadOnly: Boolean = false) : ViewModel() {
private val _uiState = private val _uiState =
MutableStateFlow( MutableStateFlow(
EditProfileContentUiState( EditProfileContentUiState(
profileName = initialProfileName,
isReadOnly = initialIsReadOnly, isReadOnly = initialIsReadOnly,
), ),
) )
@@ -211,7 +210,7 @@ class EditProfileContentViewModel(private val profileId: Long, initialProfileNam
originalContent = content, originalContent = content,
hasUnsavedChanges = false, hasUnsavedChanges = false,
isLoading = false, isLoading = false,
// Keep profileName and isReadOnly from initial state - no need to update profileName = loadedProfile.name,
) )
} }
} }
@@ -584,13 +583,12 @@ class EditProfileContentViewModel(private val profileId: Long, initialProfileNam
class Factory( class Factory(
private val profileId: Long, private val profileId: Long,
private val initialProfileName: String = "",
private val initialIsReadOnly: Boolean = false, private val initialIsReadOnly: Boolean = false,
) : ViewModelProvider.Factory { ) : ViewModelProvider.Factory {
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
override fun <T : ViewModel> create(modelClass: Class<T>): T { override fun <T : ViewModel> create(modelClass: Class<T>): T {
if (modelClass.isAssignableFrom(EditProfileContentViewModel::class.java)) { if (modelClass.isAssignableFrom(EditProfileContentViewModel::class.java)) {
return EditProfileContentViewModel(profileId, initialProfileName, initialIsReadOnly) as T return EditProfileContentViewModel(profileId, initialIsReadOnly) as T
} }
throw IllegalArgumentException("Unknown ViewModel class") throw IllegalArgumentException("Unknown ViewModel class")
} }

View File

@@ -10,6 +10,7 @@ import androidx.navigation.NavType
import androidx.navigation.compose.NavHost import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController import androidx.navigation.compose.rememberNavController
import android.net.Uri
import androidx.navigation.navArgument import androidx.navigation.navArgument
@Composable @Composable
@@ -64,12 +65,12 @@ fun EditProfileRoute(profileId: Long, onNavigateBack: () -> Unit, modifier: Modi
profileId = profileId, profileId = profileId,
onNavigateBack = onNavigateBack, onNavigateBack = onNavigateBack,
onNavigateToIconSelection = { currentIconId -> onNavigateToIconSelection = { currentIconId ->
navController.navigate("icon_selection/${currentIconId ?: "null"}") { navController.navigate("icon_selection/${Uri.encode(currentIconId ?: "null")}") {
launchSingleTop = true launchSingleTop = true
} }
}, },
onNavigateToEditContent = { profileName, isReadOnly -> onNavigateToEditContent = { isReadOnly ->
navController.navigate("edit_content/$profileName/$isReadOnly") { navController.navigate("edit_content/$isReadOnly") {
launchSingleTop = true launchSingleTop = true
} }
}, },
@@ -128,13 +129,9 @@ fun EditProfileRoute(profileId: Long, onNavigateBack: () -> Unit, modifier: Modi
} }
composable( composable(
route = "edit_content/{profileName}/{isReadOnly}", route = "edit_content/{isReadOnly}",
arguments = arguments =
listOf( listOf(
navArgument("profileName") {
type = NavType.StringType
defaultValue = ""
},
navArgument("isReadOnly") { navArgument("isReadOnly") {
type = NavType.BoolType type = NavType.BoolType
defaultValue = false defaultValue = false
@@ -165,7 +162,6 @@ fun EditProfileRoute(profileId: Long, onNavigateBack: () -> Unit, modifier: Modi
) )
}, },
) { backStackEntry -> ) { backStackEntry ->
val profileName = backStackEntry.arguments?.getString("profileName") ?: ""
val isReadOnly = backStackEntry.arguments?.getBoolean("isReadOnly") ?: false val isReadOnly = backStackEntry.arguments?.getBoolean("isReadOnly") ?: false
EditProfileContentScreen( EditProfileContentScreen(
@@ -173,7 +169,6 @@ fun EditProfileRoute(profileId: Long, onNavigateBack: () -> Unit, modifier: Modi
onNavigateBack = { onNavigateBack = {
navController.popBackStack("edit_profile", inclusive = false) navController.popBackStack("edit_profile", inclusive = false)
}, },
profileName = profileName,
isReadOnly = isReadOnly, isReadOnly = isReadOnly,
) )
} }

View File

@@ -79,7 +79,7 @@ fun EditProfileScreen(
profileId: Long, profileId: Long,
onNavigateBack: () -> Unit, onNavigateBack: () -> Unit,
onNavigateToIconSelection: (currentIconId: String?) -> Unit = {}, onNavigateToIconSelection: (currentIconId: String?) -> Unit = {},
onNavigateToEditContent: (profileName: String, isReadOnly: Boolean) -> Unit = { _, _ -> }, onNavigateToEditContent: (isReadOnly: Boolean) -> Unit = {},
viewModel: EditProfileViewModel = viewModel(), viewModel: EditProfileViewModel = viewModel(),
) { ) {
val uiState by viewModel.uiState.collectAsStateWithLifecycle() val uiState by viewModel.uiState.collectAsStateWithLifecycle()
@@ -473,7 +473,6 @@ fun EditProfileScreen(
.clip(RoundedCornerShape(12.dp)) .clip(RoundedCornerShape(12.dp))
.clickable { .clickable {
onNavigateToEditContent( onNavigateToEditContent(
uiState.name,
uiState.profileType == TypedProfile.Type.Remote, uiState.profileType == TypedProfile.Type.Remote,
) )
}, },