Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor CategorySelectionViewModel to remove Redundant code #13

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Tonnie-Dev
Copy link
Contributor

Rewrote selectAllThisCategory() on CategorySelectionViewModel to remove code redundancy.

Local Variable selectedCategories will always be empty after calling clearChanges()

    `viewModelScope.launch {
        clearChanges()
         //selectedCategories will always be empty after clearChanges() is called
        val selectedCategories = _selectedCategory.value.toMutableList()

        repeat(categories.size) {
            val category = categories[it]

         //selectedCategory will always evaluate to null as we are filtering an Empty List
            val selectedCategory = selectedCategories.firstOrNull {
                category.id == it.id
            }

          //this check is redundant as selectedCategory is null
            if (selectedCategory == null) {
                selectedCategories.add(category)
            }
        }`

**To remove the above redundancy, we should update _selectedCategories directly.

 `viewModelScope.launch {

        clearChanges()// clear the current list of selected categories

        _selectedCategories.value = categories //assign categories directly
    }`

This function is now more efficient as it doesn't perform unnecessary checks or operations. It simply replaces the old selections with the new ones

Tonnie-Dev added 2 commits May 4, 2024 16:30
Rewrite selectAllThisCategory() to remove redundant code
@nkuppan nkuppan self-requested a review July 10, 2024 11:39
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