Skip to content

Refactor: Move name validation to OnboardingViewModel#125

Closed
harrydbarnes wants to merge 1 commit into2.0from
refactor-onboarding-validation-5103387645092597876
Closed

Refactor: Move name validation to OnboardingViewModel#125
harrydbarnes wants to merge 1 commit into2.0from
refactor-onboarding-validation-5103387645092597876

Conversation

@harrydbarnes
Copy link
Owner

Moved the user name validation logic from OnboardingScreen (Composable) to OnboardingViewModel. Changed validation from isNotEmpty() to isNotBlank() to prevent saving whitespace-only names. This improves separation of concerns and testability.

Moved the user name validation logic from OnboardingScreen (Composable) to OnboardingViewModel.
Changed validation from isNotEmpty() to isNotBlank() to prevent saving whitespace-only names.
This improves separation of concerns and testability.
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @harrydbarnes, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request refactors the name validation process within the onboarding flow. It extracts the responsibility of validating user input from the OnboardingScreen Composable and delegates it to the OnboardingViewModel. Concurrently, the validation itself has been strengthened to disallow names that are empty or contain only whitespace, thereby improving data integrity and the overall architecture by enhancing separation of concerns and testability.

Highlights

  • Name Validation Relocation: The user name validation logic has been moved from the OnboardingScreen Composable to the OnboardingViewModel.
  • Improved Validation Logic: The validation condition for user names has been updated from isNotEmpty() to isNotBlank(), preventing the saving of names consisting solely of whitespace.
  • Enhanced Separation of Concerns: This refactoring improves the separation of concerns by centralizing validation logic within the ViewModel, making the UI cleaner and more focused on presentation.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the user name validation logic from the OnboardingScreen composable to the OnboardingViewModel, which is a good architectural improvement. It also correctly changes the validation from isNotEmpty() to isNotBlank(). However, the implementation introduces a logic flaw where an invalid name submission fails silently, allowing the user to proceed through the onboarding flow without a valid name. My review includes a fix to provide feedback from the ViewModel to the UI, ensuring the user flow is handled correctly.

Comment on lines 25 to 31
fun saveName() {
viewModelScope.launch {
userPreferencesRepository.saveUserName(name.value)
if (name.value.isNotBlank()) {
viewModelScope.launch {
userPreferencesRepository.saveUserName(name.value)
}
}
}
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
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant