Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions app/src/main/java/com/example/theloop/OnboardingViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ class OnboardingViewModel @Inject constructor(
}

fun saveName() {
viewModelScope.launch {
userPreferencesRepository.saveUserName(name.value)
if (name.value.isNotBlank()) {
viewModelScope.launch {
userPreferencesRepository.saveUserName(name.value)
}
}
}
Comment on lines 25 to 31
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Moving the name validation to the ViewModel is a great refactoring. However, the current implementation has a significant flaw: it fails silently.

When saveName() is called with a blank name, it does nothing. The calling code in OnboardingScreen.kt is unaware of this failure and proceeds to the next onboarding step. This allows the user to complete the name entry step without actually providing a valid name.

To fix this, saveName() should communicate the validation result back to the UI. A simple approach is to return a Boolean.

After making the suggested change below, you'll need to update OnboardingScreen.kt to use the return value to control the flow:

// In OnboardingScreen.kt's Button onClick handler
if (currentStep == 0) {
    if (!viewModel.saveName()) {
        Toast.makeText(context, "Name cannot be blank", Toast.LENGTH_SHORT).show()
        return@Button // Prevent advancing to the next step
    }
}

if (currentStep < totalSteps - 1) {
    currentStep++
} else {
    // ...
}

This ensures the user cannot proceed with an invalid name.

    fun saveName(): Boolean {
        if (name.value.isNotBlank()) {
            viewModelScope.launch {
                userPreferencesRepository.saveUserName(name.value)
            }
            return true
        }
        return false
    }


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,7 @@ fun OnboardingScreen(
Button(onClick = {
if (currentStep == 0) {
// Save Name
if (name.isNotEmpty()) {
viewModel.saveName()
}
viewModel.saveName()
}

if (currentStep < totalSteps - 1) {
Expand Down